SDL and OpenGL

Started by
47 comments, last by Stompy9999 19 years ago
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]
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Advertisement
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);}
Just so you know, the preferred, and most portable way of including SDL header files is:

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

Cheers!
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.
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 =)
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Quote:Original post by Drew_Benton
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.

*** Source Snippet Removed ***


thanks man, btw what is the default gluLookAt positions? i noticed in the md2 loader, there only a perspective (glFrustum) but no camera. if i where to place a camera, where is the default position so that i can see the object?
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
What you will want to try is:

gluLookAt( 0, 4, -60, 0, 0, 0, 0, 1, 0 );


The way it works is in this format:
Camera position (x,y,z)View point (x,y,z)Up-vector (x,y,z)


Most of the time your Up-Vector will be the same, the only thing that really changes is the position if the camera, and the point at which the camera is looking at.
Quote:Original post by Drew_Benton
What you will want to try is:

gluLookAt( 0, 4, -60, 0, 0, 0, 0, 1, 0 );


The way it works is in this format:
Camera position (x,y,z)View point (x,y,z)Up-vector (x,y,z)


Most of the time your Up-Vector will be the same, the only thing that really changes is the position if the camera, and the point at which the camera is looking at.


still dont see anything, i am using the same code you sent for the sdl md2 loader.
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
That's weird. As is, I made it so you do not need a camera initially. That is what the glTranslatef(0.0f, 4.0f, -30.0f); function was for in the Render function. It would zoom out and draw it so you can see it initially.

In that .zip file, in the VC7 workspace, you will need to remove the MD2Loading.cpp file, for it does not exist. After that you should be able to compile and see the model without any changes, so have you changed anything to the tutorial?

Take note that if you remove:
glMatrixMode( GL_PROJECTION );      glLoadIdentity();      glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, .1, 2000);      glMatrixMode(GL_MODELVIEW);

You will not be able to see anything because the viewport is then not configured.

If you are working with your own project, make sure you copied over the two model files, the .md2 and the .pcx file. See if any of that works out.
Quote:Original post by Drew_Benton
That's weird. As is, I made it so you do not need a camera initially. That is what the glTranslatef(0.0f, 4.0f, -30.0f); function was for in the Render function. It would zoom out and draw it so you can see it initially.

In that .zip file, in the VC7 workspace, you will need to remove the MD2Loading.cpp file, for it does not exist. After that you should be able to compile and see the model without any changes, so have you changed anything to the tutorial?

Take note that if you remove:
glMatrixMode( GL_PROJECTION );      glLoadIdentity();      glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, .1, 2000);      glMatrixMode(GL_MODELVIEW);

You will not be able to see anything because the viewport is then not configured.

If you are working with your own project, make sure you copied over the two model files, the .md2 and the .pcx file. See if any of that works out.


yeah i did that, and still shows up as nothing


here part of code from Main.cpp

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();	  gluLookAt( 0, 4, -60, 0, 0, 0, 0, 1, 0 );      //glFrustum(.5, -.5, -.5, 0.5, .8, 800);      //glMatrixMode(GL_MODELVIEW);	  if( !InitializeGL() )		  return 0;	  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( );            RenderScene();            SDL_GL_SwapBuffers();      }	  Shutdown();      SDL_Quit();	        return 0;}


[Edited by - Thanhda on March 27, 2005 9:22:48 PM]
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net

This topic is closed to new replies.

Advertisement