Search code examples
graphicsglslshadercg

GLSL correspondence of dot( sina, 1..xxxx ) in CG


Hi I am trying to convert a CG shader into GLSL shader. It is quite similar but I couldn't find the correspondence for

dot( sina, 1..xxxx ); 

Could you explain what this line does and help me to write GLSL equivalent.


Solution

  • Assuming that sina is a 4 component vector what the snippet does is returning the sum of sinas components. The dot product is defined as follows:

    enter image description here

    The .xxxx part of the snippet you posted is a swizzle expanding the scalar before it (1. in your case) to a four component vector, evaluated that leads to:

    dot(sina,1..xxxx) = sina.x * 1 + sina.y * 1 + sina.z * 1 + sina.w * 1
                      = sina.x + sina.y + sina.z + sina.w
    

    As GLSL does not allow swizzling scalar values the GLSL equivalent would be:

    dot(sina,vec4(1.));