2013-10-28 2 views
0

3D 장면에서 객체를 회전하고 싶습니다. 아래의 코드에서 WorldMatrix를 단순히 돌렸다. 그러나 장면에 2 개의 물체가 포함되어 있다면 어떨까요? 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 매트릭스 스택 기능 (glPush, glPop 등)은 기존 고정 기능 파이프 라인의 대부분과 함께 사용되지 않습니다. 그러나 다행스럽게도 동료 StackOverflow 사용자가 베어 본 매트릭스 스택을 게시했습니다 : Replacing glPush/PopMatrix.

2

각 개체에 대한 "개체 행렬"이 있습니다.이 개체 매트릭스는 해당 개체를 렌더링하기 전에 누른 다음 나중에 팝합니다. 이 위치를 사용하여 각 객체의 객체 행렬을 회전하여 회전시킬 수 있습니다 (또는 다른 방법으로 변환 할 수 있음).

0

먼저 회전 할 개체를 그려야합니다.

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

롤, 요우 및 피치는 Y 축의 회전 방향입니다. 그것들은 수레이며 십진수를 포함 할 수 있습니다. – MilanSxD