week05
(1)主題:旋轉Rotation
(2)實作:glRotatef()
(3)滑鼠轉、自動轉
試glRotatef(角度,x,y,z)
(一)
先到小葉老師的網站下載data、win32、glut32.lib
解壓縮windows
再將data解壓縮至windows資料夾
並將glut32.lib移至windows資料夾
(二)
開啟CODE BLOCKS的GLUT PROJECT
輸入程式碼
#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+=3;
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week05 rotate");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
就可以跑出會旋轉的茶壺
接著 修改程式碼
#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();
}
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();
}
就可以使用滑鼠來控制茶壺
(三)
再修改程式碼
#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();
}
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();
}
就可以讓茶壺和滑鼠的數值顯示出來
沒有留言:
張貼留言