Changing Uniform Variables for a Single Shader

Nibbie
Posts: 4
Joined: 2010.07
Post: #1
I'm currently trying to use a single shader to render a bunch of separate objects where each object has their own orientation in the world. I plan on moving to batching soon but some objects will still be separate.

From what I understand the glUseProgram call has quite a lot of OpenGL driver overhead so it would make sense to set a program and reuse it by changing variables for each object I render. The problem is when I do this they seem to all render to the same position on the screen.

What I am trying to do is the following:

glUseProgram(...)
glUniform(...)
glDrawArrays(...)
glUniform(...)
glDrawArrays(...)

The impression I get is that you can only set uniform variables once? From my research I have read that this should be working but please correct me if I'm wrong. I know atleast the shader and the draw code is correct, because when I add the glUseProgram line in between each glDrawArrays call it renders correctly.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2010.03
Post: #2
You're correct, you should be able to set uniforms multiple times with draw calls. You're right to want to minimise calls to glUseProgram as it's expensive.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2010.07
Post: #3
(Jul 14, 2010 01:21 PM)dazza Wrote:  You're correct, you should be able to set uniforms multiple times with draw calls. You're right to want to minimise calls to glUseProgram as it's expensive.

Yes I realise it is quite an expensive call which is why I'd like to only call it once but when I do it is like the transform matrixes don't get updated in between the drawing of entities... Even though the only thing that has changed is me calling glUseProgram once at the start of a group rather than in between.

I have no idea why this should be happening, I wonder is there anything I should be looking at? Because I was under the same impression, that I should be able to change the variables.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2010.03
Post: #4
Hmm, given what you are doing should work it's probably something simple. Check that your uniform variable is correct and that you are indeed passing a different matrix for each model.

I have a feeling you'll kick yourself when you see the problem.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  ios/mac shader - shared glsl source OptimisticMonkey 2 5,253 Jun 17, 2011 08:59 AM
Last Post: OptimisticMonkey
  passing values from vertex to fragment shader Sumaleth 6 10,958 Feb 18, 2011 01:54 AM
Last Post: Holmes
  Changing Pixel Values using CG LIPH700 1 4,520 Nov 25, 2010 03:17 PM
Last Post: SethWillits
  Texture mapping single 3D object with one draw call jeonghyunhan 1 2,732 Jul 13, 2009 06:05 AM
Last Post: ThemsAllTook
  Vertex shader particle billboarding question TomorrowPlusX 3 5,640 Sep 15, 2008 06:46 AM
Last Post: TomorrowPlusX