Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Specular mapping GLSL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 invutil   Crossbones+   -  Reputation: 1924

Like
0Likes
Like

Posted 09 November 2012 - 11:14 PM

Is this specular mapping done correctly? I followed a tutorial and my code is slightly different.

http://antongerdelan.net/opengl/index.php?title=Specular_mapping#Shaders

My vertex shader (my vpeye and vneye is different):
uniform mat4 lightMatrix;
uniform vec3 lightPos;
uniform vec3 lightDir;
varying vec4 lpos;
varying vec3 normal;
varying vec3 light_vec;
varying vec3 light_dir;
out vec4 vpeye; // vert positions in eye coords
out vec4 vneye; // normals in eye coords
void main(void)
{
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_TexCoord[1] = gl_MultiTexCoord1;
  vec4 vpos = gl_ModelViewMatrix * gl_Vertex;
  lpos = lightMatrix * vpos;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  vpeye = gl_ModelViewMatrix * gl_Vertex;
  vneye = normalize(vec4(gl_NormalMatrix * gl_Normal, 0));
  light_vec = vpos.xyz - lightPos;
  light_dir = gl_NormalMatrix * lightDir;
  normal = normalize(gl_NormalMatrix * gl_Normal);
  gl_FrontColor = gl_Color;
}

Fragment:
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D shadowMap;
uniform sampler2D texture4;
varying vec4 lpos;
varying vec3 normal;
varying vec3 light_vec;
varying vec3 light_dir;
in vec4 vpeye;
in vec4 vneye;
void main (void)
{
  vec3 smcoord = lpos.xyz / lpos.w;
  float shadow = max(0.5, float(smcoord.z <= texture2D(shadowMap, smcoord.xy).x));
  vec3 lvec = normalize(light_vec);
  float diffuse = max(dot(-lvec, normal), 0.0);
  vec4 s_eye = normalize(vec4(lpos - vpeye)); // direction from point to light
  vec4 v_eye = normalize(-vpeye); // direction from point to viewer
  vec4 r_eye = reflect(-s_eye, vneye); // reflect vector from light to point based on normal
  vec4 texColor = texture2D(texture1, gl_TexCoord[0].st) * texture2D(texture2, gl_TexCoord[1].st);
  vec4 spec = texture2D(texture4, gl_TexCoord[0].st) * pow(max(dot(r_eye, v_eye), 0), 128);
  gl_FragColor = vec4(gl_Color.xyz * texColor.xyz * diffuse * shadow * 3.0 + spec.xyz, gl_Color.w * texColor.w);
  //gl_FragColor = vec4(vec3(1,1,1) * pow(max(dot(r_eye, v_eye), 0), 10), 1);
}

Edited by polyfrag, 09 November 2012 - 11:16 PM.


Sponsor:

#2 ifthen   Members   -  Reputation: 820

Like
1Likes
Like

Posted 10 November 2012 - 03:44 AM

I think that it is okay, the lightMatrix looks like the only pitfall. It should be the same as the modelview matrix.

Look at it this way: the fragment shader needs to know only three things for calculating lightning (for point light) – the position of the fragment (variable), the normal of the fragment (variable) and the position of the light (uniform). All of them must be in the same coordinate space.

(The normals use another matrix because of scaling. If you call glScalef(1,1,0.5), the normals must be scaled by the opposite because of stuff. The normal matrix preserves the rotation, doesn't have translation (the normals are true vectors, not positions) and corrects the scaling. Take note, however, that object space normal multiplied by the normal matrix is in eye space.)

(That also means you don't have to compute lightning in eye space, it can be done in object space too; that would mean that you can have a static light source, not one moving with the camera, without performing additional matrix calculations on the CPU. But you would have to you wanted a light static to camera. It really depends what to use on whether the light is carried by the cameraman (e.g. flashlight) or not (e.g. a chandelier).)

Other than that, it looks good to me.

Edited by ifthen, 10 November 2012 - 03:49 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS