您的位置:首页 > 其它

Camera Ray Generation

2016-09-17 01:39 417 查看
#include <fstream>
#include "xmlload.cpp"
#include "scene.cpp"
using namespace std;

int main(){

// load scene: root node, camera, image
LoadScene("scenes/prj0.xml");

// variables for generating camera rays
int w = renderImage.GetWidth();
int h = renderImage.GetHeight();
int size = w * h;
Color24 white = {237, 237, 237};
Color24 black = {27, 27, 27};
Color24* img = renderImage.GetPixels();
float fov = camera.fov;
float aspectRatio = (float) w / (float) h;
float imageDistance = 1;
float imageTipY = imageDistance * tan(fov / 2.0 * M_PI / 180.0);
float imageTipX = imageTipY * aspectRatio;
float dX = (2.0 * imageTipX) / w;
float dY = (2.0 * imageTipY) / h;
Point3 *imageTopLeftV = new Point3(-imageTipX, imageTipY, -imageDistance);
Point3 *dXV = new Point3(dX, 0.0, 0.0);
Point3 *dYV = new Point3(0.0, -dY, 0.0);
Point3 firstPixel = *imageTopLeftV + (*dXV * 0.5) + (*dYV * 0.5);

// generate camera rays for every pixel
for(int i = 0; i < size; i++){

// generate current ray in camera space
int pX = i % w;
int pY = i / w;
Point3 curr = firstPixel + (*dXV * pX) + (*dYV * pY);
curr.Normalize();

// transform ray into world space

// traverse through scene DOM
// transform rays into model space
// detect ray intersections ---> update pixel

}

// output ray-traced image & z-buffer
//renderImage.SaveImage("images/image.ppm");
//renderImage.SaveZImage("images/z-image.ppm");
}


主要的思路就是:

把一张800 * 400的图片,映射到 一个w * h 大小的viewport上。

那么 图片上的一个像素的w间隔在viewport上就是w / 800, 图片上的 h 间隔在viewport上就是 h / 400 。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ray tracing