X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fobjectrenderer.h;h=66daafb7824b48486959a11d7405e171257f1b6e;hp=6dd97ae1f1d21e34e71124b6f27c5c4120df1477;hb=3172dd5af487e0f8a6e5cd5439dea594b9cbd7c9;hpb=6a1c4e752af0bd26803bc2285cff004ef7b9f53d diff --git a/src/objectrenderer.h b/src/objectrenderer.h index 6dd97ae..66daafb 100644 --- a/src/objectrenderer.h +++ b/src/objectrenderer.h @@ -11,6 +11,7 @@ #include "shaderprogram.h" #include "bufferbuilder.h" + namespace IPDF { class View; @@ -19,8 +20,8 @@ namespace IPDF * Includes GPU rendering and CPU rendering * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class * To leave unimplemented, just pass NULL filename strings - * For CPU rendering, implement RenderUsingCPU in the base class - * To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the base class + * For CPU rendering, implement RenderUsingCPU in the derived class + * To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class * The View class uses ObjectRenderer's; see view.h */ class ObjectRenderer @@ -34,7 +35,7 @@ namespace IPDF * Use the GPU to render the objects - GLSL shader approach * This way is definitely faster, but subject to the GPU's limitations on precision */ - 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,17 +47,42 @@ namespace IPDF uint8_t * pixels; int64_t w; int64_t h; + + + }; - struct CPURenderBounds + + static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y) + { + int64_t index = 4*(x+y*target.w); + return Colour(Real(target.pixels[index+0])/Real(255), + Real(target.pixels[index+1])/Real(255), + Real(target.pixels[index+2])/Real(255), + Real(target.pixels[index+3])/Real(255)); + } + + static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c) + { + int64_t index = 4*(x+y*target.w); + target.pixels[index+0] = c.r*255; + target.pixels[index+1] = c.g*255; + target.pixels[index+2] = c.b*255; + target.pixels[index+3] = c.a*255; + } + + struct PixelBounds { int64_t x; int64_t y; int64_t w; int64_t h; - CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target); + PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {} }; + static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target); + static std::pair CPUPointLocation(const std::pair & 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(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0; @@ -66,7 +92,11 @@ namespace IPDF void PrepareBuffers(unsigned max_size); void FinaliseBuffers(); void AddObjectToBuffers(unsigned index); - + + /** 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); ShaderProgram m_shader_program; /** GLSL shaders for GPU **/ GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/ @@ -80,7 +110,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(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id); }; /** Renderer for outlined rectangles **/ class RectOutlineRenderer : public ObjectRenderer @@ -88,7 +118,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(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id); }; /** Renderer for filled circles **/ class CircleFilledRenderer : public ObjectRenderer @@ -96,8 +126,45 @@ 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(const 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/bezier_texbuf_geom.glsl") {} + virtual ~BezierRenderer() {} + virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id); + virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id); + void PrepareBezierGPUBuffer(const Objects & objects); + private: + GraphicsBuffer m_bezier_coeffs; + GraphicsBuffer m_bezier_ids; + struct GPUBezierCoeffs + { + 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/rect_outline_geom.glsl") {} + virtual ~PathRenderer() {} + virtual void RenderUsingCPU(const 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) {} }; + } #endif //_OBJECT_RENDERER_H