Hi,
I’m currently learning GLSL. I’m trying to write a shader that does simple diffuse lighting together with a texture. The shader uses gl_LightSource[0].position to read the light direction that I set with glLightfv(GL_LIGHT0, GL_POSITION, …) in the cpp file. However, gl_LightSource[0].position seems to be updated only by the first call to glLightfv(). Here’s my code, is this my fault or nVidias?
Get the .exe here:
www.amnoid.de/glsl1st.zip
If you hold ‘A’, you can see what happens if the fixed function pipeline is used instead of the shader - I want the shader to look like that.
Source:
shader.vert:
varying vec3 N; //stores normal
void main()
{
N = normalize(gl_Normal);
//send interpolated color to
//fragment shader
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
shader.frag:
uniform sampler2D myTexture;
varying vec3 N;
void main()
{
vec3 light = gl_LightSource[0].position.xyz;
vec4 lightColor = gl_Color*max(0.2, dot(light, N));
gl_FragColor = lightColor*texture2D(myTexture, vec2(gl_TexCoord[0]));
}
main cpp file:
#include "simple_gl.h"
#include "modelle.h"
#include "shader.h"
GLuint g_tex;
GLhandleARB g_program;
//called one time at startup
bool init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
if(!hasShaderHardware())
return false;
walkBack(5);
g_program = loadProgram("shader.vert", "shader.frag");
printLog(g_program);
g_tex = loadTexture2DMM("basement floor.bmp");
glUseProgramObjectARB(g_program); //program has to be active to access its uniforms
int texLocation = glGetUniformLocationARB(g_program, "myTexture");
drawText("texLocation: %x", texLocation);
if(texLocation != -1)
glUniform1iARB(texLocation, 0); //bind texture unit 0 to shader
glUseProgramObjectARB(0);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
return true;
}
float frand()
{
return (rand()/float(RAND_MAX))*2.f - 1.f;
}
//called for every frame
void draw()
{
prepareCamera();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float lightPos[] = { frand(), frand(), frand(), 0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
drawText("Hold A to disable shader");
if(!isKeyPressed('A'))
glUseProgramObjectARB(g_program);
drawSphere();
glUseProgramObjectARB(0);
fertig();
}
//called at program shutdown
void exit()
{
}