0
votes

before showing my code I want to explain the Situation a littlze bit. I am trying to make a FlappyBird clone just for practice using Lwjgl2. Right now I am able to create a textured Quad which can move ONLY in x and y direction and rotate around all the axis. I was about to set up the projectionMatrix so I can also have 3D movement and the Z axis work. I followed a tutorial on youtube, doing the exact same things but it somehow does not work for me at all.

When trying to move the Object without using the projectionMatrix, it vanishes at soon as Z > 1 or Z < -1 for some reason, although nothing should happen. As soon as I add the projectionMatrix inside the vertexShader it vanishes for every coordinate I give to be rendered at... it just disappears into the void.

Here is all the relevant code:

Model model = loader.loadToVAO(vertices, textureCoords, indices);
    ModelTexture texture = new ModelTexture(loader.loadTexture("Flappy2")); 
    TexturedModel texturedModel = new TexturedModel(model, texture);        
    Entity entity = new Entity(texturedModel, new Vector3f(0,0,0),0,0,0,1 );

    float y = 0;
    float x = 0;


    //Main loop which renders stuff
    while(!Display.isCloseRequested()) {                        
        while(Keyboard.next()) {
            if (Keyboard.getEventKey() == Keyboard.KEY_SPACE) {
                if (Keyboard.getEventKeyState()) {
                    y = 0.1f;
                }    
            }
        }
        y -= 0.005f;

        entity.increasePosition(x, y, 0);
        entity.increaseRotation(0, 0,0 );
        renderer.prepare();
        shader.start();
        renderer.render(entity, shader);
        shader.stop();

        DisplayManager.updateDisplay();
    }
    shader.cleanUp();
    loader.cleanUp();
    DisplayManager.closeDisplay();

}

This is the main loop, nothing special.

#version 400 core           

in vec3 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;
out vec3 color;

uniform mat4 transformationMatrix; uniform mat4 projectionMatrix;

void main(void){

gl_Position = projectionMatrix * transformationMatrix * vec4(position,1.0);     
pass_textureCoords = textureCoords;     
color = vec3(position.x +0.5,0.0,position.y+0.5);

} That was the vertex Shader.

private void createProjectionMatrix() {
    float aspectRatio = (float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight();
    float y_scale = (float)(1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio;
    float x_scale = y_scale / aspectRatio;
    float frustum_length = FAR_PLANE - NEAR_PLANE;

    projectionMatrix = new Matrix4f();
    projectionMatrix.m00 = x_scale;
    projectionMatrix.m11 = y_scale;
    projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
    projectionMatrix.m23 = -1;
    projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
    projectionMatrix.m33 = 0;
}

Here I set up the projectionMatrix in the Rendering class. As I said, most of it is copied from a youtube tutorial, as I am new to LWJGL2. So if it works for him why does it not for me ?

1

1 Answers

0
votes

I tried copying the entire tutorial code, instead of just typing it myself and it did somehow fix my problem.

I probably had switched variable names somewhere or small errors like that which prevented the projection Matrix from working.

No need to comment anymore :) Ignore this post