1
\$\begingroup\$

I'm trying to tranlate a matrix by xy but the result is strange. can someone help me translate and rotate the matrix properly?

void Transform_Update(Transform *transform, GLfloat depth)
{
    Matrix4_SetToIdentity(transform->transformationMatrix);
    Matrix4_Set(transform->transformationMatrix, 0, 3, transform->position->x);
    Matrix4_Set(transform->transformationMatrix, 1, 3, transform->position->y);
    Matrix4_Set(transform->transformationMatrix, 2, 3, depth);
}

Here is my Matrix set value.

void Matrix4_Set(Matrix4 *m, GLuint line, GLuint column, GLfloat value)
{
    m->data[line * 4 + column] = value;
}

Here is where I make the matrix to the identity matrix

void Matrix4_SetToIdentity(Matrix4 *m)
{
    GLfloat data[16] = { 1, 0, 0, 0,
                         0, 1, 0, 0,
                         0, 0, 1, 0,
                         0, 0, 0, 1 };

    memcpy(m->data, data, sizeof(GLfloat) * 16 );
}

This is where I render my mesh and update shader values

void Renderer_Render(Renderer *renderer, Camera *camera)
{
    glUseProgram(renderer->shader->id);

    Transform_Update(renderer->gameObject->transform, renderer->depth);

    GLint location = Shader_GetUniformLocation(renderer->shader, "transformationMatrix");
    glProgramUniformMatrix4fv(renderer->shader->id, location, 1, GL_FALSE, renderer->gameObject->transform->transformationMatrix->data);

    GLint location2 = Shader_GetUniformLocation(renderer->shader, "projectionMatrix");
    glProgramUniformMatrix4fv(renderer->shader->id, location2, 1, GL_FALSE, camera->projectionMatrix);

    GLint location3 = Shader_GetUniformLocation(renderer->shader, "viewMatrix");
    glProgramUniformMatrix4fv(renderer->shader->id, location3, 1, GL_FALSE, camera->viewMatrix);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, renderer->texture->id);

    Mesh_Render(renderer->mesh);
}

Here is my shader transforming the vertices :

#version 400 core

in vec3 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main(void) { 

    vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
    vec4 positionRelativeToCamera = viewMatrix * worldPosition;

    gl_Position = projectionMatrix * positionRelativeToCamera;
    pass_textureCoords = textureCoords;
}

Here is how I get the view and projection matrix :

void Camera_Render(Screen *screen, Camera *camera)
{
    glClearColor(camera->backgroundColor->red, camera->backgroundColor->green, camera->backgroundColor->blue, camera->backgroundColor->alpha);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();            
    glTranslatef(camera->transform->position->x, camera->transform->position->y, 0);
    glRotatef(camera->transform->angle, 0, 0, 1);

    glGetFloatv(GL_PROJECTION_MATRIX, camera->projectionMatrix);
    glGetFloatv(GL_MODELVIEW_MATRIX, camera->viewMatrix);
}

The current output when I change X is this :

The output seams to rotate...Not really...it's just wierd

\$\endgroup\$
0
\$\begingroup\$

It looks like your matrices are row-major, whereas OpenGL expects column-major matrices. In your calls to glProgramUniformMatrix4fv(), you should set the transpose parameter to GL_TRUE, like this:

glProgramUniformMatrix4fv(renderer->shader->id, location, 1, GL_TRUE, renderer->gameObject->transform->transformationMatrix->data);
\$\endgroup\$
  • \$\begingroup\$ bump…. I've changed it but now nothing is rendered. Do you know what is the problem? \$\endgroup\$ – David Neves Dec 2 '18 at 4:15
  • \$\begingroup\$ Great! If you found it useful, don't forget to mark it as having solved your problem by clicking the green checkmark to the left. \$\endgroup\$ – user1118321 Dec 3 '18 at 0:49

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.