clray

view 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 source
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "ocl.h"
7 struct RendInfo {
8 int xsz, ysz;
9 int num_sph, num_lights;
10 int max_iter;
11 } __attribute__((packed));
13 struct Sphere {
14 float pos[4];
15 float kd[4], ks[4];
16 float radius;
17 float spow;
18 float kr, kt;
19 } __attribute__((packed));
21 struct Ray {
22 float origin[4], dir[4];
23 } __attribute__((packed));
25 struct Light {
26 float pos[4], color[4];
27 } __attribute__((packed));
29 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
31 static Ray *prim_rays;
32 static CLProgram *prog;
33 static int global_size;
35 static Sphere sphlist[] = {
36 {{0, 0, 0, 1}, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 1.0, 60, 0, 0},
37 {{-0.2, 0.4, -3, 1}, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 0.25, 40, 0, 0}
38 };
40 static Light lightlist[] = {
41 {{-10, 10, -20, 1}, {1, 1, 1, 1}}
42 };
44 static float xform[16] = {
45 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
46 };
48 static RendInfo rinf;
51 bool init_renderer(int xsz, int ysz, float *fb)
52 {
53 // render info
54 rinf.xsz = xsz;
55 rinf.ysz = ysz;
56 rinf.num_sph = sizeof sphlist / sizeof *sphlist;
57 rinf.num_lights = sizeof lightlist / sizeof *lightlist;
58 rinf.max_iter = 6;
60 /* calculate primary rays */
61 prim_rays = new Ray[xsz * ysz];
63 for(int i=0; i<ysz; i++) {
64 for(int j=0; j<xsz; j++) {
65 prim_rays[i * xsz + j] = get_primary_ray(j, i, xsz, ysz, 45.0);
66 }
67 }
69 /* setup opencl */
70 prog = new CLProgram("render");
71 if(!prog->load("rt.cl")) {
72 return false;
73 }
75 /* setup argument buffers */
76 prog->set_arg_buffer(0, ARG_WR, xsz * ysz * 4 * sizeof(float), fb);
77 prog->set_arg_buffer(1, ARG_RD, sizeof rinf, &rinf);
78 prog->set_arg_buffer(2, ARG_RD, sizeof sphlist, sphlist);
79 prog->set_arg_buffer(3, ARG_RD, sizeof lightlist, lightlist);
80 prog->set_arg_buffer(4, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
81 prog->set_arg_buffer(5, ARG_RD, sizeof xform, &xform);
83 global_size = xsz * ysz;
84 return true;
85 }
87 void destroy_renderer()
88 {
89 delete [] prim_rays;
90 delete prog;
91 }
93 bool render()
94 {
95 if(!prog->run(1, global_size)) {
96 return false;
97 }
99 CLMemBuffer *mbuf = prog->get_arg_buffer(0);
100 map_mem_buffer(mbuf, MAP_RD);
101 /*if(!write_ppm("out.ppm", fb, xsz, ysz)) {
102 return 1;
103 }*/
104 unmap_mem_buffer(mbuf);
105 return true;
106 }
108 void set_xform(float *matrix)
109 {
110 CLMemBuffer *mbuf = prog->get_arg_buffer(5);
111 assert(mbuf);
113 assert(map_mem_buffer(mbuf, MAP_WR) == xform);
114 memcpy(xform, matrix, sizeof xform);
115 unmap_mem_buffer(mbuf);
116 }
118 static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
119 {
120 float vfov = M_PI * vfov_deg / 180.0;
121 float aspect = (float)w / (float)h;
123 float ysz = 2.0;
124 float xsz = aspect * ysz;
126 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
127 float py = 1.0 - ((float)y / (float)h) * ysz;
128 float pz = 1.0 / tan(0.5 * vfov);
130 px *= 100.0;
131 py *= 100.0;
132 pz *= 100.0;
134 Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}};
135 return ray;
136 }