rayzor
diff src/renderer.cc @ 17:79609d482762
the renderer renders, also fixed an unnoticed matrix conversion problem between scenegraph and min3d
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 14 Apr 2014 07:34:45 +0300 |
parents | be616b58df99 |
children | 859ccadca671 |
line diff
1.1 --- a/src/renderer.cc Sun Apr 13 09:54:51 2014 +0300 1.2 +++ b/src/renderer.cc Mon Apr 14 07:34:45 2014 +0300 1.3 @@ -1,4 +1,5 @@ 1.4 #include <string.h> 1.5 +#include <stdarg.h> 1.6 #include "renderer.h" 1.7 #include "rayzor.h" 1.8 #include "scene.h" 1.9 @@ -6,6 +7,15 @@ 1.10 #include "scrman.h" 1.11 #include "timer.h" 1.12 #include "logger.h" 1.13 +#include "raytrace.h" 1.14 + 1.15 +enum { 1.16 + MSG_START, 1.17 + MSG_GAMMA, 1.18 + MSG_USE_GAMMA, 1.19 + 1.20 + NUM_MESSAGES 1.21 +}; 1.22 1.23 struct RendererImpl { 1.24 bool in_progress; 1.25 @@ -13,14 +23,13 @@ 1.26 1.27 uint32_t *pixels; 1.28 1.29 - MsgAtom msg_start; 1.30 + float gamma, inv_gamma; 1.31 + bool use_gamma; 1.32 + 1.33 + MsgAtom msg[NUM_MESSAGES]; 1.34 }; 1.35 1.36 1.37 -static Vector3 ray_trace(const Ray &ray, int iter = 0); 1.38 -static Vector3 shade(const Ray &ray, float t, int iter); 1.39 - 1.40 - 1.41 Renderer::Renderer() 1.42 { 1.43 set_name("renderer"); 1.44 @@ -37,7 +46,13 @@ 1.45 } 1.46 memset(rend->pixels, 0, fb_width * fb_height * 4); 1.47 1.48 - rend->msg_start = message_atom("start"); 1.49 + rend->gamma = 1.8; 1.50 + rend->inv_gamma = 1.0 / rend->gamma; 1.51 + rend->use_gamma = true; 1.52 + 1.53 + rend->msg[MSG_START] = message_atom("start"); 1.54 + rend->msg[MSG_GAMMA] = message_atom("gamma"); 1.55 + rend->msg[MSG_USE_GAMMA] = message_atom("use-gamma"); 1.56 1.57 return true; 1.58 } 1.59 @@ -69,6 +84,13 @@ 1.60 Ray ray = cam->get_primary_ray(rend->cur_x, rend->cur_y); 1.61 Vector3 color = ray_trace(ray); 1.62 1.63 + // gamma-correct before storing the pixel 1.64 + if(rend->use_gamma) { 1.65 + color.x = pow(color.x, rend->inv_gamma); 1.66 + color.y = pow(color.y, rend->inv_gamma); 1.67 + color.z = pow(color.z, rend->inv_gamma); 1.68 + } 1.69 + 1.70 int r = color.x > 1.0 ? 255 : (int)(color.x * 255.0); 1.71 int g = color.y > 1.0 ? 255 : (int)(color.y * 255.0); 1.72 int b = color.z > 1.0 ? 255 : (int)(color.z * 255.0); 1.73 @@ -104,24 +126,33 @@ 1.74 } 1.75 } 1.76 1.77 -void Renderer::message(MsgAtom msg) 1.78 +void Renderer::message(MsgAtom msg, ...) 1.79 { 1.80 - if(msg == rend->msg_start) { 1.81 - rend->cur_x = rend->cur_y = 0; 1.82 - rend->in_progress = true; 1.83 - memset(rend->pixels, 0, fb_width * fb_height * 4); 1.84 + va_list ap; 1.85 + va_start(ap, msg); 1.86 1.87 - printlog("starting a new rendering!\n"); 1.88 + for(int i=0; i<NUM_MESSAGES; i++) { 1.89 + if(msg == rend->msg[i]) { 1.90 + switch(i) { 1.91 + case MSG_START: 1.92 + rend->cur_x = rend->cur_y = 0; 1.93 + rend->in_progress = true; 1.94 + memset(rend->pixels, 0, fb_width * fb_height * 4); 1.95 + 1.96 + printlog("starting a new rendering!\n"); 1.97 + break; 1.98 + 1.99 + case MSG_GAMMA: 1.100 + rend->gamma = va_arg(ap, float); 1.101 + rend->inv_gamma = 1.0 / rend->gamma; 1.102 + break; 1.103 + 1.104 + case MSG_USE_GAMMA: 1.105 + rend->use_gamma = va_arg(ap, bool); 1.106 + break; 1.107 + } 1.108 + } 1.109 } 1.110 + 1.111 + va_end(ap); 1.112 } 1.113 - 1.114 - 1.115 -static Vector3 ray_trace(const Ray &ray, int iter) 1.116 -{ 1.117 - return shade(ray, 0, iter); 1.118 -} 1.119 - 1.120 -static Vector3 shade(const Ray &ray, float t, int iter) 1.121 -{ 1.122 - return Vector3(1.0, 0.0, 0.0); 1.123 -}