Reputation: 2629
This simple program was designed to draw 13 even, red and white stripes placed alternately, as in the US flag.
// 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;
}
This is how it looks in practice: Why there are blue triangles on the foreground? I've specified only "QUADS" inside Draw/DrawStripes functions.
Upvotes: 1
Views: 457
Reputation: 3092
The blue comes from your clear color:
glClearColor(0.0, 0.0, 102.0/255.0, 0.0);
That third parameter is the Blue channel of the clear color. When you call glClear(GL_COLOR_BUFFER_BIT); you initialize the render target with the clear color.
Thus the blue you are seeing is from your vertices not covering up the entire render target, and letting the blue clear color through. To see it, enable wireframe (glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);).
Also you really ought to draw in a counter-clock wise fashion: top left, bottom left, bottom right, top right. That will result in your polygons being drawn front-facing (which generally is what you want to avoid back-face culling).
At the moment your are "folding" the polygon, by doing top left, top right, bottom left, then bottom right you essentially twist the quad. Try in clockwise order (invert bottom right and bottom left) and see if you can still see the polygons (you shouldn't if back face culling is on).
Finally you also multiply only the bottom right Y coordinate by 2, why ? That is what is causing your deformation (on top of the twisting). You should have both bottom vertices with the same Y coordinate to make a rectangle (so bottom right and bottom left Y coordinate must match).
Upvotes: 3
Reputation: 35923
I think you're drawing the quad vertices in the wrong order. They should be counterclockwise, but you're drawing a 'twisted' quad.
Try swapping the 3rd and 4th vertex in your for loop.
Upvotes: 2