openGL 初试 绘制三角形 和添加鼠标键盘事件
code:
#include <gl/glut.h> #include <stdlib.h> void render(void); void keyboard(unsigned char c,int x,int y); void mouse(int button,int state,int x,int y); int main(int argc , char** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow("simple glut application"); glutDisplayFunc(render); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMainLoop(); } //鼠标事件 void mouse(int button,int state,int x,int y){ if(button == GLUT_RIGHT_BUTTON ){ exit(0); } } //键盘事件 void keyboard(unsigned char c,int x,int y){ if(c==27){ exit(0); } } void render(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); //分别设置三点的颜色是整体产生变化色 //设置颜色 glColor3f(1,0,0); //绘制点 glVertex2f(-0.5,-0.5); //设置颜色 glColor3f(0,1,0); //绘制点 glVertex2f(0.5,-0.5); //设置颜色 glColor3f(0,0,1); //绘制点 glVertex2f(0.0,0.5); glEnd(); glutSwapBuffers(); }
效果: