Linas
Linas

Reputation: 141

Camera is upside down OpenGL

I'm having an issue with my camera being upside down in OpenGL. If I render my objects without setting the camera Z rotation to 180 degrees, the objects get rendered upside down. Maybe it has something to do with glm?

Here's how I'm setting up my matrices:

//Model Matrix:
mat4 modelMatrix;
modelMatrix = translate(modelMatrix, vec3(entity.getPosition().x, entity.getPosition().y, entity.getPosition().z));
modelMatrix = rotate(modelMatrix, (float) (entity.getRotation().x / 180 * PI), vec3(1, 0, 0));
modelMatrix = rotate(modelMatrix, (float) (entity.getRotation().y / 180 * PI), vec3(0, 1, 0));
modelMatrix = rotate(modelMatrix, (float) (entity.getRotation().z / 180 * PI), vec3(0, 0, 1));
modelMatrix = scale(modelMatrix, entity.getScale());
return modelMatrix;

//View Matrix:
mat4 viewMatrix;
viewMatrix = rotate(viewMatrix, (float)(camera.getRotation().x / 180 * PI), vec3(1, 0, 0));
viewMatrix = rotate(viewMatrix, (float)(camera.getRotation().y / 180 * PI), vec3(0, 1, 0));
viewMatrix = rotate(viewMatrix, (float)(camera.getRotation().z / 180 * PI), vec3(0, 0, 1));
viewMatrix = translate(viewMatrix, camera.getPosition() * vec3(-1, -1, -1));
return viewMatrix;

//Projection Matrix:
return glm::perspective(camera.getFieldOfView(), Display::getWindowAspectRatio(), camera.getNearPlane(), camera.getFarPlane());

And here's my draw method:

for (int i = 0; i < entityMap.size(); i++)
{
    map<GLuint, vector<Entity>>::iterator iterator(entityMap.begin());
    advance(iterator, i);

    vector<Entity> entityBatch = iterator->second;

    Model entityModel = entityBatch[0].getModel();

    mat4 *modelMatrices = new mat4[entityBatch.size()];

    for (int j = 0; j < entityBatch.size(); j++)
    {
        modelMatrices[j] = Maths::createModelMatrix(entityBatch[j]);
    }

    glBindVertexArray(iterator->first);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);
    glEnableVertexAttribArray(3);
    glEnableVertexAttribArray(4);
    glEnableVertexAttribArray(5);

    GLuint vertexBufferObjectId;
    glGenBuffers(1, &vertexBufferObjectId);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId);

    glBufferData(GL_ARRAY_BUFFER, sizeof(mat4) * entityBatch.size(), modelMatrices, GL_STATIC_DRAW);
    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(0 * sizeof(vec4)));
    glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(1 * sizeof(vec4)));
    glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(2 * sizeof(vec4)));
    glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(3 * sizeof(vec4)));

    glVertexAttribDivisor(2, 1);
    glVertexAttribDivisor(3, 1);
    glVertexAttribDivisor(4, 1);
    glVertexAttribDivisor(5, 1);

    #if ENABLE_INDEXING
    glDrawElementsInstanced(GL_TRIANGLES, entityModel.getIndexCount(), GL_UNSIGNED_INT, 0, entityBatch.size());
    #else
    glDrawArraysInstanced(GL_TRIANGLES, 0, entityModel.getVertexCount(), entityBatch.size());
    #endif

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(3);
    glDisableVertexAttribArray(4);
    glDisableVertexAttribArray(5);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDeleteBuffers(1, &vertexBufferObjectId);

    glBindVertexArray(0);
}

Thank you for help!

Upvotes: 3

Views: 2818

Answers (1)

Linas
Linas

Reputation: 141

The problem was with my projection matrix.

I forgot that glm works in radians and didn't convert the field of view from degrees to radians. Everything works fine now.

I have one more question. Do you need to give the glm::perspect

Thanks for all the help!

Upvotes: 9

Related Questions