-滑鼠,鍵盤,移動(複習點線面顏色)-
1.親手重寫GLUT範例
#include <GL/glut.h>//使用GLUT外掛
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
glutSolidTeapot(0.3);
glutSwapBuffers();//交換畫面buffer便會顯示
}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
glutInit(&argc,argv);//進階參數,初始GLUT
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
glutCreateWindow("Week03 Mouse操作");//盡量不要用中文
glutDisplayFunc(display);//等一下樓上要準備 void display()
//glutMouseFunc(mouse);//等一下也會準備好 mouse()功能,按鈕
//glutMotionFunc(motion);//等一下也會準備好 motion()移動
glutMainLoop();//主要迴圈
}
2.上色 3.加入滑鼠事件
#include <GL/glut.h>void display()
{
glClearColor(1,1,0,1); //背景
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0); //茶壺
glutSolidTeapot(0.3);
glutSwapBuffers();
}
#include <stdio.h>
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse操作");
glutDisplayFunc(display);
glutMouseFunc(mouse);
//glutMotionFunc(motion);//等一下也會準備好 motion()移動
glutMainLoop();
}
4.加上移動
#include <GL/glut.h>
float teapotX=0, teapotY=0;///teapot的位置,一開始在0,0
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glTranslatef(teapotX, teapotY, 0);//glTranslatef(x,y,z);////移動的參數要準備好
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
#include <stdio.h>
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);
}
void motion(int x, int y)
{
teapotX=(x-150)/150.0; teapotY=-(y-150)/150.0;///把你的mouse的位置,設成teapot的位置
printf("%d %d\n", x, y);///印出移動的數字
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse操作");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);///等一下也會準備好 motion(),移動
glutMainLoop();
}


沒有留言:
張貼留言