2023年3月16日 星期四

大吃五斤Week05

 week05

從老師給的網址( https://jsyeh.org/3dcg10/ )下載windows.zip和data.zip,解壓縮後把data資料夾中的data放入windows的資料夾,後開啟windows裡的Transformation.exe

swap translate / rotate
#include <GL/glut.h>
float angle = 0 ;
void display ( )
{
    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
    glColor3f ( 1 , 0 , 0 ) ;
    glPushMatrix ( ) ;
        glRotatef ( angle , 0 , 0 , 1 ) ;
        glTranslatef ( 0.8 , 0 , 0 ) ;
        glutSolidTeapot( 0.3 ) ;
    glPopMatrix ( ) ;

    glColor3f ( 0 , 1 , 0 ) ;
    glPushMatrix ( ) ;
        glTranslatef ( 0.8 , 0 , 0 ) ;
        glRotatef ( angle *1.5 , 0 , 0 , 1 ) ;
        glutSolidTeapot ( 0.3 ) ;
    glPopMatrix ( ) ;
    glutSwapBuffers ( ) ;
    angle++ ;
}
int main ( int argc , char*argv[] )
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week05");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
用老師 opengl_TRT_demo 的互動程式
按 ToDraw 可以畫東西
可按住程式碼移動位置
先只放一個旋轉( 在手的前面,只影響手 )
移動手的中心點到肩膀
用glutSolidCube(1)畫出長寬1的方塊
做一個myCube函式,用glScalef( 0.5 , 0.2 , 0.2 ) 調整大小

#include <GL/glut.h>
float angle = 0 ;
void myCube() ///改函式
{
    glPushMatrix();
        glScalef(0.5,0.2,0.2); ///調大小
        glutSolidCube(1); ///正方塊
    glPopMatrix();
}
void display()
{
    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glColor3f(1,1,1); ///白色的
    glutSolidCube(1); ///身體

    glPushMatrix();
        glTranslatef(0.5,0.5,0); ///掛到右上角
        glRotatef (angle,0,0,1) ; ///轉動
        glTranslatef(0.25,0,0); ///往右移動0.25讓關節在正中心
        glColor3f(0,1,0); ///綠色的
        myCube(); ///改函式
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.5,0.5,0);
        glRotatef (angle,0,0,1) ;
        glTranslatef(0.25,0,0);
        glColor3f(0,1,0);
        myCube();
    glPopMatrix();

    glutSwapBuffers();
    angle++ ;
}
int main(int argc,char*argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week05");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}


















沒有留言:

張貼留言