rayzor

diff src/object.cc @ 14:a9a948809c6f

starting the renderer screen, plus misc stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 08:06:21 +0300
parents d94a69933a71
children 79609d482762
line diff
     1.1 --- a/src/object.cc	Sat Apr 12 23:37:55 2014 +0300
     1.2 +++ b/src/object.cc	Sun Apr 13 08:06:21 2014 +0300
     1.3 @@ -1,3 +1,4 @@
     1.4 +#include <math.h>
     1.5  #include "object.h"
     1.6  #include "vmath.h"
     1.7  #include "min3d.h"
     1.8 @@ -12,18 +13,6 @@
     1.9  {
    1.10  }
    1.11  
    1.12 -void Object::pre_draw() const
    1.13 -{
    1.14 -	m3d_matrix_mode(M3D_MODELVIEW);
    1.15 -	m3d_push_matrix();
    1.16 -	m3d_mult_matrix(get_matrix()[0]);
    1.17 -}
    1.18 -
    1.19 -void Object::post_draw() const
    1.20 -{
    1.21 -	m3d_pop_matrix();
    1.22 -}
    1.23 -
    1.24  // ---- sphere ----
    1.25  Sphere::Sphere()
    1.26  {
    1.27 @@ -36,7 +25,7 @@
    1.28  #define USUB	12
    1.29  #define VSUB	6
    1.30  
    1.31 -void Sphere::draw() const
    1.32 +void Sphere::draw(bool emph) const
    1.33  {
    1.34  	static Vector3 *varr;
    1.35  	static unsigned int *iarr;
    1.36 @@ -85,6 +74,7 @@
    1.37  	}
    1.38  
    1.39  	pre_draw();
    1.40 +	SceneNode::draw(emph);
    1.41  
    1.42  	m3d_vertex_array(&varr->x);
    1.43  	m3d_draw_indexed(M3D_QUADS, iarr, num_indices);
    1.44 @@ -93,9 +83,36 @@
    1.45  	post_draw();
    1.46  }
    1.47  
    1.48 -bool Sphere::intersect(const Ray &ray, float *dist) const
    1.49 +bool Sphere::intersect(const Ray &wray, float *dist) const
    1.50  {
    1.51 -	return false;	// TODO
    1.52 +	Ray ray = transform(get_inv_matrix(), wray);
    1.53 +
    1.54 +	// assumes center is 0,0,0, and radius is 1
    1.55 +	float a = dot(ray.dir, ray.dir);
    1.56 +	float b = 2.0 * ray.dir.x * ray.origin.x +
    1.57 +		2.0 * ray.dir.y * ray.origin.y +
    1.58 +		2.0 * ray.dir.z * ray.origin.z;
    1.59 +	float c = dot(ray.origin, ray.origin) - 1.0;
    1.60 +
    1.61 +	float discr = b * b - 4.0 * a * c;
    1.62 +	if(discr < 1e-4)
    1.63 +		return false;
    1.64 +
    1.65 +	float sqrt_discr = sqrt(discr);
    1.66 +	float t0 = (-b + sqrt_discr) / (2.0 * a);
    1.67 +	float t1 = (-b - sqrt_discr) / (2.0 * a);
    1.68 +
    1.69 +	if(t0 < 1e-4)
    1.70 +		t0 = t1;
    1.71 +	if(t1 < 1e-4)
    1.72 +		t1 = t0;
    1.73 +
    1.74 +	float t = t0 < t1 ? t0 : t1;
    1.75 +	if(t < 1e-4)
    1.76 +		return false;
    1.77 +
    1.78 +	if(dist) *dist = t;
    1.79 +	return true;
    1.80  }
    1.81  
    1.82  // ---- box ----
    1.83 @@ -117,7 +134,7 @@
    1.84     4------------5
    1.85  
    1.86   */
    1.87 -void Box::draw() const
    1.88 +void Box::draw(bool emph) const
    1.89  {
    1.90  	static const float verts[] = {
    1.91  		-1, 1, 1,
    1.92 @@ -139,6 +156,7 @@
    1.93  	};
    1.94  
    1.95  	pre_draw();
    1.96 +	SceneNode::draw(emph);
    1.97  
    1.98  	m3d_vertex_array(verts);
    1.99  	m3d_draw_indexed(M3D_QUADS, indices, sizeof indices / sizeof *indices);