0. 桌面安裝 freeglut,把 lib 的l ibfreeglut.a複製成 libglut32.a
1.File-New-Project ,Glut 專案week03_mouse記得目錄(桌面),Glut目錄(桌面/freeglut)
2.貼上10行程式碼
3.加上3行mouse
#include <GL/glut.h>///第18行存下來
#include <stdio.h>
void mouse(int button,int state,int x,int y)
{
printf("Hellow Mouse!\n");
}
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char*argv[])///第138行,main()函式
{
glutInit(&argc, argv);///140行, 開啟GLUT功能
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///148行,設定顯示模式
glutCreateWindow("week03 mouse");///145行,開一個
glutDisplayFunc(display);///148行,要用display()函式來畫圖
glutMouseFunc(mouse);
glutMainLoop();///174行, 主要的迴圈,在這裡一直運作,不結束
}
4.修改程式碼將printf("Hellow Mouse!\n");改成printf("%d %d %d %d\n",button,state,x,y);
#include <GL/glut.h>///第18行存下來
#include <stdio.h>
void mouse(int button,int state,int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
}
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char*argv[])///第138行,main()函式
{
glutInit(&argc, argv);///140行, 開啟GLUT功能
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///148行,設定顯示模式
glutCreateWindow("week03 mouse");///145行,開一個
glutDisplayFunc(display);///148行,要用display()函式來畫圖
glutMouseFunc(mouse);
glutMainLoop();///174行, 主要的迴圈,在這裡一直運作,不結束
}
5.修改程式碼將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);
#include <GL/glut.h>///第18行存下來
#include <stdio.h>
void mouse(int button,int state,int x,int y)
{
///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);
}
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char*argv[])///第138行,main()函式
{
glutInit(&argc, argv);///140行, 開啟GLUT功能
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///148行,設定顯示模式
glutCreateWindow("week03 mouse");///145行,開一個
glutDisplayFunc(display);///148行,要用display()函式來畫圖
glutMouseFunc(mouse);
glutMainLoop();///174行, 主要的迴圈,在這裡一直運作,不結束
}
week03_mouse_glTranslatef
4.File-New-Project ,Glut 專案 week03_mouse_glTranslate
5.複製程式碼並修改
#include <GL/glut.h>///第18行存下來
#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);
glPushMatrix();
glTranslatef(teapotX, teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char*argv[])///第138行,main()函式
{
glutInit(&argc, argv);///140行, 開啟GLUT功能
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///148行,設定顯示模式
glutCreateWindow("week03 mouse");///145行,開一個
glutDisplayFunc(display);///148行,要用display()函式來畫圖
glutMouseFunc(mouse);
glutMainLoop();///174行, 主要的迴圈,在這裡一直運作,不結束
}
試著了解 g;Rotatef(角度,x,y,z)的參數
1.可以利用右手比讚來了解
2.轉動軸,就是讚的那個拇指,轉動方向,就是其他手指轉動的方向
3.對Y軸轉,簡單比個讚,完成
4.對X軸轉,要把拇指向右,也可以轉
5.對-Y軸,就是比個倒讚,也可以理解
6.最難的是Z軸,向著哪裡呢?那個拇指向著你的鼻子
week03_mouse_glRotatef
1.File-New-Project ,Glut 專案 week03_mouse_glRotatef
2.複製程式並修改程式碼
#include <GL/glut.h>
#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);
glPushMatrix();
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
angle=x;
display();
}
int main(int argc, char*argv[])
{
glutInit(&argc, argv);///140行, 開啟GLUT功能
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 mouse");///145行,開一個
glutDisplayFunc(display);///148行,要用display()函式來畫圖
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();///174行, 主要的迴圈,在這裡一直運作,不結束
}
week03_mouse_translate_roltate
1.File-New-Project ,Glut 專案 mouse_translate_roltate
2.w 可以上下左右 e可以選轉
#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);///140行, 開啟GLUT功能
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 mouse");///145行,開一個
glutDisplayFunc(display);///148行,要用display()函式來畫圖
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();///174行, 主要的迴圈,在這裡一直運作,不結束
}
沒有留言:
張貼留言