enter image description here

Hello, I get this result when i try to scale and then translate a vec4 in my shader. I created a mat4x4 class on my own and some functions wich return some matrices for me, everything works, but not the translationfunction.

Currently, I use this Shader Codes:

GLchar* vertexShaderCode = {
"#version 330 core                              \n"
"layout(location=0) in vec3 position;           \n"
"uniform mat4 scale;                            \n"
"uniform mat4 translate;                        \n"
"out vec4 vertexColor;                          \n"
"void main()                                    \n"
"{                                              \n"
"   gl_Position = translate*scale*vec4(position,1.0); \n"
"   vertexColor = vec4(1.0f,0.5f,0.7f,1.0f);    \n"
"}                                              \n"

GLchar* fragmentShaderCode = {
"#version 330 core                              \n"
"in vec4 vertexColor;                           \n"
"out vec4 color;                                \n"
"void main()                                    \n"
"{                                              \n"
"   color = vertexColor;                        \n"
"}                                              \n"

The matrix setup looks like this:

h3d::mat4x4 mat_scale,
mat_scale     = h3d::Math::scale(.5f, .5f, 0.f);
mat_translate = h3d::Math::translate(.5f, .5f, 0.0);

GLint location_scale       = program.Uniform.getLocation("scale");
GLint location_translation = program.Uniform.getLocation("translate");

glUniformMatrix4fv(location_scale,      1, GL_TRUE,mat_scale.getRowWiseValues());
glUniformMatrix4fv(location_translation,1, GL_TRUE,mat_translate.getRowWiseValues());

So basically, the h3d::Math::scale(X,Y,Z) returns:

  • X,0,0,0
  • 0,Y,0,0
  • 0,0,Z,0
  • 0,0,0,1

And h3d::Math::translate(X,Y,Z) returns this:

  • 1,0,0,X
  • 0,1,0,Y
  • 0,0,1,Z
  • 0,0,0,1

The vertices are normal:

GLfloat vertices[] = {
    -1.0f, 1.0f, 0.f,
     1.0f, 1.0f, 0.f,
     1.0f,-1.0f, 0.f,
    -1.0f,-1.0f, 0.f,

What do I am doing wrong ? I really appreactiate your help ! All the other matrices are working (scale, rotate, perspective).

  • \$\begingroup\$ What do these return: mat_translate.getRowWiseValues() & mat_scale.getRowWiseValues() ? \$\endgroup\$ – mythos Jun 18 '16 at 17:30
  • 1
    \$\begingroup\$ What if you transpose your translation matrix? \$\endgroup\$ – HolyBlackCat Jun 18 '16 at 18:04

OpenGL uses column-major matrices, but as I understand from your code you pass row-major matrices. So in GLSL scale[0] is the first column of scale matrix.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.