Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lighting direction with an ARB_vertex_program




So the actual question is "Why is my lighting direction locked to the object and not the view direction, as in the Shader Builder preview?"

Ok - the confessional method of debugging strikes again! I found a real *dumb* bug that was causing the behavior... it had nothing to do with my transformations, shaders, or anything else I'd looked at before.


Turns out that when I was calculating vertex normals for my objects, the sign of the Y component was flipped. This meant that when the object was viewed along positive or negative X, it looked sort of ok. However when viewed along + or - Y the normals were pointing inward and the surface disappeared. I had assumed this was because the lighting was always from +X, but this was wrong (since viewing along + or - X were both ok). It finally occurred to me to check the normals again and voila there was the problem. Under the fixed function pipeline I had several lights and the surface did not disappear in this way (for reasons I should probably try to understand).

Rick

On May 11, 2005, at 9:42 AM, Rick Hoge wrote:


I've been experimenting with Shaders and have learned a lot playing with OpenGL Shader Builder. This is a really cool utility (please add more docs though!).


I've graduated to applying shaders in an app that in the past has used the standard OpenGL pipeline to render 3D models with some basic lighting. The basics are working nicely, but there is an issue with the lighting direction that I can't seem to get past. I'll try to describe what we're trying to do and the unexpected result here:

Basically we draw our scene based on a 3D object stored in a vertex array. Until now it's been lit using the lighting functions of the standard pipeline and that worked ok. My experiment was to load a vertex shader that is a verbatim copy of James McCombe's BetterPerVertexLighting example that comes with Shader builder. The shading is applied, but the lighting direction seems locked to the model (not the viewing direction) so that when the object is rotated to certain angles it becomes black. In the Shader Builder view the lighting seems locked to the viewer's coordinate system, so the object is always illuminated from a source above and to the right of the viewing axis.


I expect that this difference is due to how I've set up the modelview and possibly projection matrices - it must be different from the code setting up the views in shader builder. This will likely be a good exercise to get a better grasp of the viewing transformations - below I've included our matrix setup and below that a copy of the shader. I'm starting to suspect that the matrix setup I'm using might be a bit unorthodox...


Thanks for any tips on this!

Rick

/////////////////////////////// Here is the matrix setup: //////////////////////////////////////////
glMatrixMode(GL_PROJECTION);
glLoadIdentity();


// Set aspect ratio, using smallest dimention
float w = visibleRect.size.width;
float h = visibleRect.size.height;
float fov = 128.0;
float aspect = 0.0;
if (w<h) {
aspect = h/w;
glOrtho( (-fov / zoom), (fov / zoom), (aspect * -fov / zoom), (aspect * fov / zoom), -fov, fov);
} else {
aspect = w/h;
glOrtho((aspect * -fov / zoom), (aspect * fov / zoom), (-fov / zoom), (fov / zoom), -fov, fov);
}


  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  // Rotate the object based on an interactively set viewing angle
  glRotatef(orientation.x,1.0f,0.0f,0.0f);
  glRotatef(orientation.y,0.0f,1.0f,0.0f);
  glRotatef(orientation.z,0.0f,0.0f,1.0f);

gluLookAt(1.0,0.0,0.0, // Set camera position
0.0,0.0,0.0,
0.0,0.0,1.0);
/////////////////////////////// End of matrix setup ///////////////////////////////////////////////////////////





############### Here is the shader ##################################

!!ARBvp1.0

# Better Per-Vertex Lighting by James A. McCombe, Tue Jun 17 2003
#
# Implements a basic lighting model where the shadow/highlights are determined
# based on the dot product of a light direction vector and the vertex normal.
# After that, some simple math is done to accent the highlight based on a set of
# parameters. Finally, its modulated with the vertex color.
#
# Parameters:
# program.env[0].xyz - Light position
# program.env[1].x - Light softness
# program.env[1].y - Highlight size
# program.env[1].z - Highlight intensity


ATTRIB vertexPosition  = vertex.position;
ATTRIB vertexNormal    = vertex.normal;

PARAM  lightDirection   = program.env[0];
PARAM  lightParameters = program.env[1];
PARAM  zero = { 0.0, 0.0, 0.0, 0.0 };

TEMP temp1, temp2, transPosition;

# Transform the vertex position
DP4 transPosition.x, state.matrix.mvp.row[0], vertexPosition;
DP4 transPosition.y, state.matrix.mvp.row[1], vertexPosition;
DP4 transPosition.z, state.matrix.mvp.row[2], vertexPosition;
DP4 transPosition.w, state.matrix.mvp.row[3], vertexPosition;

# Transform the vertex normal into eye space
DP3 temp1.x, state.matrix.modelview.invtrans.row[0], vertexNormal;
DP3 temp1.y, state.matrix.modelview.invtrans.row[1], vertexNormal;
DP3 temp1.z, state.matrix.modelview.invtrans.row[2], vertexNormal;

DP3 temp1, lightDirection, temp1;

MAX temp1, zero, temp1;
POW temp1, temp1.x, lightParameters.x;

ADD temp2, temp1, -lightParameters.y;
MAX temp2, zero, temp2;
MUL temp2, temp2, lightParameters.z;

MUL temp1, temp1, vertex.color;
ADD temp1, temp1, temp2;

SWZ result.color, temp1, x,y,z,1;

MOV result.position, transPosition;


END
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Mac-opengl mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/mac-opengl/rhoge% 40nmr.mgh.harvard.edu


This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Mac-opengl mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/mac-opengl/email@hidden

This email sent to email@hidden
References: 
 >Lighting direction with an ARB_vertex_program (From: Rick Hoge <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.