|
@ -2,17 +2,20 @@ |
|
|
#include "util.h"
|
|
|
#include "util.h"
|
|
|
#include "Camera.h"
|
|
|
#include "Camera.h"
|
|
|
#include "Ray.h"
|
|
|
#include "Ray.h"
|
|
|
|
|
|
#include "Vector3.h"
|
|
|
|
|
|
#include "Sphere.h"
|
|
|
#pragma comment(lib, "winmm.lib")
|
|
|
#pragma comment(lib, "winmm.lib")
|
|
|
static int sTotalPixelCount = 0; |
|
|
static int sTotalPixelCount = 0; |
|
|
static int sViewportWidth = 0, sViewportHeight = 0; |
|
|
static int sViewportWidth = 0, sViewportHeight = 0; |
|
|
static Camera* sCamera = nullptr; |
|
|
static Camera* sCamera = nullptr; |
|
|
|
|
|
Sphere sphere(Vector3(0.0f, 0.0f, -5.0f), 0.5f); |
|
|
void Init(int width, int height) |
|
|
void Init(int width, int height) |
|
|
{ |
|
|
{ |
|
|
sTotalPixelCount = width * height; |
|
|
sTotalPixelCount = width * height; |
|
|
sViewportWidth = width; |
|
|
sViewportWidth = width; |
|
|
sViewportHeight = height; |
|
|
sViewportHeight = height; |
|
|
sCamera = new Camera(45.0f, float(width) / float(height)); |
|
|
sCamera = new Camera(45.0f, float(width) / float(height)); |
|
|
sCamera->LookAt(Vector3(0.0f,0.0f,1.0f), Vector3(0.0f,0.0f,0.0f), Vector3(0.0f,1.0f,0.0f)); |
|
|
|
|
|
|
|
|
sCamera->LookAt(Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, -1.0f), Vector3(0.0f, 1.0f, 0.0f)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
float GetEscaptedTime() { |
|
|
float GetEscaptedTime() { |
|
@ -31,13 +34,25 @@ void RenderOnePixel(int pixel_index) { |
|
|
float u = float(x) / sViewportWidth; |
|
|
float u = float(x) / sViewportWidth; |
|
|
float v = float(y) / sViewportHeight; |
|
|
float v = float(y) / sViewportHeight; |
|
|
Ray ray = sCamera->GetRay(u,v); |
|
|
Ray ray = sCamera->GetRay(u,v); |
|
|
float rf = ray.mDirection.x > 0.0f ? ray.mDirection.x : -ray.mDirection.x; |
|
|
|
|
|
float gf = ray.mDirection.y > 0.0f ? ray.mDirection.y : -ray.mDirection.y; |
|
|
|
|
|
float bf = ray.mDirection.z > 0.0f ? ray.mDirection.z : -ray.mDirection.z; |
|
|
|
|
|
AByte r = AByte( rf * 255.0f); |
|
|
|
|
|
AByte g = AByte( gf * 255.0f); |
|
|
|
|
|
AByte b = AByte( bf * 255.0f); |
|
|
|
|
|
SetColor(x, y, r, g, b, 255); |
|
|
|
|
|
|
|
|
HitPoint hit_point; |
|
|
|
|
|
if (sphere.HitTest(ray, 0.0f, 100.0f, hit_point)) { |
|
|
|
|
|
float rf = hit_point.mNormal.x * 0.5f + 0.5f; |
|
|
|
|
|
float gf = hit_point.mNormal.y * 0.5f + 0.5f; |
|
|
|
|
|
float bf = hit_point.mNormal.z * 0.5f + 0.5f; |
|
|
|
|
|
AByte r = AByte(rf * 255.0f); |
|
|
|
|
|
AByte g = AByte(gf * 255.0f); |
|
|
|
|
|
AByte b = AByte(bf * 255.0f); |
|
|
|
|
|
SetColor(x, y, r, g, b, 255); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
float rf = ray.mDirection.x > 0.0f ? ray.mDirection.x : -ray.mDirection.x; |
|
|
|
|
|
float gf = ray.mDirection.y > 0.0f ? ray.mDirection.y : -ray.mDirection.y; |
|
|
|
|
|
float bf = ray.mDirection.z > 0.0f ? ray.mDirection.z : -ray.mDirection.z; |
|
|
|
|
|
AByte r = AByte(rf * 255.0f); |
|
|
|
|
|
AByte g = AByte(gf * 255.0f); |
|
|
|
|
|
AByte b = AByte(bf * 255.0f); |
|
|
|
|
|
SetColor(x, y, r, g, b, 255); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Render() |
|
|
void Render() |
|
|