2016-02-06 40 views
2

我有一個問題,我的相機在OpenGL中顛倒。 如果我在沒有將相機Z旋轉設置爲180度的情況下渲染我的對象,則對象會呈現顛倒狀態。 也許它與glm有關?相機是倒過來OpenGL

下面是我如何設置我的矩陣:

//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()); 

這是我的繪製方法:

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); 
} 

感謝您的幫助!

+1

沒有顯示你如何繪圖以及如何設置矩陣,這個問題是不可能回答的。 – BDL

+0

旋轉的值是什麼?如果你根本不應用任何旋轉,是否會發生同樣的事情?我可能知道發生了什麼事情,但這並不能解釋爲什麼事情會顛倒過來,除非涉及輪換。 –

+0

我試過沒有應用旋轉,它沒有工作。順便說一句,只是相機翻轉顛倒。世界座標顛倒,物體看起來顛倒。 – Linas

回答

7

問題出在我的投影矩陣上。

我忘了glm以弧度爲單位,並沒有將視角從度數轉換爲弧度。現在一切正常。

我還有一個問題。 你需要給glm :: perspect

感謝您的幫助!

+0

你能接受這個答案嗎?這也是我的問題。 –

+0

我試圖模擬Blender中的相機視圖時遇到此問題。在將FOV從45設置爲49.134(對於35mm膠片)時,Y翻轉倒置。畢竟這一次我用45度作爲我的FOV,實際上我用了45弧度,這個弧度大概是58度。哈! – CJxD