Mouse 滑鼠
(1)把freeglut下載到桌面並把libfreeglut.a複製改成libglut32.a
week03_mouse
功能是點GLUT視窗會在小黑顯示Hello Mouse!
(1)開GLUT專案並改名成week03_mouse
(2)貼上10行程式碼並做修改
lutMouseFunc(mouse);///註冊mouse函式
void mouse(int button, int state, int x, int y)
{
printf("Hello Mouse!\n");
}
修改成輸出滑鼠狀態
button: 案什麼鍵(0左鍵 1中鍵 2右鍵)
state: 點擊狀態(0按下 1回彈)
x :X座標
y: Y座標
(1)修改成printf(" %d %d %d %d\n",button,state,x,y);
滑鼠寫程式
當滑鼠按下時輸出座標程式碼
方便從小黑複製程式碼
(1)修改成
if(state==GLUT_DOWN)
printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y);
3D旋轉程式
(1)去https://jsyeh.org/3dcg10/下載windows跟data檔案(2)開啟Transformation.exe
右上角視窗右鍵能換模型
week03_mouse_glTranslatef
功能是滑鼠點的位置 茶壺就移動過去
(1)開GLUT專案並改名成week03_mouse_glTranslatef
(2)貼上week03_mouse程式碼並做修改
#include <GL/glut.h>///貼上mouse的程式
#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行借的
glTranslatef(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);///註冊mouse函式
glutMainLoop();
}
右手的拇指來指向量方向(當轉動軸)
其他4隻手指的轉動方向就是轉動方向
EX
座標是(1,1,1)我們以右手拇指為(0,0,0)座標
接下來指向(1,1,1)
最後揮動剩下4指就能得出旋轉方向
其他4隻手指的轉動方向就是轉動方向
week03_mouse_glRotatef
功能是滑鼠點擊並拖動,茶壺會旋轉
(1)開GLUT專案並改名成week03_mouse_glRotatef
(2)貼上week03_mouse_glTranslatef程式碼並做修改
#include <GL/glut.h>///貼上mouse_glTranslatef的程式
#include <stdio.h>
float teapotX=0,teapotY=0;
float angle =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();
///glTranslatef(teapotX, teapotY,0);
glRotatef(angle, 0, 0, 1);///對Z軸轉
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
angle = x;
display();///滑鼠動呼叫display
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03_mouse_rotate");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);///註冊motion函式
glutMainLoop();
}
功能是滑鼠點擊並拖動,茶壺會旋轉
(1)開GLUT專案並改名成week03_mouse_translate_rotate
(2)貼上week03_mouse_glRotateff程式碼並做修改
(3)設定按鍵 E W
(4)設定motion
(5)設定變數、mouse、display
#include <GL/glut.h>///貼上mouse_glRotatef的程式
#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);///46
glPushMatrix();
glTranslatef(teapotX, teapotY,0);///移動
glRotatef(angle, 0, 0, 1);///對Z軸轉
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
if(method==1){///轉動 angle
angle += x - oldX;
}else if(method==2){
teapotX += (x-oldX)/150.0;
teapotY -= (y-oldY)/150.0;
}
oldX = x;
oldY = y;
///angle = x;
display();///滑鼠動呼叫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_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03_mouse_rotate");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);///註冊motion函式
glutKeyboardFunc(keyboard);///按鍵
glutMainLoop();
}
沒有留言:
張貼留言