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

Tuesday, October 11, 2011

on varyings, uniforms and being in over one's head

So, it's been a long time, I KNOW, but in my defense, I have been quite busy, and not just new-father busy, but actual indie-game-dev-wannabe busy, so a little update on how things have been going

I've got bad news and good news. I have joined up with Stanford's Online AI-Class course which has just started this week. Also, I have joined up with a University of Reddit class to Learn Game Programming. What does that mean? It means I have been SUPER busy for the past 2 weeks, and things are unlikely to change any time soon. In fact, they may only get busier in the next 2-3 months, and that is the bad news. On to the good

I have FINALLY managed to port over lesson number 3, that involves passing in information to the shaders, in the form of attributes, and passing them from vertex to fragment shader. This has also made me realise, that I had been doing the whole vertex position passing to the vertex shader in a long way all from the start of lesson 1. What this meant was, I had to go back to lesson 1 and re-write the display code for our lovely triangle. Took me a bit to understand what was going on in order to be able to port it correctly, but thankfully, a few trip to Google & LWJGL docs later, I managed to get it right. Merging forward into the code for lessons 2 & 3 turned out to be a bit easier than expected (THANKS git!)

Having ported over the code to the correct way to pass vertex points to the shaders, I was good to go and continue with porting the lesson 3 code to java/lwjgl

I am very happy with the current progress I have made; I may not be moving as fast as I would have wanted, but at least I'm getting quite a good grasp of the OpenGL programmable pipeline this way. I think this port was probably a good idea

As for the Learn Game Programming (from here on LGP) course, I will try and write more about my progress there as well. As with the lwjgl port, I am also uploading all my source code over at a github repo, and of course, everyone is invited to check it out, fork it and do whatever you want with it