1
\$\begingroup\$

Im trying to move a transformation matrix onto the gpu, and I managed to find this code to help me on the way:

vertex.shader:

#version 410 core
layout ( location = 0 ) in vec3 vertex_position ;

mat4 rotationMatrix(vec3 axis, float angle)
{
    axis = normalize(axis);
    float s = sin(angle);
    float c = cos(angle);
    float oc = 1.0 - c;

return mat4(oc * axis.x * axis.x + c,           oc * axis.x * axis.y - axis.z * s,  oc * axis.z * axis.x + axis.y * s,  0.0,
            oc * axis.x * axis.y + axis.z * s,  oc * axis.y * axis.y + c,           oc * axis.y * axis.z - axis.x * s,  0.0,
            oc * axis.z * axis.x - axis.y * s,  oc * axis.y * axis.z + axis.x * s,  oc * axis.z * axis.z + c,           0.0,
            0.0,                                0.0,                                0.0,                                1.0);
}


void main()
{

gl_Position=rotationMatrix(vertex_position,45.0);


}

However, this gives errorc1035: assignment of incompatible types. Shouldnt this work? What am I missing

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

gl_Position expects a variable of type vec4 and you're giving it mat4. So you need to multiply your rotation matrix by the vertex position and return the resulting vector. Something like:

gl_Position = rotationMatrix(axis, angle) * vec4(vertex_position, 1.0);
\$\endgroup\$
5
  • \$\begingroup\$ so what is the axis here then? a vec3? Im confused \$\endgroup\$
    – Charlie
    Oct 15, 2015 at 5:32
  • \$\begingroup\$ That would be the axis around which you wish to rotate the vertices, so that means vec3. \$\endgroup\$
    – Martin Ciz
    Oct 15, 2015 at 5:37
  • \$\begingroup\$ In most cases though, you should precalculate your transformation matrices on the CPU and not the GPU, because this way, you are calling the rotationMatrix for each vertex and I think that's going to slow things down. \$\endgroup\$
    – Martin Ciz
    Oct 15, 2015 at 5:46
  • \$\begingroup\$ I thought you wanted to get the gpu to do the work. hm, so not a good idea then \$\endgroup\$
    – Charlie
    Oct 15, 2015 at 5:48
  • \$\begingroup\$ Generally that would be true, but you also want to reuse as many calculations as possible: ) \$\endgroup\$
    – Martin Ciz
    Oct 15, 2015 at 5:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .