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.