因为有一段时间,我在OpenGL中遇到了相机旋转问题。
我尝试通过鼠标移动来旋转相机,但相机只是闪烁(相机发现的物体闪烁)。
我按如下方式初始化相机:
Camera::Camera(float x, float y, float z) {
memset(Transform, 0, 16*sizeof(float));
Transform[0] = 1.0f;
Transform[5] = 1.0f;
Transform[10] = 1.0f;
Transform[15] = 1.0f;
Transform[12] = x; Transform[13] = y; Transform[14] = z;
Left=&Transform[0];
Up=&Transform[4];
Forward=&Transform[8];
Position=&Transform[12];
old_x = 0;
old_y = 0;
}
这里的Transform是Transformation矩阵。由于OpenGL是专栏专业,它应该是对的吗?
下一部分解释了在绘制框架之前发生了什么。
首先,我使用鼠标移动刷新相机,取决于最后一个鼠标指针位置的delta x和y,移动值可以是positiv或negative:
void Camera::refresh(){
delta_x = UserInputHandler::getMouseMotion()[0];
delta_y = UserInputHandler::getMouseMotion()[1];
}
在下一步中,我正在调整场景中的相机。如果鼠标沿x轴或y轴移动,我想旋转相机:
void Camera::adjust(){
if(old_x != UserInputHandler::getMousePosition()[0]){
// rotate around y axis
rotateLocal_y(-1.0f*(delta_x));
// save old mouse position
old_x = UserInputHandler::getMousePosition()[0];
}
if(old_y != UserInputHandler::getMousePosition()[1]){
rotateLocal_x(-1.0f*(delta_y));
old_y = UserInputHandler::getMousePosition()[1];
}
// loading the calculated Transform matrix to a viewmatrix
setView();
}
围绕y轴的旋转是与旋转矩阵和变换矩阵的矩阵乘法:
//rotate a matrix around y axis
void rotateMatrixf_y(float *aMatrix, float angle){
float rotMatrix[] = {cos(angle),0,-1*sin(angle),0, 0, 1, 0, 0, sin(angle), 0, cos(angle), 0, 0, 0, 0, 1};
multMatrixMatrix(aMatrix, rotMatrix);
}
请记住,OpenGL是列专业,乘法功能是这样创建的:
void multMatrixMatrix(float *m_a, float *m_b){
// column major
float m_c[16] = {m_a[0]*m_b[0]+m_a[4]*m_b[1]+m_a[8]*m_b[2]+m_a[12]*m_b[3], //0
m_a[1]*m_b[0]+m_a[5]*m_b[1]+m_a[9]*m_b[2]+m_a[13]*m_b[3], //1
m_a[2]*m_b[0]+m_a[6]*m_b[1]+m_a[10]*m_b[2]+m_a[14]*m_b[3], // 2
m_a[3]*m_b[0]+m_a[7]*m_b[1]+m_a[11]*m_b[2]+m_a[15]*m_b[3], // 3
m_a[0]*m_b[4]+m_a[4]*m_b[5]+m_a[8]*m_b[6]+m_a[12]*m_b[7], //4
m_a[1]*m_b[4]+m_a[5]*m_b[5]+m_a[9]*m_b[6]+m_a[13]*m_b[7], //5
m_a[2]*m_b[4]+m_a[6]*m_b[5]+m_a[10]*m_b[6]+m_a[14]*m_b[7], // 6
m_a[3]*m_b[4]+m_a[7]*m_b[5]+m_a[11]*m_b[6]+m_a[15]*m_b[7], // 7
m_a[0]*m_b[8]+m_a[4]*m_b[9]+m_a[8]*m_b[10]+m_a[12]*m_b[11], // 8
m_a[1]*m_b[8]+m_a[5]*m_b[9]+m_a[9]*m_b[10]+m_a[13]*m_b[11], //9
m_a[2]*m_b[8]+m_a[6]*m_b[9]+m_a[10]*m_b[10]+m_a[14]*m_b[11], // 10
m_a[3]*m_b[8]+m_a[7]*m_b[9]+m_a[11]*m_b[10]+m_a[15]*m_b[11], // 11
m_a[0]*m_b[12]+m_a[4]*m_b[13]+m_a[8]*m_b[14]+m_a[12]*m_b[15], // 12
m_a[1]*m_b[12]+m_a[5]*m_b[13]+m_a[9]*m_b[14]+m_a[13]*m_b[15], // 13
m_a[2]*m_b[12]+m_a[6]*m_b[13]+m_a[10]*m_b[14]+m_a[14]*m_b[15], // 14
m_a[3]*m_b[12]+m_a[7]*m_b[13]+m_a[11]*m_b[14]+m_a[15]*m_b[15] // 15
};
for(int i = 0; i<16;i++){
m_a[i] = m_c[i];
}
}
此时,矩阵应该很好并加载到OpenGL。在adjust()中调用了setView方法:
void Camera::setView() {
float viewmatrix[16]={//Remove the three - for non-inverted z-axis
Transform[0], Transform[4], -Transform[8], 0,
Transform[1], Transform[5], -Transform[9], 0,
Transform[2], Transform[6], -Transform[10], 0,
-(Transform[0]*Transform[12] +
Transform[1]*Transform[13] +
Transform[2]*Transform[14]),
-(Transform[4]*Transform[12] +
Transform[5]*Transform[13] +
Transform[6]*Transform[14]),
//add a - like above for non-inverted z-axis
(Transform[8]*Transform[12] +
Transform[9]*Transform[13] +
Transform[10]*Transform[14]), 1};
}
glLoadMatrixf(viewmatrix);
}
在完整场景中调用函数,如下所示:
int DrawGLScene(GLvoid){//这是我们所有绘图的地方
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reset The Current Modelview Matrix
glPushMatrix();
cam.refresh();
cam.adjust();
// distance between object and near plane
glTranslatef(0.0f, 0.0f,-30.0f);
// testwise rotating the drawn object
float rotX,rotY,rotZ;
rotX = -90.0f;
rotZ = 0.0f;
rotY = 0.0f;
MeshNode* aMeshNode = myMeshLoader.getMeshNode();
while(aMeshNode->next){
Mesh aMesh = *aMeshNode->theMesh;
FaceNode* aFaceNode = aMesh.getFirstFaceNode();
while(aFaceNode->next){
Face theFace = *aFaceNode->aFace;
Vertex theFaceVertexA = aMesh.getVertexAt((*theFace.myVertices)[0]);
Vertex theFaceVertexB = aMesh.getVertexAt((*theFace.myVertices)[1]);
Vertex theFaceVertexC = aMesh.getVertexAt((*theFace.myVertices)[2]);
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
// glNormal3f(*theFace.myNormal[0],*theFace.myNormal[1],*theFace.myNormal[2]);
glVertex3f( theFaceVertexA.position[0], theFaceVertexA.position[1], theFaceVertexA.position[2]);
glVertex3f( theFaceVertexB.position[0], theFaceVertexB.position[1], theFaceVertexB.position[2]);
glVertex3f( theFaceVertexC.position[0], theFaceVertexC.position[1], theFaceVertexC.position[2]);
glEnd(); // Finished Drawing The Triangle
aFaceNode = aFaceNode->next;
}
aMeshNode = aMeshNode->next;
}
glPopMatrix();
return TRUE; // Everything Went OK
}
这里我选择Modelview矩阵,然后加载单位矩阵。 在矩阵推送和弹出之间是相机刷新和调整(包括setView)然后我为我想要绘制的对象设置转换,然后绘制对象。
多数民众赞成。我用矩阵的一些推动和弹出玩了很多,看了翻码摄像头教程(http://www.flipcode.com/archives/OpenGL_Camera.shtml),但闪烁仍然存在。有没有人有什么想法可能是错的?
答案 0 :(得分:0)
可能取决于您的应用程序中的DEPTH_TEST被禁用的事实。
或者,它可能取决于您的设备支持的深度缓冲区的大小。
答案 1 :(得分:0)
我发现了问题,这是因为我使用的是度数而不是辐射。所以旋转大约是90度或大约0度,愚蠢但是真实!