gpuray_glsl

view src/gpuscene.cc @ 0:f234630e38ff

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Nov 2014 13:03:36 +0200
parents
children 2ed3da7dc0bc
line source
1 #include <algorithm>
2 #include <assert.h>
3 #include "gpuscene.h"
4 #include "sphere.h"
5 #include "plane.h"
6 #include "box.h"
7 #include "opengl.h"
9 GPUScene::GPUScene()
10 {
11 glGenTextures(NUM_TEXTURES, textures);
13 for(int i=0; i<NUM_TEXTURES; i++) {
14 glBindTexture(GL_TEXTURE_2D, textures[i]);
15 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
16 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
17 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
18 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
19 }
21 xform_buf = 0;
22 }
24 GPUScene::~GPUScene()
25 {
26 glDeleteTextures(NUM_TEXTURES, textures);
27 delete [] xform_buf;
28 }
30 bool GPUScene::create_textures()
31 {
32 // must be first to generate the megatecture rectangles
33 create_megatexture();
35 Sphere *sph;
36 Plane *plane;
37 Box *box;
39 std::vector<Sphere*> spheres;
40 std::vector<Plane*> planes;
41 std::vector<Box*> boxes;
43 // collect all objects into the different type-specific arrays
44 for(auto obj : objects) {
45 if((sph = dynamic_cast<Sphere*>(obj))) {
46 spheres.push_back(sph);
48 } else if((plane = dynamic_cast<Plane*>(obj))) {
49 planes.push_back(plane);
51 } else if((box = dynamic_cast<Box*>(obj))) {
52 boxes.push_back(box);
54 } else {
55 fprintf(stderr, "skipping object of unknown type: %s\n", obj->get_name());
56 }
57 }
59 create_sphere_texture(spheres);
60 create_plane_texture(planes);
61 create_box_texture(boxes);
63 create_env_texture();
64 create_xform_texture();
66 return true;
67 }
69 unsigned int GPUScene::get_texture(int which) const
70 {
71 return textures[which];
72 }
74 #define MAT_START 4
75 static void copy_material(Vector4 *ptr, const Material *mtl)
76 {
77 ptr[MAT_START].x = mtl->diffuse.x;
78 ptr[MAT_START].y = mtl->diffuse.y;
79 ptr[MAT_START].z = mtl->diffuse.z;
80 ptr[MAT_START].w = mtl->transparency;
82 ptr[MAT_START + 1].x = mtl->specular.x;
83 ptr[MAT_START + 1].y = mtl->specular.y;
84 ptr[MAT_START + 1].z = mtl->specular.z;
85 ptr[MAT_START + 1].w = mtl->shininess;
87 ptr[MAT_START + 2].x = mtl->reflectivity;
88 ptr[MAT_START + 2].y = mtl->ior;
90 ptr[MAT_START + 3] = mtl->mega_rect;
91 }
93 int GPUScene::object_index(const Object *obj) const
94 {
95 for(int i=0; i<(int)objects.size(); i++) {
96 if(objects[i] == obj) {
97 return i;
98 }
99 }
100 abort(); // can't happen
101 return -1;
102 }
104 #define OBJ_LINE_WIDTH 16
105 void GPUScene::create_sphere_texture(const std::vector<Sphere*> &spheres)
106 {
107 int xsz = OBJ_LINE_WIDTH;
108 int ysz = (int)spheres.size() + 1;
109 int tex_ysz = next_pow2(ysz);
111 Vector4 *pixels = new Vector4[xsz * tex_ysz];
113 pixels[0].x = (float)ysz;
114 pixels[0].y = (float)tex_ysz;
115 pixels[0].z = 0.5;
116 pixels[0].w = 1.0;
118 Vector4 *pixptr = pixels + xsz;
120 for(size_t i=0; i<spheres.size(); i++) {
121 pixptr[0].x = object_index(spheres[i]);
123 pixptr[1].x = spheres[i]->pos.x;
124 pixptr[1].y = spheres[i]->pos.y;
125 pixptr[1].z = spheres[i]->pos.z;
126 pixptr[1].w = spheres[i]->radius;
128 copy_material(pixptr, &spheres[i]->material);
129 pixptr += OBJ_LINE_WIDTH;
130 }
132 glBindTexture(GL_TEXTURE_2D, textures[TEX_SPHERE]);
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, pixels);
135 delete [] pixels;
136 }
138 void GPUScene::create_plane_texture(const std::vector<Plane*> &planes)
139 {
140 int xsz = OBJ_LINE_WIDTH;
141 int ysz = (int)planes.size() + 1;
142 int tex_ysz = next_pow2(ysz);
144 Vector4 *pixels = new Vector4[xsz * tex_ysz];
146 pixels[0].x = (float)ysz;
147 pixels[0].y = (float)tex_ysz;
149 Vector4 *pixptr = pixels + xsz;
151 for(size_t i=0; i<planes.size(); i++) {
152 pixptr[0].x = object_index(planes[i]);
154 pixptr[1].x = planes[i]->normal.x;
155 pixptr[1].y = planes[i]->normal.y;
156 pixptr[1].z = planes[i]->normal.z;
157 pixptr[1].w = planes[i]->dist;
159 copy_material(pixptr, &planes[i]->material);
160 pixptr += OBJ_LINE_WIDTH;
161 }
163 glBindTexture(GL_TEXTURE_2D, textures[TEX_PLANE]);
164 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, pixels);
166 delete [] pixels;
167 }
169 void GPUScene::create_box_texture(const std::vector<Box*> &boxes)
170 {
171 int xsz = OBJ_LINE_WIDTH;
172 int ysz = (int)boxes.size() + 1;
173 int tex_ysz = next_pow2(ysz);
175 Vector4 *pixels = new Vector4[xsz * tex_ysz];
177 pixels[0].x = (float)ysz;
178 pixels[0].y = (float)tex_ysz;
180 Vector4 *pixptr = pixels + xsz;
182 for(size_t i=0; i<boxes.size(); i++) {
183 pixptr[0].x = object_index(boxes[i]);
184 pixptr[1] = boxes[i]->min;
185 pixptr[2] = boxes[i]->max;
187 copy_material(pixptr, &boxes[i]->material);
188 pixptr += OBJ_LINE_WIDTH;
189 }
191 glBindTexture(GL_TEXTURE_2D, textures[TEX_BOX]);
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, pixels);
194 delete [] pixels;
195 }
197 void GPUScene::create_megatexture()
198 {
199 // at least a 1x1 dummy white texture
200 int xsz = 1;
201 int ysz = 1;
202 int num_textures = 0;
204 for(auto obj : objects) {
205 // only need 2D textures at this point
206 Texture *tex = dynamic_cast<Texture2D*>(obj->material.tex);
207 if(tex) {
208 const Image *img = tex->get_image();
210 xsz = std::max(xsz, img->xsz);
211 ysz += img->ysz;
212 num_textures++;
213 }
214 }
216 int tex_xsz = next_pow2(xsz);
217 int tex_ysz = next_pow2(ysz);
219 Color *pixels = new Color[tex_xsz * tex_ysz];
221 // null texture
222 pixels[0] = Color(1, 1, 1);
224 Color *pixptr = pixels + tex_xsz;
226 float offs_y = 0.0;
227 for(auto obj : objects) {
228 Texture *tex = dynamic_cast<Texture2D*>(obj->material.tex);
229 if(tex) {
230 const Image *img = tex->get_image();
232 Vector4 rect{0.0, offs_y, (float)img->xsz / (float)tex_xsz,
233 (float)img->ysz / (float)tex_ysz};
235 offs_y += rect.w;
237 obj->material.mega_rect = rect;
239 for(int i=0; i<img->ysz; i++) {
240 memcpy(pixptr, img->pixels + i * img->xsz, img->xsz * sizeof *pixels);
241 pixptr += tex_xsz;
242 }
243 } else {
244 obj->material.mega_rect = Vector4(0, 0, 0, 0);
245 }
246 }
248 glBindTexture(GL_TEXTURE_2D, textures[TEX_TEXTURE]);
249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, pixels);
251 delete [] pixels;
252 }
254 void GPUScene::create_env_texture()
255 {
256 // create the scene cubemap, or a null cubemap if we don't have one
257 glDeleteTextures(1, textures + TEX_ENV); // cause it's not a 2D texture :)
259 glGenTextures(1, textures + TEX_ENV);
260 glBindTexture(GL_TEXTURE_CUBE_MAP, textures[TEX_ENV]);
261 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
262 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
263 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
264 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
265 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
267 if(envmap) {
268 // we have an environment cubemap, just pass the data to OpenGL
269 for(int i=0; i<6; i++) {
270 const Image *img = envmap->get_image(i);
272 int face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
273 glTexImage2D(face, 0, GL_RGB16F, img->xsz, img->ysz, 0, GL_RGB, GL_FLOAT, img->pixels);
274 }
275 } else {
276 // we don't have an env cubemap, make a dummy 1x1 cubemap with the background color
277 for(int i=0; i<6; i++) {
278 int face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
279 glTexImage2D(face, 0, GL_RGB16F, 1, 1, 0, GL_RGB, GL_FLOAT, &bgcolor);
280 }
281 }
282 }
284 #define XFORM_LINE_WIDTH OBJ_LINE_WIDTH
285 void GPUScene::create_xform_texture()
286 {
287 int tex_xsz = XFORM_LINE_WIDTH;
288 int tex_ysz = next_pow2((int)objects.size() + 1); // leave space for the descriptor
290 glBindTexture(GL_TEXTURE_2D, textures[TEX_XFORM]);
291 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
293 update_xform_texture();
294 }
296 void GPUScene::update_xform_texture()
297 {
298 int tex_xsz = XFORM_LINE_WIDTH;
299 int tex_ysz = next_pow2((int)objects.size() + 1); // leave space for the descriptor
301 if(!xform_buf) {
302 xform_buf = new Vector4[tex_xsz * tex_ysz];
303 xform_buf[0].x = tex_ysz; // descriptor
304 }
306 Vector4 *pixptr = xform_buf + tex_xsz;
307 for(auto obj : objects) {
308 for(int i=0; i<4; i++) {
309 pixptr[i] = obj->xform.get_column_vector(i);
310 pixptr[i + 4] = obj->inv_xform.get_column_vector(i);
311 }
312 pixptr += tex_xsz;
313 }
315 glBindTexture(GL_TEXTURE_2D, textures[TEX_XFORM]);
316 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_xsz, tex_ysz, GL_RGBA, GL_FLOAT, xform_buf);
317 }