下載:
把windows壓縮檔解壓,把glut32 & data丟到裡面,執行Transformation
下載freeglut 做好前置作業
茶壺可隨著滑鼠移動
程式碼:
#include <GL/glut.h>
float x=0, y=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((x-150)/150.0, -(y-150)/150.0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int nowx,int nowy)
{
x=nowx; y=nowy; ///更新x,y
display(); ///再畫display()
}
int main(int argc, char ** argv)
{
glutInit( &argc, argv); ///啟動GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week3");
glutDisplayFunc(display);
glutMotionFunc(motion); ///滑鼠有動作motion時,要叫motion()
glutMainLoop();
}
茶壺可隨著滑鼠左右移動來旋轉
程式碼:
#include <GL/glut.h>
float x=0, y=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清畫面
glPushMatrix();
glRotatef(x, 0, 1, 0);
///glTranslatef((x-150)/150.0, -(y-150)/150.0, 0); ///移動
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int nowx,int nowy)
{
x=nowx; y=nowy; ///更新x,y
display(); ///再畫display()
}
int main(int argc, char ** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week4");
glutDisplayFunc(display);
glutMotionFunc(motion); ///負責motion的函式,要叫motion()
glutMainLoop();
}
沒有留言:
張貼留言