【发布时间】:2016-09-07 16:05:25
【问题描述】:
在为我的渲染器实现摄像头时遇到了一些问题。正如问题所述,我想知道生成这样一个相机的必要步骤。包括视野和纵横比。重要的是坐标系是左撇子,这样 -z 将相机推离屏幕(如我明白了)。我尝试在网上查找,但大多数实现都不完整或失败了。感谢您的帮助。谢谢。
【问题讨论】:
标签: rendering raytracing
在为我的渲染器实现摄像头时遇到了一些问题。正如问题所述,我想知道生成这样一个相机的必要步骤。包括视野和纵横比。重要的是坐标系是左撇子,这样 -z 将相机推离屏幕(如我明白了)。我尝试在网上查找,但大多数实现都不完整或失败了。感谢您的帮助。谢谢。
【问题讨论】:
标签: rendering raytracing
我遇到了麻烦,花了很长时间才弄清楚。这是相机类的代码。
#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
如果您需要 Mat4 类中的 rotateVect() 代码
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);
}
【讨论】: