#week03
##mouse 滑鼠
1.桌面[葉正聖老師上課用] freeglut 拉到桌面 把lib-libfreeglut.a複製成libglut32.a
2.開GLUT week03_mouse
3.十行程式貼上
4.改完後
#include <GL/glut.h>
#include <stdio.h>///為了printf()
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{///定義mouse函數 button:0左鍵 1中鍵 2右鍵
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("week03 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);///設定mouse函式
glutMainLoop();
}
#移動 glTranslatef(x,y,z)
1.https://jsyeh.org/3dcg10/
2.下載data.zip 跟windows.zip
3.解壓縮後把 data拉進windows下
##week03
float teapotX =0,teapotY =0;///新加的程式用來放茶壺的座標
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
glPushMatrix();///備份矩陣
glTranslatef(teapotX,teapotY,0);///把座標移動
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
#旋轉
glRotatef(轉動角度,x,y,z)
1.右手定則 Z軸從畫面破出來
#include <GL/glut.h>
#include <stdio.h>///為了printf()
float teapotX =0,teapotY =0;///新加的程式用來放茶壺的座標
float angle = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
glPushMatrix();///備份矩陣
///glTranslatef(teapotX,teapotY,0);///把座標移動
glRotatef(angle,0,0,1);///最難的Z軸
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
} ///state:0 :下去 1:上來
void mouse(int button,int state,int x,int y)
{///做巧妙的修正
teapotX = (x-150/150.0);
teapotY = -(y-150/150.0);
}
void motion(int x,int y)
{
angle = x;
display();
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);///設定mouse的motion動作
glutMainLoop();
}
###縮放
#include <GL/glut.h>
#include <stdio.h>///為了printf()
float teapotX =0,teapotY =0;///新加的程式用來放茶壺的座標
float angle = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
glPushMatrix();///備份矩陣
glTranslatef(teapotX,teapotY,0);///把座標移動
glRotatef(angle,0,0,1);///最難的Z軸
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
} ///state:0 :下去 1:上來
int method = 1;
int oldX = 0,oldY = 0;
void mouse(int button,int state,int x,int y)
{///做巧妙的修正
///teapotX = (x-150/150.0);
///teapotY = -(y-150/150.0);
oldX = x;
oldY = y;
}
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;///旋轉Rotate
if(key=='w')method=2;///移動Translate
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);///設定mouse的motion動作
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言