rayzor

changeset 15:be616b58df99

continued the renderer slightly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 09:54:36 +0300
parents a9a948809c6f
children 4d87b0aafbb0
files GNUmakefile Makefile src/camera.cc src/camera.h src/inttypes.h src/modeller.cc src/renderer.cc src/renderer.h src/scene.cc src/scene.h src/screen.cc src/screen.h src/vmath.h
diffstat 13 files changed, 192 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/GNUmakefile	Sun Apr 13 08:06:21 2014 +0300
     1.2 +++ b/GNUmakefile	Sun Apr 13 09:54:36 2014 +0300
     1.3 @@ -1,4 +1,4 @@
     1.4 -baseobj = src/main.o src/logger.o src/screen.o src/scrman.o
     1.5 +baseobj = src/main.o src/logger.o src/screen.o src/scrman.o src/rbtree.o
     1.6  modelobj = src/modeller.o src/min3d.o src/m3drast.o src/lines.o
     1.7  rendobj = src/renderer.o src/vmath.o
     1.8  scnobj = src/scene.o src/object.o src/light.o src/camera.o src/snode.o
     2.1 --- a/Makefile	Sun Apr 13 08:06:21 2014 +0300
     2.2 +++ b/Makefile	Sun Apr 13 09:54:36 2014 +0300
     2.3 @@ -1,4 +1,4 @@
     2.4 -baseobj = main.obj logger.obj screen.obj scrman.obj swapbuf.obj
     2.5 +baseobj = main.obj logger.obj screen.obj scrman.obj swapbuf.obj rbtree.obj
     2.6  modelobj = modeller.obj min3d.obj m3drast.obj lines.obj
     2.7  rendobj = renderer.obj vmath.obj
     2.8  scnobj = scene.obj object.obj light.obj camera.obj snode.obj
     3.1 --- a/src/camera.cc	Sun Apr 13 08:06:21 2014 +0300
     3.2 +++ b/src/camera.cc	Sun Apr 13 09:54:36 2014 +0300
     3.3 @@ -1,9 +1,10 @@
     3.4  #include "camera.h"
     3.5 +#include "rayzor.h"
     3.6  
     3.7  Camera::Camera()
     3.8  {
     3.9  	type = NODE_CAMERA;
    3.10 -	fov = M_PI;
    3.11 +	fov = M_PI / 4.0;
    3.12  	set_position(Vector3(0, 0, 10));
    3.13  }
    3.14  
    3.15 @@ -29,14 +30,11 @@
    3.16  
    3.17  void Camera::calc_matrix() const
    3.18  {
    3.19 -	xform.set_identity();
    3.20 -	xform.lookat(pos, target.get_position(), Vector3(0, 1, 0));
    3.21 -	xform_valid = true;
    3.22 -}
    3.23 -
    3.24 -void Camera::calc_inv_matrix() const
    3.25 -{
    3.26 -	// TODO
    3.27 +	if(!xform_valid) {
    3.28 +		xform.set_identity();
    3.29 +		xform.lookat(pos, target.get_position(), Vector3(0, 1, 0));
    3.30 +		xform_valid = true;
    3.31 +	}
    3.32  }
    3.33  
    3.34  void Camera::draw() const
    3.35 @@ -47,3 +45,20 @@
    3.36  {
    3.37  	return false;
    3.38  }
    3.39 +
    3.40 +Ray Camera::get_primary_ray(int x, int y) const
    3.41 +{
    3.42 +	calc_inv_matrix();
    3.43 +
    3.44 +	float aspect = (float)fb_width / (float)fb_height;
    3.45 +	float pwidth = 2.0 * aspect / (float)fb_width;
    3.46 +	float pheight = 2.0 / (float)fb_height;
    3.47 +
    3.48 +	Vector3 dir;
    3.49 +	dir.x = (float)x * pwidth - aspect;
    3.50 +	dir.y = 1.0 - (float)y * pheight;
    3.51 +	dir.z = -1.0 / tan(fov / 2.0);
    3.52 +	dir.normalize();
    3.53 +
    3.54 +	return transform(inv_xform, Ray(Vector3(0, 0, 0), dir));
    3.55 +}
     4.1 --- a/src/camera.h	Sun Apr 13 08:06:21 2014 +0300
     4.2 +++ b/src/camera.h	Sun Apr 13 09:54:36 2014 +0300
     4.3 @@ -10,7 +10,6 @@
     4.4  	float fov;
     4.5  
     4.6  	void calc_matrix() const;
     4.7 -	void calc_inv_matrix() const;
     4.8  
     4.9  public:
    4.10  	Camera();
    4.11 @@ -24,6 +23,8 @@
    4.12  	void draw() const;
    4.13  
    4.14  	bool intersect(const Ray &ray, float *dist = 0) const;
    4.15 +
    4.16 +	Ray get_primary_ray(int x, int y) const;
    4.17  };
    4.18  
    4.19  #endif	// CAMERA_H_
     5.1 --- a/src/inttypes.h	Sun Apr 13 08:06:21 2014 +0300
     5.2 +++ b/src/inttypes.h	Sun Apr 13 09:54:36 2014 +0300
     5.3 @@ -9,6 +9,8 @@
     5.4  typedef unsigned char uint8_t;
     5.5  typedef unsigned short uint16_t;
     5.6  typedef unsigned long uint32_t;
     5.7 +
     5.8 +typedef unsigned long intptr_t;
     5.9  #else
    5.10  #include <stdint.h>
    5.11  #endif
     6.1 --- a/src/modeller.cc	Sun Apr 13 08:06:21 2014 +0300
     6.2 +++ b/src/modeller.cc	Sun Apr 13 09:54:36 2014 +0300
     6.3 @@ -12,6 +12,7 @@
     6.4  struct ModellerImpl {
     6.5  	int mx, my;
     6.6  	float cam_theta, cam_phi, cam_dist;
     6.7 +	Camera *viewport_cam;
     6.8  
     6.9  	struct m3d_image rbuf;
    6.10  
    6.11 @@ -36,6 +37,8 @@
    6.12  	mod = new ModellerImpl;
    6.13  	memset(mod, 0, sizeof *mod);
    6.14  
    6.15 +	mod->viewport_cam = new Camera;
    6.16 +
    6.17  	mod->cam_phi = 25;
    6.18  	mod->cam_dist = 5;
    6.19  
    6.20 @@ -138,11 +141,28 @@
    6.21  			break;
    6.22  
    6.23  		case 'r':
    6.24 -		case 'R':
    6.25 +		case 'p':
    6.26  			if(kb_isdown(KB_ALT) || kb_isdown(KB_CTRL)) {
    6.27 +		case KB_F5:
    6.28 +		case KB_F6:
    6.29  				Screen *rs = get_screen("renderer");
    6.30  				if(rs) {
    6.31  					activate_screen(rs);
    6.32 +
    6.33 +					if(key == 'r' || key == KB_F5) {
    6.34 +						// start a rendering, and make sure there is a camera
    6.35 +						if(!scene->get_active_camera()) {
    6.36 +							scene->set_active_camera(mod->viewport_cam);
    6.37 +						}
    6.38 +						Vector3 dir;
    6.39 +						dir.x = sin(DEG2RAD(mod->cam_theta)) * cos(DEG2RAD(mod->cam_phi)) * mod->cam_dist;
    6.40 +						dir.y = sin(DEG2RAD(mod->cam_phi));
    6.41 +						dir.z = cos(DEG2RAD(mod->cam_theta)) * cos(DEG2RAD(mod->cam_phi)) * mod->cam_dist;
    6.42 +						mod->viewport_cam->set_position(dir);
    6.43 +						mod->viewport_cam->set_target(Vector3(0, 0, 0));
    6.44 +
    6.45 +						rs->message(message_atom("start"));
    6.46 +					}
    6.47  				} else {
    6.48  					printlog("failed to find renderer screen!\n");
    6.49  				}
     7.1 --- a/src/renderer.cc	Sun Apr 13 08:06:21 2014 +0300
     7.2 +++ b/src/renderer.cc	Sun Apr 13 09:54:36 2014 +0300
     7.3 @@ -1,15 +1,26 @@
     7.4  #include <string.h>
     7.5  #include "renderer.h"
     7.6  #include "rayzor.h"
     7.7 +#include "scene.h"
     7.8  #include "keyb.h"
     7.9  #include "scrman.h"
    7.10 +#include "timer.h"
    7.11 +#include "logger.h"
    7.12  
    7.13  struct RendererImpl {
    7.14  	bool in_progress;
    7.15  	int cur_x, cur_y;	// current pixel being rendered
    7.16 -	int cur_pix;		// current pixel (linear)
    7.17 +
    7.18 +	uint32_t *pixels;
    7.19 +
    7.20 +	MsgAtom msg_start;
    7.21  };
    7.22  
    7.23 +
    7.24 +static Vector3 ray_trace(const Ray &ray, int iter = 0);
    7.25 +static Vector3 shade(const Ray &ray, float t, int iter);
    7.26 +
    7.27 +
    7.28  Renderer::Renderer()
    7.29  {
    7.30  	set_name("renderer");
    7.31 @@ -19,12 +30,22 @@
    7.32  {
    7.33  	rend = new RendererImpl;
    7.34  	memset(rend, 0, sizeof *rend);
    7.35 +
    7.36 +	rend->pixels = new uint32_t[fb_width * fb_height];
    7.37 +	if(!rend->pixels) {
    7.38 +		return false;
    7.39 +	}
    7.40 +	memset(rend->pixels, 0, fb_width * fb_height * 4);
    7.41 +
    7.42 +	rend->msg_start = message_atom("start");
    7.43 +
    7.44  	return true;
    7.45  }
    7.46  
    7.47  void Renderer::shutdown()
    7.48  {
    7.49  	if(rend) {
    7.50 +		delete [] rend->pixels;
    7.51  		delete rend;
    7.52  		rend = 0;
    7.53  	}
    7.54 @@ -32,7 +53,43 @@
    7.55  
    7.56  void Renderer::draw() const
    7.57  {
    7.58 -	memset(fb_pixels, 0, fb_width * fb_height * 4);
    7.59 +	Camera *cam = scene->get_active_camera();
    7.60 +
    7.61 +	// if a rendering is not in progress, just show the last buffer
    7.62 +	if(!rend->in_progress || !cam) {
    7.63 +		memcpy(fb_pixels, rend->pixels, fb_width * fb_height * 4);
    7.64 +		return;
    7.65 +	}
    7.66 +
    7.67 +	// render for approximately half a second, then show and return
    7.68 +	unsigned long start = get_msec();
    7.69 +	uint32_t *dest = rend->pixels + rend->cur_y * fb_width + rend->cur_x;
    7.70 +
    7.71 +	for(;;) {
    7.72 +		Ray ray = cam->get_primary_ray(rend->cur_x, rend->cur_y);
    7.73 +		Vector3 color = ray_trace(ray);
    7.74 +
    7.75 +		int r = color.x > 1.0 ? 255 : (int)(color.x * 255.0);
    7.76 +		int g = color.y > 1.0 ? 255 : (int)(color.y * 255.0);
    7.77 +		int b = color.z > 1.0 ? 255 : (int)(color.z * 255.0);
    7.78 +
    7.79 +		*dest++ = (r << 16) | (g << 8) | b;
    7.80 +
    7.81 +		if(++rend->cur_x >= fb_width) {
    7.82 +			rend->cur_x = 0;
    7.83 +			if(++rend->cur_y >= fb_height) {
    7.84 +				rend->in_progress = false;
    7.85 +				printlog("done!\n");
    7.86 +				break;
    7.87 +			}
    7.88 +		}
    7.89 +
    7.90 +		if(get_msec() - start >= 500) {
    7.91 +			break;
    7.92 +		}
    7.93 +	}
    7.94 +
    7.95 +	memcpy(fb_pixels, rend->pixels, fb_width * fb_height * 4);
    7.96  }
    7.97  
    7.98  void Renderer::handle_keyboard(int key, bool press)
    7.99 @@ -42,5 +99,29 @@
   7.100  		deactivate_screen();
   7.101  		break;
   7.102  
   7.103 +	default:
   7.104 +		break;
   7.105  	}
   7.106  }
   7.107 +
   7.108 +void Renderer::message(MsgAtom msg)
   7.109 +{
   7.110 +	if(msg == rend->msg_start) {
   7.111 +		rend->cur_x = rend->cur_y = 0;
   7.112 +		rend->in_progress = true;
   7.113 +		memset(rend->pixels, 0, fb_width * fb_height * 4);
   7.114 +
   7.115 +		printlog("starting a new rendering!\n");
   7.116 +	}
   7.117 +}
   7.118 +
   7.119 +
   7.120 +static Vector3 ray_trace(const Ray &ray, int iter)
   7.121 +{
   7.122 +	return shade(ray, 0, iter);
   7.123 +}
   7.124 +
   7.125 +static Vector3 shade(const Ray &ray, float t, int iter)
   7.126 +{
   7.127 +	return Vector3(1.0, 0.0, 0.0);
   7.128 +}
     8.1 --- a/src/renderer.h	Sun Apr 13 08:06:21 2014 +0300
     8.2 +++ b/src/renderer.h	Sun Apr 13 09:54:36 2014 +0300
     8.3 @@ -18,6 +18,8 @@
     8.4  	void draw() const;
     8.5  
     8.6  	void handle_keyboard(int key, bool press);
     8.7 +
     8.8 +	void message(MsgAtom msg);
     8.9  };
    8.10  
    8.11  #endif	// RENDERER_H_
     9.1 --- a/src/scene.cc	Sun Apr 13 08:06:21 2014 +0300
     9.2 +++ b/src/scene.cc	Sun Apr 13 09:54:36 2014 +0300
     9.3 @@ -125,6 +125,16 @@
     9.4  	return cameras[idx];
     9.5  }
     9.6  
     9.7 +void Scene::set_active_camera(Camera *cam)
     9.8 +{
     9.9 +	active_cam = cam;
    9.10 +}
    9.11 +
    9.12 +Camera *Scene::get_active_camera() const
    9.13 +{
    9.14 +	return active_cam;
    9.15 +}
    9.16 +
    9.17  void Scene::draw() const
    9.18  {
    9.19  	if(active_cam) {
    10.1 --- a/src/scene.h	Sun Apr 13 08:06:21 2014 +0300
    10.2 +++ b/src/scene.h	Sun Apr 13 09:54:36 2014 +0300
    10.3 @@ -45,6 +45,9 @@
    10.4  	Camera *get_camera(int idx);
    10.5  	const Camera *get_camera(int idx) const;
    10.6  
    10.7 +	void set_active_camera(Camera *cam);
    10.8 +	Camera *get_active_camera() const;
    10.9 +
   10.10  	void draw() const;
   10.11  
   10.12  	void select(int s);
    11.1 --- a/src/screen.cc	Sun Apr 13 08:06:21 2014 +0300
    11.2 +++ b/src/screen.cc	Sun Apr 13 09:54:36 2014 +0300
    11.3 @@ -1,5 +1,35 @@
    11.4 +#include <stdlib.h>
    11.5  #include <string.h>
    11.6  #include "screen.h"
    11.7 +#include "rbtree.h"
    11.8 +#include "logger.h"
    11.9 +
   11.10 +MsgAtom message_atom(const char *str)
   11.11 +{
   11.12 +	static struct rbtree *atoms;
   11.13 +	static MsgAtom last_atom;
   11.14 +	struct rbnode *node;
   11.15 +	MsgAtom atom;
   11.16 +
   11.17 +	if(!atoms) {
   11.18 +		if(!(atoms = rb_create(RB_KEY_STRING))) {
   11.19 +			printlog("fatal: message_atom failed to create symbol table\n");
   11.20 +			abort();
   11.21 +		}
   11.22 +	}
   11.23 +
   11.24 +	if((node = rb_find(atoms, (void*)str))) {
   11.25 +		return *(MsgAtom*)&node->data;
   11.26 +	}
   11.27 +
   11.28 +	atom = ++last_atom;
   11.29 +	if(rb_insert(atoms, (void*)str, (void*)atom) == -1) {
   11.30 +		printlog("message_atom failed to insert new atom\n");
   11.31 +		--last_atom;
   11.32 +		return -1;
   11.33 +	}
   11.34 +	return atom;
   11.35 +}
   11.36  
   11.37  Screen::Screen()
   11.38  {
   11.39 @@ -47,3 +77,7 @@
   11.40  void Screen::handle_mmotion(int x, int y)
   11.41  {
   11.42  }
   11.43 +
   11.44 +void Screen::message(MsgAtom ma)
   11.45 +{
   11.46 +}
    12.1 --- a/src/screen.h	Sun Apr 13 08:06:21 2014 +0300
    12.2 +++ b/src/screen.h	Sun Apr 13 09:54:36 2014 +0300
    12.3 @@ -1,6 +1,10 @@
    12.4  #ifndef SCREEN_H_
    12.5  #define SCREEN_H_
    12.6  
    12.7 +typedef int MsgAtom;
    12.8 +
    12.9 +MsgAtom message_atom(const char *str);
   12.10 +
   12.11  class Screen {
   12.12  private:
   12.13  	char *name;
   12.14 @@ -21,6 +25,8 @@
   12.15  	virtual void handle_keyboard(int key, bool press);
   12.16  	virtual void handle_mbutton(int bn, bool press, int x, int y);
   12.17  	virtual void handle_mmotion(int x, int y);
   12.18 +
   12.19 +	virtual void message(MsgAtom ma);
   12.20  };
   12.21  
   12.22  #endif	// SCREEN_H_
    13.1 --- a/src/vmath.h	Sun Apr 13 08:06:21 2014 +0300
    13.2 +++ b/src/vmath.h	Sun Apr 13 09:54:36 2014 +0300
    13.3 @@ -4,6 +4,9 @@
    13.4  #include <math.h>
    13.5  #include "vmathmat.h"
    13.6  
    13.7 +#define DEG2RAD(x)	(M_PI * (x) / 180.0)
    13.8 +#define RAD2DEG(x)	(180.0 * (x) / M_PI)
    13.9 +
   13.10  class Vector3 {
   13.11  public:
   13.12  	float x, y, z;