Commits

Michael Ludwig committed 11fe061

Consolidate renderElements() and renderArray() into a single render() call and add setIndices() that lets a user bind or unbind an index buffer as desired.

  • Participants
  • Parent commits 02e40e8

Comments (0)

Files changed (11)

File ferox-demos/src/main/java/com/ferox/renderer/impl/jogl/FixedFunctionRenderTest.java

 
                     g.setModelViewMatrix(f.getViewMatrix().mul(t, t));
 
-                    if (shape.getIndices() != null) {
-                        rendered += g.renderElements(shape.getPolygonType(),
-                                                     shape.getIndices(),
-                                                     shape.getIndexOffset(),
-                                                     shape.getIndexCount());
-                    } else {
-                        rendered += g.renderArray(shape.getPolygonType(),
-                                                  shape.getIndexOffset(),
-                                                  shape.getIndexCount());
-                    }
+                    g.setIndices(shape.getIndices());
+                    rendered += g.render(shape.getPolygonType(), shape.getIndexOffset(),
+                                         shape.getIndexCount());
                 }
 
                 if (!statusChecked) {
 
                     System.out.println("\nvertices status: " + surface.getFramework()
                                                                       .getStatus(shape.getVertices()
-                                                                                      .getData()));
+                                                                                      .getVBO()));
                     System.out.println("\nnormals status: " + surface.getFramework()
                                                                      .getStatus(shape.getNormals()
-                                                                                     .getData()));
+                                                                                     .getVBO()));
                     System.out.println("\ntexcoords status: " + surface.getFramework()
                                                                        .getStatus(shape.getTextureCoordinates()
-                                                                                       .getData()));
+                                                                                       .getVBO()));
 
                     System.out.println("\ntexture status: " + surface.getFramework()
                                                                      .getStatus(volume) + " " + surface.getFramework()

File ferox-demos/src/main/java/com/ferox/renderer/impl/jogl/GlslRenderTest.java

 
                 g.setUniform("transform", 2f, .25f);
 
-                int rendered = g.renderArray(shape.getPolygonType(),
-                                             shape.getIndexOffset(),
-                                             shape.getIndexCount());
+                g.setIndices(null);
+                int rendered = g.render(shape.getPolygonType(), shape.getIndexOffset(),
+                                        shape.getIndexCount());
 
                 if (!statusChecked) {
                     statusChecked = true;

File ferox-demos/src/main/java/com/ferox/renderer/impl/lwjgl/FixedFunctionRenderTest.java

 
                     g.setModelViewMatrix(f.getViewMatrix().mul(t, t));
 
-                    if (shape.getIndices() != null) {
-                        rendered += g.renderElements(shape.getPolygonType(),
-                                                     shape.getIndices(),
-                                                     shape.getIndexOffset(),
-                                                     shape.getIndexCount());
-                    } else {
-                        rendered += g.renderArray(shape.getPolygonType(),
-                                                  shape.getIndexOffset(),
-                                                  shape.getIndexCount());
-                    }
+                    g.setIndices(shape.getIndices());
+                    rendered += g.render(shape.getPolygonType(), shape.getIndexOffset(),
+                                         shape.getIndexCount());
                 }
 
                 if (!statusChecked) {
 
                     System.out.println("\nvertices status: " + surface.getFramework()
                                                                       .getStatus(shape.getVertices()
-                                                                                      .getData()));
+                                                                                      .getVBO()));
                     System.out.println("\nnormals status: " + surface.getFramework()
                                                                      .getStatus(shape.getNormals()
-                                                                                     .getData()));
+                                                                                     .getVBO()));
                     System.out.println("\ntexcoords status: " + surface.getFramework()
                                                                        .getStatus(shape.getTextureCoordinates()
-                                                                                       .getData()));
+                                                                                       .getVBO()));
 
                     System.out.println("\ntexture status: " + surface.getFramework()
                                                                      .getStatus(volume) + " " + surface.getFramework()

File ferox-demos/src/main/java/com/ferox/renderer/impl/lwjgl/GlslRenderTest.java

 
                 g.setUniform("transform", 2f, .25f);
 
-                int rendered = g.renderArray(shape.getPolygonType(),
-                                             shape.getIndexOffset(),
-                                             shape.getIndexCount());
+                g.setIndices(null);
+                int rendered = g.render(shape.getPolygonType(), shape.getIndexOffset(),
+                                        shape.getIndexCount());
 
                 if (!statusChecked) {
                     statusChecked = true;

File ferox-demos/src/main/java/com/ferox/renderer/impl/lwjgl/LightBlendingTest.java

                 //                ffp.setLightPosition(2, new Vector4(4, 4, -4, 1));
                 //                ffp.setLightEnabled(2, true);
 
-                ffp.renderArray(shape.getPolygonType(), shape.getIndexOffset(),
-                                shape.getIndexCount());
+                ffp.setIndices(null);
+                ffp.render(shape.getPolygonType(), shape.getIndexOffset(),
+                           shape.getIndexCount());
 
                 // second light
                 ffp.setBlendingEnabled(true);

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/renderer/Renderer.java

 
     /**
      * <p>
-     * Render the polygons described in the indices buffer, <tt>indices</tt>.
-     * Polygons are formed from consecutive indices as determined by the
-     * provided polygon type. The first index is taken from <tt>offset</tt>. The
-     * indices point to the vertices and attributes in the other currently
-     * configured VertexBufferObjects. In OpenGL, this operation is equivalent
-     * to <code>glDrawElements</code>. For performance reasons, it is not
-     * checked if <tt>indices</tt> contains indices that reference vertices
-     * outside the range of configured vertices. If this happens, rendering will
-     * be undefined, and may cause the JVM to crash.
+     * Set the index buffer that is used when rendering. If the index buffer is
+     * not null, the rendered polygons are described by the index values in
+     * <tt>indices</tt> and the polygon type specified by
+     * {@link #render(PolygonType, int, int)}.
      * <p>
      * The VertexBufferObject might be updated before rendering depending on its
      * update policy. The VertexBufferObject must have an integral data type of
      * INT, SHORT, or BYTE. The integer indices are considered as unsigned
      * values, even though unsigned integers are not supported in Java.
      * <p>
-     * The Renderer interface does not provide a mechanism to configure the
-     * active set of vertex attributes needed to form the actual vertices.
-     * Specifying vertex attributes depends on the type of Renderer because the
-     * fixed-function pipeline and programmable shaders use different
-     * mechanisms. However, they can both perform the actual rendering in the
-     * same manner. It is possible that specific Renderer types will support
-     * more rendering methods, such as rendering multiple instances.
+     * When the index buffer is null, any previous buffer is unbound and
+     * rendered polygons are constructed by accessing the vertex attributes
+     * consecutively.
      * 
-     * @param polyType The type of polygon to render
      * @param indices An array of integer indices into the configured vertex
      *            attributes
-     * @param offset The offset into indices
-     * @param count The number of indices to turn into polygons
-     * @return The number of polygons rendered
-     * @throws NullPointerException if polyType or indices is null
-     * @throws IllegalArgumentException if offset or count are negative, or if
-     *             indices has a data type of FLOAT
-     * @throws IndexOutOfBoundsException if offset + count is greater than the
-     *             size of the indices buffer
+     * @throws IllegalArgumentException if indices is not null and has a type of
+     *             FLOAT
      */
-    public int renderElements(PolygonType polyType, VertexBufferObject indices,
-                              int offset, int count);
+    public void setIndices(VertexBufferObject indices);
 
     /**
      * <p>
-     * Render polygons by iterating through <tt>count</tt> vertices in the
-     * currently configured vertex attributes. The first vertex is read from the
-     * index, <tt>first</tt>.The vertices form polygons based on the provided
-     * polygon type. In OpenGL, this operation is equivalent to
-     * <code>glDrawArray</code>. If <tt>first</tt> and <tt>count</tt> would
-     * cause this to access undefined vertices, undefined and probably
-     * disastrous behavior will result. This will render
-     * {@link PolygonType#getPolygonCount(int)} primitives, with <tt>count</tt>
-     * as the argument.
+     * Render polygons by iterating through <tt>count</tt> of the configured
+     * indices starting at <tt>offset</tt>. Polygons are formed based on the
+     * polygon type provided. If there is a non-null bound index buffer count
+     * index values are read and used to access the vertex attributes. If there
+     * is no index buffer, vertex attributes are read directly starting at
+     * offset.
      * <p>
      * The Renderer interface does not provide a mechanism to configure the
      * active set of vertex attributes needed to form the actual vertices.
      * @param count The number of vertices to render
      * @return The number of polygons rendered
      * @throws IllegalArgumentException if first or count are negative
+     * @throws IndexOutOfBoundsException if there's a bound index buffer and the
+     *             configured offset and count would access invalid elements
      */
-    public int renderArray(PolygonType polyType, int first, int count);
+    public int render(PolygonType polyType, int offset, int count);
 
     /**
      * Manually reset all of the OpenGL-related state in this Renderer to the

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/util/geom/text/TextRenderer.java

                         ffp.setTextureCoordinates(0, g.getTextureCoordinates());
                         ffp.setVertices(g.getVertices());
 
-                        if (g.getIndices() == null) {
-                            ffp.renderArray(g.getPolygonType(), g.getIndexOffset(),
-                                            g.getIndexCount());
-                        } else {
-                            ffp.renderElements(g.getPolygonType(), g.getIndices(),
-                                               g.getIndexOffset(), g.getIndexCount());
-                        }
+                        ffp.setIndices(g.getIndices());
+                        ffp.render(g.getPolygonType(), g.getIndexOffset(),
+                                   g.getIndexCount());
                     }
                 }
                 // FIXME support a glsl shader that can do the same rendering

File ferox-renderer/ferox-renderer-impl/src/main/java/com/ferox/renderer/impl/AbstractFixedFunctionRenderer.java

     }
 
     @Override
-    public int renderElements(PolygonType polyType, VertexBufferObject indices,
-                              int offset, int count) {
+    public int render(PolygonType polyType, int first, int count) {
         flushModelView();
-        return super.renderElements(polyType, indices, offset, count);
-    }
-
-    @Override
-    public int renderArray(PolygonType polyType, int first, int count) {
-        flushModelView();
-        return super.renderArray(polyType, first, count);
+        return super.render(polyType, first, count);
     }
 
     @Override

File ferox-renderer/ferox-renderer-impl/src/main/java/com/ferox/renderer/impl/AbstractRenderer.java

     }
 
     @Override
-    public int renderElements(PolygonType polyType, VertexBufferObject indices,
-                              int offset, int count) {
-        return delegate.renderElements(polyType, indices, offset, count);
+    public void setIndices(VertexBufferObject indices) {
+        delegate.setIndices(indices);
     }
 
     @Override
-    public int renderArray(PolygonType polyType, int first, int count) {
-        return delegate.renderArray(polyType, first, count);
+    public int render(PolygonType polyType, int first, int count) {
+        return delegate.render(polyType, first, count);
     }
 
     /**

File ferox-renderer/ferox-renderer-impl/src/main/java/com/ferox/renderer/impl/RendererDelegate.java

     protected RendererState defaultState;
 
     protected VertexBufferObjectHandle indexBindingHandle;
-    protected VertexBufferObject indexBinding;
 
     protected OpenGLContext context;
     protected ResourceManager resourceManager;
         setStencilTestEnabled(state.stencilEnabled);
 
         setViewport(state.viewX, state.viewY, state.viewWidth, state.viewHeight);
+        setIndices(state.indices);
     }
 
     /**
      */
     protected abstract void glViewport(int x, int y, int width, int height);
 
-    private void setIndexBuffer(VertexBufferObject indices) {
-        if (indexBinding != indices) {
+    public void setIndices(VertexBufferObject indices) {
+        if (state.indices != indices) {
             // Must bind a new element vbo
-            boolean hadOldIndices = indexBinding != null;
+            boolean hadOldIndices = state.indices != null;
             boolean failTypeCheck = false;
 
             if (hadOldIndices) {
                 // Unlock old vbo first
-                resourceManager.unlock(indexBinding);
-                indexBinding = null;
+                resourceManager.unlock(state.indices);
+                state.indices = null;
                 indexBindingHandle = null;
             }
 
 
             // Handle actual binding of the vbo
             if (newHandle != null) {
-                indexBinding = indices;
+                state.indices = indices;
                 indexBindingHandle = newHandle;
                 glBindElementVbo(newHandle);
             } else if (hadOldIndices) {
         }
     }
 
-    public int renderElements(PolygonType polyType, VertexBufferObject indices,
-                              int offset, int count) {
-        if (polyType == null || indices == null) {
-            throw new NullPointerException("PolygonType and indices cannot be null");
+    public int render(PolygonType polyType, int offset, int count) {
+        if (polyType == null) {
+            throw new NullPointerException("PolygonType cannot be null");
         }
         if (offset < 0 || count < 0) {
             throw new IllegalArgumentException("First and count must be at least 0, not: " + offset + ", " + count);
             return 0; // shortcut
         }
 
-        setIndexBuffer(indices);
-
-        if (indexBinding == null) {
-            // No element vbo to work with, so we can't render anything
-            return 0;
-        } else {
-            // check if the actual VBO is of the correct size (must use handle can't
-            // rely on the resource reporting the most up-to-date size)
+        if (state.indices != null) {
+            // use glDrawElements
+            // - check if the actual VBO is of the correct size (must use handle can't
+            // - rely on the resource reporting the most up-to-date size)
             if ((offset + count) > indexBindingHandle.length) {
                 throw new IllegalArgumentException("Index and count access elements outside of VBO range");
             }
 
             // Element vbo is bound this time (or from a previous rendering)
             glDrawElements(polyType, indexBindingHandle, offset, count);
+        } else {
+            // use glDrawArrays
+            glDrawArrays(polyType, offset, count);
         }
 
         return polyType.getPolygonCount(count);
                                            VertexBufferObjectHandle handle, int offset,
                                            int count);
 
-    public int renderArray(PolygonType polyType, int first, int count) {
-        if (polyType == null) {
-            throw new NullPointerException("PolygonType cannot be null");
-        }
-        if (first < 0 || count < 0) {
-            throw new IllegalArgumentException("First and count must be at least 0, not: " + first + ", " + count);
-        }
-
-        // short cut
-        if (count == 0) {
-            return 0;
-        }
-
-        glDrawArrays(polyType, first, count);
-        return polyType.getPolygonCount(count);
-    }
-
     /**
      * Perform the glDrawArrays rendering command. The inputs will be valid.
      */

File ferox-renderer/ferox-renderer-impl/src/main/java/com/ferox/renderer/impl/RendererState.java

 import com.ferox.renderer.Renderer.DrawStyle;
 import com.ferox.renderer.Renderer.StencilUpdate;
 import com.ferox.renderer.Surface;
+import com.ferox.resource.VertexBufferObject;
 
 public class RendererState {
     public static final Vector4 DEFAULT_BLEND_COLOR = new Vector4(0f, 0f, 0f, 0f);
 
     public boolean stencilEnabled;
 
+    // rendering
+    public VertexBufferObject indices;
+
     // stencil mask
     public int stencilMaskFront;
     public int stencilMaskBack;
         viewY = 0;
         viewWidth = surface.getWidth();
         viewHeight = surface.getHeight();
+
+        indices = null;
     }
 
     public RendererState(RendererState toClone) {
         viewY = toClone.viewY;
         viewWidth = toClone.viewWidth;
         viewHeight = toClone.viewHeight;
+
+        indices = toClone.indices;
     }
 }