Accidental Texture Blending

Started by
8 comments, last by AverageJoeSSU 15 years, 2 months ago
I think that my textures are blending, but it doesnt make sense. glEnable(GL_TEXTURE_2D); // Texture mapping ON glActiveTexture( GL_TEXTURE0); // We set the active texture glBindTexture(GL_TEXTURE_2D, id_texture); cgGLSetTextureParameter(Lighting->myCgFragmentParam_Textur, id_texture); glActiveTexture( GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, id_normal_map); cgGLSetTextureParameter(Lighting->myCgFragmentParam_NormalMap, id_normal_map); is this code valid?? i even put glDisable(GL_BLEND), but it didnt change anything.

------------------------------

redwoodpixel.com

Advertisement
not enuf infomation u will need to show eg the the shader used
+ possibly a screenshot

btw
this is ignored with shaders
glEnable(GL_TEXTURE_2D); // Texture mapping ON

eg set it to glDisable(GL_TEXTURE_2D);
+ it will display the same
is there a way to embed images in a post? or do i have to put it on my own server?
nm figured it out... gonna put em up then i'll post em.

------------------------------

redwoodpixel.com

Ok so the shaders are for deferred shading.

Normal Buffer

is what my normal buffer looks like which is clearly wrong.

This has nothing to do with textures but you guys might find interesting.
my position buffer outputs this.....

Position Buffer

but my results (cube from before our engine had terrain) dont really tell me that there is anything wrong.

Results

Now, when i setup the shader textures here

glEnable(GL_TEXTURE_2D); // Texture mapping ON
glActiveTexture( GL_TEXTURE0); // We set the active texture
glBindTexture(GL_TEXTURE_2D, id_texture);
cgGLSetTextureParameter(Lighting->myCgFragmentParam_Textur, id_texture);

glActiveTexture( GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, id_normal_map);
cgGLSetTextureParameter(Lighting->myCgFragmentParam_NormalMap,
id_normal_map);

.... the easiest buffer, the diffuse buffer, makes the planet look like a normal map if the normal map is bound. But if i comment out the normal map binding code, it looks like it should (a texture of the earth). this behavior doesnt sound right... i can post my shaders if neccesary when i get home.... i can also take more screens.

Phew....

------------------------------

redwoodpixel.com

Post the shader. That's probably where the problem is...
-----------------------------------------Everyboddy need someboddy!
That seems redundent. Why do you need to call glBindTexture and cgGLSetTextureParameter?
What tutorial are you reading?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
i was going along with the nvidia sdk xmas tree example...

lightingTimer.startTimer();

// bind deferred shading buffers
glActiveTexture( GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, normal);
glActiveTexture( GL_TEXTURE1);
glBindTexture( GL_TEXTURE_2D, material);
glActiveTexture( GL_TEXTURE2);
glBindTexture( GL_TEXTURE_2D, pos);

//draw a simple directional light to give base lighting
glUseProgram( defProg);

glBegin( GL_QUADS);
glVertex2f( 0.0f, 0.0f);
glVertex2f( 1.0f, 0.0f);
glVertex2f( 1.0f, 1.0f);
glVertex2f( 0.0f, 1.0f);
glEnd();


obviously not Cg, could be that its not needed.
I'll post the shaders when i get home.

[Edited by - AverageJoeSSU on January 14, 2009 4:32:54 PM]

------------------------------

redwoodpixel.com

Ok, so just a heads up, these shaders are doing a lot of stuff, all of which I have not had the chance for someone to go over because i'm the only person i know that does shader stuff. I apologize in advance if some stuff seems "noobish"

here are my "G-Buffer" Shaders...

vertex shader..

void GbufferV(  float4 position : POSITION,                float3 normal : NORMAL,                float2 texCoord : TEXCOORD0,				float3 tangent : TEXCOORD1,               out float4 oPosition : POSITION,			out float4 ObjPos : TEXCOORD4,			out float3 oNormal : TEXCOORD1,			out float3 oTangent : TEXCOORD2,            out float2 oTexCoord : TEXCOORD0,			 		uniform float4x4 ModelViewProj,        uniform float4x4 ModelToWorld,		uniform float4x4 ModelViewIT,		uniform float3 Sem,		uniform float3 eyePositionW )    {        	//Position Calculations	float4 pos = mul(ModelViewProj, position);	oPosition.xyzw = pos;		ObjPos.xyzw = mul( ModelToWorld, position);	oTexCoord = texCoord;	oNormal =  mul( (float3x3)ModelViewIT, normal );	oTangent = tangent;    	    }


and the Fragment G-buffer shader....

float3x3 invert_3x3( float3x3 M ){	float D = determinant( M );			float3x3 T = transpose( M );		return float3x3(		cross( T[1], T[2] ),		cross( T[2], T[0] ),		cross( T[0], T[1] ) ) / D;	}float3x3 compute_tangent_frame( float3 N, float3 p, float2 uv ){	// Implemenataion with full matrix inverse	// straight from theory.    // get edge vectors of the pixel triangle    float3 dp1 = ddx( p );    float3 dp2 = ddy( p );    float2 duv1 = ddx( uv );    float2 duv2 = ddy( uv );    // solve the linear system    float3x3 M = float3x3( dp1, dp2, cross( dp1, dp2 ) );    float3x3 inverseM = invert_3x3( M );    float3 T = mul( inverseM, float3( duv1.x, duv2.x, 0 ) );    float3 B = mul( inverseM, float3( duv1.y, duv2.y, 0 ) );    // construct tangent frame     float maxLength = max( length(T), length(B) );    return float3x3( T / maxLength, B / maxLength, N );}float3 compress(float3 v){	return 0.5 * (v + 1.0);}float3 expand(float3 v){	return (2.0 * v) - 1.0;}struct fs_mrt{      float4 color0 : COLOR0;   float4 color1 : COLOR1;   float4 color2 : COLOR2;};fs_mrt GbufferF( float4 oPosition : TEXCOORD4,			     float2 oTexCoord : TEXCOORD0,			     float3 oNormal : TEXCOORD1,                 float3 Sem : TEXCOORD3,				 float3 oTangent : TEXCOORD2,				uniform sampler2D Textur, 		uniform sampler2D NormalMap,		uniform float4x4 ModelViewProj				)	{		fs_mrt OUT;		float3 Normal = oNormal;	    		float3 vnormal = tex2D(NormalMap, oTexCoord).xyz;		float4 diffuse = tex2D(Textur, oTexCoord);		float3 binormal = cross(oNormal, oTangent);				float3x3 TangentFrame = compute_tangent_frame( vnormal, oPosition.xyz, oTexCoord );				/*float3x3 rotMat= float3x3(oTangent,							binormal,							oNormal);*/				float3 snormal = mul( TangentFrame , vnormal );		OUT.color0 = float4( oPosition.xyz, Sem.x );		OUT.color1 = float4( snormal.xyz , Sem.y);		OUT.color2 = float4( diffuse.xyz , Sem.z );		return OUT;	}


and then the lighting pass vertex, fragment.... full screen quad drawn
void LightV( float4 position : POSITION,			float2 texcoord : TEXCOORD0,					out float4 oPosition : POSITION,		out float2 oTex : TEXCOORD0, 				uniform float4x4 ModelViewProj 		){	oPosition = mul(ModelViewProj, position);	oTex = texcoord;}


float3 expand(float3 v){	return (2.0 * v) - 1.0;}//Blinn-Phong Lighting model, uses RTs and light variables as input.//Note: Specular power causing black artifacts, commented out for now.void LightF(	float4 color : COLOR,			out float4 oColor : COLOR,		float2 texcoord : TEXCOORD0, 	uniform float3 lightColor,	uniform float3 lightPosition,	uniform float4 Ke,	uniform float4 Ka,	uniform float4 Kd,	uniform float4 Ks,	uniform float kC,	uniform float kL,	uniform float kQ,	uniform sampler2D pos,	uniform sampler2D nor,	uniform sampler2D dif,	uniform float3 eyeP	){	float4 ambient = float4( 0.0, 0.0, 0.0, 0.0);	float4 diffuse = float4( 0.0, 0.0, 0.0, 0.0);	float4 specular = float4( 0.0, 0.0, 0.0, 0.0);	//Sample the G-Buffer and Other Accumulation Buffer 	float4 position4 = tex2D(pos, texcoord);	float4 normal4 = tex2D(nor, texcoord);	float4 diffuse4 = tex2D(dif, texcoord);			float3 position3 = position4.xyz;	float3 normalS = normal4.xyz;//expand(normal4.xyz);	float3 normal3 = normalS;	float3 diffuse3 = diffuse4.xyz;	float3 sem = float3( position4.w , normal4.w, diffuse4.w);	float4 emission = sem.yyyy;	float4 shininess = sem.x;	float3 lightDir = float3(lightPosition.xyz - position3);	float d = length(lightDir);	lightDir = normalize(lightDir);		float attenuation = 1.0/(kC + kL * d + kQ *d *d);	float3 halfAngle = (lightDir + eyeP)/2.0;	float nDotLD = max(0.0, dot(lightDir, normal3 ) );	float nDotHA = max(0.0, dot(halfAngle, normal3 ) );	float spec_power;	if(nDotLD <= 0.0) {		spec_power = 0.0; }	else		spec_power = pow(nDotHA, shininess);	float4 LightColor = float4 (lightColor.xyz, 0.0f );	ambient = Ka;	diffuse = Kd * LightColor * nDotLD;// * attenuation; No attenuation for directional light.	specular = Ks * LightColor * spec_power * attenuation; //specular power still causing black artifacts	float4 colorr;	colorr= float4( diffuse3, 0.0);	oColor = (emission + ambient + diffuse) *colorr + specular;    	oColor = diffuse4; //for debugging purposes.


and finally the images with and without the normal texture bound....

Not what it should look like...
With Normal Bound

What the diffuse buffer should look like
Without Normal Bound

and here is a picture with specular power, which isnt working.
Messed up Specular

There is a lot of stuff here, if anyone sees anything that is wrong, please say something, i've been at this for a couple months haha... (there is a lot here).

------------------------------

redwoodpixel.com

I have never bumped anything b4, sorry. But i posted the info late.
I feel like I am not normalizing stuff right (like the normal for the spec power), but i dunno it could be anything.

I will try what you guys said about not needing bindtexture with the cgGL stuff.



------------------------------

redwoodpixel.com

Hey Guys,

So after a lot of debugging i have found that my normals are incorrect...

once i commented out the half angle and other "normal" related values it worked like it was supposed to.

somewhere in the shaders i'm screwing them up.
here are the steps of how i generate and store/use the normal.

1. take the normal of the vertex and multiply it by the inverse transpose of the ModelView (the inner 3x3 portion).

2. create the tangent frame by using the example from the shaderX5 book...it is very possible something is off in here.

float3x3 compute_tangent_frame( float3 N, float3 p, float2 uv )
{
// Implemenataion with full matrix inverse
// straight from theory.

// get edge vectors of the pixel triangle
float3 dp1 = ddx( p );
float3 dp2 = ddy( p );
float2 duv1 = ddx( uv );
float2 duv2 = ddy( uv );

// solve the linear system
float3x3 M = float3x3( dp1, dp2, cross( dp1, dp2 ) );
float3x3 inverseM = invert_3x3( M );
float3 T = mul( inverseM, float3( duv1.x, duv2.x, 0 ) );
float3 B = mul( inverseM, float3( duv1.y, duv2.y, 0 ) );

// construct tangent frame
float maxLength = max( length(T), length(B) );
return float3x3( T / maxLength, B / maxLength, N );
}

float3x3 invert_3x3( float3x3 M )
{
float D = determinant( M );
float3x3 T = transpose( M );

return float3x3(
cross( T[1], T[2] ),
cross( T[2], T[0] ),
cross( T[0], T[1] ) ) / D;
}



3. get the final normal by multiplying the TangentFrame and the Normal from the normal map (tangent space normal map).

float3 snormal = mul( TangentFrame , vnormal );


4. Store the normal in the Render Target

OUT.color1 = float4( snormal.xyz , Sem.y);

5. In the Lighting Model, take the normal out and apply the light using the lighting equation.

float4 normal4 = tex2D(nor, texcoord);
float3 normal3 = normal4.xyz;

float nDotLD = max(0.0, dot(lightDir, normal3 ) );
float nDotHA = max(0.0, dot(halfAngle, normal3 ) );

etc......

One thing I notice is that i'm not normalizing anywhere. is this needed? the places that i do normalize doesnt do anything.

Thanks for the help.
-Joe

Also, this is less of an OpenGL question it seems haha.... maybe i should move the post or something.



EDIT: OMG IT WORKS!!!!! LOL was multiplying the wrong normal by the tangent frame!!!! DOHHHH!

check it out now!!!!

fin


[Edited by - AverageJoeSSU on February 12, 2009 7:13:08 PM]

------------------------------

redwoodpixel.com

This topic is closed to new replies.

Advertisement