[SDL] SDL/OpenGL and texture mapping.

Byron Wright bslayerw at mindspring.com
Tue Sep 10 08:14:00 PDT 2002


Could you briefly expain what this code does? It looks like you are
creating a surface with alpha for every image, bliting the image to it
and using it to create a texture. It doesn't really explain my I am
loosing a channel when creating the texture. Thank you for the code, I
will try it out and see if it fixes my problem.

-Byron

-----Original Message-----
From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
Matthias
Sent: Tuesday, September 10, 2002 2:45 AM
To: SDL Mailing List
Subject: Re: [SDL] SDL/OpenGL and texture mapping.


Hello

I've just the same problem a few monts before, here are (my) solutions.
Be free and suggest improvements!

just say in your main-code: texture[0]=gload_mipmap("Box12b.jpg");
texture[1]=gload_texture("Box12b.jpg");

I know for a big project this would be suck, but for leaning OpenGL and
working around it's easy.

Here's the code:

template <class foo> void swap(foo &a, foo &b)
{
	foo temp=a;
	a=b;
	b=temp;
}
void rotate_180d(SDL_Surface *image)
{
	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]);
}
SDL_Surface *prepare_image(SDL_Surface* sur)
{
	if(!sur)
	{
		cout<<SDL_GetError()<<endl;
		sur=IMG_Load("failpic.png");

		if(!sur)
		exit(-1);
	}

	SDL_Surface *image=SDL_CreateRGBSurface(SDL_HWSURFACE, sur->w,
sur->h, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);

	Uint32 saved_flags=sur->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
	Uint8  saved_alpha=sur->format->alpha;
	SDL_Rect area;
	area.x = 0;
	area.y = 0;
	area.w = sur->w;
	area.h = sur->h;

	if((saved_flags&SDL_SRCALPHA)==SDL_SRCALPHA)
	SDL_SetAlpha(sur, 0, 0);

	SDL_BlitSurface(sur, &area, image, &area);
    	SDL_FreeSurface(sur);

	rotate_180d(image);
	return image;
}
GLuint gload_texture(const char *filename)
{
	GLuint texture;
	SDL_Surface* image=prepare_image(IMG_Load(filename));

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
   	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0,
GL_RGBA, 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);

	return texture;
}
GLuint gload_mipmap(const char *filename)
{
	GLuint texture;
	SDL_Surface* image=prepare_image(IMG_Load(filename));

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image->w, image->h,
GL_RGBA, 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_MIPMAP_NEA
REST);

    	SDL_FreeSurface(image);

	return texture;
}



Hope this would help you :)

best regards

funthing


_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl





More information about the SDL mailing list