0
\$\begingroup\$

Here is what my rotation matrix looks like:

public Matrix4f initRotation(float x, float y, float z)
{
    Matrix4f rx = new Matrix4f();
    Matrix4f ry = new Matrix4f();
    Matrix4f rz = new Matrix4f();

    x = (float)Math.toRadians(x);
    y = (float)Math.toRadians(y);
    z = (float)Math.toRadians(z);

    rz.m[0][0] = (float)Math.cos(z);rz.m[0][1] = -(float)Math.sin(z);rz.m[0][2] = 0;                rz.m[0][3] = 0;
    rz.m[1][0] = (float)Math.sin(z);rz.m[1][1] = (float)Math.cos(z);rz.m[1][2] = 0;                 rz.m[1][3] = 0;
    rz.m[2][0] = 0;                 rz.m[2][1] = 0;                 rz.m[2][2] = 1;                 rz.m[2][3] = 0;
    rz.m[3][0] = 0;                 rz.m[3][1] = 0;                 rz.m[3][2] = 0;                 rz.m[3][3] = 1;

    rx.m[0][0] = 1;                 rx.m[0][1] = 0;                 rx.m[0][2] = 0;                 rx.m[0][3] = 0;
    rx.m[1][0] = 0;                 rx.m[1][1] = (float)Math.cos(x);rx.m[1][2] = -(float)Math.sin(x);rx.m[1][3] = 0;
    rx.m[2][0] = 0;                 rx.m[2][1] = (float)Math.sin(x);rx.m[2][2] = (float)Math.cos(x);rx.m[2][3] = 0;
    rx.m[3][0] = 0;                 rx.m[3][1] = 0;                 rx.m[3][2] = 0;                 rx.m[3][3] = 1;

    ry.m[0][0] = (float)Math.cos(y);ry.m[0][1] = 0;                 ry.m[0][2] = -(float)Math.sin(y);ry.m[0][3] = 0;
    ry.m[1][0] = 0;                 ry.m[1][1] = 1;                 ry.m[1][2] = 0;                 ry.m[1][3] = 0;
    ry.m[2][0] = (float)Math.sin(y);ry.m[2][1] = 0;                 ry.m[2][2] = (float)Math.cos(y);ry.m[2][3] = 0;
    ry.m[3][0] = 0;                 ry.m[3][1] = 0;                 ry.m[3][2] = 0;                 ry.m[3][3] = 1;

    m = rz.mult(ry.mult(rx)).getM();

    return this;
}

And here is how I've been implementing it for tests (using default orthographic perspective):

public class Renderer {

private WorldMesh mesh;
private Mesh _mesh;


private Matrix4f transform;

private Shader shader;

public Renderer(){

//Setup Meshes

    mesh = new WorldMesh();



    _mesh = ResLoader.loadMesh("pyramid.obj");



    mesh.addToBuffers(_mesh);

//setup Shader

    shader = new Shader();

    shader.addVertexShader(ResLoader.loadShader("basicVertex.vs"));
    shader.addFragmentShader(ResLoader.loadShader("basicFragment.fs"));
    shader.compileShader();

    shader.addUniform("transformation");

//temporary transformation matrix

    Matrix4f tran = new Matrix4f().initTranslation(0, 0, 0);
    Matrix4f rot = new Matrix4f().initRotation(0, 0, 0);     //problem is definitely with the rotation matrix;
    Matrix4f scale = new Matrix4f().initScale(1, 1, 1);


    transform = tran.mult(rot.mult(scale));

}

public void update(){}

public void render(){

    RenderUtils.clearScreen();

    shader.setUniform("transformation", transform);

    shader.bind();

    mesh.draw(true);
}

}

and finally here is my vertex shader:

#version 330

layout (location = 0) in vec3 position;

uniform mat4 transformation;

void main(){


    gl_Position = transformation * vec4(position, 1.0);
}

The problem is that setting a rotation on any other axis other than Z (for some reason z axis rotation is working fine) does not function properly. An X axis rotation seems to be doing both an x and y rotation at the same time, causing a weird stretching affect. A Y axis rotation causes the mesh to completely disappear.

The funny thing is that I've used this code over and over again and some projects don't give me a problem.

The order of multiplication seems in order, and I made sure that OpenGL knows to transpose the matrix I give it when setting the uniform.

Any ideas as to what could be causing the x and y axis rotations to completely fail?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Ok the problem turned out to be a typo in the way meshes were being loaded. It caused the Z terms of each vertex to be resolved to the X term... If anyone comes across this and wants to know here is my properly working mesh loader method.

public static Mesh loadMesh(String fileName){

    String[] splitArray = fileName.split("\\.");
    String extension = splitArray[splitArray.length -1];

    if(!extension.equals("obj")){
        System.err.println("Model file type not supported: " + extension);
        System.exit(1);
    }

    ArrayList<Vertex> vertices = new ArrayList<Vertex>();
    ArrayList<Integer> indices = new ArrayList<Integer>();

    BufferedReader modelReader = null;

    try{

        modelReader = new BufferedReader(new FileReader("./res/models/" + fileName));
        String line;

        while((line = modelReader.readLine()) != null){
            String[] tokens = line.split(" ");
            tokens = Utils.removeEmptyStrings(tokens);

            if((tokens.length == 0) || tokens[0].equals("#")){
                continue;
            }
            else if (tokens[0].equals("v")){
                vertices.add(new Vertex(new Vector3f(Float.valueOf(tokens[1]),
                                                     Float.valueOf(tokens[2]),
                                                     Float.valueOf(tokens[3]))));
            }
            else if (tokens[0].equals("f")){
                indices.add(Integer.parseInt(tokens[1]) -1);
                indices.add(Integer.parseInt(tokens[2]) -1);    
                indices.add(Integer.parseInt(tokens[3]) -1);
            }


        }

        modelReader.close();


    }catch(Exception e){
        e.printStackTrace();
        System.out.println("Model Failed to Load" + fileName);
        System.exit(1);
    }


    Vertex[] vertexData = new Vertex[vertices.size()];
    vertices.toArray(vertexData);

    Integer[] indexData = new Integer[indices.size()];
    indices.toArray(indexData);

    Mesh res = new Mesh(vertexData, Utils.toIntArray(indexData));


    return res;
}
\$\endgroup\$
0
\$\begingroup\$

Can't comment yet so I post it as an answer, but it appears that you should have a - at ry.m[2][0] and + at ry.m[0][2] when looking at the 3d rotation matrices. Does not make a difference though for an x-only rotation as both of these evaluate to 0.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .