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

glGetUniformLocation error

Started by angry_baker Aug 24, 2009 at 4:35 PM 3 replies 1.1k views
Original Post
angry_baker
angry_baker
Hi all, Can't figure this one out at all.... I'm trying to send a variable (number of lights) through to the vertex shader. When I call getUniformLocation I get -1 for m_num_lights. m_specular, m_cosine work fine. I want to use the m_num_lights variable in a simple for loop within the shader. Please see C++ code and vertex shader snippets below: C++ code glLinkProgram(m_program); m_texLoc = glGetUniformLocation(m_program, "ColTexture"); m_normLoc = glGetUniformLocation(m_program, "NormTexture"); m_occLoc = glGetUniformLocation(m_program, "OccTexture"); m_cosine = glGetUniformLocation(m_program, "CosinePower"); m_specular = glGetUniformLocation(m_program, "SpecularContribution"); m_num_lights = glGetUniformLocation(m_program, "NumLights"); vertex shader... uniform int NumLights; uniform float CosinePower; uniform float SpecularContribution; void main(void) { int count = NumLights; // loop through all lights in scene.. for( int i=0; i<count; i++ ) { // do some stuff here.. } } any help appreciated. cheers Angry
modus
modus
Dunno what "// do some stuff here.." is but if it doesn't actually use NumLights it'll be culled by the compiler, hence the -1 location.

One way to get around this limitation (at a coarser granularity) is to stuff your uniforms into blocks (added in GL 3.1) and use a shared/std140 layout. That way at least if you reference something in the block you'll get the whole shebang. Pretty new but thought I'd toss it out there...
angry_baker
angry_baker
// do some stuff here.. substitutes ripped out shader code. Didn't want to send through too much code on this thread.

NumLights is used within the for loop to sum the intensity for each light source in the scene.

As I understand it, the shader compiler strips out any un-neccessary stuff. Does anyone know if there are any issues with sending through an int to be used to control a loop??

How do other people count through the number of lights in their scenes???
V-man
V-man
Your code is fine except I would write
for(int i=0; i
use cgc to compile that GLSL code to NV_fragment_program3 and inspect the code.
It might even be simpler to test your code on some ATI/AMD GPU and also another nvidia GPU/driver combination.
angry_baker
angry_baker
Thanks for the replies.... however, I've just found out what the problem was.

For those interested in avaoiding a similar mistake please read on:

According to Game Programming Wiki - http://gpwiki.org/index.php/OpenGL:Tutorials:Virtualized_Lights_with_OpenGL_and_GLSL
"..current drivers will not let you use a variable index with gl_LightSource without first declaring with a size". This means I cannot pass in a variable to control the number of lights.

The suggested implementation works fine.... not a particulary clean method though.

cheers

Angry

Topic Locked

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

Sign in to reply to this topic.