在Windows系统中,安装GLUT库:打开glut-3.7.6-bin.zip,取出glut.h,glut32.lib,glut32.dll,分别放置。
使用Visual Studio C++编译已有项目工程,并修改代码生成以下图形:
在此基础上,修改各顶点颜色,使得每个顶点颜色不一样,多边形内部颜色渐变。
#include "gl/glut.h"
void redraw()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
//glColor3f(1, 0, 0);
glColor3f(1,1,1);
glVertex3f(0, 0, 0.5);
//开始画1
//
glColor3f(1.0, 0, 0);
glVertex3f(0, 0.5, 0.5);
glColor3f(1.0, 0, 0);
glVertex3f(0.1, 0.3, 0.5);
//2
glColor3f(1.0, 0, 1.0);
glVertex3f(0.4, 0.3, 0.5);
glColor3f(1.0, 0, 1.0);
glVertex3f(0.25, 0, 0.5);
//3
glColor3f(0, 0, 1.0);
glVertex3f(0.4, -0.3, 0.5);
glColor3f(0, 0, 1.0);
glVertex3f(0.1, -0.3, 0.5);
//4
glColor3f(0, 1.0, 1.0);
glVertex3f(0, -0.5, 0.5);
glColor3f(0, 1.0, 1.0);
glVertex3f(-0.1, -0.3, 0.5);
//5
glColor3f(0, 1.0, 0.0);
glVertex3f(-0.4, -0.3, 0.5);
glColor3f(0, 1.0, 0.0);
glVertex3f(-0.25, 0, 0.5);
//6
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.4, 0.3, 0.5);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.1, 0.3, 0.5);
//初始位置
glColor3f(1.0, 0, 0.0);
glVertex3f(0, 0.5, 0.5);
glEnd(); // GL_QUADS
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
int windowHandle
= glutCreateWindow("Simple GLUT App");
glutDisplayFunc(redraw);
glutMainLoop();
return 0;
}
主要就是如何简单地调用opengl中的函数画三角形,并且填色。