Help Learning Shaders

Hello,



I am trying to become familiar with coding shaders in GLSL.

My biggest problem I am running into right now is my lack of understanding of the different “spaces” (IE: world, projection, etc.)



So, from my understanding, these are the 4 projection matrices available:



[java]uniform mat4 g_WorldViewProjectionMatrix;

uniform mat4 g_WorldViewMatrix;

uniform mat4 g_ViewMatrix;

uniform mat4 g_WorldMatrix;[/java]





I have read the small amount of detail on this page,

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders?s[]=shader

and am still pretty confused.

So, on that page we have

Object
World
View
Projection

And I understand what they mean there, but I am having a hard time matching them to their JME3 counterparts. So any help on that would be the first part of my question.

The second part of my question would about the rules of transforming between projections using these matrices. The way it looks on the page above is that you start out in Object space and can progress to any of the different spaces, but because of the arrows delineating the progression, I am assuming I cannot go from say, view space back to object space. Is that a correct assumption?

Thanks for the help on this topic.

Given those 4 spaces (object/model, world, view, projection/clip), you can convert between them using the matrices. For example the g_WorldMatrix converts from object/model to world space. The g_WorldViewMatrix converts from object/model to view space. The g_ViewMatrix converts from world to view space. Basically the g_WorldViewMatrix is a combination of the g_WorldMatrix and g_ViewMatrix.

If you wanted to go back, you can use the inverses, for example g_WorldViewMatrixInverse will let you go from view space to object/model space.

2 Likes

Well that helps clear some things up for me.



One more question regarding this topic,



For variables entering the shader, such as the inPosition or CameraPosition, do they all default into the Object/Model projection and then get reassigned from there, or do different variables enter in different projections?





Thanks

@Eggsworth said:
For variables entering the shader, such as the inPosition or CameraPosition, do they all default into the Object/Model projection and then get reassigned from there, or do different variables enter in different projections?

The vertex attributes (in*** vars) are almost always in object/model space. World parameters (g_*** vars), are usually in world space but its best to consult the documentation to make sure.

The hidden “but” in momoko’s post (the ‘almost always’ part) is that it entirely depends on what data was shoved into the mesh… which is entirely up to you.



By convention, a mesh is in object space but you could also make a mesh with vertexes in world coordinates and ignore the world transform completely. You could shove colors into normals, normals into colors, whatever… it’s just data with a name that by convention has a meaning. The meaning is entirely dependent on the shader operating upon it.