2016-09-04 40 views
0

在爲我的渲染器實施攝像頭時遇到了一些問題。正如問題所述,我想知道生成這樣一臺相機的必要步驟,包括視場和縱橫比。重要的是座標系是左手的,以便-z將相機從屏幕上推開(如我理解它)。我嘗試過在線查看,但大部分實現不完整或失敗了我。任何幫助表示讚賞。謝謝。路徑跟蹤 - 使用左手座標系生成攝像機光線

回答

0

我遇到了麻煩,花了很長時間才弄清楚。這是相機類的代碼。

#ifndef CAMERA_H_ 
#define CAMERA_H_ 

#include "common.h" 

struct Camera { 
    Vec3fa position, direction; 
    float fovDist, aspectRatio; 
    double imgWidth, imgHeight; 
    Mat4 camMatrix; 

    Camera(Vec3fa pos, Vec3fa cRot, Vec3fa cDir, float cfov, int width, int height) { 
     position = pos; 
     aspectRatio = width/(float)height; 
     imgWidth = width; 
     imgHeight = height; 

     Vec3fa angle = Vec3fa(cRot.x, cRot.y, -cRot.z); 
     camMatrix.setRotationRadians(angle * M_PI/180.0f); 

     direction = Vec3fa(0.0f, 0.0f, -1.0f); 
     camMatrix.rotateVect(direction); 

     fovDist = 2.0f * tan(M_PI * 0.5f * cfov/180.0); 
    } 

    Vec3fa getRayDirection(float x, float y) { 
     Vec3fa delta = Vec3fa((x-0.5f) * fovDist * aspectRatio, (y-0.5f) * fovDist, 0.0f); 
     camMatrix.rotateVect(delta); 
     return (direction + delta); 
    } 
}; 

#endif 

櫃面如果你需要rotateVect()代碼在MAT4類

void Mat4::rotateVect(Vector3& vect) const 
{ 
    Vector3 tmp = vect; 
    vect.x = tmp.x * (*this)[0] + tmp.y * (*this)[4] + tmp.z * (*this)[8]; 
    vect.y = tmp.x * (*this)[1] + tmp.y * (*this)[5] + tmp.z * (*this)[9]; 
    vect.z = tmp.x * (*this)[2] + tmp.y * (*this)[6] + tmp.z * (*this)[10]; 
} 

這裏是我們的setRotationRadians代碼

void Mat4::setRotationRadians(Vector3 rotation) 
{ 
    const float cr = cos(rotation.x); 
    const float sr = sin(rotation.x); 
    const float cp = cos(rotation.y); 
    const float sp = sin(rotation.y); 
    const float cy = cos(rotation.z); 
    const float sy = sin(rotation.z); 

    (*this)[0] = (cp * cy); 
    (*this)[1] = (cp * sy); 
    (*this)[2] = (-sp); 

    const float srsp = sr * sp; 
    const float crsp = cr * sp; 

    (*this)[4] = (srsp * cy - cr * sy); 
    (*this)[5] = (srsp * sy + cr * cy); 
    (*this)[6] = (sr * cp); 

    (*this)[8] = (crsp * cy + sr * sy); 
    (*this)[9] = (crsp * sy - sr * cy); 
    (*this)[10] = (cr * cp); 
} 
+0

謝謝回答!已經實現了一個,但它不是最好的,並且有一定的問題。所以,急於嘗試別的。總是有幾個問題。Matrix初始化爲什麼?是身份矩陣還是其他一些4x4值。其次,我能否看到設置的旋轉弧度代碼?我想比較一下東西,最後只是一種好奇心 - 爲什麼要用矩陣而不是通常的交叉產品方法?這就是全部。更多......非常感謝:) – hecatonchries

+0

CamMatrix初始化爲Identity Matrix。你可以在沒有矩陣的情況下達到同樣的效果,但我最近成爲了矩陣的粉絲,所以我使用了它。 – codetiger

+0

噢好吧,我想我們都是不同的.Cuz我真的很討厭matrices.Lol.Wont使用它們,直到絕對必要。無論如何謝謝everything.Will讓我知道,只要我嘗試它!該代碼非常有意義。 – hecatonchries