Store everything in a VBO, making things faster.
[ipdf/code.git] / src / view.cpp
index 26e9507..bda0faa 100644 (file)
@@ -12,6 +12,8 @@ void View::Translate(Real x, Real y)
        m_bounds.x += x;
        m_bounds.y += y;
        Debug("View Bounds => %s", m_bounds.Str().c_str());
+       if (!m_use_gpu_transform)
+               m_bounds_dirty = true;
 }
 
 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
@@ -36,6 +38,8 @@ void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
        m_bounds.w *= scaleAmt;
        m_bounds.h *= scaleAmt;
        Debug("View Bounds => %s", m_bounds.Str().c_str());
+       if (!m_use_gpu_transform)
+               m_bounds_dirty = true;
 }
 
 Rect View::TransformToViewCoords(const Rect& inp) const
@@ -74,18 +78,11 @@ void View::DrawGrid()
        }
 }
 
+void glPrimitiveRestartIndex(GLuint index);
+
 void View::Render()
 {
-       static bool debug_output_done = false;
-       if (!debug_output_done)
-       {
-               m_document.DebugDumpObjects();
-               debug_output_done = true;
-       }
-
-
-       //DrawGrid(); // Draw the gridlines
-
+       
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        if (m_use_gpu_transform)
@@ -96,16 +93,70 @@ void View::Render()
        {
                glOrtho(0,1,1,0,-1,1);
        }
+
+       if (m_bounds_dirty)
+               ReRender();
+
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
-
        if (m_colour.a < 1.0f)
        {
                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        }
        glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
-       glBegin(GL_QUADS);
+       m_vertex_buffer.Bind();
+       m_index_buffer.Bind();
+       glEnable(GL_PRIMITIVE_RESTART);
+       glPrimitiveRestartIndex(0xFFFFFFFF);
+       glVertexPointer(2, GL_FLOAT, 0, 0);
+       glEnableClientState(GL_VERTEX_ARRAY);
+       glDrawElements(GL_TRIANGLE_STRIP, m_rendered_filled * 5, GL_UNSIGNED_INT, 0);
+       glDrawElements(GL_LINE_LOOP, m_rendered_outline*5, GL_UNSIGNED_INT,(void*)(sizeof(uint32_t)*m_rendered_filled*5));
+       glDisable(GL_PRIMITIVE_RESTART);
+       if (m_colour.a < 1.0f)
+       {
+               glDisable(GL_BLEND);
+       }
+
+
+}
+
+void View::ReRender()
+{
+       static bool debug_output_done = false;
+       if (!debug_output_done)
+       {
+               m_document.DebugDumpObjects();
+               debug_output_done = true;
+
+               m_vertex_buffer.SetType(GraphicsBuffer::BufferTypeVertex);
+               m_index_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+               m_index_buffer.SetType(GraphicsBuffer::BufferTypeIndex);
+
+               m_vertex_buffer.Upload(m_document.ObjectCount() * 8 * sizeof(float), NULL);
+               m_index_buffer.Upload(m_document.ObjectCount() * 5 * sizeof(uint32_t), NULL);
+       }
+       m_rendered_filled = m_rendered_outline = 0;
+       
+       if (m_use_gpu_transform)
+       {
+               m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+       }
+       else
+       {
+               m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
+       }
+
+
+       //DrawGrid(); // Draw the gridlines
+
+
+
+       float *vertexData = (float*)m_vertex_buffer.Map(false, true, true);
+       uint32_t *indexData = (uint32_t*)m_index_buffer.Map(false, true, true);
+
+       uint32_t currentIndex = 0;
        for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
        {
                if (m_document.m_objects.types[id] != RECT_FILLED)
@@ -119,13 +170,25 @@ void View::Render()
                {
                        obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
                }
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
-       }
-       glEnd();
+               *vertexData = Float(obj_bounds.x); vertexData++;
+               *vertexData = Float(obj_bounds.y); vertexData++;
+               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
+               *vertexData = Float(obj_bounds.y); vertexData++;
+               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
+               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
+               *vertexData = Float(obj_bounds.x); vertexData++;
+               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
+
+               *indexData = currentIndex; indexData++;
+               *indexData = currentIndex+1; indexData++;
+               *indexData = currentIndex+3; indexData++;
+               *indexData = currentIndex+2; indexData++;
+               *indexData = 0xFFFFFFFF; // Primitive restart.
+               indexData++;
+               currentIndex += 4;
+               m_rendered_filled++;
 
+       }
        
        for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
        {
@@ -140,17 +203,27 @@ void View::Render()
                {
                        obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
                }
-               glBegin(GL_LINE_LOOP);
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
-               glEnd();
+               *vertexData = Float(obj_bounds.x); vertexData++;
+               *vertexData = Float(obj_bounds.y); vertexData++;
+               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
+               *vertexData = Float(obj_bounds.y); vertexData++;
+               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
+               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
+               *vertexData = Float(obj_bounds.x); vertexData++;
+               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
+
+               *indexData = currentIndex; indexData++;
+               *indexData = currentIndex+1; indexData++;
+               *indexData = currentIndex+2; indexData++;
+               *indexData = currentIndex+3; indexData++;
+               *indexData = 0xFFFFFFFF; // Primitive restart.
+               indexData++;
+               currentIndex += 4;
+               m_rendered_outline++;
        }
+       m_vertex_buffer.UnMap();
+       m_index_buffer.UnMap();
 
-       if (m_colour.a < 1.0f)
-       {
-               glDisable(GL_BLEND);
-       }
+       m_bounds_dirty = false;
 
 }

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