Compile with float/double again.
[ipdf/code.git] / src / objectrenderer.h
index 4dd7fe1..85a413b 100644 (file)
@@ -10,6 +10,9 @@
 #include "graphicsbuffer.h"
 #include "shaderprogram.h"
 #include "bufferbuilder.h"
+#include <cstdint>
+
+#define BEZIER_CPU_DECASTELJAU
 
 namespace IPDF
 {
@@ -27,14 +30,14 @@ namespace IPDF
        {
                public:
                        /** Construct the ObjectRenderer **/
-                       ObjectRenderer(const ObjectType & type, const char * vert_glsl_file, const char * frag_glsl_file, const char * geom_glsl_file = "");
+                       ObjectRenderer(const ObjectType & type, const char * vert_glsl_file="", const char * frag_glsl_file="", const char * geom_glsl_file = "");
                        virtual ~ObjectRenderer() {}
 
                        /**
                         * Use the GPU to render the objects - GLSL shader approach
                         * This way is definitely faster, but subject to the GPU's limitations on precision
                         */
-                       virtual void RenderUsingGPU();
+                       virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
 
                        /** 
                         * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
@@ -46,20 +49,46 @@ namespace IPDF
                                uint8_t * pixels;
                                int64_t w;
                                int64_t h;
+                               
+                               
+                               
                        };
+                       
+                       static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y)
+                       {
+                               int64_t index = 4*(x+y*target.w);
+                               if (index < 0 || index >= 4*(target.w*target.h))
+                                       return Colour(0,0,0,0);
+                               return Colour(target.pixels[index+0],target.pixels[index+1],target.pixels[index+2],target.pixels[index+3]);
+                       }
+                       
+                       static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c)
+                       {
+                               int64_t index = 4*(x+y*target.w);
+                               if (index < 0 || index >= 4*(target.w*target.h))
+                                       return;
+                               
+                               target.pixels[index+0] = c.r;
+                               target.pixels[index+1] = c.g;
+                               target.pixels[index+2] = c.b;
+                               target.pixels[index+3] = c.a;
+                       }
+                       
                        struct PixelBounds
                        {
                                int64_t x; int64_t y; int64_t w; int64_t h;
-                               PixelBounds(const Rect & bounds) : x(bounds.x), y(bounds.y), w(bounds.w), h(bounds.h) {}
+                               PixelBounds(const Rect & bounds);
                        };
+                       
+                       typedef std::pair<int64_t, int64_t> PixelPoint;
 
                        static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
-
+                       static PixelPoint CPUPointLocation(const Vec2 & point, const View & view, const CPURenderTarget & target);
 
                        static void SaveBMP(const CPURenderTarget & target, const char * filename);
 
 
-                       virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target) = 0;
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
                        
                        
                        
@@ -72,6 +101,8 @@ namespace IPDF
                
                        /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
                        static void RenderLineOnCPU(int64_t x0, int64_t y0, int64_t x1, int64_t y1, const CPURenderTarget & target, const Colour & colour = Colour(0,0,0,1), bool transpose = false);
+                       
+                       static void FloodFillOnCPU(int64_t x0, int64_t y0, const PixelBounds & bounds, const CPURenderTarget & target, const Colour & fill, const Colour & stroke=Colour(0,0,0,0));
 
                        ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
                        GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
@@ -85,7 +116,7 @@ namespace IPDF
                public:
                        RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
                        virtual ~RectFilledRenderer() {}
-                       virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
        };
        /** Renderer for outlined rectangles **/
        class RectOutlineRenderer : public ObjectRenderer
@@ -93,7 +124,7 @@ namespace IPDF
                public:
                        RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
                        virtual ~RectOutlineRenderer() {}
-                       virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
        };
        /** Renderer for filled circles **/
        class CircleFilledRenderer : public ObjectRenderer
@@ -101,22 +132,58 @@ namespace IPDF
                public:
                        CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
                        virtual ~CircleFilledRenderer() {}
-                       virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
        };
 
        /** Renderer for bezier curves **/
        class BezierRenderer : public ObjectRenderer
        {
                public:
-                       BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
+                       BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
                        virtual ~BezierRenderer() {}
-                       virtual void RenderUsingGPU() 
+                       virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id); 
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
+                       void PrepareBezierGPUBuffer(Objects & objects);
+                       
+                       static void RenderBezierOnCPU(const Bezier & relative, const Rect & bounds, const View & view, const CPURenderTarget & target, const Colour & c=Colour(0,0,0,255));
+                       
+               private:
+                       GraphicsBuffer m_bezier_coeffs;
+                       GraphicsBuffer m_bezier_ids;
+                       struct GPUBezierCoeffs
                        {
-                               Error("Cannot render beziers on the GPU; they will appear as outlined rectangles.");    
-                               ObjectRenderer::RenderUsingGPU();
-                       }
-                       virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
+                               float x0, y0;
+                               float x1, y1;
+                               float x2, y2;
+                               float x3, y3;
+                       };
+
+                       GLuint m_bezier_buffer_texture;
+                       GLuint m_bezier_id_buffer_texture;
+
+       };
+       
+       /** Renderer for filled paths **/
+       class PathRenderer : public ObjectRenderer
+       {
+               public:
+                       PathRenderer() : ObjectRenderer(PATH, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbug_geom.glsl") {}
+                       virtual ~PathRenderer() {}
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
+                       // do nothing on GPU
+                       virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
+
+       };
+
+       class FakeRenderer : public ObjectRenderer
+       {
+               public:
+                       FakeRenderer() : ObjectRenderer(PATH,NULL,NULL,NULL) {}
+                       ~FakeRenderer() {}
+                       virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) {}
+                       virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
        };
+       
 }
 
 #endif //_OBJECT_RENDERER_H

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