SDL_Surface to OpenGL texture

Questions regarding game mechanics and graphic programming should go here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

SDL_Surface to OpenGL texture

Postby taymo2020 » Sun Apr 23, 2006 1:25 pm

Ok so I am using SDL for my OGL implementation cause it's way easyer. I'm using SDL_image to load a JPG into a SDL_Surface and them load that into a OGL texture object. Once I got it down and it didn't work I did some research and it seems that my implementation should work. The img loading routine is LoadTextures althought I don't think that it is the problem. Heres the WHOLE program. It's not that long. See anything wrong?

[syntax="cpp"]
#pragma comment(lib,"sdlmain.lib")
#pragma comment(lib,"sdl.lib")
#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "GLu32.lib")
#pragma comment(lib, "SDL_Image/SDL_image.lib")

#include "SDL_image.h"
#include <sdl.h>
#include <sdl_opengl.h>

SDL_Surface *g_screen;
SDL_Surface *mobo;

bool quit_app = 0;
int bpp;
GLuint texture_one;

void LoadTextures()
{
//TEST
glEnable(GL_TEXTURE_2D);
mobo = IMG_Load("test.jpg");
if(mobo)
{
glGenTextures(1,&texture_one);
glBindTexture(GL_TEXTURE_2D,texture_one);

SDL_LockSurface(mobo);

glTexImage2D(GL_TEXTURE_2D, 3, 3, mobo->w, mobo->h, 0 , GL_RGB, GL_UNSIGNED_BYTE, mobo->pixels);

SDL_UnlockSurface(mobo);
SDL_FreeSurface(mobo);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
}
else
{
printf("IMG_Load: %s\n", IMG_GetError());
}
}

bool InitSDL()
{
if(SDL_Init(SDL_INIT_VIDEO))
{
printf("Couldn't initalize SDL!");
return 0;
}

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


if((g_screen = SDL_SetVideoMode(640,480,32,SDL_OPENGL)) == NULL)
{
printf("Couldn't set video mode! Quitting...");
SDL_Quit();
return 0;
}
return 1;
}

void SetupProjection(int width, int height)
{
if(height == 0)
{
height = 1;
}

glViewport(0,0,width,height); //Reset viewport to new dimensions
glMatrixMode(GL_PROJECTION); //set the current matrix to projection
glLoadIdentity(); // reset projection matrix

//Calculate aspect ratio of window
gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

glMatrixMode(GL_MODELVIEW); //set modelview matrix
glLoadIdentity(); //reset modelview matrix
}

bool InitOGL()
{
SetupProjection(640,480);
glEnable(GL_DEPTH_TEST);
return true;

}

void RenderScene()
{
//clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

// draw a triangle
glBegin(GL_POLYGON); // start drawing a polygon
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // we're done with the polygon

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture_one );

// draw a square (quadrilateral)
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0); glVertex2f(-1.0f, 1.0f);
glTexCoord2f(0.0,1.0); glVertex2f( 1.0f, 1.0f);
glTexCoord2f(1.0,1.0); glVertex2f( 1.0f,-1.0f);
glTexCoord2f(0.0,1.0); glVertex2f(-1.0f,-1.0f);
glEnd();

// swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers();
}


int main(int argv, char **argc)
{
InitSDL();
InitOGL();
LoadTextures();
unsigned char* keys = 0;

while(!quit_app)
{
RenderScene();
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
{
quit_app = 1;
}
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
quit_app = 1;
}
}
}

}
SDL_FreeSurface(g_screen);
SDL_Quit();

return 0;
}[/syntax]
taymo2020
 
Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Return to Games and Graphics

Who is online

Users browsing this forum: No registered users and 0 guests

cron