(可以參考Week03)
2.解壓縮膽案,並把glut32.dll和data資料夾貼到windows資料夾裡面
3.選擇Transformation
3.選擇Transformation

glTranslatef() → 分別是x, y, z軸的移動距離
glRotatef() → 第一個數值是轉動角度,後面三個分別是x, y, z軸的旋轉
𝄞按下滑鼠旋轉𝄞
1.將原有程式碼刪除,並打上茶壺程式碼(可複習上週week03)
2.加上可以用滑鼠來旋轉茶壺角度的程式碼
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0,angle=0; //angle為旋轉需要的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0);
glRotatef(angle, 0, 0, 1);//讓茶壺對著(0,0,1)Z軸做旋轉角度
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers();//搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y)
{
//teapotX=(x-150)/150.0;
//teapotY=(150-y)/150.0;
angle = x;
glutPostRedisplay();//重畫畫面
}
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("week04 Mouse");
glutDisplayFunc(display);
//glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
BUT!!茶壺無法連續旋轉、連續移動!
所以必須再修改,讓茶壺能更順利的移動旋轉~
3.程式碼:


♪可以用右手定則去看,大拇指是軸心,他會往其他四指所指的方向去做轉動
glScalef() → x, y, z軸的放大比例
𝄞按下滑鼠旋轉𝄞
1.將原有程式碼刪除,並打上茶壺程式碼(可複習上週week03)
2.加上可以用滑鼠來旋轉茶壺角度的程式碼
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0,angle=0; //angle為旋轉需要的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0);
glRotatef(angle, 0, 0, 1);//讓茶壺對著(0,0,1)Z軸做旋轉角度
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers();//搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y)
{
//teapotX=(x-150)/150.0;
//teapotY=(150-y)/150.0;
angle = x;
glutPostRedisplay();//重畫畫面
}
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("week04 Mouse");
glutDisplayFunc(display);
//glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
BUT!!茶壺無法連續旋轉、連續移動!
所以必須再修改,讓茶壺能更順利的移動旋轉~
3.程式碼:
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0,angle=0;
int nowRotate=0, oldx=0, oldy=0; //用來紀錄移動前的x,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();
}
void mouse(int buttom, int state, int x, int y)
{
if(buttom==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
nowRotate=1; //左鍵做旋轉
if(buttom==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
nowRotate=0; //右鍵做移動
oldx=x; oldy=y;
}
void motion(int x, int y)
{
if(nowRotate==1){
angle+=(x-oldx);
}
else{
teapotX+=(x-oldx)/150.0;
teapotY+=(oldy-y)/150.0;
}
oldx=x; oldy=y;
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 Mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}


沒有留言:
張貼留言