Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

not getting correct TBN or view-vector with models

This topic is 4147 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

im trying to add some effects to models im loading into my engine.. and without much success as of yet... when i rendered a simple plane regularly without a model it works just fine...
inline void RenderPlaneXZ(vec3 pos, float size, float repeat)
{
	float hsize = size/2.0f;
	glBegin(GL_TRIANGLE_STRIP);
		glMultiTexCoord2f(GL_TEXTURE0	, 0		, 0 );
		glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 0.0f, 0.0f);
		glVertex3f(-hsize, pos.y, -hsize);

		glMultiTexCoord2f(GL_TEXTURE0	, 0		, repeat);
		glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 0.0f, 0.0f);
		glVertex3f(-hsize, pos.y, hsize);

		glMultiTexCoord2f(GL_TEXTURE0	, repeat		, 0 );
		glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 0.0f, 0.0f);
		glVertex3f(hsize, pos.y, -hsize);

		glMultiTexCoord2f(GL_TEXTURE0	, repeat		, repeat );
		glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 0.0f, 0.0f);
		glVertex3f(hsize, pos.y, hsize);
	glEnd();
}



and i use the following shader code
#version 110

varying vec3 ViewDir;

uniform vec3 CamPos;

void main()
{

	
//VERTEX
#version 110


varying vec3 ViewDir;


uniform vec3 CamPos;

void main()
{

	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	vec3 VertPos = gl_Vertex.xyz;
	
	
	
	
	vec3 Normal = vec3(0.0,1.0,0.0);//normalize(gl_Normal.xyz);
	vec3 Tangent = vec3(1.0,0.0,0.0);//normalize(gl_Color.xyz);	
	vec3 BiNormal = vec3(0.0,0.0,1.0);//cross(Normal,Tangent);

	
	mat3 TBN = mat3(Tangent, BiNormal, Normal);
	
	
	ViewDir = (VertPos.xyz - CamPos.xyz)*TBN;
	
	gl_Position = gl_ModelViewProjectionMatrix * vec4(VertPos,1.0);	
	

}


//FRAGMENT

#version 110
varying vec3 LightDir;
varying vec3 ViewDir;

uniform sampler2D TextureMap;
uniform sampler2D NormalMap;
uniform sampler2D ConeMap;

const float ParallaxScale = 0.05;

vec2 ConeStep(vec3 V, vec2 TexCoord)
{	
	
	vec4 t = 0.0;
	float sc=1.0;
	
		V = -V;
		
		V.z = V.z/ParallaxScale;
			  
		V = normalize(V);
			   
		float iz = sqrt(1.0-V.z*V.z);
		
		sc = 0.0;
		float offset = 1.0;		
			
		for (int i = 0; i < 10.0; i++) //while 
		{
			t = texture2D(ConeMap,(TexCoord+V.xy*sc));
			offset = (t.r + V.z*sc) / (iz/(t.g*t.g) - V.z);
			sc += offset;				
		}		
		
		
	
	return TexCoord+V.xy*sc;
}

void main()
{	
	vec3 nViewDir = normalize(-ViewDir);
	vec2 TexCoord = ConeStep(nViewDir,gl_TexCoord[0].st);//gl_TexCoord[0].st;	

	 
	gl_FragColor = texture2D(TextureMap, TexCoord);

}




}



however once i try to render a 3ds model of a plane.. everything gets messup.. my parallax effect doesnt look right and the close i get to the model the more wrong it gets... ive tried another version of the shader code.. which usually never works anyway.. but i tried it.. it makes it even worse...
#version 110

varying vec3 ViewDir;

uniform vec3 CamPos;

void main()
{

	
//VERTEX
#version 110


varying vec3 ViewDir;


uniform vec3 CamPos;

void main()
{

	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	vec3 VertPos = gl_Vertex.xyz;
	
	
	
	
	vec3 Normal = gl_NormalMatrix * vec3(0.0,1.0,0.0);//normalize(gl_Normal.xyz);
	vec3 Tangent = gl_NormalMatrix * vec3(1.0,0.0,0.0);//normalize(gl_Color.xyz);	
	vec3 BiNormal = gl_NormalMatrix * vec3(0.0,0.0,1.0);//cross(Normal,Tangent);

	
	mat3 TBN = mat3(Tangent, BiNormal, Normal);
	
	vec4 pos = gl_ModelViewMatrix*gl_Vertex;
	ViewDir = TBN*normalize(-pos.xyz);
	
	gl_Position = gl_ModelViewProjectionMatrix * vec4(VertPos,1.0);	
	

}


//FRAGMENT

#version 110
varying vec3 LightDir;
varying vec3 ViewDir;

uniform sampler2D TextureMap;
uniform sampler2D NormalMap;
uniform sampler2D ConeMap;

const float ParallaxScale = 0.05;

vec2 ConeStep(vec3 V, vec2 TexCoord)
{	
	
	vec4 t = 0.0;
	float sc=1.0;
	
		V = -V;
		
		V.z = V.z/ParallaxScale;
			  
		V = normalize(V);
			   
		float iz = sqrt(1.0-V.z*V.z);
		
		sc = 0.0;
		float offset = 1.0;		
			
		for (int i = 0; i < 10.0; i++) //while 
		{
			t = texture2D(ConeMap,(TexCoord+V.xy*sc));
			offset = (t.r + V.z*sc) / (iz/(t.g*t.g) - V.z);
			sc += offset;				
		}		
		
		
	
	return TexCoord+V.xy*sc;
}

void main()
{	
	vec3 nViewDir = normalize(-ViewDir);
	vec2 TexCoord = ConeStep(nViewDir,gl_TexCoord[0].st);//gl_TexCoord[0].st;	

	 
	gl_FragColor = texture2D(TextureMap, TexCoord);

}




}




any ideas what might be wrong?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!