(1)使用 Transformation (模擬物體移動、旋轉)
2.將data.zip、windows.zip 解壓縮
3.壓縮好後,將glut32.dll 和 data 複製到windows裡
4.執行 Transformation.exe,就可以完成
!!Transformation 介面與功能
↓各個函式
(↑可以用右手想像,先比個讚的手勢,
大拇指朝向X,Y,Z的箭頭方向,
而握緊的四指方向就是為正旋轉方向!)
(2)複習上週茶杯可以與滑鼠一樣移動(但有些BUG)
忘記的話!幫你召喚上週筆記連結!
達~達~( • ω•́ )→https://2018graphicsa.blogspot.tw/2018/03/week03_24.html
(3)用滑鼠讓茶杯旋轉與修復移動的BUG(左鍵為旋轉,右鍵為移動位置)
(紅色部分為程式重點)
程式碼:
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0;
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0;
///茶壺的座標,rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0;
int oldX=0, oldY=0, nowRotate=0;
///紀錄mouse在哪裡按下去,oldX, oldY 幫忙紀錄舊的資訊
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)
{
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)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
nowRotate=1; ///左鍵做旋轉
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
nowRotate=0; ///右鍵是移動
oldX = x;
oldY = y; ///紀錄按下去的點在哪裡
}
void motion(int x, int y) ///mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX);
oldX = x;
oldY = y; ///紀錄按下去的點在哪裡
}
void motion(int x, int y) ///mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX);
///按了左鍵,旋轉的角度,變成可以接續轉(利用增加方式)
else ///按了右鍵
{
teapotX += (x-oldX)/150.0;
else ///按了右鍵
{
teapotX += (x-oldX)/150.0;
///移動x座標,可以接續移動(利用增加方式)
teapotY += (oldY-y)/150.0;
teapotY += (oldY-y)/150.0;
///移動y座標,可以接續移動(利用增加方式)
}
oldX = x;
oldY = y; ///紀錄移動後新的位置
glutPostRedisplay(); ///重畫畫面
}
int main(int argc, char **argv) ///(2)主要的函式 main
{
glutInit(&argc, argv); ///(3)初始Initialize你的glut參數設定
glutInitDisplayMode(GLUT_DOUBLE);
///(4)顯示模式:double buffers
glutCreateWindow("QWQ 你好我是視窗名稱!!");
}
oldX = x;
oldY = y; ///紀錄移動後新的位置
glutPostRedisplay(); ///重畫畫面
}
int main(int argc, char **argv) ///(2)主要的函式 main
{
glutInit(&argc, argv); ///(3)初始Initialize你的glut參數設定
glutInitDisplayMode(GLUT_DOUBLE);
///(4)顯示模式:double buffers
glutCreateWindow("QWQ 你好我是視窗名稱!!");
///(5)建立視窗
glutMouseFunc(mouse);
glutMotionFunc(motion); ///Now: 滑鼠motion事件使用
glutMouseFunc(mouse);
glutMotionFunc(motion); ///Now: 滑鼠motion事件使用
glutDisplayFunc(display); ///(6)顯示函式display()用來畫圖的
glutMainLoop(); ///(7)主要的迴圈,用來控制程式
}
}
(4)期中考範圍
glPushMatrix(); ///備份矩陣
...;
glPopMatrix(); ///還原矩陣
glTranslatef(x,y,z); ///移動物體
glRotatef(angle,x,y,z); ///旋轉物體
glScalef(x,y,z); ///縮放物體
glBegin(Gl_......); ///開始畫
......;
glEnd(); ///結束畫
glColor3f(R,G,B); ///物體顏色
glVertex3f(x,y,z); ///物體頂點
(還沒交到部分)
glTexCoord2f(tx,ty); ///貼圖座標
glNormal3f(nx,ny,nz); ///法向量










沒有留言:
張貼留言