[SDL] Opengl textures?

Matthias matthias at zislers.de
Sat Jul 6 08:32:00 PDT 2002


Hi

how to create textures for Opengl with help of SDL?
the following code work not
but the odd thing is if I replace the glTexImage2D with gluBuild2DMipmaps
it works, but tehn only the last loaded texture is on everything, it's always
bound even if gl_BindTexture is not called!

Please help me!



gload_texture(&texture[0], "texture.tga");

void gload_texture(GLuint *texture, const char *filename)
{
	string ext = strrchr(filename, '.');
	SDL_Surface* image=IMG_Load(filename);

	if(!image)
	{
		cout<<"failed to load texture \""<<filename<<endl;
		return;
	}

	int width=image->w;
    	int height=image->h;
    	unsigned char *data=(unsigned char *)(image->pixels);
    	int BytesPerPixel=(image->format->BytesPerPixel);

	for(int i=0; i<(height/2); ++i)
	for(int j=0; j<width*BytesPerPixel; j+=BytesPerPixel)
	for(int k=0; k<BytesPerPixel; ++k)
	swap(data[(i*width*BytesPerPixel)+j+k], data[((height-i-1)*width*BytesPerPixel)+j+k]);


	glGenTextures(1, texture);
	glBindTexture(GL_TEXTURE_2D, *texture);

	if(ext==".tga" || ext==".png" || ext==".xfc" || ext==".tif")
   	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0,	GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
	else
   	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);

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

    	SDL_FreeSurface(image);
}

btw, how to test if an image has alpha?


Thank in advance
--
funthing





More information about the SDL mailing list