2022年5月16日 星期一

week13

 1.畫四邊形



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

}


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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600, 600);
    glutCreateWindow("Week13_rect_TRT");

    glutDisplayFunc(display);
    glutMainLoop();
}

3.將紅色方塊的重心旋轉移動到正中心


#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();
        ///glTranslatef(x, y, z);
        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();
}

4.用滑鼠控制揮舞"手臂"



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

    ///glutInitWindowSize(600, 600);

    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutDisplayFunc(display);

    glutMainLoop();

}


5."瘦身", 手臂要調



#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.3, 0.5, -0.3, -0.2);///身體

    glPushMatrix();

        glTranslatef(0.3, 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);

    ///glutInitWindowSize(600, 600);

    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutDisplayFunc(display);

    glutMainLoop();

}

6.加下手臂




#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.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, 1);
            glRectf(0.8, 0.5, 1.1, 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");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutDisplayFunc(display);
    glutMainLoop();
}

7.揮舞下手臂




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

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

    ///glutInitWindowSize(600, 600);

    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutDisplayFunc(display);

    glutMainLoop();

}

8.加左半身



#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.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, 1);
            glRectf(0.8, 0.5, 1.1, 0.3);///下手臂
        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, 1);
            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);
    ///glutInitWindowSize(600, 600);
    glutCreateWindow("Week13_rect_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutDisplayFunc(display);
    glutMainLoop();
}

9.關節用鍵盤及滑鼠分別揮舞





#include <GL/glut.h>
float angle[20], oldX=0;
int angleID=0;///0:第0個關節, 1:第1個關節...
void keyboard(unsigned char key, int x, int y){///鍵盤數字鍵決定旋轉部位
    if(key=='0') angleID=0;///預設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);///身體瘦身(對比1)
    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_DOUBLE | GLUT_DEPTH );
    //glutInitWindowSize(600, 600);
    glutCreateWindow("week13_rect_TRT");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言