0

I need your help! I am trying to move the vertex transform part from the cpu code to the vertex shader, here's the cpp code of the vertex transform:

//calculate the transform matrix of a refelcting surface
//@point: a point on the surface
//@normal: the normalized normal of the surface
//@return: the transform matrix
glm::mat4 flatReflect(glm::vec3& point, glm::vec3& normal)
{
    glm::vec4 R = glm::vec4(point,1);
    glm::vec4 N = glm::vec4(normal,0);
    GLfloat d = glm::dot(R,N);
    glm::mat4 result;
    result[0][0] = 1 - 2* N.x*N.x;
    result[0][1] = -2*N.x*N.y;
    result[0][2] = -2*N.x*N.z;
    result[0][3] = -2*N.x*d;
    result[1][0] = result[0][1];
    result[1][1] = 1 - 2*N.y*N.y;
    result[1][2] = -2*N.y*N.z;
    result[1][3] = -2*N.y*d;
    result[2][0] = result[0][2];
    result[2][1] = result[1][2];
    result[2][2] = 1-2*N.z*N.z;
    result[2][3] = -2*N.z*d;
    result[3][0] = 0;
    result[3][1] = 0;
    result[3][2] = 0;
    result[3][3] = 1;
    return result;
}

Any idea? Thanks in advance!

Sunf71
  • 93
  • 2
  • 8
  • 1
    Holy wall of code, Batman. Is all of that relevant to your question? Can you make a small example? Trying that will probably help you pin down your own problem, too. – minopret Mar 18 '13 at 01:49
  • OK, so I know little about the rest of the story. I mean, `glShaderSource` at runtime stores a source code string that gets compiled at need and stashed into the graphics hardware, or something? You've opened my eyes to a whole new world :-) Thanks and good luck. – minopret Mar 18 '13 at 03:47

1 Answers1

0

You can translate that fairly readily. First, to translate these lines:

glm::vec4 R = glm::vec4(point,1);
glm::vec4 N = glm::vec4(normal,0);
GLfloat d = glm::dot(R,N);

These types and functions exist natively in GLSL and need no qualification, so:

vec4 R = vec4(point,  1.0);  // An explicit decimal is important; some drivers do
vec4 N = vec4(normal, 0.0);  // not perform implicit coercions.
float d = dot(R, N);

Constructing the matrix differs a little; rather than declaring a matrix and setting its elements, you can create and return it all at once, e.g. (for the identity matrix):

return mat4(1.0, 0.0, 0.0, 0.0,
            0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 0.0, 1.0);

Accessing elements of the vectors works just as in C++. Knowing that, the rest of the translation should be easy. Good luck!

icktoofay
  • 126,289
  • 21
  • 250
  • 231