OpenGl Draw Stripes

D.V.D

Make a wish
Reaction score
73
So Im starting to use OpenGl for the first time and I'm following this tutorial : http://xoax.net/cpp/crs/opengl/lessons/Lesson3/

In that specific lesson, the video is showing how to draw stripes for the american flag. I wanted to just draw the stripes on my screen and looking at my code for DrawStripes and his, I cannot find any major difference to not allow my code to work. His generates stripes across the screen, each with a different color. Mine does not change the screen at all D: Im not sure if I'm doing somethnig wrong with setting color or anything but here's my code. The screen remains the same black color.

Code:
// A Simple OpenGL Project
// Author: Michael Hall
//
// This C++ code and project are provided "as is"
// without warranty of any kind. For personal use only,
// not for distribution. Copyright 2010 XoaX.
#include "stdafx.h"
#include <glut.h>
#include <cmath>
 
//Identifiers
void DrawCircle ();
void DrawStripes ();
 
void DrawStripes () {
    for ( int i=0; i < 13; i++ ) {
 
        if ( i % 2 == 0 ) {
            glColor3f(0.5,1.0,1.0);
        }
 
        else {
            glColor3f(1.0,.5,1.0);
        }
 
        float s_start_x = 0.0;
        float s_end_x = 1.0;
 
        float s_start_y = i*(1/13);
        float s_end_y = (i+1)*(1/13);
 
        glBegin(GL_QUADS);
         
            glVertex3f(s_start_x, s_start_y, 0.0); // 0,0  | 0,.1 | 0,.2
            glVertex3f(s_end_x, s_start_y, 0.0);  // 1,0  | 1,.1 | 1,.2
            glVertex3f(s_end_x, s_end_y, 0.0);    // 1,.1 | 1,.2 | 1,.4
            glVertex3f(s_start_x, s_end_y, 0.0);  // 0,0  | 0,.2 | 0,.4
 
        glEnd();
 
    }
}
 
void DrawCircle () {
    glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.25,0.25,0);
        glVertex3f(0.5,0.5,0);
        glVertex3f(0.5,0.75,0);
    glEnd();
}
 
void Draw() {
    glClear(GL_COLOR_BUFFER_BIT);
    DrawStripes();
    /*glBegin(GL_QUADS); 
        glVertex3f(0.1,0.1,0.0);
        glVertex3f(0.9,0.1,0.0);
        glVertex3f(0.1,.9,0.0);
        glVertex3f(.9,.9,0.0);
    glEnd();*/
    glFlush();
}
 
void Initialize() {
    glClearColor(0.0, 0.0, 0.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(1280, 720);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("Research Engine OpenGL");
    Initialize();
    glutDisplayFunc(Draw);
    glutMainLoop();
    return 0;
}

His code for draw stripes:

Code:
void DrawStripes() {
    for (int iStripeIndex = 0; iStripeIndex < 13; ++iStripeIndex) {
        // Alternate stripe colors
        if (iStripeIndex % 2 == 0) {
            glColor3f(204.0/255.0, 0.0, 0.0);
        } else {
            glColor3f(1.0, 1.0, 1.0);
        }
        float fStartX    = 0.0;
        float fEndX        = 1.0;
        float fStartY    = iStripeIndex*(1.0/13.0);
        float fEndY        = (iStripeIndex + 1)*(1.0/13.0);
        // The last seven stripes are shorter
        if (iStripeIndex > 5) {
            fStartX = 0.76/1.9;
        }
        glBegin(GL_QUADS);
            glVertex3f(fStartX, fStartY, 0.0);
            glVertex3f(fEndX, fStartY, 0.0);
            glVertex3f(fEndX, fEndY, 0.0);
            glVertex3f(fStartX, fEndY, 0.0);
        glEnd();
    }
}
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Took me a while to find that one nasty little bugger :D
Hard to do since I can't compile the code without glut.

The drawing code is probably perfectly fine, but you're experiencing a loss of precision during conversion of numbers:

Code:
//Your code
float s_start_y = i*(1/13);
float s_end_y = (i+1)*(1/13);

as opposed to:

Code:
//His code
float fStartY = iStripeIndex*(1.0/13.0);
float fEndY = (iStripeIndex + 1)*(1.0/13.0);

In your code 1 and 13 are integers. So you're doing an integer division. Now, integer results are always rounded down so 1/13 = 0. That makes s_start_y and s_end_y both equal 0, so your stripe is just infinitely thin.

In his code these numbers were promoted to doubles by adding a decimal point. If at least one operand in a math operation is a double, then the result will be a double too.

1.0 / 13 = 0.076..
1 / 13.0 = 0.076..
1.0 / 13.0 = 0.076..
1 / 13 = 0
 

D.V.D

Make a wish
Reaction score
73
I see, but wouldn't the division sign (1/13) automatically make the result a float variable instead of a integer?
 

DrEvil

FCRI Associate!
Reaction score
111
Just because the values don't create a perfect division(e.g. 1/13 leaving no remainder), it doesn't mean the variable type will change automatically.
One of the previous suggestions is to have at least one of the values with a decimal point.

Also just because the variable 'float fStartY' is of type float, (1/13) are both integers and therefore will do an integer division.
 

D.V.D

Make a wish
Reaction score
73
Oh I see, I get it now. Changed it and it works perfectly :) Thanks for the help guys :D
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top