• Advertisement
Sign in to follow this  

Rendering with multitexture: one texture on top of the previous texture? how

This topic is 2991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two RGBA images that i want to combine as one image, they can have transparency and that transparency should be combined as well, example: Blue color means 100% transparent pixels. I found some multitexture examples for something similar, but i wasnt able to modify it to my purposes, could someone give good guide for these glTexEnvf() parameters, because they makes no sense to me :E it should be simple but this code below is nothing like simple... Or give me the correct parameters to these functions to do the trick, if you feel like that :P
	glEnable(GL_BLEND);

	glActiveTextureARB(GL_TEXTURE0_ARB );
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, g_textureID_0 );
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
	glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_COMBINE_ARB);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);

	glActiveTextureARB( GL_TEXTURE1_ARB );
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, g_textureID_1 );
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
	glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_COMBINE_ARB);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
	glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS);
	glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);
	

	
	glColor4f(1, 1, 1, 1);
 
	glBegin( GL_QUADS );
		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 0.0f );
		glVertex3f( -1.0f, -1.0f, 0.0f );

		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 1.0f );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 1.0f );
		glVertex3f( -1.0f, 1.0f, 0.0f );

		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 1.0f );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 1.0f );
		glVertex3f( 1.0f,  1.0f, 0.0f );

		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 0.0f );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 0.0f );
		glVertex3f( 1.0f, -1.0f, 0.0f );
	glEnd();

	glActiveTextureARB(GL_TEXTURE0_ARB);
	glDisable(GL_TEXTURE_2D);
	glActiveTextureARB(GL_TEXTURE1_ARB);
	glDisable(GL_TEXTURE_2D);

Any help appreciated.

Share this post


Link to post
Share on other sites
Advertisement
You have to add "glBlendfunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA" before the "glBindTexture"
edit:
But i am not shure...

[Edited by - verty on December 4, 2009 3:32:31 PM]

Share this post


Link to post
Share on other sites
oh yeah i forgot to include that to this code on this page, but i have that setting in my original code already.

Share this post


Link to post
Share on other sites
I tested that code, but it doesnt work... the wall texture works, but i only see white in the transparent areas, the pipe texture isnt there at all, code:

void render()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, g_fDistance );
glRotatef( -g_fSpinY, 1.0f, 0.0f, 0.0f );
glRotatef( -g_fSpinX, 0.0f, 1.0f, 0.0f );

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//------------------------
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); //Interpolate RGB with RGB
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
//------------------------
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE); //Interpolate ALPHA with ALPHA
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);


glColor4f(1, 1, 1, 1);

glBegin(GL_QUADS);
glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, 0.0f );

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 1.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, 0.0f );

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 1.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 1.0f );
glVertex3f( 1.0f, 1.0f, 0.0f );

glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 0.0f );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, 0.0f );
glEnd();

glActiveTextureARB(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);


SwapBuffers( g_hDC );
}




any ideas?

i know for sure that textures are loaded properly, i tested without multitexturing and the alpha is there etc.

Edit: if i put color to glColor4f(1, 1, 1, 0.5f); then i can see the pipe texture 50% and wall 50% ... doesnt make any sense. how i can make both textures show there with full opacity, just like the example image i made?

if i switch g_textureID_1 and g_textureID_0, then i see pipe texture but not wall texture at all.

Share this post


Link to post
Share on other sites
Edit: This is the best result so far i got, but its still a bit wrong:

As you see the transparent edges are copied from the underlying image on the top image, so it makes holes there.

How to fix this to match the first post example result?

My current code:
	glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//------------------------
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
//------------------------
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); //Interpolate RGB with RGB
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
//------------------------
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE); //Interpolate ALPHA with ALPHA
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);



[Edited by - Emark on December 5, 2009 9:30:59 AM]

Share this post


Link to post
Share on other sites
That depends on the alpha values that you have in g_textureID_1. Be sure that you understand what that glTexEnv code does. Don't just copy and paste.

You also have called glEnable(GL_BLEND). Are you sure you want blending on?

Share this post


Link to post
Share on other sites
yes i want to see through the texture areas that are transparent.

"Be sure that you understand what that glTexEnv code does"

can you offer some good guide for these things?

Share this post


Link to post
Share on other sites
Great, i finally made it work after long trial & error ^^

GL_ADD was the keyword.

Current code:

glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//------------------------
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
//------------------------
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); //Interpolate RGB with RGB
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
//------------------------
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD); //Interpolate ALPHA with ALPHA
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);




Another question: How i can make this whole quad 50% transparent now? if i add: glColor4f(1, 1, 1, 0.5f); then only the other texture is transparent :/ i cant make it all transparent, as it was just one image. Help?

Share this post


Link to post
Share on other sites
You would need another texture unit.

glActiveTexture(GL_TEXTURE2);
glBindTexture(...you have to bind some dummy texture here so that the unit is considered valid...);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);

so that takes result of previous stage RGB and it just outputs.
It takes alpha of previous stage and multiplies by alpha of you set with

float mycolor[4];
mycolor[0]=mycolor[1]=mycolor[2]=0.0; //RGB doesn't matter since we are not using it
mycolor[3]=0.75; //Set the blend factor with this
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, mycolor);

PS : you should get rid of this 10 year old stuff and start using shaders. That is why that page shows GLSL code examples in parallel
http://www.opengl.org/wiki/Texture_Combiners

Share this post


Link to post
Share on other sites
Oh, so i can use only 3 textures then, and the 4th texture will make it transparent...

Are shaders faster than these openGL calls im using?

Share this post


Link to post
Share on other sites
Do i have to bind the texture again if i use the same texture, but just different texcoords? looks like i have to... it doesnt feel very efficient to bind 4 times the same texture for 50000 quads every frame :/

Or is there some other way to render two planes of textures in same position without z-fighting and make it look like they are exactly one, and by using GL_DEPTH_TEST? And by "exactly one" i mean it would look like using multitextures like in the first post image i made, no depth test tweaking, polygon offsets or adjusting coordinates etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emark
Do i have to bind the texture again if i use the same texture, but just different texcoords? looks like i have to... it doesnt feel very efficient to bind 4 times the same texture for 50000 quads every frame :/


No, these are GPUs states and the GPU keeps whatever you bind to some texture unit. You don't need to make 50,000 calls to glBindTexture and that would slow things down a lot.

I don't know about faster or slower, but all GPUs do shaders. These glTexEnv things are from a day when GPUs had switches. They are no longer supported directly. The driver emulates them by creating shaders.

Quote:
Original post by Emark
Or is there some other way to render two planes of textures in same position without z-fighting and make it look like they are exactly one, and by using GL_DEPTH_TEST? And by "exactly one" i mean it would look like using multitextures like in the first post image i made, no depth test tweaking, polygon offsets or adjusting coordinates etc.

That is up to you.

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
No, these are GPUs states and the GPU keeps whatever you bind to some texture unit. You don't need to make 50,000 calls to glBindTexture and that would slow things down a lot.
i tried to remove the bind commands, but it wont draw them correctly anymore, i can see it drawing two textures on top of each other, but the rest 2 isnt drawn at all. how i can fix this?

Here's my current code:



//------------------------
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_3);

//------------------------
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_2);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD);

//------------------------
glActiveTextureARB(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD);

//------------------------
glActiveTextureARB(GL_TEXTURE3);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_textureID_0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD);






Quote:
Original post by V-man
Quote:
Original post by Emark
Or is there some other way to render two planes of textures in same position without z-fighting and make it look like they are exactly one, and by using GL_DEPTH_TEST? And by "exactly one" i mean it would look like using multitextures like in the first post image i made, no depth test tweaking, polygon offsets or adjusting coordinates etc.

That is up to you.
yes, i know i am the programmer, but what program codes i can use to do what i wanted ? (if there is any)

Share this post


Link to post
Share on other sites
Huh? I didn't say remove the bind commands. I said, optimize your code.
Keep track of the GPU states.

So make a variable such as

uint CurrentBoundTextures[4];

//Init
CurrentBoundTextures[0]=0;
CurrentBoundTextures[1]=0;
CurrentBoundTextures[2]=0;
CurrentBoundTextures[3]=0;

and in your render code

glActiveTextureARB(GL_TEXTURE0_ARB );
glEnable( GL_TEXTURE_2D );
if(CurrentBoundTextures[0] != g_textureID_0)
{
CurrentBoundTextures[0] = g_textureID_0;
glBindTexture(GL_TEXTURE_2D, CurrentBoundTextures[0]);
}


You can also keep track of glEnable :

bool TextureUnitEnabled[4];

//In your render code...
if(TextureUnitEnabled[0] == FALSE)
{
TextureUnitEnabled[0] = TRUE;
glEnable(GL_TEXTURE_2D);
}

For avoiding z-fighting, just set depth test correctly
glDepthFunc(GL_LEQUAL);

Share this post


Link to post
Share on other sites
Basically this is what you need to do, you need to render two textures on to a third texture. Then you blend them together based on what ever rules or logic through a GLSL shader (the most flexible and recommended avenue).

CPU side;

iTexID = 2;
glActiveTexture(GL_TEXTURE0 + iTexID);
glBindTexture(GL_TEXTURE_2D, FBOTextures[iTexID]);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, FBOColorAttachment[0], GL_TEXTURE_2D, FBOTextures[iTexID], 0);
// ...
glUseProgramObjectARB(shProg0);
vloc = glGetUniformLocation(shProg0, "shTexSrc"); glUniform1i(vloc, 0);
vloc = glGetUniformLocation(shProg0, "shTexRef"); glUniform1i(vloc, 1);
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
glViewport(0, 0, TEXD, TEXD); // *
// Protip for GPU shaders; set glViewport to your texture's dimensions.
// (The one you're rendering to). ;]
glDrawBuffer(FBOColorAttachment[0]);
glLoadIdentity();
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, FBOTextures[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex2f( 1.0, 1.0);
glTexCoord2f(1.0, 0.0); glVertex2f( 1.0, -1.0);
glEnd();

glBindTexture(GL_TEXTURE_2D, FBOTextures[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex2f( 1.0, 1.0);
glTexCoord2f(1.0, 0.0); glVertex2f( 1.0, -1.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glPopAttrib();
// Back to fixed pipeline (preferably another shader program...)
glUseProgramObjectARB(0);
// Unbind FBO...
// Render to screen...




GPU side, fragment shader;

uniform sampler2D shTexRef, shTexSrc;
varying vec2 TexCoord; // vector shader sets TexCoord = gl_MultiTexCoord0.st;
// *note, all of my textures in this example have the same dimensions
// so using gl_MultiTexCoord0.st works for both.

void main()
{
vec4 vA = texture2D(shTexRef, TexCoord);
vec4 vB = texture2D(shTexSrc, TexCoord);
vec4 vC = vA + vB; // Combines textures, you can do more math here, etc.
gl_FragColor = vC; // Then assign the current fragment to results.
}



[Edited by - npeqp on December 9, 2009 11:17:09 AM]

Share this post


Link to post
Share on other sites
but im using only one texture in my program, it is a tilemap, i want to blend many tiles into one so i can make different combinations easily without wasting too much mem.

so basically i shouldnt bind a texture more than once... is it possible?

--

And I am using glDepthFunc(GL_LEQUAL)

Share this post


Link to post
Share on other sites
Quote:
Original post by Emark
but im using only one texture in my program, it is a tilemap, i want to blend many tiles into one so i can make different combinations easily without wasting too much mem.

so basically i shouldnt bind a texture more than once... is it possible?

--

And I am using glDepthFunc(GL_LEQUAL)


I will repeat it again. The GPU has STATES. If you set a state, it doesn't forget. The GPU has many texture image units. When you call glActiveTexture(GL_TEXTURE0) and then glBindTexture(....), then the GPU sets up tex image unit 0 and it keeps it until YOU CHANGE IT.

To me, it looks like you are writing a 2D game which typically are simple and don't have performance problems.

If you are having z-fighting issues when rendering 2 quads on top of each other, then why don't you put some distance between the quads?

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
To me, it looks like you are writing a 2D game which typically are simple and don't have performance problems.
its a 3d game, and nowhere near "simple".

Quote:
Original post by V-man
If you are having z-fighting issues when rendering 2 quads on top of each other, then why don't you put some distance between the quads?
read above.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement