How to render more than 1 cube in lwjgl3

Viewed 16

I am new to lwjgl 3 even in stackoverflow... i am trying to make a minecraft demo and following a nice tutorial on it https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter01/chapter1.html. my code is very different from source code.I am not able to render more than one object using different translation Matrix I have search about it a lot and found that we have first bind the shader than a for loop, than we have to create a translation matrix and set uniforms i am doing the same but not get proper result. The two translation matrix get plus and render only 1 object please help me.

My render Class

package org.GameEngine;
import org.joml.Matrix4f;
import java.awt.image.BufferedImage;
import java.util.Objects;
import static org.lwjgl.opengl.GL11.*;

public class Render {
    ShaderProgram shaderProgram;
        Transformation Matrix;
        float FOV = (float) Math.toRadians(60.0f);
        float Z_near = 0.01f;
        float Z_far = 1000.0f;

        private  String True = "true";

        private final Camera camera = new Camera();
        private static GameItem gameItem;

        private  String True2 = "true";

        private Mesh mesh;
        Matrix4f projection;
        private String Pass = "true";

        public Render(){
            shaderProgram = new ShaderProgram();
            Matrix = new Transformation();
            shaderProgram.createFragmentShader(Utils.readFile("D:\\Honey\\Best\\src\\main\\resources\\Frag.frag"));
            shaderProgram.createVertexShader(Utils.readFile("D:\\Honey\\Best\\src\\main\\resources\\vetrex.vert"));
            shaderProgram.link();
            Vertex vertex = new Vertex();
            mesh = new Mesh(vertex.vertex, vertex.index, vertex.texCoords);
            projection = new Matrix4f();
            projection = Transformation.setProjection(FOV, Window.getWidth(), Window.getHeight(), Z_near, Z_far);
            //Camera.setTranslate();
        }
        public void setTexture(){
            glEnable(GL_TEXTURE_2D);
            BufferedImage image = Texture.loadImage("D:\\Honey\\Best\\src\\main\\resources\\GrassBlock.png");
            assert image != null;
            int texId =  Texture.loadTexture(image);
        }

        public void setMatrix(){
            setProjection();
            ShaderProgram.createUniform("texture_sampler");
            shaderProgram.setUniforms("texture_sampler", 0);
        }
    public void setProjection(){
            ShaderProgram.createUniform("projection");
            shaderProgram.setUniform("projection", projection);
        }

        public void Renderer(long window){
            getShader().bind();
            GameItem gameItem1 = new GameItem(mesh);
            gameItem1.setPosition(0, 0.04f, -1f);
            GameItem gameItem2 = new GameItem(mesh);
            gameItem2.setPosition(-0.04f, 0, -1f);
            GameItem[] gameItems = new GameItem[]{gameItem1, gameItem2};
            for(GameItem gameItem: gameItems){
                    setMatrix();
                    Matrix4f WorldMatrix = Transformation.setWorldMatrix(
                            gameItem.getPosition(),
                            gameItem.getRotation(),
                            gameItem.getScale()
                    );
                ShaderProgram.setUniform("WorldMatrix", WorldMatrix);
                    gameItem.getMesh().render();
            }
            getShader().unbind();
        }
    public ShaderProgram getShader(){
            return shaderProgram;
     }
}

Transformation

package org.GameEngine;
import org.joml.Matrix4f;
import org.joml.Vector3f;

public class Transformation {
        private static Matrix4f projection;
        private static Matrix4f WorldMatrix;
        private static Matrix4f viewMatrix;
        private static Matrix4f translation;

    public Transformation(){
            projection = new Matrix4f();
            WorldMatrix = new Matrix4f();
            viewMatrix = new Matrix4f();
            translation = new Matrix4f();
        }
        public static Matrix4f setProjection(float FOV, int width, int height, float Z_near, float Z_far){
            float aspect = (float) width/height;
            projection.perspective(FOV, aspect, Z_near, Z_far);
            return projection;
        }
        
        public static Matrix4f setWorldMatrix( Vector3f offset,Vector3f rotation, float Scale){
            WorldMatrix.translate(offset).
                    rotateX((float) Math.toRadians(rotation.x)).
                    rotateY((float) Math.toRadians(rotation.y)).
                    rotateZ((float) Math.toRadians(rotation.z)).
                    scale(Scale);
            return WorldMatrix;
        }
        public static Matrix4f getViewMatrix(){
            return viewMatrix;
        }
        public static Matrix4f getWorldMatrix(){
            return WorldMatrix;
        }
    public static void setViewMatrix(){
            Camera camera = new Camera();
            Vector3f cameraPos = camera.getPosition();
            Vector3f Rotation = camera.getRotation();

            viewMatrix.identity();
            viewMatrix.rotate((float) Math.toRadians(Rotation.x), new Vector3f(1, 0, 0)).
                    rotate((float) Math.toRadians(Rotation.y), new Vector3f(0, 1, 0)).
                    rotate((float) Math.toRadians(Rotation.z), new Vector3f(0, 0,1));
            viewMatrix.translate(-cameraPos.x, -cameraPos.y,  -cameraPos.x);
        }
        
}

GameItem

package org.GameEngine;

import org.joml.Vector3f;

public class GameItem {
        Vector3f rotation;
     Vector3f Position;
        Mesh mesh;
        float Scale;

        public GameItem(Mesh mesh){
            this.mesh = mesh;
            rotation = new Vector3f(0, 0, 0);
            Position = new Vector3f(0, 0,0);
            Scale = 1;
        }
        public void setPosition(float x, float y, float z){
            Position.x = x;
            Position.y = y;
            Position.z = z;
        }
        public void setRotation(float x, float y, float z){
            rotation.x = x;
            rotation.y = y;
            rotation.z = z;
        }
        public Vector3f getPosition(){
            return Position;
        }
        public Vector3f getRotation(){
            return rotation;
        }

        public Mesh getMesh(){
            return mesh;
     }
        public float getScale(){
            return Scale;
     }
        public void setScale(float scale){
            Scale = scale;
        }
}

How we can render 2 object using 2 different TranslationMatrix ?

0 Answers
Related