OpenGL Texture Problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

OpenGL Texture Problem

Post 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?
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL Texture Problem

Post 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.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: OpenGL Texture Problem

Post by xiphirx »

glEnable(GL_BLEND)

Try turning that off,

glDisable(GL_BLEND)
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Texture Problem

Post 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.
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL Texture Problem

Post 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.)
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: OpenGL Texture Problem

Post 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 );
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Texture Problem

Post 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?
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL Texture Problem

Post 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.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Texture Problem

Post 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.
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL Texture Problem

Post 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?
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: OpenGL Texture Problem

Post 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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Texture Problem

Post 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?
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL Texture Problem

Post 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.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Texture Problem

Post 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.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: OpenGL Texture Problem

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply