Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple GLSL shader problem.

Started by ph33r Feb 24, 2005 at 8:58 AM 2 replies 1.2k views
Original Post
ph33r
ph33r
I'm trying to get a simple GLSL shader to work but the light that I have rotating around the cube doesn't seem to show up o nthe cube. It's just looks like there is a static light position. I think it's how I set my uniform variable g_lightPositions[0] = glGetUniformLocationARB( g_program, "lightPosition0" ); glUseProgramObjectARB( g_program ); glUniform3fARB( g_lightPositions[0], g_lights[2].x, g_lights[2].y, g_lights[2].z ); Here is my vertex shader(Simple phong illumination).

varying vec3 normal;
varying vec3 mvVertex;
varying vec3 L;
varying float pattern;

uniform vec3 lightPosition0;

void main(void)
{

	mvVertex = vec3(gl_ModelViewMatrix * gl_Vertex);	
	normal = normalize(gl_NormalMatrix * gl_Normal);
	L = normalize(lightPosition0 - mvVertex);
	pattern = fract( 4.0 * ( gl_Vertex.y  + gl_Vertex.x + gl_Vertex.z ) );
	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
And my fragment shader

uniform sampler2D textureHandle;

//uniform vec3 diffuse;
//uniform vec3 specular;
//uniform float shininess;

varying vec3 normal;
varying vec3 mvVertex;
varying vec3 L;
varying float pattern;

uniform vec3 eyePos;

void main (void)
{
	vec3 diffuse = vec3( 0.8, 0.0, 0.0 );
	vec3 specular = vec3( 0.2, 0.0, 0.0 );
	float shininess = 50.0;

	vec3 E = normalize( eyePos - mvVertex );					 // we are in Eye Coordinates, so EyePos is (0,0,0)
	vec3 R = normalize( 2.0 * dot(normal, L) * normal - L ); 
	
	//calculate Diffuse Term:
	vec4 Idiff = vec4( diffuse * max( dot( normal, L ), 0.0 ), 1.0 );

	// calculate Specular Term:
	vec4 Ispec = vec4( specular * pow( max( dot( R, E ), 0.0 ), 0.3 * shininess ), 1.0 ) * pattern; 
	
	
	
	// write Total Color:
	gl_FragColor = ( Idiff + Ispec ) * texture2D( textureHandle, gl_TexCoord[0].xy ); 
}
Thanks for the help.
ph33r
ph33r
Ok here's another possible problem, I don't know if it is or not.

I set my lightPositions every frame, my light position is always some rotation around the cube. My cube is modelled at the origin(0,0,0), so a light position could be at 2,0,0 or -2,0,0 as it rotates around the cube.

The problem I think may have to do with I move my camera back 10 units on the Z then draw the cube. So even though I set my light positions to 2,0,0 they arn't really rotating around the cube because I set my camera back? I'm not really sure if that makes sence.

Here's my draw code.

// Set light position 0
glUniform3fvARB( g_lightPositions[0], 3, g_lights[0].p );

// Apply camera transform
glPushMatrix( );
g_camera.ApplyCameraTransform( );

// Render cube
...
glPopMatrix
Kalidor
Kalidor
The light position needs to be multiplied by the modelview matrix as well.

So in your shader you would have to do something like this (just guessing at GLSL syntax)...

L = normalize((gl_ModelViewMatrix * lightPosition0) - myVertex);


One thing to remember though is that it should be multiplied only by the camera's modelview matrix (for a light that's defined in world space), so if you do other transforms per object you would need to send the modelview matrix directly after the camera transform to your shader, then transform the light position by that matrix instead of the modelview matrix for the current vertex.

A way to optimize this is transforming the light position in world space by the camera's matrix yourself, and sending that transformed position to the vertex program so you don't have to waste instructions by transforming the position by the same matrix for each vertex.
ph33r
ph33r
Thanks once again, wish I could rate you up higher then I already have!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.