C++ 相机是倒置的OpenGL
我的相机在OpenGL中颠倒了,这是个问题。 如果在未将摄影机Z旋转设置为180度的情况下渲染对象,则对象会颠倒渲染。 也许这和glm有关 下面是我如何设置矩阵的:C++ 相机是倒置的OpenGL,c++,opengl,camera,rotation,C++,Opengl,Camera,Rotation,我的相机在OpenGL中颠倒了,这是个问题。 如果在未将摄影机Z旋转设置为180度的情况下渲染对象,则对象会颠倒渲染。 也许这和glm有关 下面是我如何设置矩阵的: //Model Matrix: mat4 modelMatrix; modelMatrix = translate(modelMatrix, vec3(entity.getPosition().x, entity.getPosition().y, entity.getPosition().z)); modelMatrix = rot
//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);
}
for(int i=0;i秒;
模型entityModel=entityBatch[0]。getModel();
mat4*ModelMatrix=新mat4[entityBatch.size()];
对于(int j=0;j第一);
GlenableVertexAttributeArray(0);
GlenableVertexAttributeArray(1);
GlenableVertexAttributeArray(2);
GlenableVertexAttributeArray(3);
GlenableVertexAttributeArray(4);
GlenableVertexAttributeArray(5);
GLuint Vertexd;
glGenBuffers(1和vertexBufferObjectId);
glBindBuffer(GL_数组_BUFFER,vertexBufferObjectId);
glBufferData(GL_数组_缓冲区,sizeof(mat4)*entityBatch.size(),ModelMatrix,GL_静态_绘图);
glvertexattributepointer(2,4,GL_FLOAT,GL_FALSE,4*sizeof(vec4),(GLvoid*)(0*sizeof(vec4));
glvertexattributepointer(3,4,GL_FLOAT,GL_FALSE,4*sizeof(vec4),(GLvoid*)(1*sizeof(vec4));
glvertexattributepointer(4,4,GL_FLOAT,GL_FALSE,4*sizeof(vec4),(GLvoid*)(2*sizeof(vec4));
glvertexattributepointer(5,4,GL_FLOAT,GL_FALSE,4*sizeof(vec4),(GLvoid*)(3*sizeof(vec4));
glvertexattributionor(2,1);
glvertexattributionor(3,1);
glvertexattributionor(4,1);
glvertexattributionor(5,1);
#如果启用索引
GLDrawerElementsInstanced(GL_三角形,entityModel.getIndexCount(),GL_UNSIGNED_INT,0,entityBatch.size());
#否则
glDrawArraysInstanced(GL_三角形,0,entityModel.getVertexCount(),entityBatch.size());
#恩迪夫
glDisableVertexAttributeArray(0);
glDisableVertexAttributeArray(1);
GLDisableVertexAttributeArray(2);
GLDisableVertexAttributeArray(3);
GLDisableVertexAttributeArray(4);
GLDisableVertexAttributeArray(5);
glBindBuffer(GL_数组_BUFFER,0);
glDeleteBuffers(1和vertexBufferObjectId);
glBindVertexArray(0);
}
谢谢你的帮助 问题在于我的投影矩阵 我忘记了glm是以弧度工作的,并且没有将视野从度转换为弧度。现在一切都好了 我还有一个问题。 您需要给出glm::透视图吗
谢谢你的帮助 如果不显示您是如何绘制和设置矩阵的,这个问题是不可能回答的。旋转的值是多少?如果根本不应用任何旋转,是否会发生相同的情况?我可能知道发生了什么,但它无法解释为什么事情会颠倒,除非涉及旋转。我试着不应用旋转,但它不起作用。顺便说一句,只是相机上下颠倒了。世界坐标颠倒了,物体看起来颠倒了。我在尝试模拟Blender中的相机视图时遇到了这个问题。将视场从45设置到49.134(对于35mm胶片),Y翻转过来。在这段时间里,我一直使用45度作为视野,实际上我一直使用45弧度,环绕到大约58度。哈