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();
    }
}
 
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
 
I see, but wouldn't the division sign (1/13) automatically make the result a float variable instead of a integer?
 
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.
 
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 The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top