Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Resizing an SDL window and drawing in GL, view position moves

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    5

    Question Resizing an SDL window and drawing in GL, view position moves

    I am a little unsure as to what exactly is going on with this, but basically I have the following code:

    Code :
    #include "SDL/SDL.h"
    #include "Button.h"
    #include "SDL/SDL_opengl.h"
    #include <iostream>
     
    using namespace std;
     
    const int SCREENWIDTH  = 800;
    const int SCREENHEIGHT = 600;
     
    void buttonAction()
    {
        cout<<"Button Pressed"<<endl;
    }
     
    void init_GL(int width, int height)
    {
        glClearColor( 0, 0, 0, 0 );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( 0, width, height, 0, -1, 1 );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    }
     
    int main( int argc, char* args[] )
    {
        //The images
        SDL_Surface* hello  = NULL;
        SDL_Surface* screen = NULL;
        bool         quit   = false;
        SDL_Event    event;
     
        int screenWidth  = SCREENWIDTH;
        int screenHeight = SCREENHEIGHT;
     
        //Start SDL
        SDL_Init( SDL_INIT_EVERYTHING );
     
        //Set up screen
        screen = SDL_SetVideoMode( screenWidth, screenHeight, 32, SDL_OPENGL | SDL_RESIZABLE );
     
        //Start OpenGL
        init_GL(screenWidth, screenHeight);
     
     
        SDL_WM_SetCaption( "Button Test", NULL );
        Button button(30, 30, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", *&buttonAction);
     
        //While the user hasn't quit
        while( quit == false )
        {
     
     
                //If there's events to handle
                if( SDL_PollEvent( &event ) )
                {
                    if(event.type == SDL_VIDEORESIZE)
                    {
                        screenWidth  = event.resize.w;
                        screenHeight = event.resize.h;
     
                        glViewport(0, 0, screenWidth, screenHeight);
                        glMatrixMode( GL_PROJECTION );
                        glLoadIdentity();
                        glOrtho( 0, screenWidth, screenHeight, 0, -1, 1 );
                        glMatrixMode( GL_MODELVIEW );
                        glLoadIdentity();
                    }
     
                    //Handle button events
                    button.handleEvents(event);
     
                    //If the user has Xed out the window
                    if( event.type == SDL_QUIT )
                    {
                        //Quit the program
                        quit = true;
                    }
                }
     
            glPushMatrix();
     
                //Fill the screen black
                glBegin( GL_QUADS );
                    glColor4f( 0.0, 0.0, 0.0, 1.0 );
                    glVertex3f( 0, 0, 0 );
                    glVertex3f( screenWidth, 0, 0 );
                    glVertex3f( screenWidth, screenHeight, 0 );
                    glVertex3f( 0, screenHeight, 0 );
                glEnd();
     
                //Show the button
                button.draw();
     
            glPopMatrix();
     
            //Update the screen
            SDL_GL_SwapBuffers();
        }
     
        //Quit SDL
        SDL_Quit();
     
        return 0;
    }

    and when i first draw everything seems to be working fine, and looks like this:
    Click image for larger version. 

Name:	opengldrawing1.jpg 
Views:	118 
Size:	5.2 KB 
ID:	795

    but when i resize i see the following:
    Click image for larger version. 

Name:	opengldrawing2.jpg 
Views:	137 
Size:	19.3 KB 
ID:	796

    any idea as to what is causing my problem?

    Thank you for your time!
    Brandon Murphy

  2. #2
    Intern Contributor
    Join Date
    Jul 2006
    Posts
    72
    You define a clear color but never actually clear the screen. Add a "glClear(GL_COLOR_BUFFER_BIT)" somewhere.

    edit:
    Code :
    //Fill the screen black
                glBegin( GL_QUADS );
                    glColor4f( 0.0, 0.0, 0.0, 1.0 );
                    glVertex3f( 0, 0, 0 );
                    glVertex3f( screenWidth, 0, 0 );
                    glVertex3f( screenWidth, screenHeight, 0 );
                    glVertex3f( 0, screenHeight, 0 );
                glEnd();
    Use the mentioned glClear(GL_COLOR_BUFFER_BIT) here instead.
    Last edited by tanzanite; 06-28-2012 at 10:18 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    5
    That fixed the issue of it drawing over itself, but the image still moves around the screen as I resize the window. I have the origin set to be the upper left corner (or so I believe, if i did it correctly).

    Thanks!
    Brandon

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2009
    Posts
    600
    You need to call glViewport when the window is resized.. Also, you really should call glClear() at the start of every frame.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    5
    I do call glViewPort when the screen is resized. I have boiled my problem down a little more and have the following code now:

    Code :
    #include "SDL/SDL.h"
    #include "SDL/SDL_opengl.h"
    #include <iostream>
     
    using namespace std;
     
    const int SCREENWIDTH  = 800;
    const int SCREENHEIGHT = 600;
     
    void init_GL(int width, int height)
    {
        glClearColor( 0, 0, 0, 0 );
        glViewport(0, 0, width, height);
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( 0, width, height, 0, -1, 1 );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    }
     
    int main( int argc, char* args[] )
    {
        SDL_Surface* screen = NULL;
        bool         quit   = false;
        SDL_Event    event;
     
        int screenWidth  = SCREENWIDTH;
        int screenHeight = SCREENHEIGHT;
     
        SDL_Init( SDL_INIT_EVERYTHING );
     
        screen = SDL_SetVideoMode( screenWidth, screenHeight, 32, SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF);
     
        init_GL(screenWidth, screenHeight);
     
        SDL_WM_SetCaption( "Button Test", NULL );
     
        while( quit == false )
        {
                if( SDL_PollEvent( &event ) )
                {
                    if(event.type == SDL_VIDEORESIZE) // The window is resized
                    {
                        screenWidth  = event.resize.w;
                        screenHeight = event.resize.h;
     
                        glViewport(0, 0, screenWidth, screenHeight);
                        glMatrixMode( GL_PROJECTION );
                        glLoadIdentity();
                        glOrtho( 0, screenWidth, screenHeight, 0, -1, 1 );
                        glMatrixMode( GL_MODELVIEW );
                        glLoadIdentity();
                    }
     
                    if( event.type == SDL_QUIT )
                    {
                        quit = true;
                    }
                }
     
            glPushMatrix();
     
                glClear(GL_COLOR_BUFFER_BIT);
     
                glTranslatef( 50, 50, 0 );
     
                glBegin(GL_LINE_STRIP);
                    glColor4f( 1.0, 1.0, 1.0, 1.0 );
     
                    glVertex3f( 0,  0,  0 );
                    glVertex3f( 50, 0,  0 );
                    glVertex3f( 50, 50, 0 );
                    glVertex3f( 0,  50, 0 );
                    glVertex3f( 0,  0,  0 );
     
                glEnd();
     
            glPopMatrix();
     
            SDL_GL_SwapBuffers();
        }
     
        SDL_Quit();
     
        return 0;
    }

    if you run that you will notice that as i make the window shorter the square that I drew moves downward, yet I am not calling a translate method on it. when i make the window taller the square moves upward. Resizing the width of the window has no effect on the position of the square.

    Thank you for your time,
    Brandon Murphy

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •