2013-10-28 59 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