Page 1 of 2

OpenGL Texture Problem

Posted: Tue Mar 22, 2011 5:06 pm
by RandomDever

Code: Select all

	SDL_Surface *initial;
	SDL_Surface *intermediary;
	SDL_Surface *temp;
	GLtexture texture;
	SDL_Color temporary = { blue, green, red };
	
	initial = TTF_RenderText_Blended( font, text, temporary );
	
	texture.w = NextPowerof2( initial->w );
	texture.h = NextPowerof2( initial->h );
	
	intermediary = SDL_CreateRGBSurface( NULL, texture.w, texture.h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );

	SDL_BlitSurface( initial, 0, intermediary, 0 );

	glEnable( GL_TEXTURE_2D );
	int mode;

	texture.surface = intermediary;

	texture.w = texture.surface->w;
	texture.h = texture.surface->h;

	if( texture.surface->format->BytesPerPixel == 3 )
	{
		mode = GL_RGB;
	}
	else if( texture.surface->format->BytesPerPixel == 4 )
	{
		mode = GL_RGBA;
	}
	else
	{
		SDL_FreeSurface( texture.surface );
	}

	glDeleteTextures( 1, &texture.id );

	glGenTextures( 1, &texture.id );

	glBindTexture( GL_TEXTURE_2D, texture.id );

	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture.surface->w, texture.surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.surface->pixels );

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

	texture.rotation = 0;
	texture.scale = 1;
	texture.alpha = 1;

	glPushMatrix();
	glBindTexture( GL_TEXTURE_2D, texture.id );
	glDisable( GL_DEPTH_TEST );
	glEnable( GL_BLEND );
	glBlendFunc( GL_ONE, GL_ONE );

	glTranslatef( (float)x + ( texture.w / 2 ), (float)y + ( texture.h / 2 ), 0 );
	glRotatef( rotation, 0, 0, 1 );


	glColor4f( 1, 1, 1, alpha );

	glBegin( GL_QUADS );

	glTexCoord2i( 0, 0 );
	glVertex3f( (float)-texture.w / 2, (float)-texture.h / 2, 0 );

	glTexCoord2i( 1, 0 );
	glVertex3f( (float)texture.w / 2, (float)-texture.h / 2, 0 );

	glTexCoord2i( 1, 1 );
	glVertex3f( (float)texture.w / 2, (float)texture.h / 2, 0 );

	glTexCoord2i( 0, 1 );
	glVertex3f( (float)-texture.w / 2, (float)texture.h / 2, 0 );

	glEnd();
	glDisable( GL_TEXTURE_2D );
	glDisable( GL_BLEND );
	glPopMatrix();

	SDL_FreeSurface( initial );
	SDL_FreeSurface( intermediary );
	glDeleteTextures( 1, &texture.id );
For some reason the text is semi-transparent.
What am I doing wrong?

Re: OpenGL Texture Problem

Posted: Tue Mar 22, 2011 8:17 pm
by JesseGuarascia
My assumption is one of two things. The following code is from you, and it may create a problem (it has for me sometimes).

Code: Select all

   intermediary = SDL_CreateRGBSurface( NULL, texture.w, texture.h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );

   SDL_BlitSurface( initial, 0, intermediary, 0 );
Another thing may be your texture creation.

Code: Select all

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture.surface->w, texture.surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.surface->pixels );
I believe the second parameter is required for SDL based surfaces (at least, it has for me in the past). Try changing it from 0 to 3 or 4.

inb4 Falco comes in and completely trumps my ass.

Re: OpenGL Texture Problem

Posted: Tue Mar 22, 2011 8:34 pm
by xiphirx
glEnable(GL_BLEND)

Try turning that off,

glDisable(GL_BLEND)

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 10:28 am
by RandomDever
@JesseGuarascia: I tried changing it to 3 and 4 and both just replaced the texture turn white but that's probably just the quad and the texture was gone.
@xiphirx: No. If I did that then the text would be in an ugly black box.

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 10:30 am
by JesseGuarascia
Then it may be the way you're copying the texture. Alternatively, it could be the way you're using glColor4f. Try without applying glColor4f. That may get rid of any annoying transparency. If not, go over the way you are copying your texture (it could be the problem as well.)

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 12:32 pm
by xiphirx

Code: Select all

intermediary = SDL_CreateRGBSurface( NULL, texture.w, texture.h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );
That tells SDL that the surface uses ARGB, while

Code: Select all

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture.surface->w, texture.surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.surface->pixels )
Tells OpenGL to read it as RGBA

Try changing the first line to

Code: Select all

intermediary = SDL_CreateRGBSurface( NULL, texture.w, texture.h, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff );

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 1:39 pm
by RandomDever
@xiphirx: Now it disappears with blending and is a black box without blending.
@JesseGuarascia: Tried it and it didn't work and what copying are you talking about?

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 1:46 pm
by JesseGuarascia
I mean this code:

Code: Select all

   intermediary = SDL_CreateRGBSurface( NULL, texture.w, texture.h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );

   SDL_BlitSurface( initial, 0, intermediary, 0 );
I have a funny feeling that could be causing a problem, but perhaps not. Just always did for me.

If you want an example of how I do it (with no errors as far as I've seen).

Code: Select all

	// Generate the texture using the image given
	glGenTextures(1, &tex);

	glBindTexture(GL_TEXTURE_2D, tex);

	GLenum texFormat = GL_RGBA; // Format used for the texture

	// Decide which format to use
	if (sur->format->BytesPerPixel == 4){
		texFormat = GL_RGBA;
	} else if (sur->format->BytesPerPixel == 3){
		texFormat = GL_RGB;
	}

	// Create the texture
	glTexImage2D(GL_TEXTURE_2D, 0, 4, sur->w, sur->h, 
				0, texFormat, GL_UNSIGNED_BYTE, sur->pixels);

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

	// Get the sizes of the texture
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texWidth);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texHeight);

	if (glGetError() != GL_NO_ERROR){
		printf ("OpenGL Error - Creating textures failed!");
	}
The last line 4 lines are just stuff specific to my engine. I do nothing special to the textures (like you are), I just pass them in as is. Seems to work relatively well.

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 3:51 pm
by RandomDever
@ Jesse: Maybe that works for you but there is one line of code that's important:

Code: Select all

initial = TTF_RenderText_Blended( font, text, temporary );
That's the reason for all the special stuff.

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 4:28 pm
by JesseGuarascia
I use SDL_TTF for text as well, and I use that same line of code to generate the texture used by those same lines of code that I sent you. I do no other changes to anything else within the the surface.

I've never asked either, what kind of transparency problem did you have before? Was it REALLY transparent? Or was it just very slightly transparent, like almost unnoticeable?

Re: OpenGL Texture Problem

Posted: Wed Mar 23, 2011 5:02 pm
by dandymcgee
RandomDever wrote:@xiphirx: No. If I did that then the text would be in an ugly black box.
Then re-enable it before you render the text?

Re: OpenGL Texture Problem

Posted: Thu Mar 24, 2011 3:50 pm
by RandomDever
@Jesse: It looks to be about half-transparent and it is noticeable.
But as for the texture loading code I tried putting it in and it didn't work so can you show me the code for drawing your texture?

Re: OpenGL Texture Problem

Posted: Thu Mar 24, 2011 4:06 pm
by JesseGuarascia
RandomDever wrote:@Jesse: It looks to be about half-transparent and it is noticeable.
But as for the texture loading code I tried putting it in and it didn't work so can you show me the code for drawing your texture?
Sure:

Code: Select all

void Render::DrawTexturedQuad(float x, float y, float w, float h, Texture2D &texture, float rotation){
	// Draw the quad with the given texture
	glTranslatef(x, y, 0.0f);
	glRotatef(rotation, 0.0f, 0.0f, 1.0f);
	texture.Bind();

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-(w / 2), -(h / 2), 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f((w / 2), -(h / 2), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f((w / 2), (h / 2), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-(w / 2), (h / 2), 0.0f);
	glEnd();

	glLoadIdentity();
}
This draw the surface from the center point (x, y) that is given. This generally renders any texture. Texture2D's bind texture just calls "glBindTexture(GL_TEXTURE_2D, tex)" where "tex" is the texture name for the Texture2D in OpenGL.

This is the exact same thing as rendering the text, because my font class creates a Texture2D and calls this function to render. Creating a texture with my Texture2D class uses the same code I gave you earlier for generating a texture.

Re: OpenGL Texture Problem

Posted: Fri Mar 25, 2011 9:40 am
by RandomDever
@Jesse: well I tried it with your code and it appears to not be working.
when I render the texture with the initial surface nothing appears.
When I render with the intermediary surface it's a white quad so I'm about to just quit.
Because after working for 3 weeks on one core feature of my engine I still haven't finished.
I tried to implement FreeType but it was REALLY annoying so I hoped on using SDL_TTF but I can't get it to work and it's really starting to piss me off.
If someone can figure out how to do this or why my text isn't working PLEASE tell me.

Re: OpenGL Texture Problem

Posted: Fri Mar 25, 2011 9:46 am
by MrDeathNote
RandomDever wrote:@Jesse: well I tried it with your code and it appears to not be working.
when I render the texture with the initial surface nothing appears.
When I render with the intermediary surface it's a white quad so I'm about to just quit.
Because after working for 3 weeks on one core feature of my engine I still haven't finished.
I tried to implement FreeType but it was REALLY annoying so I hoped on using SDL_TTF but I can't get it to work and it's really starting to piss me off.
If someone can figure out how to do this or why my text isn't working PLEASE tell me.
If you can wait till the end of the day i'll try post some code when i get back to the house. And I know it works because I use it all the time.