j_derius

03-23-2016, 12:02 PM

Hi, I'm new in glsl shader programming.

My goal is to modify a mesh in real-time transforming the verts inside the vertex shader program. I've managed to do it correctly but the program renders the geometry as its normals are still the same as in the original mesh.

Is there a way to update the normals according to the vertex transform?

I'm using RenderMonkey, I've started modifing the base phong texture scene, maybe you can notice other errors in the code.

thanks

vertex shader:

uniform vec3 fvLightPosition;

uniform vec3 fvEyePosition;

varying vec2 Texcoord;

varying vec3 ViewDirection;

varying vec3 LightDirection;

varying vec3 Normal;

void main( void )

{

vec4 Pos = gl_Vertex;

// omitted modify vertex code Pos.x = bla bla; Pos.y = bla bla; Pos.z = bla bla;

gl_Position = gl_ModelViewProjectionMatrix * Pos;

Texcoord = gl_MultiTexCoord0.xy;

vec4 fvObjectPosition = gl_ModelViewMatrix * Pos;

ViewDirection = fvEyePosition - fvObjectPosition.xyz;

LightDirection = fvLightPosition - fvObjectPosition.xyz;

Normal = gl_NormalMatrix * gl_Normal;

}

fragment shader:

uniform vec4 fvAmbient;

uniform vec4 fvSpecular;

uniform vec4 fvDiffuse;

uniform float fSpecularPower;

uniform sampler2D baseMap;

varying vec2 Texcoord;

varying vec3 ViewDirection;

varying vec3 LightDirection;

varying vec3 Normal;

void main( void )

{

vec3 fvLightDirection = normalize( LightDirection );

vec3 fvNormal = normalize( Normal );

float fNDotL = dot( fvNormal, fvLightDirection );

vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );

vec3 fvViewDirection = normalize( ViewDirection );

float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );

vec4 fvBaseColor = texture2D( baseMap, Texcoord );

vec4 fvTotalAmbient = fvAmbient * fvBaseColor;

vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;

vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );

gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );

}

My goal is to modify a mesh in real-time transforming the verts inside the vertex shader program. I've managed to do it correctly but the program renders the geometry as its normals are still the same as in the original mesh.

Is there a way to update the normals according to the vertex transform?

I'm using RenderMonkey, I've started modifing the base phong texture scene, maybe you can notice other errors in the code.

thanks

vertex shader:

uniform vec3 fvLightPosition;

uniform vec3 fvEyePosition;

varying vec2 Texcoord;

varying vec3 ViewDirection;

varying vec3 LightDirection;

varying vec3 Normal;

void main( void )

{

vec4 Pos = gl_Vertex;

// omitted modify vertex code Pos.x = bla bla; Pos.y = bla bla; Pos.z = bla bla;

gl_Position = gl_ModelViewProjectionMatrix * Pos;

Texcoord = gl_MultiTexCoord0.xy;

vec4 fvObjectPosition = gl_ModelViewMatrix * Pos;

ViewDirection = fvEyePosition - fvObjectPosition.xyz;

LightDirection = fvLightPosition - fvObjectPosition.xyz;

Normal = gl_NormalMatrix * gl_Normal;

}

fragment shader:

uniform vec4 fvAmbient;

uniform vec4 fvSpecular;

uniform vec4 fvDiffuse;

uniform float fSpecularPower;

uniform sampler2D baseMap;

varying vec2 Texcoord;

varying vec3 ViewDirection;

varying vec3 LightDirection;

varying vec3 Normal;

void main( void )

{

vec3 fvLightDirection = normalize( LightDirection );

vec3 fvNormal = normalize( Normal );

float fNDotL = dot( fvNormal, fvLightDirection );

vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );

vec3 fvViewDirection = normalize( ViewDirection );

float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );

vec4 fvBaseColor = texture2D( baseMap, Texcoord );

vec4 fvTotalAmbient = fvAmbient * fvBaseColor;

vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;

vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) );

gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );

}