2023年3月2日 星期四

mian𓃹 Week03 移動

一。課本範本



step1. 至https://jsyeh.org/3dcg10/下載windows.zip和data.zip
-windows.zip 解壓縮 下載/windows/Transformation.exe
-data.zip 解壓縮 下載/data/很多3D模型檔案



step2. 執行Transformation.exe
-gLTranslaterf(x,y,z) 綠色數字 上下拉動
-右上角可右鍵換模型


step3. 如果閃退→data.zip解錯目錄

二。移動茶壺


step1. 參照week01-2打開



step2. 刪除main中的177行程式打上下面10行可得白色茶壺

#include <GL/glut.h>


void display()

{

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}


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

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week03");


    glutDisplayFunc(display);


    glutMainLoop();

}



step3. 欲使茶壺移動至指定位子,需更改其座標

在void display中增加

glPushMatrix();

glTranslatef(0.5,0.5,0);

glPopMatrix();



step4. 修改程式為

#include <GL/glut.h>


float X=0,Y=0,Z=0;


void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef(X,Y,Z);

        glutSolidTeapot(0.3);

    glPopMatrix();


    glutSwapBuffers();

}


void mouse(int button,int state,int x,int y)

{

    X=(x-150)/150.0;

    Y=-(y-150)/150.0;

}


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

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week03");


    glutMouseFunc(mouse);

    glutDisplayFunc(display);


    glutMainLoop();

}


三。讓作業更漂亮



step1. 參照week03-2


step2.將茶壺部分刪掉作更改
#include <GL/glut.h>
#include <stdio.h>

float X=0,Y=0,Z=0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
    glutSwapBuffers();
}

void mouse(int button,int state,int x,int y)
{
    X=(x-150)/150.0;
    Y=-(y-150)/150.0;
    if(state==GLUT_DOWN) printf("glVertex2f(%.2f,%.2f);\n",X,Y);
}

int main(int argc,char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week03");

    glutMouseFunc(mouse);
    glutDisplayFunc(display);

    glutMainLoop();
}


step3. build&run








 

沒有留言:

張貼留言