Jump to content
  • Advertisement
Sign in to follow this  
grim_scythe

OpenGL Problems with OpenGL and SDL

This topic is 3812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having problems trying to get OpenGL to work with SDL. I am learning both at the moment and so I am not sure what's wrong with the code. All I get is the blank screen no 2d images were drawn. Can anyone help? ( I am using VSC 5 Pro) #include <SDL.h> #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #define WIN32_EXTRA_LEAN #include <windows.h> #endif #include <glut.h> HDC hDC=NULL; // Private GDI Device Context HGLRC hRC=NULL; // Permanent Rendering Context HWND hWnd=NULL; // Holds Our Window Handle HINSTANCE hInstance; // Holds The Instance Of The Application bool keys[256]; // Array Used For The Keyboard Routine bool active=TRUE; // Window Active Flag Set To TRUE By Default bool fullscreen=TRUE; // Set To Fullscreen Mode By Default int draw(void) { //NEW//////////////////NEW//////////////////NEW//////////////////NEW///////////// glTranslatef(-1.5f,0.0f,-6.0f); // Move 1.5 Left And 6.0 Into The Screen. glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing glTranslatef(3.0f,0.0f,0.0f); // Move Right glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Le glEnd(); return true; } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); draw(); SDL_GL_SwapBuffers(); } /** * Initialize OpenGL */ int initGL() { glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glViewport(0,0,500,500); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix return true; } int main(int argc, char *argv[]) { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return 1; } //Set up the screen SDL_Surface *screen = SDL_SetVideoMode( 500, 500, 32, SDL_OPENGL | SDL_RESIZABLE); //If there was in error in setting up the screen if( screen == NULL ) return 1; // set double buffering SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Initialize OpenGL state machine initGL(); //Set the window caption SDL_WM_SetCaption( "trackball", NULL ); bool done = false; // Main event loop SDL_Event event; while((!done)) { if(SDL_PollEvent(&event) != 0) { switch(event.type) { case SDL_QUIT: done = true; break; default: break; } display(); } } SDL_Quit(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Although technically I suppose you can use SDL, GLUT and Win32 together there is really no sane reason to do so. Pick one and use it. I would strongly recommend just using SDL as it gives you everything you need in a nice, easy to use API. GLUT is dead, and Win32 is just painful to use. So get rid of the following lines at the top of your source:

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows.h>
#endif
#include <glut.h>


HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application


I am not familiar with "VSC 5 Pro". Surely you don't mean Microsoft Visual C++ version 5 (circa 1996)? The newest version of Visual Studio is 2008, which is freely available from Microsoft. I would strongly suggest you get it.
Now that you have downloaded a compiler from this century, and gotten rid of the unnecessary code, I only see one problem. You need to set up your projection matrix properly. Right now you just call glLoadIdentity(), so you have the identity matrix as your projection matrix. Not a particularly useful configuration. You should use gluOrtho or glPerspective to help build your projection matrix depending on whether you want a parallel or perspective projection. Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
You should use gluOrtho or glPerspective to help build your projection matrix depending on whether you want a parallel or perspective projection.

glOrtho and gluPerspective :P

Share this post


Link to post
Share on other sites
Well that's the thing my assignment is to use both SDL and OpenGL together...Yeah it would be easier to use one but I can't....I redid my Init() in this. Also yeah I upgraded to Visual Studio 8 from the service pack.



#include <SDL.h>
#include <GL/glut.h>
#include <SDL_opengl.h>

int draw(void)
{

glTranslatef(-1.5f,0.0f,-6.0f); // Move 1.5 Left And 6.0 Into The Screen.
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing

glTranslatef(3.0f,0.0f,0.0f); // Move Right
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Le

glEnd();
return true;

}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
draw();
SDL_GL_SwapBuffers();
}


/**
* Initialize OpenGL
*/
int initGL()
{
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

return true;
}

int main(int argc, char *argv[])
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}

//Set up the screen
SDL_Surface *screen = SDL_SetVideoMode( 500, 500, 32, SDL_OPENGL | SDL_RESIZABLE);

//If there was in error in setting up the screen
if( screen == NULL ) return 1;
// set double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Initialize OpenGL state machine
initGL();
//Set the window caption
SDL_WM_SetCaption( "trackball", NULL );

bool done = false;
// Main event loop
SDL_Event event;
while((!done))
{

if(SDL_PollEvent(&event) != 0)
{
switch(event.type)
{



case SDL_QUIT:
done = true;
break;

default:
break;
}
display();
}

}


SDL_Quit();
return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by grim_scythe
Well that's the thing my assignment is to use both SDL and OpenGL together...Yeah it would be easier to use one but I can't....I redid my Init() in this. Also yeah I upgraded to Visual Studio 8 from the service pack.



#include <SDL.h>
#include <GL/glut.h>
#include <SDL_opengl.h>

int draw(void)
{

glTranslatef(-1.5f,0.0f,-6.0f); // Move 1.5 Left And 6.0 Into The Screen.
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing

glTranslatef(3.0f,0.0f,0.0f); // Move Right
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Le

glEnd();
return true;

}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
draw();
SDL_GL_SwapBuffers();
}


/**
* Initialize OpenGL
*/
int initGL()
{
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

return true;
}

int main(int argc, char *argv[])
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}

//Set up the screen
SDL_Surface *screen = SDL_SetVideoMode( 500, 500, 32, SDL_OPENGL | SDL_RESIZABLE);

//If there was in error in setting up the screen
if( screen == NULL ) return 1;
// set double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Initialize OpenGL state machine
initGL();
//Set the window caption
SDL_WM_SetCaption( "trackball", NULL );

bool done = false;
// Main event loop
SDL_Event event;
while((!done))
{

if(SDL_PollEvent(&event) != 0)
{
switch(event.type)
{



case SDL_QUIT:
done = true;
break;

default:
break;
}
display();
}

}


SDL_Quit();
return 0;
}


Here is some source code for a simple opengl/sdl example:

http://nehe.gamedev.net/data/lessons/linuxsdl/lesson03.tar.gz

Here is the windows version of the program:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=03

Share this post


Link to post
Share on other sites
Quote:
Well that's the thing my assignment is to use both SDL and OpenGL together

There is no problem with that. SDL and OpenGL work beautifully together. What I was saying is you don't need to use GLUT (glut.h) or Win32 (windows.h) if you are using SDL. They are mutually exclusive libraries, not complimentary. You still have a #include <GL/glut.h> at the top you can get rid of.
I see you have a glOrtho call. That is fine, but you are making your frustum too small. The first call to glTranslatef(-1.5f,0.0f,-6.0f) puts you outside the bounds, and so nothing is drawn. Either increase your view volume, or don't translate quite as much.

Share this post


Link to post
Share on other sites
There is also at least one problem with the initialization code. You need to do SDL_GL_SetAttribute before requesting the video surface or (I think) it won't do anything. At least every SDL+OpenGL tutorial I've seen does it that way. There are also some other attributes you should probably set, so I'd suggest finding yourself an SDL/OpenGL tutorial to look at how they do it. Personally I'd stay away from NeHe, particularly since the earlier linked example has at least one minor error and doesn't include the additional attributes I mentioned. This one seems better to me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!