0
\$\begingroup\$

This semester I have a class that I using OpenGL and I'm trying open a window with random colors inside rectangles.

For this, I created a class that I define RGB in constructor. If I use debugging mode and I pass step by step, OpenGL renders rectangles with the correct colors but if I run my app in Release mode, all the rectangles receive the last color set.

It is strange because I created two objects. I think that each object sould have its own properties. Am I correct?

Bellow I put two screenshots, one with the program in debugging mode and the other in Release mode.

I'm a beginner in OpenGL and have some questions in c++.

Thanks,

Debugging mode

Same program but without debugging mode

And bellow is my code:

Constructor of my class square GEO_Square.cpp:

GEO_Square::GEO_Square()
{
    srand(time(NULL));  
    red = ((rand() % 10) / 10.0);
    green = ((rand() % 10) / 10.0);
    blue = ((rand() % 10) / 10.0);
}

//Function to draw
void GEO_Square::drawSquare()
{
    fillVectorWithPosition(width, height, posX, posY); //function to calculate positions


    glBegin(GL_QUADS);      
    glColor3f(red, green, blue); 
        glVertex2f(positions[0], positions[1]);
        glVertex2f(positions[2], positions[3]);
        glVertex2f(positions[4], positions[5]);
        glVertex2f(positions[6], positions[7]); 
    glEnd();

    drawEdge();
    changeColor();
}

ColorGameMain.cpp:

int CALLBACK WinMain(
    __in  HINSTANCE hInstance,
    __in  HINSTANCE hPrevInstance,
    __in  LPSTR lpCmdLine,
    __in  int nCmdShow
)
{    
    DrawRecs dRecs;     
    GLFWwindow* window;

    if (!glfwInit()) return -1;

    window = glfwCreateWindow(WIDTH, HEIGHT, "Forms", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);     
    gluOrtho2D(0.0, WIDTH, HEIGHT, 0.0);

    GEO_Square* quad = new GEO_Square();
    GEO_Square* quad2 = new GEO_Square();

    while (!glfwWindowShouldClose(window))
    {   
        quad->setWidth(40.0);
        quad->setHeight(15.0);
        quad->setPosX(0.0);
        quad->setPosY(0.0);
        quad->drawSquare();    

        quad2->setWidth(40.0);
        quad2->setHeight(15.0);
        quad2->setPosX(60.0);
        quad2->setPosY(20.0);
        quad2->drawSquare();

        quad->drawSquare(40.0, 15.0, 100.0, 40.89, 0.5, 0.5, 0.0);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Please show the code you have \$\endgroup\$
    – Bálint
    Sep 7, 2016 at 14:41
  • \$\begingroup\$ Hi Bálint, I put my code above \$\endgroup\$ Sep 7, 2016 at 14:55

1 Answer 1

1
\$\begingroup\$

It's not an issue with OpenGL, it's an issue with the way you use srand().

Take srand out of there and put it in your main.

What happens is that the code executes so fast in release that you initialize the pseudo-random-number-generator with the same seed, which results in the same sequence of generated pseudo-random numbers, thus giving you the exact same scheme of colours.

It "works" in debug because it takes some time between the first quad and the second quad so the time you use to seed the generator is not the same, thus resulting in different sequences.

\$\endgroup\$
2
  • \$\begingroup\$ Hi Alexandre, thanks! I put srand on main and work. \$\endgroup\$ Sep 7, 2016 at 15:04
  • \$\begingroup\$ @GabrielCastroFernandes Glad I could help! :) \$\endgroup\$
    – Vaillancourt
    Sep 7, 2016 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .