2.請把解壓縮的win32資料夾打開,並把glut32.dll和data資料夾放到裡面後
即可開啟Transformation.exe
3.將會出現這畫面
glTranslatef 移動
glRotatef 旋轉
glScale 縮放
畫出可自由的移動茶壺
1.先開啟CodeBlocks的軟體,並從File→New中建立Project
2.點選GLUT的圖示並點選GO

3.點選Next

4.請命名你的標題

5.這時一樣打開上週下載的freeglut的資料夾,並檢查是否已複製libfreeglut.a的檔案
和重新命名成libglut32.a

6.把資料夾的網址複製到location的下方空白欄

7.點選Finish即可

8.會看到一大堆的程式碼,請先全部刪除

9.請先輸入以下程式 (中文為註解,而螢光筆標註的是跟上週相比所需增加的程式
#include<stdio.h>
#include<GL/glut.h>
float teapotX=0,teapotY=0, angle=0;///rotate旋轉需要angle
int nowRotate=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glTranslatef(teapotX,teapotY,0);
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y)
{
if(nowRotate==1)angle=x;///滑鼠的座標,就是轉的角度
else{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
}
glutPostRedisplay();///畫出畫面
}
void mouse(int button ,int state ,int x, int y)
{
if(button==GLUT_LEFT_BUTTON &&state==GLUT_DOWN) nowRotate=1;///左邊做旋轉
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN) nowRotate=0;///右邊做移動
}
int main(int argc, char*argv[])///完整的main()參數
{
glutInit(&argc,argv);///初始的參數,照著丟進去
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week03 Mouse");
glutDisplayFunc(display);///等一下樓上會有display()
glutMouseFunc(mouse);///等一下樓上會有mouse()滑鼠的事件
glutMotionFunc(motion);///等一下樓上會有mouse()滑鼠motion事件
glutMainLoop();///主要GLUT迴圈
}
10.請再修改有畫螢光筆的部分即可完成
自由移動的茶壺
#include<stdio.h>///printf()要用到
#include<GL/glut.h>///整套GLUT外掛
float teapotX=0, teapotY=0 , angle=0;///茶壺的座標
int oldX=0, oldY=0, nowRotate=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glTranslatef(teapotX,teapotY,0);///依照茶壺的座標移動
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}
void mouse(int button ,int state ,int x, int y)
{
///button(左,中,右鍵, state: 接下來/彈起來, x,y: mouse位置
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;
oldX=x; oldY=y;
///printf("%d %d %d %d\n", button,state,x,y);
}
void motion(int x, int y)
{
if(nowRotate==1)angle += (x-oldX);
else{
teapotX +=(x-oldX)/150.0;
teapotY +=(oldY-y)/150.0;///依照motion時的x,y來改teapot的座標
}
oldX=x; oldY=y;
glutPostRedisplay();
}
int main(int argc, char*argv[])///完整的main()參數
{
glutInit(&argc,argv);///初始的參數,照著丟進去
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week03 Mouse");
glutDisplayFunc(display);///等一下樓上會有display()
glutMouseFunc(mouse);///等一下樓上會有mouse()滑鼠的事件
glutMotionFunc(motion);///等一下樓上會有mouse()滑鼠motion事件
glutMainLoop();///主要GLUT迴圈
}
沒有留言:
張貼留言