这个简单的程序旨在绘制 13 条均匀的、交替放置的红色和白色条纹,就像美国国旗一样。
// A Simple OpenGL Project
// Author: Michael Hall
//
// This C++ code and project are provided "as is" without warranty of any kind.
//
// Copyright 2010 XoaX - For personal use only, not for distribution
//
// Elaborated by me :)
#include <glut.h>
void DrawStripes(const int quantity)
{
glBegin(GL_QUADS);
for(int i=1; i <= quantity ; i++)
{
if(i%2)
glColor3f(1.0,0.0,0.0);
else
glColor3f(1.0,1.0,1.0);
glVertex2f(0,static_cast<float>(i-1)/static_cast<float>(quantity));
glVertex2f(1.0,static_cast<float>(i-1)/static_cast<float>(quantity));
glVertex2f(0,static_cast<float>(i)/static_cast<float>(quantity));
glVertex2f(1.0,2*static_cast<float>(i)/static_cast<float>(quantity));
}
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
DrawStripes(13);
glEnd();
glFlush();
}
void Initialize()
{
glClearColor(0.0, 0.0, 102.0/255.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int iArgc, char** cppArgv)
{
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(950,500);
glutInitWindowPosition(200, 200);
glutCreateWindow("Rough draft");
Initialize();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
这就是它在实践中的样子: 为什么前景上有蓝色三角形?我在 Draw/DrawStripes 函数中只指定了“QUADS”。