pancho villa
pancho villa

Reputation: 1

opengl quad not rendering

i have a GL_QUADS from a book example, i threw the color black one, and i still dont get it, all i get is my clear white screen what could be wrong?

#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
int main(int argc, char* args[])
{
    // Memory usage specifics
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    // name on the window
    SDL_WM_SetCaption( "our first game", NULL );

    //set screen
    SDL_SetVideoMode(600,400,32, SDL_OPENGL);

    // when screen is clear
    glClearColor(1,1,1,1); //red, green, blue, alpha

    //portion to display
    glViewport(0,0,600,400);

    //shade
    glShadeModel(GL_SMOOTH);

    // 3D or 2D
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); //saves

    //if only 2D?
    glDisable(GL_DEPTH_TEST);
    //this handles the main loop
    bool isRunning = true;
    //this is for the events
    SDL_Event event;

    //main rectangle
    float myX = 300;
    float myY = 350;
    float width = 50;
    float height = 30;

    //main loop
    while ( isRunning )
    {
        //EVENTS
        while ( SDL_PollEvent(&event) )
        {
            //logic that should happen for certain events
            //quiting the window
            if ( event.type == SDL_QUIT )
            {
                isRunning = false;
            }
            // if ESC pressed
            if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
            {
                isRunning = false;
            }
        }
        //LOGIC

        //RENDERING TO THE SCREEN
         glClear(GL_COLOR_BUFFER_BIT);
         //start rendering phase
         glPushMatrix();
         //set the matix
         glOrtho(0,600,400,0,-1,1);
         //black color
         glColor4ub(0,0,0,255);

         //drawing
         //GL_QUADS, GL_POINTS, GL_LINES, GL_LINE_STRIP,
         //GL_LINE_LOOP, GL_QUADS, GL_TRIANGLES, GL_POLIGON

         glBegin(GL_QUADS);
            glVertex2f(myX,myY);
            glVertex2f(myX+width,myY);
            glVertex2f(myX+width,myY+height);
            glVertex2f(myX,myY+height);
         glEnd();



        //render
        SDL_GL_SwapBuffers();
    }
    //house keeping
    SDL_Quit();
    return 0;
}

Upvotes: 0

Views: 446

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43359

This is not surprising, you have a stack overflow waiting to happen due to this line:

//start rendering phase
glPushMatrix();

Each frame, you push the "current" matrix onto the top of the stack, and never pop anything off. Eventually you will overflow and glOrtho(0,600,400,0,-1,1); will have undefined results.

Might I ask why you are pushing the matrix onto the stack in the first place? Your code would work perfectly fine if you overwrote the projection matrix on every frame.

I am also puzzled by these lines of code:

// 3D or 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //saves

glLoadIdentity (...) does not save anything, it writes an identity matrix to the current matrix:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Since glOrtho (...) multiplies the current matrix, I would actually replace the first tidbit of code I mentioned with the second. This way you will always produce the same result, instead of multiplying by the previous value of your matrix.

More importantly, however, assuming you never modify the matrix anywhere else in your code you do not have to keep setting the projection matrix everytime you draw. OpenGL keeps the state for all of its matrices across buffer swaps, clears, etc... states do not simply disappear because you begin a new frame.

Upvotes: 0

Related Questions