2022年5月30日 星期一

week13

用用之前的程式畫出一個四邊形。

-幫白色四邊形畫一個手臂,畫出一個紅長方形。

-把紅手移動到中心,並旋轉45度。


-點擊滑鼠並向右拖曳時,紅手臂逆時鐘旋轉,反之順時鐘。

-將白四邊形消瘦,並增加一個綠手。


-程式碼讓綠手能旋轉。

-新增程式碼,使用者輸入數字0123時,能分別對不同部位進行轉動。

#include <GL/glut.h>
float angle[20], oldX=0; 
int angleID=0;
void keyboard(unsigned char key, int x, int y)
{
    if(key == '0') angleID=0;
    if(key == '1') angleID=1;
    if(key == '2') angleID=2;
    if(key == '3') angleID=3;
}
void mouse(int button, int state, int x, int y)
{
    oldX= x;
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    oldX= x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1, 1, 1);
    glRectf(0.3, 0.5, -0.3, -0.2);

    glPushMatrix();
        glTranslatef(0.3, 0.4, 0);
        glRotatef(angle[0], 0, 0, 1);
        glTranslatef(-0.3, -0.4, 0);
        glColor3f(1, 0, 0);
        glRectf(0.3, 0.5, 0.8, 0.3); 
        glPushMatrix();
            glTranslatef(0.8, 0.4, 0);
            glRotatef(angle[1], 0, 0, 1);
            glTranslatef(-0.8, -0.4, 0);
            glColor3f(0, 1, 0);
            glRectf(0.8, 0.5, 1.1, 0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3, 0.4, 0);
        glRotatef(angle[2], 0, 0, 1);
        glTranslatef(0.3, -0.4, 0);
        glColor3f(1, 0, 0);
        glRectf(-0.3, 0.5, -0.8, 0.3);
        glPushMatrix();
            glTranslatef(-0.8, 0.4, 0);
            glRotatef(angle[3], 0, 0, 1);
            glTranslatef(0.8, -0.4, 0);
            glColor3f(0, 1, 0);
            glRectf(-0.8, 0.5, -1.1, 0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); 
    glutMainLoop();
}

沒有留言:

張貼留言