2013-10-28 75 views
0

我想旋轉一個3D場景中的對象。在下面的代碼中,我簡單地旋轉了WorldMatrix。但是如果場景包含兩個物體而不是一個物體呢?如果我旋轉的WorldMatrix都會旋轉(以一種奇怪的方式)。如何在不改變其他模型的情況下旋轉場景中的單個對象?如何旋轉3D場景中的單個對象?

// Clear the buffers to begin the scene. 
m_OpenGL->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); 

// Generate the view matrix based on the camera's position. 
m_Camera->Render(); 

// Get the world, view, and projection matrices from the opengl and camera objects. 
m_OpenGL->GetWorldMatrix(worldMatrix); 
m_Camera->GetViewMatrix(viewMatrix); 
m_OpenGL->GetProjectionMatrix(projectionMatrix); 

// Get the light properties. 
m_Light->GetDirection(lightDirection); 
m_Light->GetDiffuseColor(diffuseLightColor); 
m_Light->GetAmbientLight(ambientLight); 

// Rotate the world matrix by the rotation value so that the object will spin. 
m_OpenGL->MatrixRotationY(worldMatrix, rotation); 

// Set the light shader as the current shader program and set the matrices that it will use for rendering. 
m_LightShader->SetShader(m_OpenGL); 
m_LightShader->SetShaderParameters(m_OpenGL, worldMatrix, viewMatrix, projectionMatrix, 0, lightDirection, diffuseLightColor, ambientLight); 

// Render the model using the light shader. 
m_Model->Render(m_OpenGL); 

// Present the rendered scene to the screen. 
m_OpenGL->EndScene(); 

回答

2

您希望呈現的每個「對象」至少應包含其自己的包含旋轉和位置信息的4x4矩陣。這樣,如果您只想旋轉一個對象,您只需編輯它自己的個人矩陣。

管理所有這些矩陣操作的最簡單方法是通用目的matrix stack

不幸的是,內置的OpenGL矩陣堆棧功能(glPushglPop等)與大多數舊的固定功能管線一起被棄用。幸運的是,對於您來說,一位StackOverflow用戶發佈了一個裸骨架矩陣堆棧:Replacing glPush/PopMatrix

2

對每個對象都有一個「對象矩陣」,在渲染該對象之前推送它,然後彈出。有了這個,您可以修改每個對象的對象矩陣以便旋轉它(或以任何其他方式進行轉換)。

0

首先,你應該畫你的對象旋轉。

void DrawObject(Object* object) 
{ 
    glTranslate(object->y); 
    glRotate(object->rotationY, roll, yaw , pitch); 
} 
+0

滾動,偏航和俯仰是Y軸上的旋轉方向。這些是浮動的,可以包含小數。 – MilanSxD