[英]OpenGL Camera rotation flickering
一段時間以來,我一直在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;
}
這里的“變換”是“變換矩陣”。 由於OpenGL是專欄文章,所以應該正確嗎?
下一部分將說明繪制框架之前發生的情況。
首先,我用鼠標移動刷新相機,具體取決於最后一個鼠標指針位置的增量x和y,移動值可以是正值或負值:
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。 setView方法是在adjust()中調用的:
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),然后為要繪制的對象設置一個變形,然后繪制該對象。
就這樣。 我在矩陣的一些推動和彈出過程中進行了大量操作,看了一下flipcode相機教程( http://www.flipcode.com/archives/OpenGL_Camera.shtml ),但是閃爍仍然存在。 有人有什么主意嗎?
這可能取決於您的應用程序中的DEPTH_TEST被禁用的事實。
或者,這可能取決於設備支持的深度緩沖區的大小。
我發現了問題,這是因為我使用的是度數而不是輻射度。 所以旋轉大約是90度或大約0度,這很愚蠢,但是正確!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.