简体   繁体   中英

OpenGL VBO Cube not Rendering Correctly (Java, LWJGL)

Note: Apologies for links/images being nothing more than text - as a new user I can't post images and can't post >2 hyperlinks.

So I've been using deprecated OpenGL for some time, and decided a few weeks ago to finally make the jump to the more modern methods. I got quite far, using Open.gl as a resource (I found the LWJGL tutorials to be inconsistent and sparse), and am able to render up to this tutorial's last image . However, I run into some serious problems on the next page (rendering a cube).

I've done a lot of research on the issue and have refined/reorganised my code so many times so as to understand every component's purpose fully and ensure I've not missed something basic (like forgetting to flip buffers), and have only managed to narrow down the areas I believe the error could be occurring.

So, my basic goal is to be rendering a cube, using this example code: http://open.gl/content/code/c5_cube.txt Here is my code for comparison: http://pastie.org/5864112#241

The results I get from my code are as follows:

If I translate the View Matrix only in the Z direction, I get exactly the same as the image at the end of the last tutorial: https://dl.dropbox.com/u/38489921/vbo11.png

//set up view matrix - TODO: problem 1?
    Matrix4f view = new Matrix4f();
    view.setIdentity();
    view.translate(new Vector3f(0.0f, 0.0f, -2f)); 
    view.m13 = 1f;
    FloatBuffer viewSend = BufferUtils.createFloatBuffer(16);
    view.store(viewSend);
    viewSend.flip();
    int uniView = glGetUniformLocation(shaderProgram, "view");
    glUniformMatrix4(uniView, false, viewSend);

This could actually be my problem here - the example code uses glm::lookAt(), which I don't have access to, so I could just be doing the complete wrong thing with the matrix.

Moving on, if I translate the view Matrix any amount in the Y direction (code below image), I get a kind of distorted cube: https://dl.dropbox.com/u/38489921/vbo12.png

//set up view matrix - TODO: problem 1?
    Matrix4f view = new Matrix4f();
    view.setIdentity();
    view.translate(new Vector3f(0.0f, 1.0f, -2f)); 
    view.m13 = 1f;
    FloatBuffer viewSend = BufferUtils.createFloatBuffer(16);
    view.store(viewSend);
    viewSend.flip();
    int uniView = glGetUniformLocation(shaderProgram, "view");
    glUniformMatrix4(uniView, false, viewSend);

This led me to believe it was a problem with perspective projection, so I did a bit more research and ensured that my perspective() function is correct. Almost no changes to the projection implementation change the visual results, so instead I'll just highlight the function and how I use it here:

//set up projection matrix //TODO: problem 2?
    Matrix4f proj = perspective(90f, 600f / 800f, 1f, 10f);
    FloatBuffer projSend = BufferUtils.createFloatBuffer(16);
    proj.store(projSend);
    projSend.flip();
    int uniPersp = glGetUniformLocation(shaderProgram, "proj");
    glUniformMatrix4(uniPersp, false, projSend);

private Matrix4f perspective(float fov, float aspectRatio, float zNear, float zFar) {
    float zm = zFar - zNear;
    float zp = zFar + zNear;

    Matrix4f persp = new Matrix4f();

    persp.m00 = (1 / (float)Math.tan(Math.toRadians(fov / 2))) / aspectRatio;
    persp.m11 = 1 / (float)Math.tan(Math.toRadians(fov / 2));
    persp.m22 = -zp / zm;
    persp.m23 = -1;
    persp.m32 = -((2 * zNear * zFar) / zm);

    return persp;
}

This, in my opinion, is a very strange error to be getting, and I completely fail to understand it. After the mound of research to try and fix it myself, honestly it's gotten to the point where my brain hurts, so I thought I'd ask for some help.

My goal here is to ascertain what is causing this error, why, and how I would fix it (and, if necessary, any special tips or tricks I can use to ensure this type of error doesn't occur again). I'm appreciative of any help and will give any further information needed.

Thanks much, - Jonno.

Can't quite understand what is your problem. You say that when you translating your view matrix you get unexpected behaviour... Right?

I don't see any errors in your matrix setup, so, please, provide your GLSL shaders code and the moment when you declare your vertices information of the image (not image as a texture, but image as a 3D object in space). I guess there is something wrong with that (you can't just get a cube while you have only 4 vertices, so maybe you have more).

Also, just to help you, this is code for "Modern gluLookAt"

public Matrix4f set(float cameraX, float cameraY, float cameraZ, 
float lookX, float lookY, float lookZ){

//My advice to you: you shouldn't create new matrix everytime, 
//it will give you little glitches. 
//Create some new class containing this method, change type to void 
//and declare "matrix" as a field - make your own "camera" object (java object)

Matrix4f matrix = new Matrix4f();
Matrix4f aux = new Matrix4f();

Vector3f dir = new Vector3f(lookX - cameraX, lookY - cameraY, lookZ - cameraZ);
Vector3f up = new Vector3f(0, 1f, 0);
Vector3f right = new Vector3f();
dir.normalise();

Vector3f.cross(dir,up,right);
right.normalise();

Vector3f.cross(right,dir,up);
up.normalise();

matrix.m00 = right.getX();
matrix.m01 = right.getY();
matrix.m02 = right.getZ();
matrix.m03 = 0.0f;

matrix.m10 = up.getX();
matrix.m11 = up.getY();
matrix.m12 = up.getZ();
matrix.m13 = 0.0f;

matrix.m20 = -dir.getX();
matrix.m21 = -dir.getY();
matrix.m22 = -dir.getZ();
matrix.m23 =  0.0f;

matrix.m30 = 0.0f;
matrix.m31 = 0.0f;
matrix.m32 = 0.0f;
matrix.m33 = 1.0f;

//setup aux as a translation matrix by placing positions in the last column
aux.m30 = -cameraX;
aux.m31 = -cameraY;
aux.m32 = -cameraZ;

//multiplication(in fact translation) viewMatrix with aux
Matrix4f.mul(matrix, aux, matrix);

return matrix;
}

I've found your problem:

view.m13 = 1f;

This means that your camera is in fourth W dimension, so your image moves in W direction too, when you translate your view matrix. So, remove this line.

Also , this is a tip for better understanding matrices in LWJGL:

m00 | m10 | m20 | m30 |||_||| Xx | Yx | Zx | Wx

m01 | m11 | m21 | m31 |||_||| Xy | Yy | Zy | Wy

m02 | m12 | m22 | m32 |||_||| Xz | Yz | Zz | Wz

m03 | m13 | m23 | m33 |||_||| Xw | Yw | Zw | Ww

m03, m13, m23 almost always must be 0, and m33 = 1.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM