這周一開始我們先去https://jsyeh.org/3dcg10/的網站裡把data跟win32下載下來
之後把兩個檔案都解壓縮,然後把Transformation.exe打開
並且在右上角模型上按右鍵,就可以更換其他不同的模型
接著我們用上週的程式碼
把茶壺移動到右上角
再來我們可以利用函式,把它分成四份
#include <GL/glut.h>
void myTeapot(float x,float y)
{
glPushMatrix();///備份矩陣
///移動會累積,因為它會修改矩陣
// glTranslatef(0.5,0.5,0);///右上角
glTranslatef(x,y,0);///
glColor3f(1,1,0);///黃色
glutSolidTeapot(0.3);///茶壺
glPopMatrix();///還原矩陣(還原舊的位置)
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
myTeapot(0.5,0.5);
myTeapot(0.5,-0.5);
myTeapot(-0.5,-0.5);
myTeapot(-0.5,0.5);
glutSwapBuffers();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week02_1");
glutDisplayFunc(display);
glutMainLoop();
}
之後再加上mouse函式,只要滑鼠點擊左鍵,茶壺就會移動到滑鼠所在的位置
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void myTeapot(float x,float y)
{
glPushMatrix();///備份矩陣
///移動會累積,因為它會修改矩陣
// glTranslatef(0.5,0.5,0);///右上角
glTranslatef(x,y,0);///
glColor3f(1,1,0);///黃色
glutSolidTeapot(0.3);///茶壺
glPopMatrix();///還原矩陣(還原舊的位置)
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
myTeapot((mouseX-150)/150.0,-(mouseY-150)/150.0);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
mouseX=x;mouseY=y;
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week02_1");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
然後我們加上#include <stdio.h>
和printf()
讓他能夠印出茶壺所在的3D座標
沒有留言:
張貼留言