-2

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?

Peter111
  • 803
  • 3
  • 11
  • 21
  • If your "world transformation of the model" is a transform from model-space to world-space, isn't it just a matter of using that transformation? `world_transform * in_vertex` –  Jan 26 '17 at 23:42

2 Answers2

1

The answer was:

vec4 world_vertex = model_matrix * vec4( vertexPosition, 1.0f );

vec3 world_position = world_vertex.xyz;
Peter111
  • 803
  • 3
  • 11
  • 21
-2

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.

Jadh4v
  • 130
  • 3
  • 2
    After model-view-projection, you're in clip space. World space is after just applying the model matrix. Might be the world transformation op talks about, but who knows. – BDL Jan 26 '17 at 23:19
  • I assume that it is the world transformation matrix (sadly there is no documentation). I just think that it is strange that the worldtransformation matrix is mat4 but the world_position is a vec3 – Peter111 Jan 26 '17 at 23:21
  • 1
    I don't see anything strange with that. Unless you are using homogeneous coordinates `[world_position, 1]` it is impossible to handle translations. Thats also the reason why world_matrix is a mat4 while normal_matrix is a mat3: Normals don't have to be translated, just rotated. – BDL Jan 26 '17 at 23:24
  • ah ok, I am new to this. To the solution is basically what Jadh4v wrote but `model_matrix` instead of `MVP` – Peter111 Jan 26 '17 at 23:27
  • 1
    Most probably. But I wouldn't accept it until the matrix is fixed. Comments could be deleted and only the wrong answer remains. Actually, it is even worth: Since a projection is involved, also a perspective divide would be required. – BDL Jan 26 '17 at 23:33