電腦圖學 Week04
這禮拜要教用滑鼠mouse 來寫的程式
1.printf出滑鼠的參數
=== 紅色是新增的程式碼 ==
#include <GL/glut.h> ///使用GLUT外掛
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
glutSolidTeapot(0.3); ///實心茶壺
glutSwapBuffers(); ///交換兩倍的buffers
}
void mouse(int button,int state,int x,int y)
{
printf("button:%d state:%d x:%d y:%d\n",button,state,x,y);
}
int main(int argc,char ** argv)
{
glutInit( &argc,argv); ///GLUT初始設定
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
glutCreateWindow("08161106貝貝寶貝貝"); ///開視窗
glutDisplayFunc(display); ///等一下要顯示的函式
glutMouseFunc( mouse );
glutMainLoop(); ///主要迴圈
}
2.用滑鼠來點座標出來
#include <GL/glut.h> ///使用GLUT外掛
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
glBegin(GL_POLYGON);
glVertex3f((70-150)/150.0,-(32-150)/150.0,0);
glVertex3f((137-150)/150.0,-(79-150)/150.0,0);
glVertex3f((217-150)/150.0,-(42-150)/150.0,0);
glVertex3f((171-150)/150.0,-(146-150)/150.0,0);
glVertex3f((142-150)/150.0,-(86-150)/150.0,0);
glVertex3f((100-150)/150.0,-(184-150)/150.0,0);
glVertex3f((228-150)/150.0,-(220-150)/150.0,0);
glVertex3f((258-150)/150.0,-(144-150)/150.0,0);
glVertex3f((261-150)/150.0,-(245-150)/150.0,0);
glVertex3f((130-150)/150.0,-(260-150)/150.0,0);
glVertex3f((34-150)/150.0,-(249-150)/150.0,0);
glVertex3f((70-150)/150.0,-(175-150)/150.0,0); ///這是用滑鼠點出來的座標
glEnd();
glutSwapBuffers(); ///交換兩倍的buffers
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){
printf("glVertex3f((%d-150)/150.0,-(%d-150)/150.0,0);\n",x,y);
}
}
int main(int argc,char ** argv)
{
glutInit( &argc,argv); ///GLUT初始設定
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
glutCreateWindow("08161106貝貝寶貝貝"); ///開視窗
glutDisplayFunc(display); ///等一下要顯示的函式
glutMouseFunc( mouse );
glutMainLoop(); ///主要迴圈
}
3.用滑鼠來移動茶壺
#include <GL/glut.h> ///使用GLUT外掛
#include <stdio.h>
float teapotX=0, teapotY=0; ///茶壺的座標 -1.0~~~+1.0
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
glPushMatrix(); ///矩陣備份
glTranslatef( teapotX, teapotY, 0); ///照著座標移動
glutSolidTeapot(0.3);
glPopMatrix(); ///矩陣還原
glEnd();
glutSwapBuffers(); ///交換兩倍的buffers
}
void motion(int x,int y) ///mouse motion 的函式
{
teapotX = (x-150)/150.0; ///換算座標
teapotY = -(y-150)/150.0;
display(); ///可以讓畫面很流暢
}
int main(int argc,char ** argv)
{
glutInit( &argc,argv); ///GLUT初始設定
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
glutCreateWindow("08161106貝貝寶貝貝"); ///開視窗
glutDisplayFunc(display); ///等一下要顯示的函式
glutMotionFunc( motion ); ///準備mouse motion移動時的函式
glutMainLoop(); ///主要迴圈
}




沒有留言:
張貼留言