2022年5月16日 星期一

上課筆記week13-09160970蘇賀生

 step01-01

用glut寫出一個正方形


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

}


step01-02

加上一個手臂



#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(0.3,0,0);
       /// glRotatef(angle ,0 ,0 ,1);
       /// glTranslatef(0.2,0,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();
}


step01-03
把紅色手臂移動到中間並并且調45°
把第一個trt的translate注解掉



#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(0.3,0,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");

    glutDisplayFunc (display);
    glutMainLoop();
}


step01-4


把紅色手臂放到右上角
然後加入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_DOUBLE |GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc (display);
    glutMainLoop();
}



step02-01
開了一個新的專案
TRTTRT多弄一個關節

step02-02
幫他多加一個綠色的手臂


#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,0,0);
        glRotatef(angle ,0 ,0 ,1);
        glTranslatef(0,0,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();
}


step02-03
先改變綠手臂的旋轉位置
後面再改位置把他連在紅手臂的尾端


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

step03-01
複製TRT的第一隻手臂
貼下去加一條手臂
多少push就多少pop不然會影響程式碼


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


step03-2
angle[angleID]
就是讓他知道angle的ID是什麽

加上keyboard的程式碼
可以用鍵盤的0123來分開控制每個關節
要在下面加上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_DOUBLE |GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");

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













沒有留言:

張貼留言