From 0a8df749f20dca6d35973b0a1cb3e8bc8812342f Mon Sep 17 00:00:00 2001 From: ubuntu20 Date: Mon, 26 Oct 2020 09:59:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=90=E5=83=8F=E7=B4=A0=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scene.cpp | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/Scene.cpp b/Scene.cpp index e8b2a87..6e854f8 100644 --- a/Scene.cpp +++ b/Scene.cpp @@ -2,19 +2,45 @@ #include "util.h" #include "Camera.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) { - 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.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); } +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() { + 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; + } + } }