Browse Source

逐像素渲染

master
ubuntu20 4 years ago
parent
commit
0a8df749f2
  1. 38
      Scene.cpp

38
Scene.cpp

@ -2,19 +2,45 @@
#include "util.h" #include "util.h"
#include "Camera.h" #include "Camera.h"
#include "Ray.h" #include "Ray.h"
#pragma comment(lib, "winmm.lib")
static int sTotalPixelCount = 0;
static int sViewportWidth = 0, sViewportHeight = 0;
void Init(int width, int height) void Init(int width, int height)
{ {
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
SetColor(y, x, 255, 0, 0, 255);
}
}
sTotalPixelCount = width * height;
sViewportWidth = width;
sViewportHeight = height;
Camera camera(45.0f, float(width) / float(height)); Camera camera(45.0f, float(width) / float(height));
camera.LookAt(Vector3(0.0f,0.0f,1.0f), Vector3(0.0f,0.0f,0.0f), Vector3(0.0f,1.0f,0.0f)); camera.LookAt(Vector3(0.0f,0.0f,1.0f), Vector3(0.0f,0.0f,0.0f), Vector3(0.0f,1.0f,0.0f));
Ray ray = camera.GetRay(0.5f, 0.5f); Ray ray = camera.GetRay(0.5f, 0.5f);
} }
float GetEscaptedTime() {
static unsigned long sTimeSinceComputerStart = 0;
static unsigned long sLastFrameTime = 0;
sTimeSinceComputerStart = timeGetTime();
unsigned long frame_time = sLastFrameTime == 0 ? 0 : sTimeSinceComputerStart - sLastFrameTime;
sLastFrameTime = sTimeSinceComputerStart;
return float(frame_time) / 1000.0f;
}
void RenderOnePixel(int pixel_index) {
int x = pixel_index % sViewportWidth;
int y = pixel_index / sViewportWidth;
SetColor(x, y, 255, 255, 0, 255);
}
void Render() void Render()
{ {
static int sCurrentRenderPixel = 0;
float current_render_time = 0.0f;
while (sCurrentRenderPixel < sTotalPixelCount) {
RenderOnePixel(sCurrentRenderPixel);
sCurrentRenderPixel++;
current_render_time = GetEscaptedTime();
if (true || current_render_time > 0.033f) {
break;
}
}
} }
Loading…
Cancel
Save