Started by Dec 31 2007 04:17 AM

,
5 replies to this topic

Posted 31 December 2007 - 04:17 AM

First, let me just wish you all a Happy New Year!
Im currently writing a shader to shade the models in my scene, everything works fine, until i rotate the models.
is there anyway of sending the models to the shader after performing the transforms? or is there something i should do in the shader to transform the vertices?
Thanks in advance
Emiug

Posted 31 December 2007 - 05:18 AM

Well, you can solve your problem by two ways :

1. Calculate everything in world space (so use light * gl_ModelViewMatrix, normal * gl_ModelViewMatrix, .... - multiply everything with model view matrix)

2. Multiply your light with inverse modelview matrix of transformed model.

1. Calculate everything in world space (so use light * gl_ModelViewMatrix, normal * gl_ModelViewMatrix, .... - multiply everything with model view matrix)

2. Multiply your light with inverse modelview matrix of transformed model.

Posted 01 January 2008 - 12:37 AM

For all but the most simple shaders (and possibly post-process shaders that only operate on a screen-aligned quad), you should always be multiplying your vertices by the modelview matrix. There's a GLSL function specifically for this:

gl_Position = ftransform();It's pretty much the equivalent of multplying your untransformed vertex position by the modelview matrix, as Vilem Otte mentioned.

My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

Posted 01 January 2008 - 02:01 AM

Hey, thanks for the replies.

Im not sure if this is right, but my shader should look something like this?

Thanks for the help

Emiug

Im not sure if this is right, but my shader should look something like this?

varying vec3 lightDir;

varying vec3 eyeDir;

attribute vec3 Tangent;

uniform vec3 LightPos;

void main(void)

{

//LightPos = gl_ModelViewMatrix * vec4(LightPos, 1);

gl_Position = ftransform();

vec4 v = vec4(gl_Vertex);

vec3 norm = normalize(gl_NormalMatrix * gl_Normal);

norm = vec4(norm,1) * gl_ModelViewMatrix;

vec3 tang = normalize(gl_NormalMatrix * Tangent);

tang = vec4(tang,1) * gl_ModelViewMatrix;

vec3 binorm = cross(norm,tang);

eyeDir = vec3(gl_ModelViewMatrix * v);

vec3 tmp;

tmp.x = dot(LightPos,tang);

tmp.y = dot(LightPos,binorm);

tmp.z = dot(LightPos,norm);

lightDir = normalize(tmp);

tmp.x = dot(eyeDir,tang);

tmp.y = dot(eyeDir,binorm);

tmp.z = dot(eyeDir,norm);

eyeDir = normalize(tmp);

gl_TexCoord[0] = gl_MultiTexCoord0;

}

Thanks for the help

Emiug

Posted 01 January 2008 - 04:23 AM

Well, this code should be all right (but I'm not 100% sure).

Anyway you'll calculate lighting in fragment shader like this:

Well, good luck with your project and I hope this codes are all right, if not, let me know, i'll post some "better to understand".

varying vec3 lightDir;

varying vec3 eyeDir;

attribute vec3 Tangent;

uniform vec3 LightPos;

void main(void)

{

gl_Position = ftransform();

vec4 v = vec4(gl_Vertex * gl_ModelViewMatrix);

vec3 norm = normalize(gl_Normal);

vec3 tang = normalize(Tangent);

vec3 binorm = cross(norm,tang);

binorm = vec4(binorm,1) * gl_ModelViewMatrix;

norm = vec4(norm,1) * gl_ModelViewMatrix;

tang = vec4(tang,1) * gl_ModelViewMatrix;

// Well, i'm not sure, if this works, i'm sending camera position from

// application to shader as vec3, then take direction as (cameraPos - v)

// and normalize it, btw. direction needs to be normalized!

eyeDir = normalize(vec3(gl_ModelViewMatrix * v));

// Light direction is vector from position to vertices on screen, we have

// vertices in world space, so LightPos needs to be in world space too!

// and normalization is for direction needed too!

vec4 lightDirection = (vec4(LightPos, 1.0) * gl_ModelViewMatrix) - v;

lightDirection = normalize(lightDirection);

vec3 tmp;

tmp.x = dot(lightDirection.xyz,tang);

tmp.y = dot(lightDirection.xyz,binorm);

tmp.z = dot(lightDirection.xyz,norm);

lightDir = normalize(tmp);

tmp.x = dot(eyeDir,tang);

tmp.y = dot(eyeDir,binorm);

tmp.z = dot(eyeDir,norm);

eyeDir = normalize(tmp);

gl_TexCoord[0] = gl_MultiTexCoord0;

}

Anyway you'll calculate lighting in fragment shader like this:

varying vec3 lightDir;

varying vec3 eyeDir;

uniform sampler2D normalMap;

void main()

{

vec3 normalTex = texture2D(normalMap, gl_TexCoord[0].xy).xyz;

normalTex = normalTex * 2.0 - 1.0;

float DiffuseLight = max(dot(normalTex, lightDir), 0.0);

float SpecularLightBlinn = pow(max(dot(normalize(lightDir + eyeDir), normalTex), 0.0), 32.0);

gl_FragColor = vec4(DiffuseLight + SpecularLightBlinn, DiffuseLight + SpecularLightBlinn, DiffuseLight + SpecularLightBlinn, 1.0);

}

Well, good luck with your project and I hope this codes are all right, if not, let me know, i'll post some "better to understand".

Posted 01 January 2008 - 04:51 PM

Hey thanks for the help,

the modifications you made worked, and i learned a few things today also.

Thanks

Emiug

the modifications you made worked, and i learned a few things today also.

Thanks

Emiug