如何使用glUniformMatrix4fv将4x4转换矩阵传递给vShader?

时间:2012-10-17 20:48:39

标签: c++ opengl

我确定有一种更好的方法让我从四元数中计算出我的变换矩阵,但这就是我现在所拥有的。我需要将16个值传递到着色器,但是我遇到了问题。我一直遇到类型不兼容错误。此外,一旦我得到它,我必须做任何事情来将它增加到我已经拥有的吗?

这是我的显示和着色器

void display()
{    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if (trackballMove) 
{
   glRotatef(angle, axis[0], axis[1], axis[2]);
}
//colorcube();
glUniform3fv( theta, 1, Theta );

float a = angle;
float b = axis[0];
float c = axis[1];
float d = axis[2];
float xform[16] = {(a*a+b*b-c*c-d*d),(2*b*c-2*a*d),(2*b*d+2*a*c),0.0,
(2*b*c+2*a*d),(a*a-b*b+c*c-d*d),(2*c*d-2*a*b),0.0,
(2*b*d-2*a*c),(2*c*d+2*a*b),(a*a-b*b-c*c+d*d),0.0,
0.0,0.0,0.0,1.0};
        GLuint Transform = glGetUniformLocation( program, "vTransform");
        glUniformMatrix4fv(Transform,  xform);
glDrawArrays( GL_TRIANGLES, 0, NumVertices );
glutSwapBuffers();
}    

这是着色器

#version 150
in vec4 vPosition;
in vec4 vColor;
out vec4 color;
uniform vec3 theta;
uniform vec4 vTransform;

void main()
{
// Compute the sines and cosines of theta for each of
// the three axes in one computation.
vec3 angles = radians( theta );
vec3 c = cos( angles );
vec3 s = sin( angles );

// Remember: these matrices are column-major
mat4 rx = mat4( 1.0, 0.0, 0.0, 0.0,
0.0, c.x, -s.x, 0.0,
0.0, s.x, c.x, 0.0,
0.0, 0.0, 0.0, 1.0 );
mat4 ry = mat4( c.y, 0.0, s.y, 0.0,
0.0, 1.0, 0.0, 0.0,
-s.y, 0.0, c.y, 0.0,
0.0, 0.0, 0.0, 1.0 );
mat4 rz = mat4( c.z, -s.z, 0.0, 0.0,
s.z, c.z, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 );
color = vColor;

gl_Position = vTransform *(rx * ry * rz * vPosition);
}

1 个答案:

答案 0 :(得分:2)

你的glUniformMatrix4fv没有足够数量的参数。函数签名是

glUniformMatrix4fv(GLint location, GLsizei count,
    GLboolean transpose, const GLfloat *value);

请在此处查看完整的API文档: http://www.opengl.org/sdk/docs/man4/xhtml/glUniform.xml