初学OpenGL,写了段关于glutMouseFunc函数的示例。新手,代码写的不规范的地方请指正。
#include<stdio.h>
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(3);
glBegin(GL_POINTS);
glVertex2i(0,0);
glEnd();
glFlush();
}
void mymouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
while(x<500&&y<500){
printf("%d,%d\n",x,y);
glBegin(GL_LINES);
glVertex2i(0,0);
glVertex2i(x-250,250-y);
glVertex2i(x-250,250-y);
glEnd();
glFlush();
x=x+3;
y=y+3;
}}
}
init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-250.0,250.0,-250.0,250.0);
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("ok");
glutDisplayFunc(display);
glutKeyboardFunc(mykey);
init();
glutMouseFunc(mymouse);
glutMainLoop();
}
随鼠标的点击而画图,执行效果: