Step 1. 下載老師網頁上的 data and win32 壓縮檔,將 data 解壓縮後丟進 win32的解壓縮資料夾中,
並打開 Tansformation 就會有以下畫面。( 在右上角的視窗按右鍵可以換模型 )
Step 2. 修改glRotatef的參數 ( 角度 , x , y ,z )
Step 3. 若改成 ( 角度 ,1.0 ,1.0 ,0.0 ) 模型會斜著旋轉
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(180,0,0,1);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc ,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week05 Rotate");
glutDisplayFunc(display);
glutMainLoop();
}
Step 5. 將剛剛的程式碼,再增加一個變數 float angle ,並多寫一個函式 void motion ,且在裡面設定 讓滑鼠可以拖著它動
程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glColor3f(1,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);
glutMotionFunc(motion);
glutMainLoop();
}
Step 6. 因為剛剛的程式碼執行後,雖然可以拖著茶壺轉動,但放開後會轉回去,所以多加一個oldX
固定住按下mouse的位置,且放開mouse後位置不動
程式碼:
#include <GL/glut.h>
float angle=0,oldX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{ ///按下mouse、放開mouse
oldX = x;
}
void motion(int x,int y)
{
angle += (x-oldX);
oldX = x;
display();
}
int main(int argc ,char**argv)
{
glutInit(&argc ,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week05 Rotate");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
Step 7. 複習上禮拜的 mouse 程式碼
程式碼:
#include <stdio.h>
#include <GL/glut.h>
int N=0;
int x[1000],y[1000];
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for(int i=0;i<N;i++){
glVertex2f((x[i]-150)/150.0,-(y[i]-150)/150.0);
}
glEnd();
glutSwapBuffers();
}
void mouse(int button,int state,int mouseX,int mouseY)
{
if(state==GLUT_DOWN){
N++;
x[N-1]=mouseX;
y[N-1]=mouseY;
printf("現在按下滑鼠,得到新座標 %d %d\n",x[N-1],y[N-1]);
}
display();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week05 複習 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
Step 8.把 if 註解掉,讓按著滑鼠時可以不中斷畫圖。
#include <GL/glut.h>
int N=0;
int x[1000],y[1000];
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for(int i=0;i<N;i++){
glVertex2f((x[i]-150)/150.0,-(y[i]-150)/150.0);
}
glEnd();
glutSwapBuffers();
}
///void mouse(int button,int state,int mouseX,int mouseY)
void motion(int mouseX,int mouseY)
{
// if(state==GLUT_DOWN){
N++;
x[N-1]=mouseX;
y[N-1]=mouseY;
printf("現在按下滑鼠,得到新座標 %d %d\n",x[N-1],y[N-1]);
// }
display();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week05 複習 mouse");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言