2023年3月23日 星期四

91 week06

 開啟codeblock,新的glut專案

week06-1,使用上周的程式week05-2複製貼上:


中心加個點,茶壺把柄對齊點:

#include <GL/glut.h>

float angle=0;

void display()

{

     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);

    glPopMatrix();

    glPushMatrix();

        glRotatef(angle,0,0,1);

        glTranslatef(0.45,0,0);

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06");

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutMainLoop();

}


把茶壺掛在右上角:
添加程式碼:glTranslatef(0.5,0.5,0);
week0加個小方塊:
#include <GL/glut.h>
float angle=0;
void myCube()
{
    glPushMatrix();
        glScalef(1,0.3,0.3);
        glutSolidCube(0.5);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);
    glPopMatrix();
    glPushMatrix();
        ///glTranslatef(0.5,0.5,0);
        ///glRotatef(angle,0,0,1);
        ///glTranslatef(0.45,0,0);
        myCube();
    glPopMatrix();
    glutSwapBuffers();
    angle++;
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
移動完旋轉掛在點上:
再移動:
再複製貼上一個方塊,類似關節:
#include <GL/glut.h>
float angle=0;
void myCube()
{
    glPushMatrix();
        glScalef(1,0.3,0.3);
        glutSolidCube(0.5);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(0.01,30,30);
    glPushMatrix();
        glTranslatef(0.25,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        myCube();
    glPushMatrix();
        glTranslatef(0.25,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        myCube();
    glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
    angle++;
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
week06-3:
增加左手臂,複製貼上程式碼: 
    glPushMatrix();
        glTranslatef(-0.25,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(-0.25,0,0);
        myCube();
    glPushMatrix();
        glTranslatef(-0.25,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(-0.25,0,0);
        myCube();
    glPopMatrix();
    glPopMatrix();
0.25改成-0.25的左手臂位置:
week06-4:
複製貼上week06-3,把angle++註解掉
增加程式碼:
兩個angle改-angle
void motion(int x,int y)
{
    angle=x;
}
glutMotionFunc(motion);
week06-4-2:
滑鼠移動函式
修改程式碼week06-4:
int 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;
}
glutMouseFunc(mouse);

















沒有留言:

張貼留言