Sign in to follow this  
Iiimage

OpenGL/SDL texture not mapping to plane

Recommended Posts

I just recently started trying to use OpenGL with SDL and I have an idea why its not working but no idea on how to fix it

I think it has something to do with this function

void SDL_loadGLTexture(SDL_Surface* bmpFile, GLuint &textr, GLsizei n/*, int red = NULL, int green = NULL, int blue = NULL*/)
{

/* Standard OpenGL texture creation code */
glPixelStorei(GL_UNPACK_ALIGNMENT,4);

glGenTextures(n,&textr);
glBindTexture(GL_TEXTURE_2D,textr);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, 3, bmpFile->w, bmpFile->h,
0, GL_BGR, GL_UNSIGNED_BYTE, bmpFile->pixels);

}



if thats not it, it might be in one of these but I honestly have no idea


SDL_Surface* LoadBMP2texture (char* filename, GLuint &text, GLsizei n)
{
SDL_Surface* temp=SDL_LoadBMP(filename);
SDL_loadGLTexture(temp,text,n);
return temp;
}



void DisplayImage(float x, float y, SDL_Surface* image, GLuint textr, float angle, float scale, bool center=false)
{
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, textr);
glTranslatef(x,y,0);
glRotatef(angle,0.0f,0.0f,1);
glBegin(GL_QUADS);
if (center == true)
{
glTexCoord2f(0.0f, 0.0f); glVertex3f(-(image->w/2)*scale, -(image->h/2)*scale,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f((image->w/2)*scale, -(image->h/2)*scale, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f((image->w/2)*scale, (image->h/2)*scale, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-(image->w/2)*scale, (image->h/2)*scale, 0);
}
else
{
glTexCoord2f(0.0f, 0.0f); glVertex3f(1*scale, 1*scale,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(image->w*scale, 1*scale, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(image->w*scale, image->h*scale, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1*scale, image->h*scale, 0);
}
glEnd();
}


Please help :\

Share this post


Link to post
Share on other sites
Have you called "glEnable(GL_TEXTURE_2D);" anywhere in your program? OpenGL won't sample texels in the fixed pipeline unless you enable texturing.

Share this post


Link to post
Share on other sites
Yeah right here:


void SDL_GL_INIT(SDL_Surface* &surface, int WIDTH, int HEIGHT)
{
SDL_Init(SDL_INIT_VIDEO);
surface = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL);
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);

glViewport(0, 0, WIDTH, HEIGHT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 640, 480, 0, 1, -1);

glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

glLoadIdentity();
}

Share this post


Link to post
Share on other sites
Quote:
Original post by karwosts
Please describe in more detail what "not working" means.


it does everything I want except put the image on the plane, they are just white blocks... and I look at my code and nothing looks wrong to me. but obviously it is haha.

Share this post


Link to post
Share on other sites
Hmm I don't notice anything wrong after a bit of scrutiny. Have you tried calling glGetError in your rendering loop? If it returns nonzero then you have a problem somewhere.

Share this post


Link to post
Share on other sites
Can you give me an example haha... as I said I recently started using it (VERY RECENT) and I haven't really got into error checking yet...

Share this post


Link to post
Share on other sites
glGetError

Each time you make a call to opengl it can create an error. This will never be known to you unless you check for errors. You should always check at least once during each main loop so that you know if something is going wrong.

You just put this:

int err = glGetError();



Somewhere in your main loop (not between glBegin and glEnd though, that's illegal).

If it returns zero then you are fine. Any other number is an error code that you can use to diagnose what is wrong. They are not very descriptive, so you have to do a bit of investigating to find out what is wrong. You can end up with some code like this:


int err;
err = glGetError(); //A (returns 0)
glBlahBlah();
glBlahBlah();
glBlahBlah();
err = glGetError(); //B (returns 0)
glBlahBlah();
glBlahBlah();
glBlahBlah();
err = glGetError(); //C (returns nonzero)



In this case you know that somewhere between B and C you've done something illegal. But just once per frame is enough to tell if there's anything wrong, the error is stored until you check for error (and then cleared).

If you get an error post the block of code that you've narrowed it down to and we can look at it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this