本周目標:讓茶壺移動
#include <GL/glut.h>
float teapotX=0, teapotY=0;///teapot的位置,一開始在0,0
void display()
{
glClearColor(1,1,0, 1);///用來指定Clear的顏色(R,G,B, alpha);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///2種模式
glPushMatrix();///備份矩陣
glTranslatef(teapotX, teapotY, 0);///glTranslatef(x,y,z);///移動的參數要準備好!!
glColor3f(0,1,0);///標示顏色
glutSolidTeapot(0.3);
glutSwapBuffers();///交換畫Buffer便會顯示
}
#include <stdio.h>
void mouse(int button, int state, int x, int y)
{
printf("%d %d %d %d\n", button, state, x, y);
///(滑鼠鍵:左鍵0 中鍵1 右鍵2,狀態:點擊0 放開1,X座標,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();///貼個便利貼Post,有空要記得重畫畫面Redisplay
}
int main(int argc, char**argv)///進階的main()參數(個數,字串)
{
glutInit(&argc, argv);///進階參數,初始GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///2種模式
glutCreateWindow("Week03 Mouse操作");
glutDisplayFunc(display);
glutMouseFunc(mouse);///滑鼠按鈕
glutMotionFunc(motion);///滑鼠移動
glutMainLoop();///主要迴圈
}


沒有留言:
張貼留言