Anonymous avatar Anonymous committed 7b3384b

Rewrote VBO class to use new Vertex class and not be interleaved

Comments (0)

Files changed (4)

 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>sandbox</groupId>
             <artifactId>slick</artifactId>
             <version>${slick.version}</version>
         </dependency>
+        <dependency>
+            <groupId>java3d</groupId>
+            <artifactId>vecmath</artifactId>
+            <version>1.3.1</version>
+        </dependency>
     </dependencies>
 
     <build>
         <pluginManagement>
             <plugins>
                 <plugin>
-                    <!-- Needed only for eclipse.  Piece of junk.  -->
+                    <!-- Used (and needed) only with Eclipse Indigo and later.  -->
+                    <!--suppress MavenModelInspection -->
                     <groupId>org.eclipse.m2e</groupId>
+                    <!--suppress MavenModelInspection -->
                     <artifactId>lifecycle-mapping</artifactId>
+                    <!--suppress MavenModelInspection -->
                     <version>1.0.0</version>
                     <configuration>
                         <lifecycleMappingMetadata>
                                         </goals>
                                     </pluginExecutionFilter>
                                     <action>
-                                        <ignore />
+                                        <ignore/>
                                     </action>
                                 </pluginExecution>
                             </pluginExecutions>

src/main/java/sandbox/misc/HelloVBO.java

 package sandbox.misc;
 
-import org.lwjgl.util.vector.Vector2f;
-import org.lwjgl.util.vector.Vector3f;
-import org.lwjgl.util.vector.Vector4f;
 import sandbox.common.BaseApp;
 import sandbox.util.VBO;
+import sandbox.util.Vertex;
 
 import static org.lwjgl.opengl.GL11.*;
 
 
     public HelloVBO(String title) {
         super(title);
-//        setPerspectiveProjection(false);
     }
 
     public static void main(String[] args) {
 
     @Override
     protected void setup() {
-        vbo.addVertex(
-                new Vector3f(-50, -50, 0),
-                new Vector3f(0, 1, 0),
-                new Vector4f(1, 0, 0, 1),
-                new Vector2f(0, 0)
-        );
-
-        vbo.addVertex(
-                new Vector3f(50, -50, 0),
-                new Vector3f(0, 1, 0),
-                new Vector4f(0, 1, 0, 1),
-                new Vector2f(0, 0)
-        );
-
-        vbo.addVertex(
-                new Vector3f(50, 50, 0),
-                new Vector3f(0, 1, 0),
-                new Vector4f(0, 0, 1, 1),
-                new Vector2f(0, 0)
-        );
-
-        vbo.addVertex(
-                new Vector3f(-50, 50, 0),
-                new Vector3f(0, 1, 0),
-                new Vector4f(1, 1, 0, 1),
-                new Vector2f(0, 0)
-        );
-        vbo.compile();
+        vbo.addVertex(new Vertex().position(-50,-50,0).color(1,0,0,1));
+        vbo.addVertex(new Vertex().position(50, -50, 0).color(0, 1, 0, 1));
+        vbo.addVertex(new Vertex().position(50,50,0).color(0,0,1,1));
+        vbo.addVertex(new Vertex().position(-50,50,0).color(1,1,1,1));
+        vbo.bake();
     }
 
     @Override
         glClear(GL_COLOR_BUFFER_BIT);
 
         glEnableClientState(GL_VERTEX_ARRAY);
-        glEnableClientState(GL_NORMAL_ARRAY);
         glEnableClientState(GL_COLOR_ARRAY);
 
         vbo.bind();
-        glDrawElements(GL_QUADS, vbo.getSize(), GL_UNSIGNED_INT, 0);
 
+        glDrawArrays(GL_QUADS, 0, vbo.getSize());
+        
         glDisableClientState(GL_VERTEX_ARRAY);
-        glDisableClientState(GL_NORMAL_ARRAY);
         glDisableClientState(GL_COLOR_ARRAY);
         glPopMatrix();
     }

src/main/java/sandbox/util/VBO.java

 package sandbox.util;
 
-import org.lwjgl.util.vector.Vector2f;
-import org.lwjgl.util.vector.Vector3f;
-import org.lwjgl.util.vector.Vector4f;
+import org.lwjgl.opengl.GL11;
 
+import javax.vecmath.Tuple2f;
+import javax.vecmath.Tuple3f;
+import javax.vecmath.Tuple4f;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.IntBuffer;
 import static org.lwjgl.opengl.GL15.*;
 
 public class VBO {
-    private List<Vector3f> positions = new ArrayList<Vector3f>();
-    private List<Vector3f> normals = new ArrayList<Vector3f>();
-    private List<Vector4f> colors = new ArrayList<Vector4f>();
-    private List<Vector2f> texcoords = new ArrayList<Vector2f>();
+    private boolean baked = false;
 
-    private ByteBuffer vertexBuffer;
-    private int vertexBufferId;
+    // This will only be non-null before baking
+    private List<Vertex> vertices = new ArrayList<Vertex>();
 
-    private ByteBuffer indexBuffer;
-    private int indexBufferId;
-
+    // These will only be set after baking
     private int size = 0;
 
-    public void addVertex(Vector3f position, Vector3f normal, Vector4f color, Vector2f texcoord) {
-        positions.add(position);
-        normals.add(normal);
-        colors.add(color);
-        texcoords.add(texcoord);
+    private ByteBuffer positionBuffer;
+    private int positionBufferId = 0;
+
+    private ByteBuffer normalBuffer;
+    private int normalBufferId = 0;
+
+    private ByteBuffer colorBuffer;
+    private int colorBufferId = 0;
+
+    private ByteBuffer texCoordBuffer;
+    private int texCoordBufferId = 0;
+
+    // private ByteBuffer indexBuffer;
+    // private int indexBufferId = 0;
+
+    public void addVertex(Vertex v) {
+        assertNotBaked();
+        checkTemplateVertex(v);
+        vertices.add(v);
     }
 
-    public void compile() {
-        int posSize = 3 * Float.SIZE / 8;
-        int normSize = 3 * Float.SIZE / 8;
-        int colSize = 4 * Float.SIZE / 8;
-        int tcSize = 2 * Float.SIZE / 8;
-        int vertexSize = posSize + normSize + colSize + tcSize;
+    private void checkTemplateVertex(Vertex v) {
+        if (vertices.size() > 0)
+            vertices.get(0).assertFormatEquals(v);
+    }
 
-        size = positions.size();
-
-        vertexBufferId = glGenBuffers();
-        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
-        glBufferData(GL_ARRAY_BUFFER, vertexSize * size, GL_STATIC_DRAW);
-        vertexBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, vertexSize * size, null);
-
-        FloatBuffer b = vertexBuffer.asFloatBuffer();
-        for (int i = 0; i < size; i++) {
-            positions.get(i).store(b);
-            normals.get(i).store(b);
-            colors.get(i).store(b);
-            texcoords.get(i).store(b);
-        }
-
-        glUnmapBuffer(GL_ARRAY_BUFFER);
-        glBindBuffer(GL_ARRAY_BUFFER, 0);
-
-        indexBufferId = glGenBuffers();
-        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
-        glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * Integer.SIZE / 8, GL_STATIC_DRAW);
-
-        indexBuffer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY, size * Integer.SIZE / 8, null);
-        IntBuffer ib = indexBuffer.asIntBuffer();
-        for (int i = 0; i < size; i++) {
-            ib.put(i);
-        }
-        glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
-        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+    private void assertNotBaked() {
+        if (baked) throw new IllegalStateException("VBO is already baked");
     }
 
     public void bind() {
-        int posSize = 3 * Float.SIZE / 8;
-        int normSize = 3 * Float.SIZE / 8;
-        int colSize = 4 * Float.SIZE / 8;
-        int tcSize = 2 * Float.SIZE / 8;
-        int vertexSize = posSize + normSize + colSize + tcSize;
+        if (!baked)
+            bake();
+        size = vertices.size();
+        if (positionBufferId != 0) {
+            glBindBuffer(GL_ARRAY_BUFFER, positionBufferId);
+            glVertexPointer(4, GL_FLOAT, 0, 0);
+        }
+        if (normalBufferId != 0) {
+            glBindBuffer(GL_ARRAY_BUFFER, texCoordBufferId);
+            glNormalPointer(GL_FLOAT, 0, 0);
+        }
+        if (colorBufferId != 0) {
+            glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
+            glColorPointer(4, GL_FLOAT, 0, 0);
+        }
+        if (texCoordBufferId != 0) {
+            glBindBuffer(GL_ARRAY_BUFFER, texCoordBufferId);
+            glTexCoordPointer(2, GL_FLOAT, 0, 0);
+        }
+    }
 
-        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
-        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
+    public void bake() {
+        assertNotBaked();
+        if (vertices.size() == 0)
+            throw new IllegalStateException("No vertices in VBO");
 
-        glVertexPointer(3, GL_FLOAT, vertexSize, 0);
-        glNormalPointer(GL_FLOAT, vertexSize, posSize);
-        glColorPointer(4, GL_FLOAT, vertexSize, posSize + normSize);
-        glTexCoordPointer(2, GL_FLOAT, vertexSize, posSize + normSize + tcSize);
+        Vertex templateVertex = vertices.get(0);
+        if (templateVertex.position != null)
+            bakePositions();
+        if (templateVertex.normal != null)
+            bakeNormals();
+        if (templateVertex.color != null)
+            bakeColors();
+        if (templateVertex.texCoord != null)
+            bakeTexCoords();
+
+//        bakeIndices();
 
     }
 
+    private void bakePositions() {
+        positionBufferId = glGenBuffers();
+        glBindBuffer(GL_ARRAY_BUFFER, positionBufferId);
+        int size = Vertex.POSITION_SIZE * vertices.size();
+        glBufferData(GL_ARRAY_BUFFER, size, GL_STATIC_DRAW);
+        positionBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, size, null);
+
+        FloatBuffer b = positionBuffer.asFloatBuffer();
+        for (Vertex v : vertices) {
+            Tuple4f t = v.position;
+            b.put(t.x);
+            b.put(t.y);
+            b.put(t.z);
+            b.put(t.w);
+        }
+        glUnmapBuffer(GL_ARRAY_BUFFER);
+        glBindBuffer(GL_ARRAY_BUFFER, 0);
+    }
+
+    private void bakeNormals() {
+        normalBufferId = glGenBuffers();
+        glBindBuffer(GL_ARRAY_BUFFER, normalBufferId);
+        int size = Vertex.NORMAL_SIZE * vertices.size();
+        glBufferData(GL_ARRAY_BUFFER, size, GL_STATIC_DRAW);
+        normalBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, size, null);
+
+        FloatBuffer b = normalBuffer.asFloatBuffer();
+        for (Vertex v : vertices) {
+            Tuple3f t = v.normal;
+            b.put(t.x);
+            b.put(t.y);
+            b.put(t.z);
+        }
+        glUnmapBuffer(GL_ARRAY_BUFFER);
+        glBindBuffer(GL_ARRAY_BUFFER, 0);
+    }
+
+    private void bakeColors() {
+        colorBufferId = glGenBuffers();
+        glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
+        int size = Vertex.COLOR_SIZE * vertices.size();
+        glBufferData(GL_ARRAY_BUFFER, size, GL_STATIC_DRAW);
+        colorBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, size, null);
+        FloatBuffer b = colorBuffer.asFloatBuffer();
+        for (Vertex v : vertices) {
+            Tuple4f t = v.color;
+            b.put(t.x);
+            b.put(t.y);
+            b.put(t.z);
+            b.put(t.w);
+        }
+        glUnmapBuffer(GL_ARRAY_BUFFER);
+        glBindBuffer(GL_ARRAY_BUFFER, 0);
+    }
+
+    private void bakeTexCoords() {
+        texCoordBufferId = glGenBuffers();
+        glBindBuffer(GL_ARRAY_BUFFER, texCoordBufferId);
+        int size = Vertex.POSITION_SIZE * vertices.size();
+        glBufferData(GL_ARRAY_BUFFER, size, GL_STATIC_DRAW);
+        texCoordBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, size, null);
+        FloatBuffer b = texCoordBuffer.asFloatBuffer();
+        for (Vertex v : vertices) {
+            Tuple2f t = v.texCoord;
+            b.put(t.x);
+            b.put(t.y);
+        }
+        glUnmapBuffer(GL_ARRAY_BUFFER);
+        glBindBuffer(GL_ARRAY_BUFFER, 0);
+    }
+
+//    private void bakeIndices() {
+//        indexBufferId = glGenBuffers();
+//        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
+//        glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertices.size() * Integer.SIZE / 8, GL_STATIC_DRAW);
+//
+//        indexBuffer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY, vertices.size() * Integer.SIZE / 8, null);
+//        IntBuffer ib = indexBuffer.asIntBuffer();
+//        for (int i = 0; i < vertices.size(); i++) {
+//            ib.put(i);
+//        }
+//        glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
+//        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+//    }
+
     public int getSize() {
         return size;
     }

src/main/java/sandbox/util/Vertex.java

+package sandbox.util;
+
+import javax.vecmath.*;
+
+public class Vertex {
+    Point4f position;
+    Vector3f normal;
+    Color4f color;
+    TexCoord2f texCoord;
+
+    public static final int POSITION_SIZE = 4 * Float.SIZE / 8;
+    public static final int NORMAL_SIZE = 3 * Float.SIZE / 8;
+    public static final int COLOR_SIZE = 4 * Float.SIZE / 8;
+    public static final int TEXCOORD_SIZE = 2 * Float.SIZE / 8;
+
+
+    public boolean FormatEquals(Vertex v) {
+        return ((getPosition() == null) == (v.getPosition() == null)
+                && (getNormal() == null) == (v.getNormal() == null)
+                && (getColor() == null) == (v.getColor() == null)
+                && (getTexCoord() == null) == (v.getTexCoord() == null));
+    }
+
+    public void assertFormatEquals(Vertex v) {
+        // Does more comparisons than formatEquals in order to have more informative errors
+        if (getPosition() == null && v.getPosition() != null)
+            throw new IllegalArgumentException("Spurious position attri`bute in vertex");
+        if (getPosition() != null && v.getPosition() == null)
+            throw new IllegalArgumentException("Position attribute missing from vertex");
+
+        if (getNormal() == null && v.getNormal() != null)
+            throw new IllegalArgumentException("Spurious normal attribute in vertex");
+        if (getNormal() != null && v.getNormal() == null)
+            throw new IllegalArgumentException("Normal attribute missing from vertex");
+
+        if (getColor() == null && v.getColor() != null)
+            throw new IllegalArgumentException("Spurious color attribute in vertex");
+        if (getColor() != null && v.getColor() == null)
+            throw new IllegalArgumentException("Color attribute missing from vertex");
+
+        if (getTexCoord() == null && v.getTexCoord() != null)
+            throw new IllegalArgumentException("Spurious texcoord attribute in vertex");
+        if (getTexCoord() != null && v.getTexCoord() == null)
+            throw new IllegalArgumentException("Texcoord attribute missing from vertex");
+
+    }
+
+    // Happy Fun Builder Methods
+    public Vertex position(Point4f position) {
+        setPosition(position);
+        return this;
+    }
+
+    public Vertex position(Tuple3f position) {
+        Point4f pos = new Point4f();
+        pos.x = position.x;
+        pos.y = position.y;
+        pos.z = position.z;
+        pos.w = 1;
+        setPosition(pos);
+        return this;
+    }
+
+    public Vertex position(float x, float y, float z, float w) {
+        return position(new Point4f(x, y, z, w));
+    }
+
+    public Vertex position(float x, float y, float z) {
+        return position(x, y, z, 1);
+    }
+
+    public Vertex normal(Vector3f normal) {
+        setNormal(normal);
+        return this;
+    }
+
+    public Vertex normal(float x, float y, float z) {
+        return normal(new Vector3f(x, y, z));
+    }
+
+    public Vertex color(Color4f color) {
+        setColor(color);
+        return this;
+    }
+
+    public Vertex color(float r, float g, float b, float a) {
+        return color(new Color4f(r, g, b, a));
+    }
+
+    public Vertex texCoord(TexCoord2f texCoord) {
+        setTexCoord(texCoord);
+        return this;
+    }
+
+    public Vertex texCoord(float s, float t) {
+        return texCoord(new TexCoord2f(s, t));
+    }
+
+    // boilerplate
+    public Point4f getPosition() {
+        return position;
+    }
+
+    public void setPosition(Point4f position) {
+        this.position = position;
+    }
+
+    public Vector3f getNormal() {
+        return normal;
+    }
+
+    public void setNormal(Vector3f normal) {
+        this.normal = normal;
+    }
+
+    public Color4f getColor() {
+        return color;
+    }
+
+    public void setColor(Color4f color) {
+        this.color = color;
+    }
+
+    public TexCoord2f getTexCoord() {
+        return texCoord;
+    }
+
+    public void setTexCoord(TexCoord2f texcoord) {
+        this.texCoord = texcoord;
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.