You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #include "Scene.h"
  2. #include "util.h"
  3. #include "Camera.h"
  4. #include "Ray.h"
  5. #pragma comment(lib, "winmm.lib")
  6. static int sTotalPixelCount = 0;
  7. static int sViewportWidth = 0, sViewportHeight = 0;
  8. static Camera* sCamera = nullptr;
  9. void Init(int width, int height)
  10. {
  11. sTotalPixelCount = width * height;
  12. sViewportWidth = width;
  13. sViewportHeight = height;
  14. sCamera = new Camera(45.0f, float(width) / float(height));
  15. sCamera->LookAt(Vector3(0.0f,0.0f,1.0f), Vector3(0.0f,0.0f,0.0f), Vector3(0.0f,1.0f,0.0f));
  16. }
  17. float GetEscaptedTime() {
  18. static unsigned long sTimeSinceComputerStart = 0;
  19. static unsigned long sLastFrameTime = 0;
  20. sTimeSinceComputerStart = timeGetTime();
  21. unsigned long frame_time = sLastFrameTime == 0 ? 0 : sTimeSinceComputerStart - sLastFrameTime;
  22. sLastFrameTime = sTimeSinceComputerStart;
  23. return float(frame_time) / 1000.0f;
  24. }
  25. void RenderOnePixel(int pixel_index) {
  26. int x = pixel_index % sViewportWidth;
  27. int y = pixel_index / sViewportWidth;
  28. float u = float(x) / sViewportWidth;
  29. float v = float(y) / sViewportHeight;
  30. Ray ray = sCamera->GetRay(u,v);
  31. float rf = ray.mDirection.x > 0.0f ? ray.mDirection.x : -ray.mDirection.x;
  32. float gf = ray.mDirection.y > 0.0f ? ray.mDirection.y : -ray.mDirection.y;
  33. float bf = ray.mDirection.z > 0.0f ? ray.mDirection.z : -ray.mDirection.z;
  34. AByte r = AByte( rf * 255.0f);
  35. AByte g = AByte( gf * 255.0f);
  36. AByte b = AByte( bf * 255.0f);
  37. SetColor(x, y, r, g, b, 255);
  38. }
  39. void Render()
  40. {
  41. static int sCurrentRenderPixel = 0;
  42. float current_render_time = 0.0f;
  43. while (sCurrentRenderPixel < sTotalPixelCount) {
  44. RenderOnePixel(sCurrentRenderPixel);
  45. sCurrentRenderPixel++;
  46. current_render_time = GetEscaptedTime();
  47. if (current_render_time > 0.016f) {
  48. break;
  49. }
  50. }
  51. }