Jump to content
  • Advertisement
Sign in to follow this  
JCK

No Rendering with SDL/OGL

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

Hi, I've been working with both SDL and OGL but have never tried to use them together. So far every time I try creating an OGL window in SDL, I'm not seeing any rendering. The current code I'm working with includes the Brett Porter's MS3D source, though the code specific to MS3D models should not have affected the rest of the project. The SDL portion I'm using was taken from the SDL docs that show how to initialize OGL windows. Any help as to what is going on or where I can go to find solutions would be great. Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
Here ya go [smile]


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

// Bad practice, but for tutorials it's good [wink]
#pragma comment(lib,"SDLmain.lib")
#pragma comment(lib,"SDL.lib")
#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"Glu32.lib")

bool Initialize() ;
void Draw();
void Update();
void Deinitialize();

bool g_Done = 0;
Uint8 *Keys;

int g_Width = 640;
int g_Height = 480;
int g_BPP = 32;

int main(int argc, char *argv[])
{
if ( SDL_Init(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( g_Width, g_Height, g_BPP, SDL_OPENGL ) )
exit(1);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, (float)g_Width/(float)g_Height, 1, 1000 );
glMatrixMode(GL_MODELVIEW);

if( !Initialize() )
{
g_Done = 1;
}

while( g_Done == false )
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
g_Done = 1;
}

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );

Update();
Draw();

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


bool Initialize()
{
return true;
}

void Draw()
{
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();
}

void Update()
{
Keys = SDL_GetKeyState( 0 );

if( Keys[ SDLK_ESCAPE ] )
{
g_Done = 1;
}
}

void Deinitialize()
{
}





Just a very basic example that you can turn into a customizable framework. If you have any questions/comments feel free to ask. I added a few things to customize.

- Drew

[Edited by - Drew_Benton on March 29, 2005 10:20:10 PM]

Share this post


Link to post
Share on other sites
Thanks Drew, however I have already structured my code in a very similar fashion.

I did try out the SDL_OpenGL.h instead of the standard OGL headers. Still the same problem. I'm not sure if I'm setting the right GL attributes or if I'm not setting up SDL correctly.

I'm using VS.NET 2k3 if that helps narrow the problem down. Don't think that should affect it. And before anyone asks, it is set up as Win32 app with Multithreaded DLL set for SDL.

This is what I have so far. It worked before with Windows code, but I tried converting to SDL and it just seems to want to make trouble :P

static void draw_screen( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 100, 100, 100, 0, 0, 0, 0, 1, 0 );

glRotatef(yrot,0.0f,1.0f,0.0f);

pModel->draw();

yrot+=.05f;
SDL_GL_SwapBuffers();
}

static void setup_opengl( int width, int height)
{
pModel->reloadTextures();

glEnable(GL_TEXTURE_2D);
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);
}

int main( int argc, char* argv[] )
{
const SDL_VideoInfo* info = NULL;

int width = 0;
int height = 0;
int bpp = 0;
int flags = 0;

pModel = new MilkshapeModel();
//try to load the model
if ( pModel->loadModelData( "data/model.ms3d" ) == false ){
MessageBox( NULL, "Couldn't load the model", "Error", MB_OK);
quit_program( 1 );
}

/* Attempt to initialize SDL video */
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
MessageBox(NULL, "SDL Video Initialization Failed","Error", MB_OK);
quit_program( 1 );
}

/* Attempt to query the video information */
info = SDL_GetVideoInfo();
if( !info ){
MessageBox(NULL, "Video Information Query Failed","Error", MB_OK);
quit_program( 1 );
}

// Set the dimensions of our window and the bits per pixel
width = 640;
height = 480;
bpp = info->vfmt->BitsPerPixel;

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);

flags = SDL_OPENGL;

if( SDL_SetVideoMode( width, height, bpp, flags ) == NULL){
fprintf( stderr, "Video mode settings failed: %s\n",
SDL_GetError( ) );
quit_program( 1 );
}

setup_opengl( width, height);


while( 1 ){
process_events( );
draw_screen( );
}

return 0;
}

Share this post


Link to post
Share on other sites
Don't bother trying to draw models until you've done some programmatic geometry first.

I suggest something like some points with GL_POINTS or a GL_LINES or GL_LINE_LOOP.

Make sure you're drawing in a colour which shows up clearly on your background, and that you're drawing within the frustrum, and not nearer than your near plane or further than your far plane.

Ensure that you don't have blending enabled in a mode which makes things invisible. Make sure that you're not drawing the wrong face if you've got face culling enabled.

But DO make sure you've got some geometry which you are absolutely sure of its whereabouts - so hard-code it at first.

Also ensure that you clear your depth buffer (if using z-buffering) with a reasonable value. Or disable depth testing entirely while trying to make it draw something.

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by markr
Make sure you're drawing in a colour which shows up clearly on your background, and that you're drawing within the frustrum, and not nearer than your near plane or further than your far plane.


I think that is what your problem is. You do not setup your viewport at all. If you glance at my example, I have a block of code that does this (modified for your vars):


glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, (float)width/(float)height, 1, 1000 );
glMatrixMode(GL_MODELVIEW);




I think you just need to add this to the beginning of your setup_opengl function. Give that a shot and see if you can see anything.

Share this post


Link to post
Share on other sites
Quote:
Original post by markr
... and that you're drawing within the frustrum, and not nearer than your near plane or further than your far plane.


Quote:
Original post by Drew_Benton
You do not setup your viewport at all.


[embarrass] I can't believe I didn't catch that at all... omg, I should know better.

Thanks guys that helped out alot. Time to beat my head against the walls [lol]

Share this post


Link to post
Share on other sites
One thing I find very useful: Set your background clear color to something other than black. Very often, a bug where 'nothing is drawn' is actually a bug where things are being drawn, but it's only drawing black because something like a texture stage wasn't set up properly. Having a different background color will make situations like these a lot more obvious.

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!