• Advertisement
Sign in to follow this  

SDL and OpenGL

This topic is 4709 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

Well, I've been trying to get OpenGL working outside of windows. I first tried AllegroGL which didn't work. I am currently working with SDL to interface with OpenGL. It's working so far, but I can't find any good tutorials that explain how to correctly initialize SDL with OpenGL. Does anybody know of any good tutorials on this subject?

Share this post


Link to post
Share on other sites
Advertisement
Well, I found the answer through the game programming wiki. In case anyone is interested, here is where I found the answer:

Answer

Share this post


Link to post
Share on other sites
I ran my first SDL and OpenGL program. Does anyone know why the window that is produced is seethrough? Meaning, you can see the window frame but the inside of the window is seethrough.

Share this post


Link to post
Share on other sites
Probably because the buffer contains whatever pixel data was in it before the window was created and the context initialized. Call glClear(GL_COLOR_BUFFER_BIT) to clear this random junk out.

EDIT: And if you create it with a double buffer, don't forget to call SDL_GL_SwapBuffers() after glClear(), to swap the back buffer to the front...

Share this post


Link to post
Share on other sites
Quote:
Original post by Stompy9999
Well, I found the answer through the game programming wiki. In case anyone is interested, here is where I found the answer:

Answer

I question that author's use of SDL_SWSURFACE without any explanation.

Share this post


Link to post
Share on other sites
I did clear the buffer but I didn't swap buffers so that's probably the problem. I also found out that I should download the latest SDL devpak so I could have the SDL and OpenGL template.

Share this post


Link to post
Share on other sites
heh, I guess its too late since you figured out, but I just added a link to my tutorial about it to my sig.

Oh well at least I get to test it [smile]

Share this post


Link to post
Share on other sites
The wierdest thing happened. I put in the swap buffer code and compiled it again. When it ran, it showed a repeating image of a city in the window. This isn't my wallpaper though. I tried to get screenshots but my print screen key isn't working. What is it?![wow]

Share this post


Link to post
Share on other sites
How can I make a screenshot so I can show you guys what is going on? This is really weird...

My print screen key isn't allowing to copy the screenshot to MS paint.

Share this post


Link to post
Share on other sites
Quote:
Original post by Stompy9999
How can I make a screenshot so I can show you guys what is going on? This is really weird...

My print screen key isn't allowing to copy the screenshot to MS paint.


Because you need to program that functionality. Try taking a screen shot of Media Player playing a video and you'll get the same thing. The video is in your video card's memory. PrintScreen's default functionality will just copy the overlay - like a blue screen (but probably pink).

Share this post


Link to post
Share on other sites
Well, I installed version 1.2.8 of SDL and it replaced the city that used to show up with a grey blur. Here is a screenshot of what is going on:

Screenshot

Here is the source code:



#include <SDL/SDL.h>
#include <gl/gl.h>

/*
PLEASE NOTE: the program will require SDL.dll which is located in
dev-c++'s dll directory. You have to copy it to you
program's home directory or the path.
*/


int main(int argc, char *argv[]){
SDL_Event event;
float theta = 0.0f;

SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE);

glViewport(0, 0, 600, 300);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);

int done;
for(done = 0; !done;){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(theta, 0.0f, 0.0f, 1.0f);

glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();

theta += .5f;
SDL_GL_SwapBuffers();
SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
}
SDL_Quit();
return(0);
}




Any suggestions?

Share this post


Link to post
Share on other sites
Heh. It's anybody's guess what's going on there. I tested that code and it works fine for me, if fine means a rapidly spinning red/blue/green triangle. Which it does. I'd say your problems lie elsewhere, perhaps corrupted libraries or DLLs. Try checking the various error codes possibly generated by SDL or OpenGL and see if anything funny shows up.

Share this post


Link to post
Share on other sites
Quote:
Original post by Stompy9999
Any suggestions?


First, let's make sure SDL can be initialized [wink]:


// First thing to do is initialze SDL
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
abort();
}


See if that does anything in terms of telling you if SDL was initialized or not. If it was see if this code works for you:


#include <stdio.h>

#include <stdlib.h>

#include <SDL/SDL.h>

#include <windows.h>

#include <GL/gl.h>

#include <GL/glu.h>



#pragma comment(lib,"SDLmain.lib")

#pragma comment(lib,"SDL.lib")

#pragma comment(lib,"OpenGL32.lib")

#pragma comment(lib,"Glu32.lib")



int main(int argc, char *argv[])

{

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )

{

printf("Unable to init SDL: %s\n", SDL_GetError());

exit(1);

}

atexit(SDL_Quit);



SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );

SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );

SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );

SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );



if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL ) )

exit(1);



glMatrixMode( GL_PROJECTION );

glLoadIdentity();

glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, 1, 50);

glMatrixMode(GL_MODELVIEW);



bool done = 0;

while( !done )

{

SDL_Event event;

while ( SDL_PollEvent(&event) )

{

if ( event.type == SDL_QUIT )

done = 1;



if ( event.type == SDL_KEYDOWN )

if ( event.key.keysym.sym == SDLK_ESCAPE )

done = 1;

}

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glLoadIdentity( );

/****************************************/

glTranslatef( 0, 0, -5 );

glColor3f(0.5f,0.5f,1.0f);

glBegin(GL_QUADS);

glVertex3f(-1.0f, 1.0f, 0.0f);

glVertex3f( 1.0f, 1.0f, 0.0f);

glVertex3f( 1.0f,-1.0f, 0.0f);

glVertex3f(-1.0f,-1.0f, 0.0f);

glEnd();

/****************************************/

SDL_GL_SwapBuffers();

}

SDL_Quit();

return 0;

}




Sorry about the formatting, its from my web page [lol].

- Drew

Share this post


Link to post
Share on other sites
OMG, it's even doing this with non-SDL OpenGL apps. I wrote a windows OpenGL app and it did the same thing!

WHAT IS GOING ON?!

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Quote:
Original post by Stompy9999
Any suggestions?


First, let's make sure SDL can be initialized [wink]:


// First thing to do is initialze SDL
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
abort();
}


See if that does anything in terms of telling you if SDL was initialized or not. If it was see if this code works for you:

*** Source Snippet Removed ***

Sorry about the formatting, its from my web page [lol].

- Drew


hey nice to see some SDL OGL samples. but would you know by anychance how to do it without having to include windows.h?

Share this post


Link to post
Share on other sites
Quote:
Original post by Thanhda
hey nice to see some SDL OGL samples. but would you know by anychance how to do it without having to include windows.h?


Actually yes! I will make a sample of that.


#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_OpenGL.h>

#pragma comment(lib,"SDLmain.lib")
#pragma comment(lib,"SDL.lib")
#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"Glu32.lib")

int main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL ) )
exit(1);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, 1, 50);
glMatrixMode(GL_MODELVIEW);

bool done = 0;

while( !done )
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
done = 1;

if ( event.type == SDL_KEYDOWN )
if ( event.key.keysym.sym == SDLK_ESCAPE )
done = 1;
}

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );

/****************************************/
glTranslatef( 0, 0, -5 );
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
/****************************************/
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}




That's it! [wink]. I need to do some updating of my articles. I have had someone give me a list of fixes and suggestions I am working on right now. This is done in my SDL vs GLFW article but not this one, I'm slacking!

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Quote:
Original post by Thanhda
hey nice to see some SDL OGL samples. but would you know by anychance how to do it without having to include windows.h?


Actually yes! I will make a sample of that.

*** Source Snippet Removed ***

That's it! [wink]. I need to do some updating of my articles. I have had someone give me a list of fixes and suggestions I am working on right now. This is done in my SDL vs GLFW article but not this one, I'm slacking!

- Drew


excellent it works, thanks!

Share this post


Link to post
Share on other sites
Guys, check out the NeHe basecode. It has ports to SDL+OpenGL, It's listed as "linux/SDL", though i'm sure it compiles fine on windows too. It just provides the basic "open window, wait to press ESC" functionality:

basecode link

Tutorial page

Share this post


Link to post
Share on other sites
Thanks leiavoia! I myself have seen that and used it before, but the reason I made this version is that it is something a little bit more simpler and cleaner in showing just how to set it up. In the SDL docs they have a nasty example of doing it. So it is quite confusing [headshake].

However, I would like to point out that in some of the tutorials, the SDL code will not work properly on Windows. This is just because it was made for Linux, and while the syntax is the same for Windows, the procedures are not. The code I am referring to is just the way of setting up the window - everything else is great to use.

Share this post


Link to post
Share on other sites
okay another question, when you use SDL with ogl, how do you implement the 2d in the 3d? like for gui, when i try to code it, it will only show the 2d not
the 3d.

Code:


#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_OpenGL.h>

#pragma comment(lib,"SDLmain.lib")
#pragma comment(lib,"SDL.lib")
#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"Glu32.lib")

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;

int xpos=0,ypos=0;

int InitImages()
{
back = SDL_LoadBMP("bg.bmp");
image = SDL_LoadBMP("image.bmp");
return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
//DrawIMG(back, 0, 0);
}

void DrawScene()
{
DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
DrawIMG(image, xpos, ypos);

SDL_Flip(screen);
}

void render()
{
glTranslatef( 0, 0, -5 );
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}


int main(int argc, char *argv[])
{
Uint8* keys;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL ) )
exit(1);
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );



glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, 1, 50);
glMatrixMode(GL_MODELVIEW);

bool done = 0;

InitImages();
//DrawBG();

while( !done )
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 1; }
if ( keys[SDLK_DOWN] ) { ypos += 1; }
if ( keys[SDLK_LEFT] ) { xpos -= 1; }
if ( keys[SDLK_RIGHT] ) { xpos += 1; }

DrawScene();

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );

render();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}



[Edited by - Thanhda on March 28, 2005 1:53:24 PM]

Share this post


Link to post
Share on other sites
In order to get 2D in OpenGL you will need to take a look at the glOrtho function. NeHe's lesson 32 shows this. You just setup a new model view, draw your 2D objects then switch back to 3D. Here's an example based of that tutorial. You should take a look at it though to know exactly what is going on. You will have to make a few modifications in terms of the size of the 2D screen as well.


void OpenGL2D()
{
RECT window; // Storage For Window Dimensions
GetClientRect (g_window->hWnd,&window); // Get Window Dimensions
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0,window.right,0,window.bottom,-1,1); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glTranslated(mouse_x,window.bottom-mouse_y,0.0f); // Move To The Current Mouse Position


.... Draw 2D Stuff here ....

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glMatrixMode(GL_MODELVIEW);
}


Share this post


Link to post
Share on other sites
Just so you know, the preferred, and most portable way of including SDL header files is:

#include "SDL.h"
#include "SDL_opengl.h"

Cheers!

Share this post


Link to post
Share on other sites
Quote:
Original post by James Trotter
Just so you know, the preferred, and most portable way of including SDL header files is:

#include "SDL.h"
#include "SDL_opengl.h"

Cheers!


Ah yes - I've yet to implement setting the compiler directories yet [wink]. It was something I've learned of only as of recent [smile] I will be making those changes in my next update.

Share this post


Link to post
Share on other sites
Quote:
Original post by James Trotter
Just so you know, the preferred, and most portable way of including SDL header files is:

#include "SDL.h"
#include "SDL_opengl.h"

Cheers!



i like to keep them in seperate folders to keep it organized so i know where everything is =)

Share this post


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

  • Advertisement