(2)實作:glrotatef()
(3)滑鼠轉、自動轉
1:進入http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載這三個檔案
2:將win32解壓縮,再把data跟 glut32.dll放入windows資料夾裡面
3:開啟Transformation.exe
glTranslatef:水平移動
glRotatef:旋轉
4:開啟CodeBlocks的GLUT,讓茶壺可以自動旋轉,程式碼如下
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
angle+=5;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week05 rotate");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
5:用滑鼠旋轉茶壺,程式碼如下
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);///對Y軸,轉angle角度
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
angle=x;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week05 rotate");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
6:滑鼠點擊茶壺的視窗,另一個黑視窗會顯示座標,滑鼠左鍵是0,滑鼠中鍵是1,滑鼠右鍵是2,程式碼如下
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);///對Y軸,轉angle角度
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
angle=x;
display();
}
#include <stdio.h>
void mouse(int button, int state, int x , int y)
{
printf("%d %d %d %d\n", button ,state ,x, y);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week05 rotate");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言