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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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