# Dot product, matrix mult

What is the equivalent of
/All are vec4
DP3 temp, variable1, variable2;

It could be
temp = vec4(dot(variable1.xyz, variable2.xyz), dot(variable1.xyz, variable2.xyz), dot(variable1.xyz, variable2.xyz), dot(variable1.xyz, variable2.xyz));

//worldMatrix is a 4x4
//vector is a vec4
mult3x3 temp, worldMatrix, vector;

/All are vec4
DP3 temp, variable1, variable2;

=> vec4 temp = vec4( dot( vec3(var1), vec3(var2) ) );

//worldMatrix is a 4x4
//vector is a vec4
mult3x3 temp, worldMatrix, vector;

=>vec3 temp = mat3( worldMatrix ) * vec3( vector );
(only with latest GLSlang specs, since previously you couldnâ€™t build a mat3 from a mat4 AFAIR)

Thanks, the first one looks good.

For the second, yes I did notice they fix that.
WHat would be the way with the current GLSL 1.10

My take on this:

vec4 temp;
temp.xyz = dot( var1.xyz, var2.xyz );

// Depending on the column ordering of the matrix it is either:

temp.x = dot( worldMatrix[0].xyz, vector.xyz );
temp.y = dot( worldMatrix[1].xyz, vector.xyz );
temp.z = dot( worldMatrix[2].xyz, vector.xyz );

or

temp.xyz = worldMatrix[0].xyz * vector.xyz;
temp.xyz = (worldMatrix[1].xyz * vector.xyz) + temp.xyz;
temp.xyz = (worldMatrix[2].xyz * vector.xyz) + temp.xyz;

@sqrt[-1]: I think you are doing the vec3*mat3, because you dot columns of a matrix, not rows.
Edit: sorry, your second version is okay.

If in GLSL 1.2 you can write:

``````vec3 temp = mat3( worldMatrix ) * vec3( vector );
``````

in GLSL 1.1 it must be this way:

``````mat3 m3 = mat3( worldMatrix[0].xyz, worldMatrix[1].xyz, worldMatrix[2].xyz );
vec3 temp = m3 * vec3( vector );
``````

vec4 temp;
temp.xyz = dot(var1.xyz, var2.xyz);
Thatâ€™s actually not good because it will complain that you are trying to cast a float to vec3
This will work
temp.xyz = vec3(dot(var1.xyz, var2.xyz));

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.