2022年5月16日 星期一

CY.hsin_week13

 1.
step1:白色框框

#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_DOUBLE|GLUT_DEPTH);

        glutInitWindowSize(600,600);

        glutCreateWindow("week13 rect TRT");


        glutDisplayFunc(display);

        glutMainLoop();

}



step2:加紅色手臂
#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);
    glPushMatrix();
        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_DOUBLE|GLUT_DEPTH);
        ///glutInitWindowSize(600,600);
        glutCreateWindow("week13 rect TRT");
        glutDisplayFunc(display);
        glutMainLoop();
}

 

step3:把紅方塊轉45度

#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);

    glPushMatrix();

        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_DOUBLE|GLUT_DEPTH);

        ///glutInitWindowSize(600,600);

        glutCreateWindow("week13 rect TRT");


        glutDisplayFunc(display);

        glutMainLoop();

}


step04:能用滑鼠操控手臂
#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_DOUBLE|GLUT_DEPTH);
        ///glutInitWindowSize(600,600);
        glutCreateWindow("week13 rect TRT");
        glutMouseFunc(mouse);
        glutMotionFunc(motion);
        glutDisplayFunc(display);
        glutMainLoop();
}




step05:加下手臂

#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);

        glRotatef(angle,0,0,1);

        glTranslatef(-0.3,-0.4,0);

        glColor3f(1,0,0);

        glRectf(0.3,0.5,0.8,0.3);

    glPushMatrix();

        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_DOUBLE|GLUT_DEPTH);

        ///glutInitWindowSize(600,600);

        glutCreateWindow("week13 rect TRT");

        glutMouseFunc(mouse);

        glutMotionFunc(motion);

        glutDisplayFunc(display);

        glutMainLoop();

}



step06:讓綠手臂轉起來

 #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);

        ///glRotatef(angle,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,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_DOUBLE|GLUT_DEPTH);

        ///glutInitWindowSize(600,600);

        glutCreateWindow("week13 rect TRT");

        glutMouseFunc(mouse);

        glutMotionFunc(motion);

        glutDisplayFunc(display);

        glutMainLoop();

}



step07:做出左手臂(複製右手臂把x改成負的)
#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);
        glRotatef(angle,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,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,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,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_DOUBLE|GLUT_DEPTH);
        ///glutInitWindowSize(600,600);
        glutCreateWindow("week13 rect TRT");
        glutMouseFunc(mouse);
        glutMotionFunc(motion);
        glutDisplayFunc(display);
        glutMainLoop();
}










沒有留言:

張貼留言