
Advertisement
Sign in to follow this
Followers
0
This topic is 3789 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
Advertisement
TheChuckster 130
No, that's completely wrong for camera coordinates. With your code, the reflections move (rotate and translate) with the camera. Perhaps the Orange Book assumes you are using a static scene. As a hack, I multiplied the ReflectDir vector by the inverse camera transformation matrix and got proper results:
However, this is unacceptable if I plan on extending this to support normal mapping and perfragment lighting. It's important that all of my vectors are ALREADY in the correct coordinate systems especially when I'm dealing with tangent space and light vectors as well. Also, my Fresnel equation will "move with the camera" and be in the wrong coordinate system.
uniform mat4 CameraInv;
varying vec3 ReflectDir;
void main () {
gl_Position = ftransform();
// Standard normal calculation
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
// The position in eye (camera) coordinate space.
vec3 ecPos = gl_ModelViewMatrix * gl_Vertex;
// The direction of the eye from the vertex (the incident vector)
vec3 eyeDir = ecPos.xyz;
// The reflection of our incident vector from the surface (eyeDir).
ReflectDir = mat3(CameraInv)*reflect(eyeDir, normal);
}
However, this is unacceptable if I plan on extending this to support normal mapping and perfragment lighting. It's important that all of my vectors are ALREADY in the correct coordinate systems especially when I'm dealing with tangent space and light vectors as well. Also, my Fresnel equation will "move with the camera" and be in the wrong coordinate system.
0
TheChuckster 130
I also tried bump mapping with perfragment lighting, hoping to avert the problem by setting aside reflections for now. Same coordinate system problems. I have a rotating cube and a static light. The lit and unlit sides of the cube rotate WITH the cube, instead of staying put like they should. I tried multiplying the light vector and the eye vector by the model view matrix without any luck. I'm trying to keep everything in eye coordinates here...
Here's the YouTube video for this one:
">Here's the YouTube video for this one:
Once again, here are my flawed GLSL code listings. The obscure stuff near the beginning of the fragment shader just procedurally generates a spotted bump map.
Vertex:
varying vec3 LightDir;
varying vec3 EyeDir;
attribute vec3 Tangent;
uniform mat4 CameraInv;
void main()
{
EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
vec3 tangent;
vec3 binormal;
vec3 c1 = cross(gl_NormalMatrix * gl_Normal, vec3(0.0, 0.0, 1.0));
vec3 c2 = cross(gl_NormalMatrix * gl_Normal, vec3(0.0, 1.0, 0.0));
if(length(c1)>length(c2))
{
tangent = c1;
}
else
{
tangent = c2;
}
tangent = normalize(tangent);
binormal = cross(gl_NormalMatrix * gl_Normal, tangent);
binormal = normalize(binormal);
vec3 normal = gl_NormalMatrix * gl_Normal;
vec3 v;
v.x = dot(gl_ModelViewMatrix * gl_LightSource[0].position, tangent);
v.y = dot(gl_ModelViewMatrix * gl_LightSource[0].position, binormal);
v.z = dot(gl_ModelViewMatrix * gl_LightSource[0].position, normal);
LightDir = normalize(v);
v.x = dot(EyeDir, tangent);
v.y = dot(EyeDir, binormal);
v.z = dot(EyeDir, normal);
EyeDir = normalize(v);
}
Fragment:
#extension GL_ARB_draw_buffers : enable
uniform sampler2D colorMap;
uniform sampler2D normalMap;
varying vec3 LightDir;
varying vec3 EyeDir;
void main()
{
vec3 litColor;
vec2 c = 16.0 * gl_TexCoord[0].st;
vec2 p = fract(c)  vec2(0.5);
float d, f;
d = p.x * p.x + p.y * p.y;
f = 1.0 / sqrt(d + 1.0);
if (d >= 0.15)
{
p = vec2(0.0);
f = 1.0;
}
vec3 normDelta = vec3(p.x, p.y, 1.0) * f;
vec4 base = texture2D(colorMap, gl_TexCoord[0]);
float Diffuse = max(dot(normDelta, LightDir), 0.0) * 2.0;
vec3 reflectDir = reflect(LightDir, normDelta);
float Specular = max(dot(EyeDir, reflectDir), 0.0);
Specular = pow(Specular, 1.0);
litColor = base * Diffuse + Specular;
vec4 FinalColor = vec4(litColor, 1.0);
gl_FragData[0] = min(FinalColor, 1.0);
gl_FragData[1] = max(FinalColor  1.0, 0.0);
}
0
TheChuckster 130
Good news! I sat there and thought about it for a few days, and I realized I could just multiply the gl_Position output of my vertex shader by the separate camera matrix instead of using gluLookAt() to mess up my modelview matrix for all of the light, normal, position vectors.
Makes things a LOT easier than perpetually trying to scratch my head without any linear algebra courses.
gl_Position = gl_ProjectionMatrix * CameraMat * gl_ModelViewMatrix * gl_Vertex;
Makes things a LOT easier than perpetually trying to scratch my head without any linear algebra courses.
0
Sign in to follow this
Followers
0

Advertisement
(Code adapted from the Orange Book. I've ignored lighting here for simplicity.)
Share this post
Link to post
Share on other sites