Week03
1.去小葉老師的網站Computer Graphics (jsyeh.org)中下載source,data,win32,glut32.dll並解壓縮
2.將Data資料夾複製到Windows資料夾中 執行Transformation.exe檔
1.將下列程式碼插入原本的程式碼中 讓茶壺移至右上角
glPushMatrix();
glTranslatef(0.5,0.5,0);
glutSolidTeapot(0.3);
glPopMatrix();
1.將原本程式碼更改為下列程式碼讓茶壺能按照滑鼠點擊方位進行移動
#include <GL/glut.h>
float X=0,Y=0,Z=0; //使用Global變數
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //清背景
glPushMatrix(); //備份矩陣
glTranslatef(X,Y,Z); //照著XYZ移動
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();
}
2.將程式碼作為調成下列形式 就能用滑鼠點出各個點座標
#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();
}
沒有留言:
張貼留言