想用OpenGL的GLUT库自己写一个简单的关于鼠标响应的小例子,要求如下:
绘制一个茶壶,当左键按下时,使其跟随鼠标移动(
嘿嘿,就这么弱智的一个小例子,可就是没出结果,
还请多多指教啊,我可是OpenGL的菜鸟级初学者)
我自己写的代码如下:(不知道哪里错了,左键按下时没有任何的反应)
/**//*
* Teapot move when leftbutton down
*/
#include <windows.h>
#include <gl/glut.h>
static GLdouble red = 1.0 ;
static GLdouble green = 1.0 ;
static GLdouble blue = 1.0 ;
float spinX , spinY ;
int curX , curY , myX , myY ;
void init ( void )
{
glClearColor ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
glShadeModel ( GL_FLAT ) ;
}
void display ( void )
{
glClear ( GL_COLOR_BUFFER_BIT ) ;
glColor3f ( red , green , blue ) ;
glutSolidTeapot ( 50 ) ;
glutSwapBuffers () ;
}
void reshape ( int w , int h )
{
glViewport ( 0 , 0 , ( GLsizei ) w , ( GLsizei ) h ) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity () ;
glOrtho ( -w / 2 , w / 2 , -h / 2 , h / 2 , -100.0 , 100.0 ) ;
glMatrixMode ( GL_MODELVIEW ) ;
glLoadIdentity () ;
}
void myKeyboard ( unsigned char key , int x , int y )
{
switch ( key )
{
case 'r':
case 'R':
red = 1.0 ;
green = 0.0 ;
blue = 0.0 ;
break ;
case 'g':
case 'G':
red = 0.0 ;
green = 1.0 ;
blue = 0.0 ;
break ;
case 'b':
case 'B':
red = 0.0 ;
green = 0.0 ;
blue = 1.0 ;
break ;
default :
break ;
}
glutPostRedisplay () ;
}
void motion ( int xPos , int yPos )
{
myX = xPos ;
myY = yPos ;
glutPostRedisplay () ;
}
void mouse ( int button , int state , int mouseX , int mouseY )
{
switch ( button )
{
case GLUT_LEFT_BUTTON :
curX = mouseX ;
curY = mouseY ;
motion ( curX , curY ) ;
break ;
}
}
int main ( int argc , char** argv )
{
glutInit ( &argc , argv ) ;
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ) ;
glutInitWindowSize ( 800 , 600 ) ;
glutInitWindowPosition ( 100 , 100 ) ;
glutCreateWindow ( " Teapot Move " ) ;
init () ;
glutDisplayFunc ( display ) ;
glutReshapeFunc ( reshape ) ;
glutKeyboardFunc ( myKeyboard ) ;
glutMouseFunc ( mouse ) ;
glutMotionFunc ( motion ) ;
glutMainLoop () ;
return 0 ;
}
还请多多指教啊,本人非常愿意听取他人的意见,感谢啊!!