2 * @file objectrenderer.cpp
3 * @brief Implements ObjectRenderer and derived classes
6 #include "objectrenderer.h"
15 * ObjectRenderer constructor
16 * Note we cannot compile the shaders in the ShaderProgram constructor
17 * because the Screen class needs to initialise GL first and it has a
18 * ShaderProgram member
20 ObjectRenderer::ObjectRenderer(const ObjectType & type,
21 const char * vert_glsl_file, const char * frag_glsl_file, const char * geom_glsl_file)
22 : m_type(type), m_shader_program(), m_indexes(), m_buffer_builder(NULL)
24 m_shader_program.InitialiseShaders(vert_glsl_file, frag_glsl_file, geom_glsl_file);
25 m_shader_program.Use();
26 glUniform4f(m_shader_program.GetUniformLocation("colour"), 0,0,0,1); //TODO: Allow different colours
32 void ObjectRenderer::RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id)
34 // If we don't have anything to render, return.
35 if (first_obj_id == last_obj_id) return;
36 // If there are no objects of this type, return.
37 if (m_indexes.empty()) return;
38 unsigned first_index = 0;
39 while (m_indexes.size() > first_index && m_indexes[first_index] < first_obj_id) first_index ++;
40 unsigned last_index = first_index;
41 while (m_indexes.size() > last_index && m_indexes[last_index] < last_obj_id) last_index ++;
43 m_shader_program.Use();
45 glDrawElements(GL_LINES, (last_index-first_index)*2, GL_UNSIGNED_INT, (GLvoid*)(2*first_index*sizeof(uint32_t)));
50 * Default implementation for rendering using CPU
52 void ObjectRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id)
54 Error("Cannot render objects of type %d on CPU", m_type);
55 //TODO: Render a rect or something instead?
59 * Prepare index buffers for both CPU and GPU rendering to receive indexes (but don't add any yet!)
61 void ObjectRenderer::PrepareBuffers(unsigned max_objects)
63 if (m_buffer_builder != NULL) // We already have a BufferBuilder
65 Fatal("Has been called before, without FinaliseBuffers being called since!");
67 // Empty and reserve the indexes vector (for CPU rendering)
69 m_indexes.reserve(max_objects); //TODO: Can probably make this smaller? Or leave it out? Do we care?
71 // Initialise and resize the ibo (for GPU rendering)
73 m_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
74 m_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
75 m_ibo.SetName("m_ibo: ObjectRenderer GPU indices");
76 m_ibo.Resize(max_objects * 2 * sizeof(uint32_t));
77 // BufferBuilder is used to construct the ibo
78 m_buffer_builder = new BufferBuilder<uint32_t>(m_ibo.Map(false, true, true), m_ibo.GetSize()); // new matches delete in ObjectRenderer::FinaliseBuffers
83 * Add object index to the buffers for CPU and GPU rendering
85 void ObjectRenderer::AddObjectToBuffers(unsigned index)
87 if (m_buffer_builder == NULL) // No BufferBuilder!
89 Fatal("Called without calling PrepareBuffers");
91 m_buffer_builder->Add(2*index); // ibo for GPU rendering
92 m_buffer_builder->Add(2*index+1);
93 m_indexes.push_back(index); // std::vector of indices for CPU rendering
97 * Finalise the index buffers for CPU and GPU rendering
99 void ObjectRenderer::FinaliseBuffers()
101 if (m_buffer_builder == NULL) // No BufferBuilder!
103 Fatal("Called without calling PrepareBuffers");
105 // For GPU rendering, UnMap the ibo
107 // ... and delete the BufferBuilder used to create it
108 delete m_buffer_builder; // delete matches new in ObjectRenderer::PrepareBuffers
109 m_buffer_builder = NULL;
111 // Nothing is necessary for CPU rendering
118 void RectFilledRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id)
120 for (unsigned i = 0; i < m_indexes.size(); ++i)
122 if (m_indexes[i] < first_obj_id) continue;
123 if (m_indexes[i] >= last_obj_id) continue;
124 PixelBounds bounds(CPURenderBounds(objects.bounds[m_indexes[i]], view, target));
125 for (int64_t x = max((int64_t)0, bounds.x); x <= min(bounds.x+bounds.w, target.w-1); ++x)
127 for (int64_t y = max((int64_t)0, bounds.y); y <= min(bounds.y+bounds.h, target.h-1); ++y)
129 int index = (x+target.w*y)*4;
130 target.pixels[index+0] = 0;
131 target.pixels[index+1] = 0;
132 target.pixels[index+2] = 0;
133 target.pixels[index+3] = 255;
142 void RectOutlineRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id)
144 //Debug("Render %u outlined rectangles on CPU", m_indexes.size());
145 for (unsigned i = 0; i < m_indexes.size(); ++i)
147 if (m_indexes[i] < first_obj_id) continue;
148 if (m_indexes[i] >= last_obj_id) continue;
149 PixelBounds bounds(CPURenderBounds(objects.bounds[m_indexes[i]], view, target));
151 // Using bresenham's lines now mainly because I want to see if they work
153 ObjectRenderer::RenderLineOnCPU(bounds.x, bounds.y, bounds.x+bounds.w, bounds.y, target);
155 ObjectRenderer::RenderLineOnCPU(bounds.x, bounds.y+bounds.h, bounds.x+bounds.w, bounds.y+bounds.h, target);
157 ObjectRenderer::RenderLineOnCPU(bounds.x, bounds.y, bounds.x, bounds.y+bounds.h, target);
159 ObjectRenderer::RenderLineOnCPU(bounds.x+bounds.w, bounds.y, bounds.x+bounds.w, bounds.y+bounds.h, target);
161 // Diagonal for testing (from bottom left to top right)
162 //ObjectRenderer::RenderLineOnCPU(bounds.x,bounds.y+bounds.h, bounds.x+bounds.w, bounds.y,target, C_BLUE);
163 //ObjectRenderer::RenderLineOnCPU(bounds.x+bounds.w, bounds.y+bounds.h, bounds.x, bounds.y, target,C_GREEN);
170 void CircleFilledRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id)
172 for (unsigned i = 0; i < m_indexes.size(); ++i)
174 if (m_indexes[i] < first_obj_id) continue;
175 if (m_indexes[i] >= last_obj_id) continue;
176 PixelBounds bounds(CPURenderBounds(objects.bounds[m_indexes[i]], view, target));
177 int64_t centre_x = bounds.x + bounds.w / 2;
178 int64_t centre_y = bounds.y + bounds.h / 2;
180 //Debug("Centre is %d, %d", centre_x, centre_y);
181 //Debug("Bounds are %d,%d,%d,%d", bounds.x, bounds.y, bounds.w, bounds.h);
182 //Debug("Windos is %d,%d", target.w, target.h);
183 for (int64_t x = max((int64_t)0, bounds.x); x <= min(bounds.x+bounds.w, target.w-1); ++x)
185 for (int64_t y = max((int64_t)0, bounds.y); y <= min(bounds.y + bounds.h, target.h-1); ++y)
187 Real dx(2); dx *= Real(x - centre_x)/Real(bounds.w);
188 Real dy(2); dy *= Real(y - centre_y)/Real(bounds.h);
189 int64_t index = (x+target.w*y)*4;
191 if (dx*dx + dy*dy <= Real(1))
193 target.pixels[index+0] = 0;
194 target.pixels[index+1] = 0;
195 target.pixels[index+2] = 0;
196 target.pixels[index+3] = 255;
203 Rect ObjectRenderer::CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target)
205 Rect result = view.TransformToViewCoords(bounds);
206 result.x *= Real(target.w);
207 result.y *= Real(target.h);
208 result.w *= Real(target.w);
209 result.h *= Real(target.h);
216 * Not sure how to apply De'Casteljau, will just use a bunch of Bresnham lines for now.
218 void BezierRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id)
220 //Warn("Rendering Beziers on CPU. Things may explode.");
221 for (unsigned i = 0; i < m_indexes.size(); ++i)
223 if (m_indexes[i] < first_obj_id) continue;
224 if (m_indexes[i] >= last_obj_id) continue;
225 Rect bounds(CPURenderBounds(objects.bounds[m_indexes[i]], view, target));
226 PixelBounds pix_bounds(bounds);
229 Bezier control(objects.beziers[objects.data_indices[m_indexes[i]]], bounds);
230 //Debug("%s -> %s via %s", objects.beziers[objects.data_indices[m_indexes[i]]].Str().c_str(), control.Str().c_str(), bounds.Str().c_str());
231 // Draw a rectangle around the bezier for debugging the coord transforms
232 //ObjectRenderer::RenderLineOnCPU(pix_bounds.x, pix_bounds.y, pix_bounds.x+pix_bounds.w, pix_bounds.y, target);
233 //ObjectRenderer::RenderLineOnCPU(pix_bounds.x, pix_bounds.y+pix_bounds.h, pix_bounds.x+pix_bounds.w, pix_bounds.y+pix_bounds.h, target);
234 //ObjectRenderer::RenderLineOnCPU(pix_bounds.x, pix_bounds.y, pix_bounds.x, pix_bounds.y+pix_bounds.h, target);
235 //ObjectRenderer::RenderLineOnCPU(pix_bounds.x+pix_bounds.w, pix_bounds.y, pix_bounds.x+pix_bounds.w, pix_bounds.y+pix_bounds.h, target);
237 // Draw lines between the control points for debugging
238 //ObjectRenderer::RenderLineOnCPU((int64_t)control.x0, (int64_t)control.y0, (int64_t)control.x1, (int64_t)control.y1,target);
239 //ObjectRenderer::RenderLineOnCPU((int64_t)control.x1, (int64_t)control.y1, (int64_t)control.x2, (int64_t)control.y2,target);
243 Real x[2]; Real y[2];
244 control.Evaluate(x[0], y[0], Real(0));
245 //Debug("target is (%lu, %lu)", target.w, target.h);
247 //blen = min(max((int64_t)2, (int64_t)(target.w/view.GetBounds().w)), (int64_t)100);
249 Real invblen(1); invblen /= blen;
250 //Debug("Using %li lines, inverse %f", blen, Double(invblen));
251 for (int64_t j = 1; j <= blen; ++j)
253 control.Evaluate(x[j % 2],y[j % 2], invblen*j);
254 ObjectRenderer::RenderLineOnCPU((int64_t)Double(x[0]),(int64_t)Double(y[0]), (int64_t)Double(x[1]),(int64_t)Double(y[1]), target);
262 Real x; Real y; control.Evaluate(x,y,u);
263 int64_t index = ((int64_t)x + (int64_t)y*target.w)*4;
264 if (index >= 0 && index < 4*(target.w*target.h))
266 target.pixels[index+0] = 0;
267 target.pixels[index+1] = 0;
268 target.pixels[index+2] = 0;
269 target.pixels[index+3] = 255;
277 void BezierRenderer::PrepareBezierGPUBuffer(const Objects& objects)
279 m_bezier_coeffs.SetType(GraphicsBuffer::BufferTypeTexture);
280 m_bezier_coeffs.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
281 m_bezier_coeffs.SetName("m_bezier_coeffs: Bezier coefficients");
282 m_bezier_coeffs.Resize(objects.beziers.size()*sizeof(GPUBezierCoeffs));
283 BufferBuilder<GPUBezierCoeffs> builder(m_bezier_coeffs.Map(false, true, true), m_bezier_coeffs.GetSize());
285 for (auto bez = objects.beziers.begin(); bez != objects.beziers.end(); ++bez)
287 GPUBezierCoeffs coeffs = {
288 Float(bez->x0), Float(bez->y0),
289 Float(bez->x1), Float(bez->y1),
290 Float(bez->x2), Float(bez->y2),
291 Float(bez->x3), Float(bez->y3)
295 m_bezier_coeffs.UnMap();
296 glGenTextures(1, &m_bezier_buffer_texture);
297 glBindTexture(GL_TEXTURE_BUFFER, m_bezier_buffer_texture);
298 glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, m_bezier_coeffs.GetHandle());
300 m_bezier_ids.SetType(GraphicsBuffer::BufferTypeTexture);
301 m_bezier_ids.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
302 m_bezier_ids.SetName("m_bezier_ids: object data_indices");
303 m_bezier_ids.Upload(objects.data_indices.size() * sizeof(uint32_t), &objects.data_indices[0]);
305 glGenTextures(1, &m_bezier_id_buffer_texture);
306 glActiveTexture(GL_TEXTURE1);
307 glBindTexture(GL_TEXTURE_BUFFER, m_bezier_id_buffer_texture);
308 glTexBuffer(GL_TEXTURE_BUFFER, GL_R32I, m_bezier_ids.GetHandle());
309 glActiveTexture(GL_TEXTURE0);
312 void BezierRenderer::RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id)
314 if (!m_shader_program.Valid())
315 Warn("Shader is invalid (objects are of type %d)", m_type);
317 // If we don't have anything to render, return.
318 if (first_obj_id == last_obj_id) return;
319 // If there are no objects of this type, return.
320 if (m_indexes.empty()) return;
322 unsigned first_index = 0;
323 while (m_indexes.size() > first_index && m_indexes[first_index] < first_obj_id) first_index ++;
324 unsigned last_index = first_index;
325 while (m_indexes.size() > last_index && m_indexes[last_index] < last_obj_id) last_index ++;
327 m_shader_program.Use();
328 glUniform1i(m_shader_program.GetUniformLocation("bezier_buffer_texture"), 0);
329 glUniform1i(m_shader_program.GetUniformLocation("bezier_id_buffer_texture"), 1);
331 glDrawElements(GL_LINES, (last_index-first_index)*2, GL_UNSIGNED_INT, (GLvoid*)(2*first_index*sizeof(uint32_t)));
335 * For debug, save pixels to bitmap
337 void ObjectRenderer::SaveBMP(const CPURenderTarget & target, const char * filename)
339 SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(target.pixels, target.w, target.h, 8*4, target.w*4,
340 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
341 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
343 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
344 #endif //SDL_BYTEORDER
347 Fatal("SDL_CreateRGBSurfaceFrom(pixels...) failed - %s", SDL_GetError());
348 if (SDL_SaveBMP(surf, filename) != 0)
349 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
352 SDL_FreeSurface(surf);
358 void ObjectRenderer::RenderLineOnCPU(int64_t x0, int64_t y0, int64_t x1, int64_t y1, const CPURenderTarget & target, const Colour & colour, bool transpose)
360 int64_t dx = x1 - x0;
361 int64_t dy = y1 - y0;
362 bool neg_m = (dy*dx < 0);
366 // If positive slope > 1, just swap x and y
369 RenderLineOnCPU(y0,x0,y1,x1,target,colour,!transpose);
373 int64_t two_dy = 2*dy;
374 int64_t p = two_dy - dx;
375 int64_t two_dxdy = 2*(dy-dx);
376 int64_t x; int64_t y; int64_t x_end;
377 int64_t width = (transpose ? target.h : target.w);
378 int64_t height = (transpose ? target.w : target.h);
381 rgba[0] = 255*colour.r;
382 rgba[1] = 255*colour.g;
383 rgba[2] = 255*colour.b;
384 rgba[3] = 255*colour.a;
401 if (x_end < 0) return;
402 y = (neg_m ? y - (dy*-x)/dx : y + (dy*-x)/dx);
408 if (x > width) return;
412 // TODO: Avoid extra inner conditionals
415 if (x >= 0 && x < width && y >= 0 && y < height)
417 int64_t index = (transpose ? (y + x*target.w)*4 : (x + y*target.w)*4);
418 for (int i = 0; i < 4; ++i)
419 target.pixels[index+i] = rgba[i];
426 if (neg_m) --y; else ++y;
429 } while (++x <= x_end);