2012-07-23 8 views
0

1 인칭 카메라의 스타일을 모방 한 카메라를 만들려고 최선을 다했습니다. 방금 이전 OpenGL 렌더링 방법에서 전환하여 이제 카메라 매트릭스를 처리 할 준비가되었습니다. 내 카메라 업데이트 코드는 다음과 같습니다. OpenGL - 1 인칭 카메라 매트릭스의 문제

void Camera::update(float dt) 
{ 
// Get the distance the camera has moved 
float distance = dt * walkSpeed; 

// Get the current mouse position 
mousePos = mouse->getPosition(); 

// Translate the change to yaw and pitch 
angleYaw -= ((float)mousePos.x-400.0f)*lookSpeed/40; 
anglePitch -= ((float)mousePos.y-300.0f)*lookSpeed/40; 

// Clamp the camera to a max/min viewing pitch 
if(anglePitch > 90.0f) 
    anglePitch = 90.0f; 

if(anglePitch < -90.0f) 
    anglePitch = -90.0f; 

// Reset the mouse position 
mouse->setPosition(mouseReset); 

// Check for movement events 
sf::Event event; 
while (window->pollEvent(event)) 
{ 

    // Calculate the x, y and z values of any movement 
    if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::W) 
    { 
     position.x -= (float)sin(angleYaw*M_PI/180)*distance*25; 
     position.z += (float)cos(angleYaw*M_PI/180)*distance*25; 
     position.y += (float)sin(anglePitch * M_PI/180) * distance * 25; 
     angleYaw = 10.0; 
    } 
    if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::S) 
    { 
     position.x += (float)sin(angleYaw*M_PI/180)*distance*25; 
     position.z -= (float)cos(angleYaw*M_PI/180)*distance*25; 
     position.y -= (float)sin(anglePitch * M_PI/180) * distance * 25; 
    } 
    if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R) 
    { 
     position.x += (float)cos(angleYaw*M_PI/180)*distance*25; 
     position.z += (float)sin(angleYaw*M_PI/180)*distance*25; 
    } 
    if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A) 
    { 
     position.x -= (float)cos(angleYaw*M_PI/180)*distance*25; 
     position.z -= (float)sin(angleYaw*M_PI/180)*distance*25; 
    } 
} 

// Update our camera matrix 
camMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(-position.x, -position.z, -position.y)); 
camMatrix = glm::rotate(camMatrix, angleYaw, glm::vec3(0, 1, 0)); 
camMatrix = glm::rotate(camMatrix, anglePitch, glm::vec3(1, 0, 0)); 
} 

마지막 3 개 라인 제가

번역의 반대측으로 카메라를 업데이트 할 것으로 (예를, Z는 I가 작업하고있는 포맷으로 전환) 무엇. 나는 그들을 잘못된 순서로 했습니까?

#version 120 

attribute vec4 position; 
uniform mat4 camera; 

void main() 
{ 
    gl_Position = position * camera; 
} 

#version 120 
void main(void) 
{ 
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 
} 

이 단순히 빨간색 삼각형을 만든다 :

은 여기 내 아주 간단한 쉐이더입니다. 카메라 종류가 삼각형 주위로 회전합니다. 이는 내가 원하는 것이 아닙니다. 카메라를 돌리고 싶습니다. 나는 각 정점에 의한 카메라 행렬의 곱셈이 카메라 공간에서 렌더링을 줄 것이라고 생각했다. 아니면 프로젝션 매트릭스로도 곱해야합니까?

w, a, s 또는 d를 이동하면 실제로 한 번에 모두 닫히고 모든 부분이 빨간색 조각으로 왜곡됩니다.

답변

2

역순으로 행렬 연산을 작성하십시오. 따라서 (카메라 위치로) 번역하고 회전하려면 다음 순서로 작성하십시오.

// Update our camera matrix 
camMatrix = glm::rotate(glm::mat4(1.0f), anglePitch, glm::vec3(1, 0, 0)); 
camMatrix = glm::rotate(camMatrix, angleYaw, glm::vec3(0, 1, 0)); 
camMatrix = glm::translate(camMatrix, glm::vec3(-position.x, -position.z, -position.y));