fixed pixel direction and flipped coordinate system

This commit is contained in:
arvid schröder 2022-11-10 18:14:11 +01:00
parent ef79acef73
commit f5a6e9e119
2 changed files with 11 additions and 6 deletions

View file

@ -7,9 +7,9 @@ Ray PerspectiveCamera::createRay(float x, float y) const {
// Set up a left-handed coordinate system, // Set up a left-handed coordinate system,
// in which the camera looks along the positive z-Axis // in which the camera looks along the positive z-Axis
Vector3d normalVector = normalized(crossProduct(forwardDirection, upDirection)); Vector3d normalVector = normalized(crossProduct(upDirection, forwardDirection));
Vector3d normalVector_fov = normalVector * tan(this->fovAngle / 360 * PI); Vector3d normalVector_fov = normalVector * std::abs(tan(this->fovAngle / 360.0f * PI));
Vector3d upVector_fov = this->upDirection * tan(this->fovAngle / 360 * PI); Vector3d upVector_fov = this->upDirection * std::abs(tan(this->fovAngle / 360.0f * PI));
Vector3d directionVector = normalized(forwardDirection + x * normalVector_fov + y * upVector_fov); Vector3d directionVector = normalized(forwardDirection + x * normalVector_fov + y * upVector_fov);
return Ray {this->position, directionVector}; return Ray {this->position, directionVector};

View file

@ -14,11 +14,16 @@ Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, in
// Create the image by casting one ray into the scene for each pixel // Create the image by casting one ray into the scene for each pixel
float pixel_step_x = 2 * max_x / width; float pixel_step_x = 2 * max_x / width;
float pixel_step_y = 2 * max_y / width; float pixel_step_y = 2 * max_y / height;
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
Ray ray = camera.createRay((x - width/2) * pixel_step_x, (y - width/2) * pixel_step_y); Ray ray = camera.createRay((x - width/2) * pixel_step_x, (y - height/2) * pixel_step_y);
image.setPixelAt(x, y, scene.traceRay(ray)); image.setPixelAt(x, height - 1 - y, scene.traceRay(ray));
}
}
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {
image.setPixelAt(x, y, Color(0.0f, 1.0f, 0.0f));
} }
} }
return image; return image;