C++ 如何在3d场景中旋转单个对象?
我想在3d场景中旋转一个对象。在下面的代码中,我只是简单地旋转了WorldMatrix。但是,如果场景包含两个对象而不是一个,该怎么办?如果我旋转WorldMatrix,两者都会旋转(以一种奇怪的方式)。如何在不改变任何其他模型的情况下旋转场景中的单个对象C++ 如何在3d场景中旋转单个对象?,c++,matrix,opengl-4,C++,Matrix,Opengl 4,我想在3d场景中旋转一个对象。在下面的代码中,我只是简单地旋转了WorldMatrix。但是,如果场景包含两个对象而不是一个,该怎么办?如果我旋转WorldMatrix,两者都会旋转(以一种奇怪的方式)。如何在不改变任何其他模型的情况下旋转场景中的单个对象 // Clear the buffers to begin the scene. m_OpenGL->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); // Generate the view matrix ba
// 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();
为每个对象创建一个“对象矩阵”,在渲染该对象之前推送该矩阵,然后弹出。有了它,您可以修改每个对象的对象矩阵以旋转它(或以任何其他方式变换它)。首先,您应该绘制要旋转的对象
void DrawObject(Object* object)
{
glTranslate(object->y);
glRotate(object->rotationY, roll, yaw , pitch);
}
要渲染的每个“对象”至少应包含其自己的4x4矩阵,其中包含旋转和位置信息。这样,如果只想旋转单个对象,只需编辑它自己的个人矩阵即可
管理所有这些矩阵运算的最简单方法是通用的
不幸的是,内置的OpenGL矩阵堆栈功能(
glPush
,glPop
等)与大多数旧的固定函数管道一起被弃用。但幸运的是,StackOverflow的一位用户发布了一个裸体矩阵堆栈:。滚动、偏航和俯仰是Y轴上的旋转方向。这些是浮点数,可以包含小数。