Commits

John Marsden committed 75705fe

First working version in years

  • Participants
  • Parent commits dd422b5

Comments (0)

Files changed (8)

File src/main/java/cc/plural/ecs/component/spatial/SpatialComponent.java

 //        
 //    }
     public void crossoverLocalToWorld() {
-        world.set(local);
+        world.store(local);
     }
 
     public void combineWorldWithParent(SpatialComponent parentSpatial) {
     /**
      * Intent Events
      */
+    
     public void onXTranslationChange(float x, float xDelta) {
         if(activeIntentTopicRecipients(xChangeTopic)) {
             sendIntent(new SpatialComponentLocalTXChangeIntent(this, x, xDelta));
         }
     }
 
-    public void get(Matrix4f matrix) {
-        world.get(matrix);
+    public void load(Matrix4f matrix) {
+        world.load(matrix);
     }
 }

File src/main/java/cc/plural/ecs/provider/LWJGLInMemoryMesh.java

 package cc.plural.ecs.provider;
 
 import cc.plural.ecs.renderer.Mesh;
+import cc.plural.ecs.renderer.Shader;
 import cc.plural.graphics.Vertex;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 
     public void disable() {
     }
+    
+    public void enable(Shader shader) {
+    }
+
+    public void disable(Shader shader) {
+    }
 
     public void unLoad() {
         GL15.glDeleteBuffers(indiciesHandle);

File src/main/java/cc/plural/ecs/provider/LWJGLInMemoryMeshVAO.java

  */
 public class LWJGLInMemoryMeshVAO extends Mesh {
 
-    public Shader shader;
     public Vertex[] verticies;
     public short[] indicies;
     public int vertaxArrayHandle;
     public boolean initialized;
     public boolean enabled;
 
-    public LWJGLInMemoryMeshVAO(Shader shader, Vertex[] verticies, short[] indicies) {
-        this.shader = shader;
+    public LWJGLInMemoryMeshVAO(Vertex[] verticies, short[] indicies) {
         vertaxArrayHandle = -1;
         vertexHandle = -1;
         indiciesHandle = -1;
         FloatBuffer verticesFloatBuffer = convert(verticies);
         //System.out.println("ECS Vertex Buffer:\t" + ArrayPrinter.arrayToString(verticesFloatBuffer));
         verticesFloatBuffer.rewind();
-        
+
         vertaxArrayHandle = GL30.glGenVertexArrays();
-   
+
         GL30.glBindVertexArray(vertaxArrayHandle);
 
         vertexHandle = GL15.glGenBuffers();
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexHandle);
         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW);
 
-        if (shader.hasAttribute("in_Position")) {
-            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
-        }
-        if (shader.hasAttribute("in_Normal")) {
-            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Normal"), Vertex.NORMAL_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.NORMAL_BYTE_OFFSET);
-        }
-        if (shader.hasAttribute("in_Color")) {
-            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
-        }
-        if (shader.hasAttribute("in_TextureCoord")) {
-            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
-        }
-
+//        if (shader.hasAttribute("in_Position")) {
+//            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
+//        }
+//        if (shader.hasAttribute("in_Normal")) {
+//            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Normal"), Vertex.NORMAL_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.NORMAL_BYTE_OFFSET);
+//        }
+//        if (shader.hasAttribute("in_Color")) {
+//            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
+//        }
+//        if (shader.hasAttribute("in_TextureCoord")) {
+//            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
+//        }
         indicesCount = indicies.length;
         ByteBuffer indiciesBuffer = BufferUtils.createByteBuffer(indicesCount * 2);
         ShortBuffer indiciesShortBuffer = indiciesBuffer.asShortBuffer();
         indiciesHandle = GL15.glGenBuffers();
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indiciesHandle);
         GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indiciesShortBuffer, GL15.GL_STATIC_DRAW);
-        
+
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
         GL30.glBindVertexArray(0);
     }
 
     public void enable() {
+
+    }
+
+    public void enable(Shader shader) {
         GL30.glBindVertexArray(vertaxArrayHandle);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexHandle);
+
+        if (shader.hasAttribute("in_Position")) {
+            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
+        }
+        if (shader.hasAttribute("in_Normal")) {
+            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Normal"), Vertex.NORMAL_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.NORMAL_BYTE_OFFSET);
+        }
+        if (shader.hasAttribute("in_Color")) {
+            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
+        }
+        if (shader.hasAttribute("in_TextureCoord")) {
+            GL20.glVertexAttribPointer(shader.getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
+        }
+
         if (shader.hasAttribute("in_Position")) {
             int location = shader.getAttributeLocation("in_Position");
             GL20.glEnableVertexAttribArray(location);
 
     public void disable() {
 
+    }
+
+    public void disable(Shader shader) {
+
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
         if (shader.hasAttribute("in_TextureCoord")) {
             GL20.glDisableVertexAttribArray(shader.getAttributeLocation("in_TextureCoord"));
 
         return state.toString();
     }
-    
-     @Override
+
+    @Override
     public int getIndicesCount() {
         return indicesCount;
     }

File src/main/java/cc/plural/ecs/provider/LWJGLRenderer.java

     }
 
     public Mesh createStaticMesh(Vertex[] vertices, short[] indicies) {
-        LWJGLInMemoryMeshVAO mesh = new LWJGLInMemoryMeshVAO(getDefaultShader(), vertices, indicies);
+        LWJGLInMemoryMeshVAO mesh = new LWJGLInMemoryMeshVAO(vertices, indicies);
         return mesh;
     }
     
     public Mesh createStaticMesh(Shader shader, Vertex[] vertices, short[] indicies) {
-        LWJGLInMemoryMeshVAO mesh = new LWJGLInMemoryMeshVAO(shader, vertices, indicies);
+        LWJGLInMemoryMeshVAO mesh = new LWJGLInMemoryMeshVAO(vertices, indicies);
         return mesh;
     }
 

File src/main/java/cc/plural/ecs/provider/LWJGLRendererGL2.java

 import static cc.plural.ecs.renderer.GLSLVersion.GLSL120;
 import static cc.plural.ecs.renderer.GLSLVersion.GLSL130;
 import static cc.plural.ecs.renderer.GLSLVersion.GLSL140;
+import static cc.plural.ecs.renderer.GLSLVersion.GLSL430;
+import cc.plural.ecs.renderer.Mesh;
 import cc.plural.ecs.renderer.Shader;
 import cc.plural.ecs.renderer.ShaderDefaults;
 import cc.plural.ecs.renderer.Texture;
 import cc.plural.ecs.renderer.Uniform;
+import cc.plural.graphics.Vertex;
 import cc.plural.math.Transformation;
+import cc.plural.utils.ArrayPrinter;
 import cc.plural.utils.MatrixPrinter;
+import cc.plural.utils.PNGDecoder;
+import java.io.IOException;
 import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+import java.nio.ShortBuffer;
+import org.lwjgl.BufferUtils;
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL13;
+import org.lwjgl.opengl.GL15;
 import org.lwjgl.opengl.GL20;
 import org.lwjgl.opengl.GL30;
+import org.lwjgl.util.vector.Vector3f;
 
 public class LWJGLRendererGL2 extends LWJGLRenderer {
 
-    float[] test = new float[]{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
-
     public LWJGLRendererGL2(LWJGLDisplay display) {
         super(display);
     }
 
+    @Override
     public void init() {
         GL11.glClearColor(backgroundColor.getRed() / 255F, backgroundColor.getGreen() / 255F, backgroundColor.getBlue() / 255F, 1.0f);
 
                 break;
             case GLSL140:
                 if (defaultShader == null) {
-                    createDefaultShader(ShaderDefaults.DEFAULT_VERTEX_SHADER_130, ShaderDefaults.DEFAULT_FRAGMENT_SHADER_130);
+                    createDefaultShader(ShaderDefaults.DEFAULT_VERTEX_SHADER_140, ShaderDefaults.DEFAULT_FRAGMENT_SHADER_140);
                 }
                 break;
             case GLSL150:
                 if (defaultShader == null) {
-                    createDefaultShader(ShaderDefaults.DEFAULT_VERTEX_SHADER_130, ShaderDefaults.DEFAULT_FRAGMENT_SHADER_130);
+                    createDefaultShader(ShaderDefaults.DEFAULT_VERTEX_SHADER_150, ShaderDefaults.DEFAULT_FRAGMENT_SHADER_150);
+                }
+                break;
+            case GLSL330:
+            case GLSL400:
+            case GLSL410:
+            case GLSL420:
+            case GLSL430:
+                if (defaultShader == null) {
+                    createDefaultShader(ShaderDefaults.DEFAULT_VERTEX_SHADER_150, ShaderDefaults.DEFAULT_FRAGMENT_SHADER_150);
                 }
                 break;
+
             default:
-                throw new RuntimeException("Dont Support " + GLSLVersion.GLSL120);
+                if (getGLSLVersion().getVersion() > GLSL430.getVersion()) {
+                    createDefaultShader(ShaderDefaults.DEFAULT_VERTEX_SHADER_150, ShaderDefaults.DEFAULT_FRAGMENT_SHADER_150);
+                }
+
+                throw new RuntimeException("Dont Support " + getGLSLVersion());
         }
 
         if (!defaultShader.isInitialized()) {
         if (defaultShader.inError()) {
             throw new RuntimeException("Default Shader No Compile: " + defaultShader.getVertexCompileError() + " " + defaultShader.getFragmentCompileError() + " " + defaultShader.getLinkError());
         }
+
+//        ecsProjectionMatrix = new Matrix4f();
+//        ecsViewMatrix = Matrix4f.identity();
+//        ecsModelMatrix = Matrix4f.identity();
+
+        /* **
+         * Debug
+         */
+        //setupShaders();
+        //shader = getDefaultShader();
+        //setupTextures();
+        //setupQuad();
     }
 
     public StringLWJGLShader createShader(String vertexSource, String fragmentSource) {
         defaultShader = createShader(vertexSource, fragmentSource);
         return defaultShader;
     }
-    
+
     public StringLWJGLShader createDefaultShader(URL vertexURL, URL fragmentURL) {
         if (defaultShader != null) {
             defaultShader.release();
             defaultShader.load();
         }
 
-        synchronized (this) {
-            if (skipRender) {
-                batch.clear();
-                lock = false;
-                return;
-            }
+        if (skipRender) {
+            batch.clear();
+            lock = false;
+            return;
+        }
 
-            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
+        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
 
-            GL20.glUseProgram(defaultShader.programHandle);
+        projection.load(projectionMatrix);
 
-            Uniform projectionMatrixUniform = defaultShader.getUniform("projectionMatrix");
-            Uniform viewMatrixUniform = defaultShader.getUniform("viewMatrix");
-            Uniform modelMatrixUniform = defaultShader.getUniform("modelMatrix");
+        camera.load(viewMatrix);
 
-            /**
-             * @todo move this to be global.
-             */
-            float[] mat4x4 = new float[16];
-            projection.load(mat4x4);
+        for (GameObject gameObject : batch.queue) {
 
-            if (logger.isDebugEnabled()) {
-                logger.debug("Projection: \n" + MatrixPrinter.matrix2String(MatrixPrinter.MAJOR.ROW, mat4x4));
+            if (gameObject.geometry == null || !gameObject.geometry.isValid()) {
+                continue;
+            }
+            if (gameObject.spatial == null) {
+                continue;
             }
 
-            mat4x4Buffer.put(mat4x4);
-            mat4x4Buffer.flip();
-            projectionMatrixUniform.setUniformMatrix4f(true, mat4x4Buffer);
-
-            //viewMatrix.get(mat4x4);
-            //System.out.println("ViewMatrix:\n" + MatrixPrinter.matrix2String(MatrixPrinter.MAJOR.ROW, test));
-            //viewMatrix.setColumnMajorBuffer(mat4x4Buffer);
-            mat4x4Buffer.put(test);
-            mat4x4Buffer.flip();
-            viewMatrixUniform.setUniformMatrix4f(true, mat4x4Buffer);
-
-            //modelMatrix.get(mat4x4);
-            //System.out.println("ModelMatrix:\n" + MatrixPrinter.matrix2String(MatrixPrinter.MAJOR.ROW, test));
-            //modelMatrix.setColumnMajorBuffer(mat4x4Buffer);
-            GL20.glUseProgram(0);
-
-            for (GameObject gameObject : batch.queue) {
-                //LogManager.getLogger(LWJGLRenderer.class).info("Render:" + gameObject);
-
-                if (gameObject.geometry == null || !gameObject.geometry.isValid()) {
-                    continue;
-                }
-                if (gameObject.spatial == null) {
-                    continue;
-                }
-
-                if (gameObject.geometry.shader != null) {
-                    GL20.glUseProgram(gameObject.geometry.shader.programHandle);
-                } else {
-                    GL20.glUseProgram(defaultShader.programHandle);
-                }
-
-//                gameObject.spatial.crossoverLocalToWorld();
-//                Transformation transformation = gameObject.spatial.world;
-                Transformation transformation = gameObject.getWorld();
-                transformation.get(mat4x4);
-
-                if (logger.isDebugEnabled()) {
-                    logger.debug("View: \n" + MatrixPrinter.matrix2String(MatrixPrinter.MAJOR.ROW, mat4x4));
-                }
+            Shader shader = getDefaultShader();
+            Texture texture = gameObject.geometry.texture;
+            Mesh mesh = gameObject.geometry.mesh;
 
-                mat4x4Buffer.put(mat4x4);
-                mat4x4Buffer.flip();
-                modelMatrixUniform.setUniformMatrix4f(true, mat4x4Buffer);
+            Transformation transformation = gameObject.getWorld();
+            transformation.load(modelMatrix);
 
-                GL13.glActiveTexture(GL13.GL_TEXTURE0);
+            texture.enable();
+            {
+                shader.enable();
+                {
+                    shader.setUniform(shader.getUniformLocation("projectionMatrix"), projectionMatrix);
+                    shader.setUniform(shader.getUniformLocation("viewMatrix"), viewMatrix);
+                    shader.setUniform(shader.getUniformLocation("modelMatrix"), modelMatrix);
 
-                Texture texture = gameObject.geometry.texture;
-                if (!texture.isLoaded()) {
-                    texture.load();
+                    mesh.enable(shader);
+                    GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndicesCount(), GL11.GL_UNSIGNED_SHORT, 0);
+                    mesh.disable(shader);
                 }
-                GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.textureHandle);
-
-//                GL30.glBindVertexArray(gameObject.geometry.mesh.vaoHandle);
-//                GL20.glEnableVertexAttribArray(0);
-//                GL20.glEnableVertexAttribArray(1);
-//                GL20.glEnableVertexAttribArray(2);
-//
-//                GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, gameObject.geometry.mesh.eboHandle);
-//
-//                GL11.glDrawElements(GL11.GL_TRIANGLES, gameObject.geometry.mesh.elementCount, GL11.GL_UNSIGNED_BYTE, 0);
-//
-//                GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
-                GL20.glDisableVertexAttribArray(0);
-                GL20.glDisableVertexAttribArray(1);
-                GL20.glDisableVertexAttribArray(2);
-                GL30.glBindVertexArray(0);
-
-                GL20.glUseProgram(0);
-
+                shader.disable();
             }
-            batch.clear();
-            lock = false;
+            texture.disable();
         }
 
+        batch.clear();
+        lock = false;
     }
-
 }

File src/main/java/cc/plural/ecs/renderer/Mesh.java

 
     public abstract void enable();
     
+    public abstract void enable(Shader shader);
+    
     public abstract void disable();
     
+    public abstract void disable(Shader shader);
+    
     public String dumpState() {
         StringBuilder state = new StringBuilder();
         state.append(getClass().getCanonicalName()).append('\n');

File src/main/java/cc/plural/ecs/runtime/RuntimeApplication.java

         tank.setScale(2);
         tank.updateWorldTransforms();
         
-        tank.getWorld().get(mat4x4);
+        tank.getWorld().load(mat4x4);
         System.out.println(MatrixPrinter.matrix2String(MatrixPrinter.MAJOR.ROW, mat4x4));
 
 //        Node node1 = engine.createNode("Node 1");

File src/main/java/cc/plural/math/Transformation.java

         rotation = Matrix3f.identity();
     }
 
-    public float[] get(float[] mat4x4) {
+    public float[] load(float[] mat4x4) {
         mat4x4[0] = scale.x * rotation.m00;
         mat4x4[1] = scale.y * rotation.m01;
         mat4x4[2] = scale.z * rotation.m02;
         return mat4x4;
     }
 
-    public Matrix4f get(Matrix4f transformation) {
+    public Matrix4f load(Matrix4f transformation) {
         transformation.mat4x4[0] = scale.x * rotation.m00;
         transformation.mat4x4[1] = scale.y * rotation.m01;
         transformation.mat4x4[2] = scale.z * rotation.m02;
         translation.setData(0, 0, 0);
     }
 
-    public void set(Transformation t) {
+    public void store(Transformation t) {
         rotation.setData(t.rotation);
         scale.setData(t.scale);
         translation.setData(t.translation);