Planning the Spontaneous

It's more than just a blueprint.

Catchup: City > Town > Village > ?

Posted by Robert Chow on 01/10/2009

So I started work here a little over 2 months ago, and to keep this consistent by being inconsistent, I’m going to try and sum up, but detail what I’ve been up to over the past couple of months.

So it’s 13/07/2009. Some might say it’s unlucky to start work on the 13th. I couldn’t really care less. I’ve just been given a not-so-brief debriefing of what to expect for the upcoming year or so. IGI – Integrated Geochemical Interpretation. They develop and use a program to help the geochemists decipher on whether or not a rock sample is worth it’s value in money when looking for oil. My job – to help create the visual elements of the next release – these are Graphs and Maps.

I’ve had little experience with OpenGl, and I’ve come to the company having learnt Java on Linux for the first two years of university. You can probably imagine my thought process when I was asked to write in C# using VS. And if I’m honest, it’s brilliant compared to what I’ve been used to.

Lambda Expressions

x => x * x

I’ve never seen this EVER before I came here. I know C# isn’t exactly new, but to me, it is. This essentially will assign the value the square of the original value given. Another interesting thing:

Extension Methods

public static void Times(this int times, Action<int> action)
for (int i = 0; i < times; ++i)

This will essentially take an integer, and do the required action the number of integer times.

Combine the two, and you can get something like this:

// data: a float array, containing random numbers
// Bar(float height): a vertical bar to be drawn on a bar chart, it’s value, determined from an entry in data

bars = new Bar[data.Length];
bars.Length.Times((i) => bars[i] = new Bar(data[i]));

So this will essentially take the lambda expression, and do it by integer times. Saves so much time, and space, and of course the extension method code is reusable. We’ve essentially taken 6 lines of code, and pushed it into 1.

So that’s July C# for you. But I haven’t begun on OpenGl.

The Tao Framework is a specifically designed framework that wraps around OpenGl, and works with C#. It’s fairly newish, which is the problem. I can’t seem to find examples ANYWHERE (turns out I found them a month and a half later in the Tao Framework examples). They’re all written in C++, of which, I can hardly say I’ve any prior experience of. Not to mention that, but my OpenGl skills aren’ t exactly amazing either. So I learn about this thing called retained mode – instead of drawing out your polygons as the code behaves, all the data is pushed into the graphics card itself, which is then displayed on screen. The great thing about this is that it frees up disc space because all the data is already stored in the graphics card. This in turn speeds up the process of drawing to screen. It was a nightmare trying to render the graphics version of “Hello World!” – a triangle – onto the screen with this retained mode, especially when I couldn’t find any examples whatsoever. But I got there in the end.

Vertex Buffer Objects

So we start off with creating pointers to the buffers – this should only need be done once. Here I have only used 2 buffers – one for vertices, and the other for colours. You can of course, use more buffers for normals and texture coordinates.

int[] buffers = new int[2];
Gl.glGenBuffers(2, buffers);

Now that the buffers are generated, load the data into the buffers, by first binding them, then loading the data. I have unbound the buffers at the end just as a safety precaution.

Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, buffer[0]);
Gl.glBufferData(Gl.GL_ARRAY_BUFFER, (IntPtr)(vertices.Length * sizeof(float)), vertices, Gl.GL_STATIC_DRAW);
Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, buffer[1]);
Gl.glBufferData(Gl.GL_ARRAY_BUFFER, (IntPtr)(colors.Length * sizeof(float)), colors, Gl.GL_STATIC_DRAW);
Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, 0);

Here, I have used float arrays to determine the values of the vertices and the colors. This only needs to be done once, again, as long as the data doesn’t change. As the data is now stored in the graphics card, you no longer need the arrays on disc you originally used to store the data. With the data now binded to the buffers, we can now go about drawing the information onto the screen. This is done by binding the buffers again, setting an appropriate pointer, and drawing the arrays into the device context. To draw the arrays, you need to give it a drawing type, and the vertex count. Before this however, you need to enable the appropriate client states.

Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, buffer[0]);
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, (IntPtr)0);
Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, buffer[1]);
Gl.glColorPointer(3, Gl.GL_FLOAT, 0, (IntPtr)0);
Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, 0);
Gl.glDrawArrays(drawType, 0, count);

Seems simple enough really doesn’t it? It gets harder (I found this out next month).

So on top of learning a new language and getting to grips with the advanced basics of OpenGl, a little light reading – Design Patterns. A design pattern is suppose to a “simple and elegant solution to a specific problem”. Ha. I must admit, yes some are particularly useful, and do help with the maintainability and scalability of a system, but some of them are more bother than they’re worth. Of course, with more than 23 to learn, I am going to be a little biased. An interesting read, but not really my cup of tea. Still, I shouldn’t really be dismissing what could be potentially the future.


3 Responses to “Catchup: City > Town > Village > ?”

  1. agentti said

    Well advanced graphics should be a doddle next year!

  2. […] So we transpose it into the new structure, before copying it to texture.  Note: I’ve used an extension method to find the next power of 2! And also, lambda expressions for the for […]

  3. […] an array, and transfer them to the graphics card to be stored and used there.  Sound familiar?  Vertex Buffer Objects of course.  Hello wikipedia […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: