clray

diff src/rt.cc @ 8:deaf85acf6af

interactive spheres
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 Jul 2010 19:48:43 +0100
parents 575383f3a239
children a09622aaa043
line diff
     1.1 --- a/src/rt.cc	Fri Jul 23 01:22:03 2010 +0100
     1.2 +++ b/src/rt.cc	Fri Jul 23 19:48:43 2010 +0100
     1.3 @@ -1,4 +1,5 @@
     1.4  #include <stdio.h>
     1.5 +#include <string.h>
     1.6  #include <math.h>
     1.7  #include <assert.h>
     1.8  #include "ocl.h"
     1.9 @@ -10,25 +11,21 @@
    1.10  } __attribute__((packed));
    1.11  
    1.12  struct Sphere {
    1.13 -	cl_float4 pos;
    1.14 -	cl_float4 kd, ks;
    1.15 -	cl_float radius;
    1.16 -	cl_float spow;
    1.17 -	cl_float kr, kt;
    1.18 +	float pos[4];
    1.19 +	float kd[4], ks[4];
    1.20 +	float radius;
    1.21 +	float spow;
    1.22 +	float kr, kt;
    1.23  } __attribute__((packed));
    1.24  
    1.25  struct Ray {
    1.26 -	cl_float4 origin, dir;
    1.27 +	float origin[4], dir[4];
    1.28  } __attribute__((packed));
    1.29  
    1.30  struct Light {
    1.31 -	cl_float4 pos, color;
    1.32 +	float pos[4], color[4];
    1.33  } __attribute__((packed));
    1.34  
    1.35 -struct Matrix4x4 {
    1.36 -	cl_float m[16];
    1.37 -};
    1.38 -
    1.39  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
    1.40  
    1.41  static Ray *prim_rays;
    1.42 @@ -36,16 +33,16 @@
    1.43  static int global_size;
    1.44  
    1.45  static Sphere sphlist[] = {
    1.46 -	{{0, 0, 8, 1}, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 1.0, 60, 0, 0},
    1.47 -	{{-0.2, 0.4, 5, 1}, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 0.25, 40, 0, 0}
    1.48 +	{{0, 0, 0, 1}, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 1.0, 60, 0, 0},
    1.49 +	{{-0.2, 0.4, -3, 1}, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 0.25, 40, 0, 0}
    1.50  };
    1.51  
    1.52  static Light lightlist[] = {
    1.53  	{{-10, 10, -20, 1}, {1, 1, 1, 1}}
    1.54  };
    1.55  
    1.56 -static Matrix4x4 xform = {
    1.57 -	{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
    1.58 +static float xform[16] = {
    1.59 +	1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
    1.60  };
    1.61  
    1.62  static RendInfo rinf;
    1.63 @@ -72,7 +69,7 @@
    1.64  	/* setup opencl */
    1.65  	prog = new CLProgram("render");
    1.66  	if(!prog->load("rt.cl")) {
    1.67 -		return 1;
    1.68 +		return false;
    1.69  	}
    1.70  
    1.71  	/* setup argument buffers */
    1.72 @@ -108,6 +105,16 @@
    1.73  	return true;
    1.74  }
    1.75  
    1.76 +void set_xform(float *matrix)
    1.77 +{
    1.78 +	CLMemBuffer *mbuf = prog->get_arg_buffer(5);
    1.79 +	assert(mbuf);
    1.80 +
    1.81 +	assert(map_mem_buffer(mbuf, MAP_WR) == xform);
    1.82 +	memcpy(xform, matrix, sizeof xform);
    1.83 +	unmap_mem_buffer(mbuf);
    1.84 +}
    1.85 +
    1.86  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
    1.87  {
    1.88  	float vfov = M_PI * vfov_deg / 180.0;