電腦圖學
week03-1
先到講義[https://jsyeh.org/3dcg10/]下載[data][win32]這兩個壓縮檔
把兩個壓縮檔解壓縮,並把[data]解壓縮檔移到[win32]的檔案裡面
重複上個禮拜(第一周)的第二個程式>>新增一個project[week03-1_translate]>>把project原本裡面的177行程式,貼上week02-1的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("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
這邊是用[gist.github]貼上程式碼,名稱的副檔名為(.cpp)
加入幾行程式碼可以使茶壺移動
程式碼:
#include <GL/glut.h>
void display()
{
glPushMatrix();///備份矩陣
glTranslatef(0.5,0.5,0);///會改變你的矩陣
glutSolidTeapot( 0.3 );
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
week03-2
重複上個禮拜(第一周)的第二個程式>>新增一個project[week03-1_translate_mouse]>>把week03-1的程式碼貼進去
寫程式讓茶壺可以跟著滑鼠一起移動
程式碼:
#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();
}






沒有留言:
張貼留言