Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL with SDL doesn't show anything


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1   Members   

400
Like
0Likes
Like

Posted 13 February 2013 - 05:37 PM

I wrote a short OpenGL/SDL program (this is the first time I've done so). The program is based off the one in the OpenGL Red Book but, nothing is on screen when I run it.

 

Here's the code:

#include <stdio.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

SDL_Surface *screen;

SDL_Event event;

int main(void)
{

 SDL_Init(SDL_INIT_VIDEO); //Initilization

 SDL_WM_SetCaption("\"Hello, World!\"", "\"Hello, World!\""); //The window caption is "Hello, World!"

 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );     //-+
 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );   // |
 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );    // |-Window attributes
 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );  // |
 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); //-+

 screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL); //Create a window

 //Drawing
  //Setup

   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0, 1.0, 1.0);
   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

  //Polygon

   glBegin(GL_POLYGON);
    glVertex2f(0.0, 0.0);
    glVertex2f(0.0, 240.0);
    glVertex2f(320.0, 240.0);
    glVertex2f(320.0, 0.0);
   glEnd();
   glFlush();

 while(1)
 {
  glFlush();
  SDL_Flip(screen); //Update the screen

 }

 return 0;
}

 



#2   Members   

133
Like
0Likes
Like

Posted 13 February 2013 - 07:00 PM

SDL_Flip() is the wrong function, SDL_GL_SwapBuffers() or something is necessary for opengl, flip is for the SDL drawing.

 

edit: And I think those vertexes are going to be in the next county with those matrices loaded, but I think they will cover the screen.  

 

edit: also not sure the backbuffer is guaranteed to work that way in OpenGL, where repeated calls to flip without drawing will maintain the image.  I would just draw it once with an SDL_Delay(2000); or something just to see if it's working.  and while I'm at it:

 

 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

make the glOrtho call essentially a no op, since you LoadIdentity afterwards.  

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(.......);

 

is basically the way you always do it.  


Edited by DaDweeDow, 13 February 2013 - 07:10 PM.


#3   Members   

300
Like
0Likes
Like

Posted 13 February 2013 - 09:03 PM

Your draw code needs to be in your while loop.



#4   Members   

102
Like
0Likes
Like

Posted 14 February 2013 - 01:38 AM

Hey,

 

 

I actually ran into this problem a while ago myself.

 

I think you need to use something like GLEW and just make a call to

glewInit();

 

but this might be just if you are using the modern graphics technique of shaders.

 

And yah, your draw code should ideally be in your while loop.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.