2016-09-04 2 views
0

Probleme mit der Implementierung einer Kamera für meinen Renderer. Wie die Frage besagt, würde ich gerne die notwendigen Schritte kennen, um eine solche Kamera zu erstellen. Mit Sichtfeld und Seitenverhältnis inklusive. Es ist wichtig, dass das Koordinatensystem linkshändig ist, so dass -z die Kamera vom Bildschirm wegdrückt (z Ich verstehe es). Ich habe versucht, online zu schauen, aber die meisten Implementierungen sind unvollständig oder haben mich gescheitert. Jede Hilfe wird geschätzt. Danke.Path Tracing - Kamerastrahlen mit einem Linkshänder-Koordinatensystem generieren

Antwort

0

Ich hatte Probleme damit und brauchte eine lange Zeit, um herauszufinden. Hier ist der Code für die Kameraklasse.

#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 

Incase, wenn Sie die rotateVect() Code in der Klasse 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]; 
} 

Hier ist unser setRotationRadians Code

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

Dank für die Beantwortung benötigen! Bereits implementiert, aber es ist nicht das beste und hat bestimmte Probleme.So, eifrig, etwas anderes zu versuchen.Anyways einige Fragen.Was ist die Cam-Matrix initialisiert? Ist es die Identitätsmatrix oder einige andere 4x4-Werte. Zweitens kann ich bitte den eingestellten Rotationsradians-Code sehen? Ich möchte etwas vergleichen.Und schließlich nur eine Kuriosität mehr als alles andere - Warum eine Matrix anstelle der üblichen Cross-Produkt-Methoden verwenden? That's all.Again ...... Vielen Dank :) – hecatonchries

+0

CamMatrix wird auf Identity Matrix initialisiert. Sie können dasselbe ohne Matrix erreichen, aber ich bin kürzlich ein Fan von Matrizen geworden, also habe ich es benutzt. – codetiger

+0

Oh ok.Ich denke, wir sind alle unterschiedlich.Cuz ich hasse Matrizen wirklich.Lol.Weiß sie nicht benutzen, bis absolut notwendig.Anyways danke für alles.Will lass es dich wissen, sobald ich es versuche !! Der Code macht Sinn. – hecatonchries