Search code examples
c++openglvertex-shader

How to calculate world_position in Shader


I want to program a shader and I have the position of a vertex and 3 matrices:

  • mat4 model-view-projection matrix
  • mat4 world transformation of the model
  • mat3 world transformation for normals

The output has to be

vec3 world_position

How can I calculate this?


Solution

  • Multiply the vertex position as a vec4( pos, 1.0f) with the model matrix to get the world coordinates.

    For example,

    vec4 world_vertex = model_transform * vec4( vertexPosition, 1.0f );
    
    vec3 world_position = world_vertex.xyz;
    

    EDIT

    Apologies for reading the question fleetingly. I assumed the mvp multiplication because it is the typical transformation needed before fragment shader is called. As OP mentioned, vertex position should be multiplied by the model matrix.