Performance graphs!
[ipdf/code.git] / src / objectrenderer.cpp
1 /**
2  * @file objectrenderer.cpp
3  * @brief Implements ObjectRenderer and derived classes
4  */
5
6 #include "objectrenderer.h"
7 #include "view.h"
8
9 using namespace std;
10
11 namespace IPDF
12 {
13
14 /**
15  * ObjectRenderer constructor
16  * Note the ShaderProgram constructor which compiles the shaders for GPU rendering (if they exist)
17  */
18 ObjectRenderer::ObjectRenderer(const ObjectType & type, 
19                 const char * vert_glsl_file, const char * frag_glsl_file, const char * geom_glsl_file)
20                 : m_type(type), m_shader_program(), m_indexes(), m_buffer_builder(NULL)
21 {
22         m_shader_program.InitialiseShaders(vert_glsl_file, frag_glsl_file, geom_glsl_file);
23         m_shader_program.Use();
24         glUniform4f(m_shader_program.GetUniformLocation("colour"), 0,0,0,1); //TODO: Allow different colours
25 }
26
27 /**
28  * Render using GPU
29  */
30 void ObjectRenderer::RenderUsingGPU()
31 {
32         if (!m_shader_program.Valid())
33                 Warn("Shader is invalid (objects are of type %d)", m_type);
34         m_shader_program.Use();
35         m_ibo.Bind();
36         glDrawElements(GL_LINES, m_indexes.size()*2, GL_UNSIGNED_INT, 0);
37 }
38
39 /**
40  * Helper structuretransforms coordinates to pixels
41  */
42
43 ObjectRenderer::CPURenderBounds::CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target)
44 {
45         Rect view_bounds = view.TransformToViewCoords(bounds);
46         x = view_bounds.x * Real(target.w);
47         y = view_bounds.y * Real(target.h);
48         w = view_bounds.w * Real(target.w);
49         h = view_bounds.h * Real(target.h);
50         //Debug("CPURenderBounds %s -> %s -> {%li,%li,%li,%li}", bounds.Str().c_str(), view_bounds.Str().c_str(), x, y, w, h);
51 }
52
53 /**
54  * Default implementation for rendering using CPU
55  */
56 void ObjectRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target)
57 {
58         Error("Cannot render objects of type %d on CPU", m_type);
59 }
60
61 /**
62  * Prepare index buffers for both CPU and GPU rendering to receive indexes (but don't add any yet!)
63  */
64 void ObjectRenderer::PrepareBuffers(unsigned max_objects)
65 {
66         if (m_buffer_builder != NULL) // We already have a BufferBuilder
67         {
68                 Fatal("Has been called before, without FinaliseBuffers being called since!");
69         }
70         // Empty and reserve the indexes vector (for CPU rendering)
71         m_indexes.clear();
72         m_indexes.reserve(max_objects); //TODO: Can probably make this smaller? Or leave it out? Do we care?
73
74         // Initialise and resize the ibo (for GPU rendering)
75         m_ibo.Invalidate();
76         m_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
77         m_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
78         m_ibo.Resize(max_objects * 2 * sizeof(uint32_t));
79         // BufferBuilder is used to construct the ibo
80         m_buffer_builder = new BufferBuilder<uint32_t>(m_ibo.Map(false, true, true), m_ibo.GetSize()); // new matches delete in ObjectRenderer::FinaliseBuffers
81
82 }
83
84 /**
85  * Add object index to the buffers for CPU and GPU rendering
86  */
87 void ObjectRenderer::AddObjectToBuffers(unsigned index)
88 {
89         if (m_buffer_builder == NULL) // No BufferBuilder!
90         {
91                 Fatal("Called without calling PrepareBuffers");
92         }
93         m_buffer_builder->Add(2*index); // ibo for GPU rendering
94         m_buffer_builder->Add(2*index+1);
95         m_indexes.push_back(index); // std::vector of indices for CPU rendering
96 }
97
98 /**
99  * Finalise the index buffers for CPU and GPU rendering
100  */
101 void ObjectRenderer::FinaliseBuffers()
102 {
103         if (m_buffer_builder == NULL) // No BufferBuilder!
104         {
105                 Fatal("Called without calling PrepareBuffers");
106         }
107         // For GPU rendering, UnMap the ibo
108         m_ibo.UnMap();
109         // ... and delete the BufferBuilder used to create it
110         delete m_buffer_builder; // delete matches new in ObjectRenderer::PrepareBuffers
111         m_buffer_builder = NULL;
112         
113         // Nothing is necessary for CPU rendering
114 }
115
116
117 /**
118  * Rectangle (filled)
119  */
120 void RectFilledRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target)
121 {
122         for (unsigned i = 0; i < m_indexes.size(); ++i)
123         {
124                 CPURenderBounds bounds(objects.bounds[m_indexes[i]], view, target);
125                 for (int64_t x = max(0L, bounds.x); x <= min(bounds.x+bounds.w, target.w-1); ++x)
126                 {
127                         for (int64_t y = max(0L, bounds.y); y <= min(bounds.y+bounds.h, target.h-1); ++y)
128                         {
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;
134                         }
135                 }
136         }
137 }
138
139 /**
140  * Rectangle (outine)
141  */
142 void RectOutlineRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target)
143 {
144         for (unsigned i = 0; i < m_indexes.size(); ++i)
145         {
146                 CPURenderBounds bounds(objects.bounds[m_indexes[i]], view, target);
147                 for (int64_t x = max(0L, bounds.x); x <= min(bounds.x+bounds.w, target.w-1); ++x)
148                 {
149                         int64_t top = (x+target.w*bounds.y)*4;
150                         int64_t bottom = (x+target.w*(bounds.y+bounds.h))*4;
151
152                         if (top >= 0L && top <4*target.w*target.h)
153                         {
154                                 for (int j = 0; j < 3; ++j)
155                                         target.pixels[top+j] = 0;
156                                 target.pixels[top+3] = 255;
157                         }
158                         if (bottom >= 0L && bottom <4*target.w*target.h)
159                         {
160                                 for (int j = 0; j < 3; ++j)
161                                         target.pixels[bottom+j] = 0;
162                                 target.pixels[bottom+3] = 255;
163                         }
164                 }
165
166                 for (int64_t y = max(0L, bounds.y); y <= min(bounds.y+bounds.h, target.h-1); ++y)
167                 {
168                         int64_t left = (bounds.x >= 0L && bounds.x < target.w) ? (bounds.x + target.w*y)*4 : -1L;
169                         int64_t right = (bounds.x+bounds.w >= 0L && bounds.x+bounds.w < target.w) ? (bounds.x+bounds.w + target.w*y)*4 : -1L;
170                         if (left >= 0L && left <4*target.w*target.h)
171                         {
172                                 for (int j = 0; j < 3; ++j)
173                                         target.pixels[left+j] = 0;
174                                 target.pixels[left+3] = 255;
175                         }
176                         if (right >= 0L && right <4*target.w*target.h)
177                         {
178                                 for (int j = 0; j < 3; ++j)
179                                         target.pixels[right+j] = 0;
180                                 target.pixels[right+3] = 255;
181                         }
182                 }
183         }
184 }
185
186 /**
187  * Circle (filled)
188  */
189 void CircleFilledRenderer::RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target)
190 {
191         for (unsigned i = 0; i < m_indexes.size(); ++i)
192         {
193                 CPURenderBounds bounds(objects.bounds[m_indexes[i]], view, target);
194                 int64_t centre_x = bounds.x + bounds.w / 2;
195                 int64_t centre_y = bounds.y + bounds.h / 2;
196                 
197                 //Debug("Centre is %d, %d", centre_x, centre_y);
198                 //Debug("Bounds are %d,%d,%d,%d", bounds.x, bounds.y, bounds.w, bounds.h);
199                 //Debug("Windos is %d,%d", target.w, target.h);
200                 for (int64_t x = max(0L, bounds.x); x <= min(bounds.x+bounds.w, target.w-1); ++x)
201                 {
202                         for (int64_t y = max(0L, bounds.y); y <= min(bounds.y + bounds.h, target.h-1); ++y)
203                         {
204                                 double dx = 2.0*(double)(x - centre_x)/(double)(bounds.w);
205                                 double dy = 2.0*(double)(y - centre_y)/(double)(bounds.h);
206                                 int64_t index = (x+target.w*y)*4;
207                                 
208                                 if (dx*dx + dy*dy <= 1.0)
209                                 {
210                                         target.pixels[index+0] = 0;
211                                         target.pixels[index+1] = 0;
212                                         target.pixels[index+2] = 0;
213                                         target.pixels[index+3] = 255;
214
215                                 }
216                         }
217                 }
218         }
219 }
220
221
222 /**
223  * For debug, save pixels to bitmap
224  */
225 void ObjectRenderer::SaveBMP(const CPURenderTarget & target, const char * filename)
226 {
227         SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(target.pixels, target.w, target.h, 8*4, target.w*4,
228         #if SDL_BYTEORDER == SDL_LIL_ENDIAN
229                 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
230         #else
231                 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
232         #endif //SDL_BYTEORDER  
233         );      
234         if (surf == NULL)
235                 Fatal("SDL_CreateRGBSurfaceFrom(pixels...) failed - %s", SDL_GetError());
236         if (SDL_SaveBMP(surf, filename) != 0)
237                 Fatal("SDL_SaveBMP failed - %s", SDL_GetError());
238
239         // Cleanup
240         SDL_FreeSurface(surf);
241 }
242
243 }

UCC git Repository :: git.ucc.asn.au