Wednesday, October 12, 2011

on Matrices, transformations and buffer flipping

So, I sat down yesterday and decided I was going to BLAST through lesson 4, so that I am completely free from the porting effort all the way till the weekend. I still have an AI-Class lesson to go through tonight, and a LGP assignment due by tomorrow. So, really, yesterday night was all the time I had left to get through lesson 4, because I REALLY want to get to lesson 5, where we move up to... are you ready? 3D! YES! where all the awesomness begins. But, until then, I still had to go through lesson 4, so let's get to that

Reading in the lesson code, I realised it was probably not going to be a big deal. I was HUGELY mistaken, but I did not know it at the time! Anyways, the lesson introduces the concept of transformation (basically altering the models in any way, moving, rotating, etc) by applying a transformation matrix. Not really much of a shock there, since this is what I know from older days of dabbling with OpenGL. The major change though, was that the transformation matrix was not applied to the vertices in the code, but rather passed to the vertex shader;
(OMG I just ended a sentence with a semi-colon instead of a period. I guess I should take some time off of coding this weekend... *thinks about it*... NEVER!)

Since the matrix to be passed was a constant value (it did not alter from one vertex to the next for each drawing cycle) it was going to be passed as a uniform value. Quick trip to the lwjgl javadocs, and it was clear all I needed was a simple call of glUniformMatrix4() with a buffer loaded up with the matrix data. The Matrix4 class was even nice enough to have a store() function that would fill up the buffer passed to it, just like OpenGL liked it

On the vertex shader's side, the only thing required was to multiply the inbound coordinates passed to the shader, by the transformation matrix, and that was really all there is to it. Simple enough

I hacked in the code, double-checked everything and ran the program and BAM! a blank screen with nothing on it. Fast forward a couple of hours, and I had tried literally EVERYTHING I know, to no avail. I had even tried replacing the transformation matrix with identity matrix (in essence, negating the effect of the multiplication) and still, nothing would show up

I spent the next 3-4 hours playing around with the code, google, opengl docs, forums and then repeating the cycle all over again. When I had last given up, I posted up a question at the forums with the SUPER helpful community at JGO and hit the sheets

To my frustration, but still luckily enough, the fix was a very simple thing. I had forgotten to flip() the buffer I was writing to, and so it was not yet ready for reading and hence OpenGL was only getting 0s off of the matrix. This of course would make all the vertices end up at the exact same coordinates on screen of (0,0,0) of which no GL_TRIANGLES call would ever be able to draw anything

I put in the fix today morning, and finally called a wrap on lesson 4. As I said earlier, I will be getting into the second assignment for LGP course, which is basically a SHMUP, one of my favourite game types ever. I have discovered yesterday the very interesting Google PlayN, and will try to do the second assignment using it tonight. If I fail to get things setup and running quickly enough, though, I will just revert to the ever reliable Slick2D and play around with PlayN later on

But that, is a different matter, and for that, I'll probably have a different post. Till then

No comments:

Post a Comment