2022年5月30日 星期一

09163076 week13

1.建立glut專案(week13_rect_TRT)
2.四邊形

#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glRectf(0.5, 0.5, -0.5, -0.5);

    glutSwapBuffers();

}

int main(int argc, char **argv)

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

     glutInitWindowSize(600,600);

     glutCreateWindow("week13 rect TRT");

     glutDisplayFunc(display);

     glutMainLoop();

}

3.白色方塊加紅色方塊手臂

#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.5, 0.5, -0.5, -0.5);

    glPopMatrix();

        glColor3f(1,0,0);

        glRectf(0.5,0.5,1.0,0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char **argv)

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

     glutInitWindowSize(600,600);

     glutCreateWindow("week13 rect TRT");

     glutDisplayFunc(display);

     glutMainLoop();

}

4.將紅色手臂部分的連接部分移置白色方塊中心

#include <GL/glut.h>

float angle=45;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.5, 0.5, -0.5, -0.5);

    glPopMatrix();

        glRotatef(angle,0,0,1);

        glTranslatef(-0.5,-0.4,0);

        glColor3f(1,0,0);

        glRectf(0.5,0.5,1.0,0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char **argv)

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

     glutInitWindowSize(600,600);

     glutCreateWindow("week13 rect TRT");

     glutDisplayFunc(display);

     glutMainLoop();

}

5.新增mouse、motion函式,可使用滑鼠使手臂擺動(紅色方塊)

#include <GL/glut.h>

float angle=45,oldx=0;

void mouse(int button, int state, int x, int y){

    oldx = x;

}

void motion(int x,int y){

    angle+=(x-oldx);

    oldx = x;

    glutPostRedisplay();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.5, 0.5, -0.5, -0.5);

     glPushMatrix();

        glTranslatef(0.5,0.5,0);

        glRotatef(angle ,0,0,1);

        glTranslatef(-0.5 , -0.4 , 0);

        glColor3f(1,0,0);

        glRectf(0.5,0.5,1.0,0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char **argv)

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

     glutCreateWindow("week13 rect TRT");

     glutMouseFunc(mouse);

     glutMotionFunc(motion);

     glutDisplayFunc(display);

     glutMainLoop();

     return 0;

}

6.增加下手臂(綠色方塊).更改數值

#include <GL/glut.h>

float angle=0,oldx=0;

void mouse(int button, int state, int x, int y){

    oldx = x;

}

void motion(int x,int y){

    angle+=(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.5,0);

        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,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");

     glutMouseFunc(mouse);

     glutMotionFunc(motion);

     glutDisplayFunc(display);

     glutMainLoop();

     return 0;

}

7.複製glPushMatrix到glPopMatrix的程式碼,將所有X的數值正負數值顛倒,做出左邊的手臂

8.新增glutKeyboardFunc(keyboard);函式,再結合陣列使每個關節分開改角度

#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.5,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.5,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");

     glutMouseFunc(mouse);

     glutMotionFunc(motion);

     glutKeyboardFunc(keyboard);

     glutDisplayFunc(display);

     glutMainLoop();

     return 0;

}













沒有留言:

張貼留言