2022年5月16日 星期一

鯉魚-Week13

1、用 glRectf(); 畫圖

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

2、

#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();
        //glTranslatef(x,y,z);//等一下掛在身體上
        //glRotatef(angle,0,0,1);//z軸
        //glTranslatef(x2,y2,z2);
            //用不到的先註解掉
        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();
}

3、


    把旋轉中心放正中心
    glTranslatef(-0.5,-0.4,0);


4、

    旋轉
    float angle = 45;
    glRotatef(angle,0,0,1);


5、可以用滑鼠控制紅色小方塊旋轉(揮手)

#include <GL/glut.h>
float angle = 45,oldX=0;
void mouse(int button, int state, int x, int y){ //mouse按下去
    oldX = x;
}
void motion(int x, int y){
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay(); //重畫畫面 Re display
}
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);//等一下掛在0.5,0.5
        glRotatef(angle,0,0,1);//z軸旋轉
        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();
}

6、白色方塊瘦身,手臂位置調整


7、下手肘



8、下手肘旋轉

#include <GL/glut.h>
float angle = 45,oldX=0;
void mouse(int button, int state, int x, int y){ //mouse按下去
    oldX = x;
}
void motion(int x, int y){
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay(); //重畫畫面 Re display
}
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);//等一下掛在0.5,0.5
        //glRotatef(angle,0,0,1);//z軸旋轉
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);//紅色的
        glRectf(0.5,0.5,1.0,0.3);//手臂
    glPopMatrix();
        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);//下手臂
    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();
}

9、兩邊的手


#include <GL/glut.h>
float angle = 45,oldX=0;
void mouse(int button, int state, int x, int y){ //mouse按下去
    oldX = x;
}
void motion(int x, int y){
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay(); //重畫畫面 Re display
}
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);//等一下掛在0.5,0.5
        glRotatef(angle,0,0,1);//z軸旋轉
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);//紅色的
        glRectf(0.3,0.5,0.8,0.3);//手臂
        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();

        glPushMatrix();//左半邊
        glTranslatef(-0.3,0.5,0);//等一下掛在0.5,0.5
        glRotatef(angle,0,0,1);//z軸旋轉
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);//紅色的
        glRectf(-0.3,0.5,-0.8,0.3);//手臂
        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();

    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();
}



10、用鍵盤選擇控制那個手的移動
#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
void keyboard(unsigned char key, int x, int y){
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
}
void mouse(int button, int state, int x, int y){ //mouse按下去
    oldX = x;
}
void motion(int x, int y){
    angle[angleID] += (x-oldX);
    oldX = x;
    glutPostRedisplay(); //重畫畫面 Re display
}
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);//等一下掛在0.5,0.5
        glRotatef(angle[0],0,0,1);//z軸旋轉
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);//紅色的
        glRectf(0.3,0.5,0.8,0.3);//手臂
        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();

        glPushMatrix();//左半邊
        glTranslatef(-0.3,0.5,0);//等一下掛在0.5,0.5
        glRotatef(angle[2],0,0,1);//z軸旋轉
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);//紅色的
        glRectf(-0.3,0.5,-0.8,0.3);//手臂
        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();

    glutSwapBuffers();
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}



沒有留言:

張貼留言