[SDL] Opengl textures?

Krata krata at matfyz.cz
Sun Jul 7 14:33:01 PDT 2002


On Sat, 6 Jul 2002, Matthias wrote:

> 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!
> 

Of course it shouldn't work. In glTexImage2D you can't give width and 
height which is not 2^n (or +1 if using borders) Read OpenGL tutorials or 
redbook. 
How to determine if surface have alfa channel? I am checking 
SDL_Surface->format->Amask (maybe it is wrong but it is functional for me 
purposes)
Krata

> 
> 
> 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
> 
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
> 





More information about the SDL mailing list