0

So lately i've took my first serious steps (or at least i think so) into opengl/glsl and shaders in general. Ive managed to construct and render VBOs, create and compile shaders and also mess with them in some sort of way. I'm using a vertex shader to fix my opengl view (correct the aspect ratio) and also perform animation. This is achieved with varius matrix manipulations.

One would ask why am i using vertex shaders for animation, but reading articles around the globe i got the impression it's best to maintain static VBOs rather updating them constantly. Some sort of GPU>CPU battle. Now i may be wrong about it that's why im reaching here for aid on the matter. My view on it is that in the future i might make a game which (for instance) will have a lot of coins for a player to grab and i would like them to be staticly stored at the GPU side. And then use the shader for rotating them.

Moving on.. "Let there be light".

I've also managed to use my normals in the vertex shader to reproduce lighting. It all worked fine with the exception that light rotates with my cube (currently im using a cube as a test dummy). Now, i know what's wrong here. It's my vertex shader transforming absolutely everything (even my light source i guess). And i can think of a way or two on how to solve this problem. One would be to apply reverse-negative transformation forces on my light source so i can keep it static as everything else rotates.

And here's where everything blurs. Im reaching stackoverflow for guidance on how to move forward. I am trying to think bigger in a way-sense : what if, in the future, i'll have plenty objects i'd like to perform basic animations for (such as rotation, scaling, translations). Would that require me to have different shaders or even a packed one with every function in it. And how would i even use this. Would i pass different values before every object rending inside the same shader?

Right now, to be honest, i want to handle the lighting issue. But i have a feeling that the way im about to approach this will set my general approach in shading animations in general. One suggested (here in stackoverflow in another question) that one should really use different shaders and swap them before every VBO rendering. I have my concerns on wether that's efficient enough, but i definately like the idea.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Ap Jatrid
  • 15
  • 1

1 Answers1

0

One suggested (here in stackoverflow in another question) that one should really use different shaders and swap them before every VBO rendering.

Which question/answer was this? Because you normally should avoid switching shaders where possible. Maybe the person meant uniforms, which are parameters to shaders, but can be changed for cheap.

Also your question is far too broad and also not very concise (all that backstory hides the actual issue). I strongly suggest you split up your doubts into a number of small questions which can be answered in separate.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • this one -> http://stackoverflow.com/questions/7218147/render-multiple-objects-with-opengl-es-2-0 You are absolutely right about my question but i think it's because i'm thinking of treating my light source like an object that needs special(different) transformation in my scene. And if this is the way, then the rest should follow the same(meaning every other object/vbo that will require different transformation in my scene.) Did that help at all or made it worst? :| – Ap Jatrid Dec 05 '12 at 10:36
  • I'd just like to add that my question reflects the confusion state im currently into. What i'm seeking is not a code fix but rather an orientation/proper guidance. That's why my question lacks any code. Also i forgot to thank you for answering, so there you go :-) – Ap Jatrid Dec 05 '12 at 10:42
  • @ApJatrid: None of the answer suggests switching shaders. They always suggest to switch the Vertex Array binding (either the Buffer Object, or a compund Array Object). Regarding how to treat lights. Well, the usual approach is to use global (=world) coordinates for the light positions and pass them as a uniform array to the fragment shader. However when you want to render a scene with lots and lots of light sources you may want to look into deferred shading or precomputed light volumes, or a combination of them. Illumination is a rather complex subject. – datenwolf Dec 05 '12 at 10:50
  • "They always suggest to switch the Vertex Array binding (either the Buffer Object, or a compund Array Object)." <- Does this point towards keyframe animations, shall i drop the whole vertex shader animation thing?) Im only interested in one light source and what's happening now is that the light source seems to be rotating along with my object so my guess is that my vertex shader applies transformation to everything all together. ... – Ap Jatrid Dec 05 '12 at 13:03
  • ... Im thinking i should split the object and the light source and handle them differently in the vertex shader (if that's even possible) so the light source stays still while everything else around it (the object) rotates – Ap Jatrid Dec 05 '12 at 13:03
  • @ApJatrid: Well, that's a completely different issue (light moving along). So far you did not show your shader code. But the general idea is, to split vertex transformation into 3 stages: 1st transform into vertices eye space coordinates (modelview matrix); 2nd determine illumination vectors in eye space (normal transformation with the normal matrix); 3rd transform from eye space into clip space (projection matrix). Then in the fragment shader do the interpolation of light directions and perform the lighting calculation. The position of the light sources must be transformed into eye space. – datenwolf Dec 05 '12 at 13:06
  • @ApJatrid: However since you'll have only a handfull of light sources, do the transformation on the CPU and pass the eye space coordinates of the light sources to the shader as uniform. – datenwolf Dec 05 '12 at 13:07
  • The perfect road map i was looking for. It is obvious to me now that steps from this chain must have been neglected. Sometimes i feel sad about taking this opengl journey alone. Anyways thank you so much for your time on my question :-) – Ap Jatrid Dec 05 '12 at 13:22