#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>
void renderScene()
{
// 清除颜色缓存
glClear(GL_COLOR_BUFFER_BIT);
const GLfloat vertices[3][2]={
{0.0,0.0},
{50.0,0.0},
{25.0,50.0}
};
// 设置顶点颜色
glColor3f(1.0,0.0,0.0);
glBegin(GL_TRIANGLES);
glVertex2fv(vertices[0]);
glVertex2fv(vertices[1]);
glVertex2fv(vertices[2]);
glEnd();
glFlush();
}
void myInit()
{
// 设置清屏颜色为白色
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50,0.0,50);
glMatrixMode(GL_MODELVIEW);
}
//主程序入口
int main(int argc, char* argv[])
{
// 初始化窗口和opengl之间的交互
glutInit(&argc,argv);
// 初始化显示模式,使用RGB颜色,使用单缓存
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
// 初始化窗口大小
glutInitWindowSize(500,500);
// 初始化窗口位置
glutInitWindowPosition(200,200);
// 创建窗口
glutCreateWindow("draw triangle");
// 绑定显示回调函数
glutDisplayFunc(renderScene);
glewInit();
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
printf_s("Ready for GLSL/n");
else {
printf_s("Not totally ready :( /n");
exit(1);
}
if (glewIsSupported("GL_VERSION_2_0"))
printf("Ready for OpenGL 2.0\n");
else {
printf("OpenGL 2.0 not supported\n");
exit(1);
}
myInit();
glutMainLoop();
return 0;
}