1. John Marsden
  2. ECSExamples

Commits

John Marsden  committed ed82b87

First working version in years

  • Participants
  • Parent commits d3eefab
  • Branches master

Comments (0)

Files changed (4)

File src/main/java/cc/plural/dev/TestECSBasic.java

View file
         if (logger.isInfoEnabled()) {
             logger.info(rendererDump);
         }
-        Shader shader = renderer.getDefaultShader();
-        String shaderDump = shader.getState();
-        if (logger.isInfoEnabled()) {
-            logger.info(shaderDump);
-        }
 
         float left = 0;
-        float right = 1;
-        float top = 1;
+        float right = 6;
+        float top = 6;
         float bottom = 0;
         float near = 0;
         float far = 1;
 
         renderer.setOrthoProjection(left, right, top, bottom, near, far);
-//        Projection projection = renderer.getProjection();
-//        cc.plural.math.Matrix4f projectionDebug = new cc.plural.math.Matrix4f();
-//        projection.load(projectionDebug);
-//        System.out.println("Debug: ECS Othogonal Projection Matrix: \n" + MatrixPrinter.matrix2String(projectionDebug));
-
+        
         engine = new Engine();
         GameObject testObject = engine.createGameObject("Test Game Object");
 
         testObject.geometry.mesh.load();
 
         testObject.createSpatial();
-        testObject.spatial.setTranslation(0, 0);
-        //testObject.spatial.setTranslation(2, 1);
+        testObject.spatial.setTranslation(3, 3);
 
         RotateGameObject rotateGameObject = new RotateGameObject();
         testObject.registerComponent(rotateGameObject);

File src/main/java/cc/plural/test/FullPipelineECS3MatrixProjectionShader130.java

View file
         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW);
 
         // Put the position coordinates in attribute list 0
-        GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
+        //GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
         // Put the color components in attribute list 1
-        GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
+        //GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
         // Put the texture coordinates in attribute list 2
-        GL20.glVertexAttribPointer(shader.getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
+        //GL20.glVertexAttribPointer(shader.getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
 
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
 
         /**
          * ECS Setup Quad
          */
-        mesh = new LWJGLInMemoryMeshVAO(shader, vertices, indices);
+        mesh = new LWJGLInMemoryMeshVAO(vertices, indices);
         mesh.init();
         mesh.load();
 
 
         SpatialComponent spatial = new SpatialComponent();
         ecsModelMatrix = new cc.plural.math.Matrix4f();
-        spatial.get(ecsModelMatrix);
+        spatial.load(ecsModelMatrix);
         System.out.println("ECS Model Matrix: \n" + MatrixPrinter.matrix2String(ecsViewMatrix));
     }
 

File src/main/java/cc/plural/test/ecs/TestECSRuntimeApplication.java

View file
                 ClassLoader.class.getResource("/assets/shaders/vertexDefault130.glsl"),
                 ClassLoader.class.getResource("/assets/shaders/fragmentDefault130.glsl"));
 
-        testObject.geometry.shader = renderer.getDefaultShader();
+        //testObject.geometry.shader = renderer.getDefaultShader();
         testObject.geometry.mesh = renderer.createStaticMesh(renderer.getDefaultShader(), new Vertex[]{v0, v1, v2, v3}, new short[]{0, 1, 2, 2, 3, 0});
         testObject.geometry.mesh.init();
         testObject.geometry.mesh.load();
 
         testObject.createSpatial();
-        testObject.spatial.setTranslation(2, 1);
+        testObject.spatial.setTranslation(1, 1);
 
         engine.dumpState();
     }

File src/main/java/cc/plural/test/provider/TestRenderer.java

View file
 package cc.plural.test.provider;
 
-import cc.plural.ecs.component.spatial.SpatialComponent;
 import cc.plural.ecs.engine.GameObject;
 import cc.plural.ecs.provider.FileLWJGLTexture;
 import cc.plural.ecs.provider.LWJGLDisplay;
 import cc.plural.ecs.provider.LWJGLInMemoryMeshVAO;
 import cc.plural.ecs.provider.LWJGLRenderer;
 import cc.plural.ecs.provider.StringLWJGLShader;
-import cc.plural.ecs.renderer.Camera;
 import cc.plural.ecs.renderer.GLSLVersion;
 import static cc.plural.ecs.renderer.GLSLVersion.GLSL430;
 import cc.plural.ecs.renderer.Mesh;
-import cc.plural.ecs.renderer.OrthogonalProjection;
 import cc.plural.ecs.renderer.Shader;
 import cc.plural.ecs.renderer.ShaderDefaults;
-import cc.plural.ecs.renderer.SimpleCamera;
 import cc.plural.ecs.renderer.Texture;
 import cc.plural.graphics.Vertex;
 import org.lwjgl.opengl.GL11;
 import cc.plural.math.Matrix4f;
+import cc.plural.math.Transformation;
 import cc.plural.utils.ArrayPrinter;
 import cc.plural.utils.MatrixPrinter;
 import cc.plural.utils.PNGDecoder;
 import java.nio.FloatBuffer;
 import java.nio.ShortBuffer;
 import org.lwjgl.BufferUtils;
-import org.lwjgl.input.Keyboard;
-import org.lwjgl.opengl.Display;
 import org.lwjgl.opengl.GL13;
 import org.lwjgl.opengl.GL15;
 import org.lwjgl.opengl.GL20;
  */
 public class TestRenderer extends LWJGLRenderer {
 
-    private int projectionMatrixLocation = 0;
-    private int viewMatrixLocation = 0;
-    private int modelMatrixLocation = 0;
 
-    private cc.plural.math.Matrix4f ecsProjectionMatrix = null;
-    private cc.plural.math.Matrix4f ecsViewMatrix = null;
-    private cc.plural.math.Matrix4f ecsModelMatrix = null;
+    boolean firstRenderCycle;
 
     /**
-     * **
      * Debug
      */
+    /**
+     * @deprecated
+     */
     private int vaoId = 0;
+    /**
+     * @deprecated
+     */
     private int vboId = 0;
+    /**
+     * @deprecated
+     */
     private int vboiId = 0;
+    /**
+     * @deprecated
+     */
     private int indicesCount = 0;
+    /**
+     * @deprecated
+     */
     private Vertex[] vertices = null;
+    /**
+     * @deprecated
+     */
     private ByteBuffer verticesByteBuffer = null;
     // Shader variables
+    /**
+     * @deprecated
+     */
     private int vsId = 0;
+    /**
+     * @deprecated
+     */
     private int fsId = 0;
+    /**
+     * @deprecated
+     */
     private int pId = 0;
-    private Shader shader;
+    /**
+     * @deprecated
+     */
+    private Shader testShader;
     // Texture variables
+    /**
+     * @deprecated
+     */
     private int[] texIds = new int[]{0, 0};
+    /**
+     * @deprecated
+     */
     private int textureSelector = 1;
 
     // LWJGL Objects
-    private org.lwjgl.util.vector.Matrix4f projectionMatrix = null;
-    private org.lwjgl.util.vector.Matrix4f viewMatrix = null;
-    private org.lwjgl.util.vector.Matrix4f modelMatrix = null;
+    /**
+     * @deprecated
+     */
     private Vector3f modelPos = null;
+    /**
+     * @deprecated
+     */
     private Vector3f modelAngle = null;
+    /**
+     * @deprecated
+     */
     private Vector3f modelScale = null;
+    /**
+     * @deprecated
+     */
     private Vector3f cameraPos = null;
 
+    /**
+     * @deprecated
+     */
     private cc.plural.math.Transformation ecsTransform = null;
+    /**
+     * @deprecated
+     */
     private FloatBuffer matrix44Buffer = null;
-    private LWJGLInMemoryMeshVAO mesh = null;
+    /**
+     * @deprecated
+     */
+    private LWJGLInMemoryMeshVAO testMesh = null;
+    /**
+     * @deprecated
+     */
     private Texture[] textures = null;
 
     public TestRenderer(LWJGLDisplay display) {
         super(display);
+        firstRenderCycle = true;
     }
 
     @Override
             throw new RuntimeException("Default Shader No Compile: " + defaultShader.getVertexCompileError() + " " + defaultShader.getFragmentCompileError() + " " + defaultShader.getLinkError());
         }
 
+//        ecsProjectionMatrix = new Matrix4f();
+//        ecsViewMatrix = Matrix4f.identity();
+//        ecsModelMatrix = Matrix4f.identity();
+
         /* **
          * Debug
          */
-        setupShaders();
+        //setupShaders();
         //shader = getDefaultShader();
-        setupTextures();
-        setupQuad();
-        setupMatrices();
-
+        //setupTextures();
+        //setupQuad();
     }
 
     public StringLWJGLShader createShader(String vertexSource, String fragmentSource) {
 
         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
 
-        shader.enable();
-        shader.setUniform(projectionMatrixLocation, ecsProjectionMatrix);
-        shader.setUniform(viewMatrixLocation, ecsViewMatrix);
-        shader.setUniform(modelMatrixLocation, ecsModelMatrix);
-        shader.disable();
+        projection.load(projectionMatrix);
 
-        projection.load(ecsProjectionMatrix);
+        camera.load(viewMatrix);
 
-        camera.load(ecsViewMatrix);
+        for (GameObject gameObject : batch.queue) {
 
-        for (GameObject go : batch.queue) {
+            if (gameObject.geometry == null || !gameObject.geometry.isValid()) {
+                continue;
+            }
+            if (gameObject.spatial == null) {
+                continue;
+            }
 
-//            Shader shader = go.geometry.shader;
-            Texture texture = go.geometry.texture;
-//            Mesh mesh = go.geometry.mesh;
+            Shader shader = getDefaultShader();
+            Texture texture = gameObject.geometry.texture;
+            Mesh mesh = gameObject.geometry.mesh;
 
-//            System.out.println("Shader:" + shader.getState());
-//            System.out.println("Texture:" + texture.toString());
-//            System.out.println("Mesh:" + mesh.dumpState());
-            ecsModelMatrix.setData(0, 3, 3);
-            ecsModelMatrix.setData(1, 3, 3);
+            Transformation transformation = gameObject.getWorld();
+            transformation.load(modelMatrix);
 
-//            System.out.println("ECS Othogonal Projection Matrix: \n" + MatrixPrinter.matrix2String(ecsProjectionMatrix));
-//            System.out.println("ECS View Matrix: \n" + MatrixPrinter.matrix2String(ecsViewMatrix));
-//            System.out.println("ECS Model Matrix: \n" + MatrixPrinter.matrix2String(ecsModelMatrix));
             texture.enable();
-
-            shader.enable();
-            shader.setUniform(projectionMatrixLocation, ecsProjectionMatrix);
-            shader.setUniform(viewMatrixLocation, ecsViewMatrix);
-            shader.setUniform(modelMatrixLocation, ecsModelMatrix);
-
-            mesh.enable();
-            GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndicesCount(), GL11.GL_UNSIGNED_SHORT, 0);
-            mesh.disable();
-
-            shader.disable();
-
+            {
+                shader.enable();
+                {
+
+                    if (firstRenderCycle) {
+                        System.out.println("First Render Cycle Debug");
+                        System.out.println("Shader:" + shader.getState());
+                        System.out.println("Texture:" + texture.toString());
+                        System.out.println("Mesh:" + mesh.dumpState());
+                        System.out.println("ECS Othogonal Projection Matrix: \n" + MatrixPrinter.matrix2String(projectionMatrix));
+                        System.out.println("ECS View Matrix: \n" + MatrixPrinter.matrix2String(viewMatrix));
+                        System.out.println("ECS Model Matrix: \n" + MatrixPrinter.matrix2String(modelMatrix));
+                        firstRenderCycle = false;
+                    }
+
+                    shader.setUniform(shader.getUniformLocation("projectionMatrix"), projectionMatrix);
+                    shader.setUniform(shader.getUniformLocation("viewMatrix"), viewMatrix);
+                    shader.setUniform(shader.getUniformLocation("modelMatrix"), modelMatrix);
+
+                    mesh.enable(shader);
+                    GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndicesCount(), GL11.GL_UNSIGNED_SHORT, 0);
+                    mesh.disable(shader);
+                }
+                shader.disable();
+            }
             texture.disable();
         }
 
         lock = false;
     }
 
-    private void logicCycle() {
-        //-- Input processing
-        float rotationDelta = 15f;
-        float scaleDelta = 0.01f;
-        float posDelta = 0.01f;
-        Vector3f scaleAddResolution = new Vector3f(scaleDelta, scaleDelta, scaleDelta);
-        Vector3f scaleMinusResolution = new Vector3f(-scaleDelta, -scaleDelta, -scaleDelta);
-
-        while (Keyboard.next()) {
-            // Only listen to events where the key was pressed (down event)
-            if (!Keyboard.getEventKeyState()) {
-                continue;
-            }
-
-            // Switch textures depending on the key released
-            switch (Keyboard.getEventKey()) {
-                case Keyboard.KEY_1:
-                    textureSelector = 0;
-                    break;
-                case Keyboard.KEY_2:
-                    textureSelector = 1;
-                    break;
-            }
-
-            // Change model scale, rotation and translation values
-            switch (Keyboard.getEventKey()) {
-                // Move
-                case Keyboard.KEY_UP:
-                    modelPos.y += posDelta;
-                    break;
-                case Keyboard.KEY_DOWN:
-                    modelPos.y -= posDelta;
-                    break;
-                // Scale
-                case Keyboard.KEY_P:
-                    Vector3f.add(modelScale, scaleAddResolution, modelScale);
-                    break;
-                case Keyboard.KEY_M:
-                    Vector3f.add(modelScale, scaleMinusResolution, modelScale);
-                    break;
-                // Rotation
-                case Keyboard.KEY_LEFT:
-                    modelPos.x -= posDelta;
-                    break;
-                case Keyboard.KEY_RIGHT:
-                    modelPos.x += posDelta;
-                    break;
-            }
-        }
-
-        modelAngle.z += 0.1;
-
-        //-- Update matrices
-        // Reset view and model matrices
-        viewMatrix = new org.lwjgl.util.vector.Matrix4f();
-
-        ecsModelMatrix.setData(0, 3, modelPos.x);
-        ecsModelMatrix.setData(1, 3, modelPos.y);
-
-        // Upload matrices to the uniform variables
-        shader.enable();
-        shader.setUniform(projectionMatrixLocation, ecsProjectionMatrix);
-        shader.setUniform(viewMatrixLocation, ecsViewMatrix);
-        shader.setUniform(modelMatrixLocation, ecsModelMatrix);
-        shader.disable();
-    }
-
     private void setupQuad() {
 
         /**
         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW);
 
         // Put the position coordinates in attribute list 0
-        GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
+        GL20.glVertexAttribPointer(getDefaultShader().getAttributeLocation("in_Position"), Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.POSITION_BYTE_OFFSET);
         // Put the color components in attribute list 1
-        GL20.glVertexAttribPointer(shader.getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
+        GL20.glVertexAttribPointer(getDefaultShader().getAttributeLocation("in_Color"), Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.COLOR_BYTE_OFFSET);
         // Put the texture coordinates in attribute list 2
-        GL20.glVertexAttribPointer(shader.getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
+        GL20.glVertexAttribPointer(getDefaultShader().getAttributeLocation("in_TextureCoord"), Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, Vertex.TEXTURE_BYTE_OFFSET);
 
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
 
         /**
          * ECS Setup Quad
          */
-        mesh = new LWJGLInMemoryMeshVAO(shader, vertices, indices);
-        mesh.init();
-        mesh.load();
-
-        System.out.println(mesh.dumpState());
-    }
-
-    private void setupShaders() {
-        shader = new StringLWJGLShader(
-                ClassLoader.class.getResource("/assets/shaders/vertexDefault130.glsl"),
-                ClassLoader.class.getResource("/assets/shaders/fragmentDefault130.glsl"));
-        shader.init();
-        shader.load();
+        testMesh = new LWJGLInMemoryMeshVAO(vertices, indices);
+        testMesh.init();
+        testMesh.load();
 
-        projectionMatrixLocation = shader.getUniformLocation("projectionMatrix");
-        viewMatrixLocation = shader.getUniformLocation("viewMatrix");
-        modelMatrixLocation = shader.getUniformLocation("modelMatrix");
+        System.out.println(testMesh.dumpState());
     }
 
+    /**
+     * @deprecated
+     */
     private void setupTextures() {
         texIds[0] = this.loadPNGTexture("/assets/images/grid1.png", GL13.GL_TEXTURE0);
         texIds[1] = this.loadPNGTexture("/assets/images/grid2.png", GL13.GL_TEXTURE0);
 
         return texId;
     }
-
-    private void setupMatrices() {
-        // Create a FloatBuffer with the proper size to store our matrices later
-        matrix44Buffer = BufferUtils.createFloatBuffer(16);
-
-        setupProjection();
-        setupView();
-        setupModel();
-    }
-
-    private void setupProjection() {
-        float[] output = new float[16];
-
-        // Setup projection matrix
-        float left = 0;
-        float right = 1;
-        float top = 1;
-        float bottom = 0;
-        float near = 0;
-        float far = 1;
-
-        /**
-         * LWJGL Setup Projection Matrix
-         */
-        projectionMatrix = new org.lwjgl.util.vector.Matrix4f();
-        projectionMatrix.m00 = 2f / (right - left);
-        projectionMatrix.m11 = 2f / (top - bottom);
-        projectionMatrix.m22 = -2f / (far - near);
-        projectionMatrix.m33 = 1f;
-
-        projectionMatrix.m30 = -((right + left) / (right - left));
-        projectionMatrix.m31 = -((top + bottom) / (top - bottom));
-        projectionMatrix.m32 = -((far + near) / (far - near));
-
-        projectionMatrix.store(matrix44Buffer);
-        matrix44Buffer.flip();
-        matrix44Buffer.get(output);
-        System.out.println("DEBUG Othogonal Projection Matrix: \n" + MatrixPrinter.glMatrix2String(output));
-
-        /**
-         * ECS Setup Projection Matrix
-         */
-        OrthogonalProjection projection = new OrthogonalProjection(left, right, top, bottom, near, far);
-        ecsProjectionMatrix = new cc.plural.math.Matrix4f();
-        projection.load(ecsProjectionMatrix);
-        System.out.println("ECS Othogonal Projection Matrix: \n" + MatrixPrinter.matrix2String(ecsProjectionMatrix));
-
-    }
-
-    private void setupView() {
-        float[] output = new float[16];
-        /**
-         * LWJGL Setup View Matrix
-         */
-        viewMatrix = new org.lwjgl.util.vector.Matrix4f();
-
-        matrix44Buffer.clear();
-        viewMatrix.store(matrix44Buffer);
-        matrix44Buffer.flip();
-
-        matrix44Buffer.get(output);
-        System.out.println("View Matrix: \n" + MatrixPrinter.glMatrix2String(output));
-        matrix44Buffer.flip();
-
-        /**
-         * ECS Setup Projection Matrix
-         */
-        Camera camera = new SimpleCamera();
-        ecsViewMatrix = new cc.plural.math.Matrix4f();
-        camera.load(ecsViewMatrix);
-        System.out.println("ECS View Matrix: \n" + MatrixPrinter.matrix2String(ecsViewMatrix));
-    }
-
-    private void setupModel() {
-        float[] output = new float[16];
-        /**
-         * LWJGL Setup Model Matrix
-         */
-        modelMatrix = new org.lwjgl.util.vector.Matrix4f();
-        modelMatrix.m30 = 0f;
-        modelMatrix.m31 = 0f;
-
-        matrix44Buffer.clear();
-        modelMatrix.store(matrix44Buffer);
-        matrix44Buffer.flip();
-
-        matrix44Buffer.get(output);
-        System.out.println("Model Matrix: \n" + MatrixPrinter.glMatrix2String(output));
-        matrix44Buffer.flip();
-
-        SpatialComponent spatial = new SpatialComponent();
-        ecsModelMatrix = new cc.plural.math.Matrix4f();
-        spatial.get(ecsModelMatrix);
-        System.out.println("ECS Model Matrix: \n" + MatrixPrinter.matrix2String(ecsViewMatrix));
-    }
-
 }