Hello Guest

LWJGL And Camera rotation

  • 1 Replies
  • 5951 Views
LWJGL And Camera rotation
« on: December 22, 2015, 20:31:11 »
Hi, i try to rotate camera by setLookAt, and set matrix to ml matrix but it doesn't work.
I try search in google, but i don't saw actual sample.

It's my camera code:
Code: [Select]
public class Camera {

    private Vector3f position;
    private Vector3f rotation;

    private Matrix4f viewMatrix;
    private Matrix4f positionMatrix;

    private final Vector3f xAxis =  new Vector3f( 1, 0, 0 );
    private final Vector3f yAxis = new Vector3f( 0, 1, 0 );
    private final Vector3f up     = new Vector3f( 0, 1, 0 );
    private final Vector3f zAxis = new Vector3f( 0, 0, 1 );

    Shader shader;



    public Camera(Vector3f position) {
        this.position = position;
        this.rotation = new Vector3f();
        this.viewMatrix = new Matrix4f();
        this.positionMatrix = new Matrix4f();
        build();
    }

    public void build() {
        glActiveTexture(GL_TEXTURE1);
        glEnable(GL_DEPTH_TEST);

        shader = ShaderManager.getDefaultShader();
        shader.enable();
        Matrix4f perspectiveMatrix = new Matrix4f();
        perspectiveMatrix.perspective(1, Syswow.graphics.getAspectRatio(), -10, 0);
        positionMatrix.identity();
        positionMatrix.translate(position);
        shader.setUniformMat4f("pr_matrix",  perspectiveMatrix);
        shader.setUniformMat4f("vw_matrix", positionMatrix);
        shader.setUniform1i("tex", 1);
        shader.disable();
    }

    public void update (float deltaTime) {

    }

    public void render() {
        shader.enable();
        positionMatrix.identity();
        positionMatrix.translate(position);
        viewMatrix.setLookAt(rotation, position, up);
        shader.setUniformMat4f("vw_matrix", positionMatrix);
        shader.setUniformMat4f("ml_matrix", viewMatrix);

        shader.disable();
    }

    public Vector3f getPosition() {
        return position;
    }

    public void setPosition(Vector3f position) {
        this.position = position;
    }


    public void setFPSRotation(double xpos, double ypos) {

        xpos = xpos - Syswow.graphics.getWindowWight() / 2;
        Syswow.debug.log("xpos" + xpos );
        xpos = xpos / 100;

        rotation.x = rotation.x + (float)xpos;
        rotation.y = rotation.y + (float)ypos;


        if(rotation.x >= 360.0f || rotation.x <= -360.0f)
            rotation.x = rotation.x % 360.0f;

        if(rotation.y >= 360.0f || rotation.y <= -360.0f)
            rotation.y = rotation.y % 360.0f;

        if(rotation.z >= 360.0f || rotation.z <= -360.0f)
            rotation.z = rotation.z % 360.0f;

        if(rotation.x <= -90.0f)
            rotation.x = -90.0f;
        else if(rotation.x >= 90.0f)
            rotation.x = 90.0f;

        Syswow.debug.log(rotation.x + " " + rotation.y + " " + rotation.z);
    }
}

my shader code:
Code: [Select]
#version 330 core

layout ( location = 0 ) in vec4 position;
layout ( location = 1 ) in vec2 tc;

uniform mat4 pr_matrix;
uniform mat4 vw_matrix;
uniform mat4 ml_matrix = mat4(1.0);

out DATA
{
vec2 tc;
} vs_out;

void main()
{
gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
vs_out.tc = tc;
}

*

Kai

Re: LWJGL And Camera rotation
« Reply #1 on: December 22, 2015, 21:00:12 »
Hi,
You can have a look at the joml-lwjgl3-demos repository. That should get you covered with how to setup matrices and upload them to OpenGL via LWJGL 3.
Other than that there are many awkward things happening in your code. :-)
First of all, a negative value like your -10 as the near clipping plane distance for perspective/setPerspective is actually invalid/wrong, as is 0 for the far clipping plane distance; though the JavaDocs do not explicitly mention that. But have instead a look at: https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml
Then using 1 as the field of view, which gives you an angle of about 57 degrees is probably fine, though unintuitive.
Next, you seem to somehow assume that the first parameter to setLookAt/lookAt is somewhat (Euler?) rotation angles, which it is not. It's basically the position of the camera, and the second parameter is meant to be the point which the camera is looking at. See this for more info: https://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml