魚鱗☆゚.*・。電腦圖學 Week03
1. week03_mouse ~滑鼠可以幫忙寫程式~
(1)在桌面安裝freeglut
(2)新增glut專案---> week03_mouse
(3)複製10行程式
void display()
{
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
(4)加上今天教的程式
(5)執行看看
(6)再加一行
#include <stdio.h> ///為了printf()
void mouse(int button ,int state , int x, int y)
{
///printf("Hello Mouse!\n");
printf("%d %d %d %d\n",button,state,x,y);
}
void display()
{
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse); ///(1)註冊mouse函式
glutMainLoop();
}
#4個變數分別代表的
button 按鍵 (0是左鍵,2是右鍵)
state 按下次數 (一次兩行,分別是按下和彈起)
x 橫坐標
y 縱坐標
#底下是座標軸的圖
(6)再加一行
#現在按下去就會自動寫程式了
2. 看課本範例
(1)到老師目錄:http://jsyeh.org/3dcg10
(2)下載 win32 和 data ☆゚.*・。
(3)解壓縮windows.zip和data.zip
(4)把data資料夾放入windows資料夾
(5)進入 Transformation.exe
##右視窗右鍵可換模型
##下面視窗座標可以改角度距離
##下視窗右鍵可重置數據
上面那幾行程式碼期中考會考!佔40分!
glRotate(角度, x, y, z)
y軸 (小葉老師tips:對y軸轉,簡單比個讚,完成)
x軸 (小葉老師tips:對x軸轉,要把拇指向右,也可以轉)
-y軸 (小葉老師tips:對-y軸轉,就是比個倒讚,也可以理解)
x=1,y=1,對角線轉
3. week03_mouse_Translatef ~滑鼠移動茶壺~
(1)新增glut專案---> week03_mouse_Translatef
(2)把原程式的第46行、第49行、第54行留著,其他砍掉
(3)把今天1.教的程式複製貼上
(4)改一下程式
#include <stdio.h>
float teapotX=0,teapotY=0;
void mouse(int button ,int state , int x, int y)
{
teapotX=(x-150)/150.0;
teapotY=-(y-150)/150.0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///拿原程式第46行
glPushMatrix();///拿原程式第49行
glTranslated(teapotX,teapotY,0);///自己寫
glutSolidTeapot( 0.3 );
glPopMatrix();///拿原程式第54行
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
(5)執行程式,就能移動茶壺了
4. week03_mouse_glRotatef ~滑鼠移動茶壺角度~(1)新增glut專案---> week03_mouse_glRotatef
(2)把今天3.教的程式複製貼上
float teapotX=0,teapotY=0;
void mouse(int button ,int state , int x, int y)
{
teapotX=(x-150)/150.0;
teapotY=-(y-150)/150.0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///拿原程式第46行
glPushMatrix();///拿原程式第49行
glTranslated(teapotX,teapotY,0);///自己寫
glutSolidTeapot( 0.3 );
glPopMatrix();///拿原程式第54行
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
(3)改一下程式
(4)執行程式,就能轉動茶壺了
5. week03_mouse_translate_rotate ~滑鼠同時移動和轉動茶壺~
(1)新增glut專案---> week03_mouse_translate_rotate
(2)把今天4.教的程式複製貼上
(3)改一下程式
#include <stdio.h>
float teapotX=0,teapotY=0;
float angle=0;
int method=1;///1是轉動,2是移動
int oldX=0,oldY=0;///舊的座標
void mouse(int button ,int state , int x, int y)
{
oldX=x; ///teapotX=(x-150)/150.0;
oldY=y; ///teapotY=-(y-150)/150.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();
}
void motion(int x, int y)
{
if(method==1){
angle+=x-oldX;
}else if(method==2){
teapotX+=(x-oldX)/150.0;
teapotY-=(y-oldX)/150.0;
}
oldX=x;
oldY=y;
display();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='e') method=1;///轉動
if(key=='w') method=2;///移動
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 mouse rotate");
glutDisplayFunc(display);
glutMouseFunc(mouse); ///(1)註冊mouse函式
glutMotionFunc(motion);///(3)motion細節
glutKeyboardFunc(keyboard);///按鍵
glutMainLoop();
}
(4)執行程式,就能移動茶壺了














沒有留言:
張貼留言