Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

OpenGL ES 1.1 to ES 2.0 Lighting

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

I'm currently updating my OpenGL ES 1.1 game engine to support ES 2.0 for shader support. The first thing I'm trying to do is replicate all the ES 1.1 functionality in a default shader.

The problem I'm having is with lighting. I am using a face model as a test and attached an image demonstrating the problem.

Here is the lighting part of my shader.

[source]vec3 computeLighting(vec3 normal)
{
vec3 n = (normalMatrix * vec4(normal, 1.0)).xyz;
vec3 l = normalize(lightPosition);
vec3 e = vec3(0, 0, 1);
vec3 h = normalize(l + e);

float df = max(0.0, dot(n, l));
float sf = max(0.0, dot(n, h));
sf = pow(sf, shininessLight);

return ambientLight + df * diffuseLight + sf * specularLight;
}

void main()
{
gl_Position = projectionMatrix * modelviewMatrix * vec4(positionVertex, 1.0);

texCoord = (textureMatrix * vec4(textureVertex, 0.0, 1.0)).xy;
color = vec4(computeLighting(normalVertex), 1.0);
}[/source]

The lighting does appear to be working when I leave the "normalMatrix" as identity but it is obviously in object space because the lighting stays the same as the model is rotated. According to the book I'm reading the normalMatrix needs to be the inverse-transpose of the model-view matrix.

So what I'm doing is assigning the inverse of the modelviewMatrix to normalMatrix using the following method

[source]Matrix4 Inverse() const
{
Matrix4<T> m;

T pxx = y.y * (z.z * w.w - z.w * w.z) + y.z * (z.w * w.y - z.y * w.w) + y.w * (z.y * w.z - z.z * w.y);
T pyx = y.x * (z.w * w.z - z.z * w.w) + y.z * (z.x * w.w - z.w * w.x) + y.w * (z.z * w.x - z.x * w.z);
T pzx = y.x * (z.y * w.w - z.w * w.y) + y.y * (z.w * w.x - z.x * w.w) + y.w * (z.x * w.y - z.y * w.x);
T pwx = y.x * (z.z * w.y - z.y * w.z) + y.y * (z.x * w.z - z.z * w.x) + y.z * (z.y * w.x - z.x * w.y);

T t = 1 / (x.x * pxx + x.y * pyx + x.z * pzx + x.w * pwx);

m.x.x = pxx * t;
m.x.y = (x.y * (z.w * w.z - z.z * w.w) + x.z * (z.y * w.w - z.w * w.y) + x.w * (z.z * w.y - z.y * w.z)) * t;
m.x.z = (x.y * (y.z * w.w - y.w * w.z) + x.z * (y.w * w.y - y.y * w.w) + x.w * (y.y * w.z - y.z * w.y)) * t;
m.x.w = (x.y * (y.w * z.z - y.z * z.w) + x.z * (y.y * z.w - y.w * z.y) + x.w * (y.z * z.y - y.y * z.z)) * t;

m.y.x = pyx * t;
m.y.y = (x.x * (z.z * w.w - z.w * w.z) + x.z * (z.w * w.x - z.x * w.w) + x.w * (z.x * w.z - z.z * w.x)) * t;
m.y.z = (x.x * (y.w * w.z - y.z * w.w) + x.z * (y.x * w.w - y.w * w.x) + x.w * (y.z * w.x - y.x * w.z)) * t;
m.y.w = (x.x * (y.z * z.w - y.w * z.z) + x.z * (y.w * z.x - y.x * z.w) + x.w * (y.x * z.z - y.z * z.x)) * t;

m.z.x = pzx * t;
m.z.y = (x.x * (z.w * w.y - z.y * w.w) + x.y * (z.x * w.w - z.w * w.x) + x.w * (z.y * w.x - z.x * w.y)) * t;
m.z.z = (x.x * (y.y * w.w - y.w * w.y) + x.y * (y.w * w.x - y.x * w.w) + x.w * (y.x * w.y - y.y * w.x)) * t;
m.z.w = (x.x * (y.w * z.y - y.y * z.w) + x.y * (y.x * z.w - y.w * z.x) + x.w * (y.y * z.x - y.x * z.y)) * t;

m.w.x = pwx * t;
m.w.y = (x.x * (z.y * w.z - z.z * w.y) + x.y * (z.z * w.x - z.x * w.z) + x.z * (z.x * w.y - z.y * w.x)) * t;
m.w.z = (x.x * (y.z * w.y - y.y * w.z) + x.y * (y.x * w.z - y.z * w.x) + x.z * (y.y * w.x - y.x * w.y)) * t;
m.w.w = (x.x * (y.y * z.z - y.z * z.y) + x.y * (y.z * z.x - y.x * z.z) + x.z * (y.x * z.y - y.y * z.x)) * t;

return m;
}[/source]

In the picture you will see the results called "Inverse Matrix". I have lightPosition the same in both ES 1.1 and 2.0. What am I doing wrong and how can I replicate the lighting from 1.1 to 2.0?

Share this post


Link to post
Share on other sites
Advertisement
Nevermind I just had to multiply the inverse by the transpose of the model-view matrix.

Although I do have a question still. The Transpose() method provided in the book doesn't work properly; it will have moments where the model will go completely black when rotated.

Matrix4 Transpose() const
{
Matrix4 m;
m.x.x = x.x; m.x.y = y.x; m.x.z = z.x; m.x.w = w.x;
m.y.x = x.y; m.y.y = y.y; m.y.z = z.y; m.y.w = w.y;
m.z.x = x.z; m.z.y = y.z; m.z.z = z.z; m.z.w = w.z;
m.w.x = x.w; m.w.y = y.w; m.w.z = z.w; m.w.w = w.w;
return m;
}


When I modify the method like so the lighting is perfect.

Matrix4 Transpose() const
{
Matrix4 m;
m.x.x = x.x; m.x.y = y.x; m.x.z = z.x; m.x.w = x.w;
m.y.x = x.y; m.y.y = y.y; m.y.z = z.y; m.y.w = y.w;
m.z.x = x.z; m.z.y = y.z; m.z.z = z.z; m.z.w = z.w;
m.w.x = w.x; m.w.y = w.y; m.w.z = w.z; m.w.w = w.w;
}


Is this a mistake in the book?

Share this post


Link to post
Share on other sites
I'm not expert with this stuff, but I think you should set the vector's w component to zero when multiplying with normalMatrix:
vec3 n = (normalMatrix * vec4(normal, 0.0)).xyz;

Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!