# week03 mouse
桌面裝 freeglut,把lib的libfreeglut.a複製改成 libglut32.a
開新專案,複製10行程式碼,再加3行
#include <stdio.h>
void mouse(int button, int state, int x, int y)
{
printf("Hello Mouse!\n");
}
glutMouseFunc(mouse);
把hello mouse 改掉 印參數
printf("%d %d %d %d\n", button , state, x, y);
}
再改 滑鼠寫程式
if(state==GLUT_DOWN)
printf("glVertex2f((%d-150)/150.0), -(%d-150)/150.0);\n", x, y);
#看課本範例
請到jsyeh.org/3dcg10下載課本範例 data.zip windows.zip
windows.zip-transformation.exe
data.zip-windows\data 一堆模型檔
試試 transformation.exe
glTranslatef(x,y,z)
glRotatef(角度,x,y,z)
## week03_mouse_glTranslatef
先偷原本46.49.54 程式碼 和上一個的
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);
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
# 轉動---右手定則(讚)
轉動軸--拇指,方向為其他手指握的方向
拇指: x--右 y--上 z-- 對著自己
#week03_glRotatef
新專案,把上一個程式碼拿來用
增加 float angle = 0
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 0, 1);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
angle = x;
display();
}
下面再加glutMotionFunc(motion);
#week03_mouse_translate_rotate
前一個程式碼拿來用
#include <GL/glut.h>
#include <stdio.h>
float teapotX = 0, teapotY = 0;
float angle = 0;
int method = 1;
int oldX = 0, oldY = 0;
void mouse(int button, int state, int x, int y)
{
oldX = x;
oldY = 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 motion(int x, int y)
{
if(method==1){
angle += x-oldX;
}else if(method==2){
teapotX += (x-oldX)/150.0;
teapotY -= (y-oldY)/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("2024電腦圖學第03週");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言