Archive for the ‘University’ Category

Okay, no updates in a long time. As is the way with hastily organised out of term projects, a lot of them have been progressing very slowly, or not at all. So as a result, I’ve not really had anything blog-worthy.

However I feel it is blog-worthy that I’ve had my results back from the University, First Class with Honours :)

Hopefully that means the PhD is now in the bag (at least as far as getting the position goes), although I still have to apply and interview for it.

I’m heading out to germany in two weeks time to take part in an intensive course on embedded AI at the University of Kiel, so hopefully we’ll have internet access, and I can blog about the course.

University is done!

Author: Snikks

University now completed! I’m hopeful of getting the 2.1 I need (a first as I’d wanted is an outside possibility.. maybe.. I’m not holding my breathe though. My time management suffered because I got too into my final year project) in order to continue and become a graduate student, undertaking a PhD at Glamorgan.

Dr. Sayers the third has a ring to it, I feel. The ladies knees will buckle.

Anyways, planned summer coding projects are:

Learn PHP – I have already begun this, using XAMPP Windows as my test bed and the book available here. I like the idea of knowing a few web-languages and having a grip on interacting with database backends via dynamic web-pages.

Learn more about XNA – Probably using this website as a source of tutorials to follow.

Conquistador – Continue developing the 2D game engine I’ve been working on with Anthony Watts.

Pursue shader programming – I was disappointed with my final coursework in Advanced Realtime rendering (hence why it hasn’t shown up here, I hesitate to show sub-par work, although I guess I probably will). In any case, I wish to improve my knowledge in this area.

So I should have plenty to blog about if I seriously pursue all that! Watch this space.

Okay in this video, the surface being demoed is actually rendered as a flat 2D plane with no colour at all. If you remove the shaders, you simply get a black screen, since black is also the background colour and you cannot see the plane against the background.

The vertex shader (which is executed once for every vertex passed to the graphics card) sets the heights of the vertices according to a heightmap texture loaded in the C code and passed into the shader via a Uniform variable as set up in this piece of code:


// set active texture unit
glActiveTextureARB(GL_TEXTURE0);
surfaceTexture = LoadTextureFromHeightMap("Assets\\Heightmaps\\example_surface_32x32.txt");
glUniform1iARB(glGetUniformLocationARB(surfaceShader, "myTexture"), 0);

This next piece of code produces a 2D plane in 3D space, with the texture mapped accordingly to it.


int size = 32;

//Terrain rendering
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//Render 2D Plane using rectangular quads
glUseProgramObjectARB(surfaceShader);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

for (int z=0;z<=size;z++)
{
glBegin(GL_QUAD_STRIP);
for (int x=0;x<=size+1;x+=)
{
glTexCoord2f((GLfloat)x/size, (GLfloat)z/size);
glVertex3f((x-size/2.0f), 0.0f, (z-size/2.0f));

glTexCoord2f((GLfloat)x/size, (GLfloat)(z+1)/size);
glVertex3f((x-size/2.0f), 0.0f, (z+1-size/2.0f));
}
glEnd();
}

This is fairly general OpenGL code so I won’t bother to explain it. The shaders are the real point of the program for this demo:

Vertex Shader


uniform sampler2D myTexture;

void main(void)
{
gl_TexCoord[0].st = gl_MultiTexCoord0.st;

gl_Vertex.y = texture2D(myTexture, gl_TexCoord[0]).a*3;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}

The first line here sets the first two parameters of gl_TexCoord[0] to the same as gl_MultiTexCoord0.

The second line sets the Y position of the current vertex according to the heightmap.

It uses the vec2 provided by “gl_TexCoord[0]” to do a lookup in myTexture and find the relevant Y value. It then multiples the result by three  and sets gl_Vertex.y to that value.

Finally, transform the current vertex to the correct position in world-space.

Fragment Shader


uniform sampler2D myTexture;

void main(void)
{
//The 0.05 makes things easier to visualise

float modt = mod(gl_TexCoord[0].t,0.05)/0.05;
float mods = mod(gl_TexCoord[0].s,0.05)/0.05;

vec4 texColor  = texture2D(myTexture, gl_TexCoord[0].st).rgba;

//For efficiencies sake, the or can be removed and only one value compared.
//However this results in a "lopsided" appearance to the grid, since
//only one side of the divide is being coloured, not a line spanning both sides.

if (modt <= 0.05f || modt >= 0.95f)
gl_FragColor = vec4(1.0,1.0,1.0,1.0);
else
if (mods <= 0.05f || mods >= 0.95f)
gl_FragColor = vec4(1.0,1.0,1.0,1.0);
else
gl_FragColor = vec4(0.3f,0.3f,texColor.a,1.0);

}

If it helps to visualise think of ‘t’ as ‘Y’ and ’s’ as ‘X’.

We’re taking the modulus of X and Y divided by 0.05. This gives us the remainder of the division.

Dividing it by 0.05 again gives us numbers that are easier to visualise (in my opinion) than the normal values returned by the modulus (it gives us values between 0 and 1).

So now, as you move further from a value exactly divisible by 0.05, this number will gradually increase from 0, until flipping back to 0 before it quite hits 1.

It can be seen where this is going – it’s going to allow us to draw the white grid that’s visible in the video.

Anyway, we perform another lookup to get the texcolor for the fragment and then move directly to our grid drawing. There are probably far more efficient ways in which to achieve this than a bunch of branching in a shader but it works and given that we’re only working with a flat plane, even the puniest of modern graphics cards should be fine (I’m looking at you onboard graphics!).

At this point it should be obvious how the grid is drawn, for every fragment with a return to our “difference from a multiple of 0.05 in terms of 0-1″ operation on either the X or Y axis that’s between 0 and 0.05 or 0.95 and 1 colour that fragment white.

The last line is a bonus, if the fragment isn’t part of our grid, colour it with:

  • Red = 0.3f
  • Green = 0.3f
  • Blue = the Y value we extracted from the heightmap in the vertex shader.

This gives you a nice gradient effect that varies with the height of the vertex.

Sample code can be downloaded from here.

So yeah, got my Mesh Skinning coursework back today, 68% =) quite happy with that.

For anyone who doesn’t know (I.e. most of the people likely to read this) mesh skinning is essentially rendering a series of static vertices, which will then be tranformed in some fashion by your bone matrices, according to a weight value associated with each vertex.

Another term for this is skeletal animation. In the case of this coursework, all the transformations take place in the vertex shader, although the initial modelling is done in standard C++.

Final year project is still chuntering along. Had a meeting with Colin earlier today, he doesn’t seem to have given up on me as a student just yet!

Back-propagation still doesn’t work, due to some very strange memory bugs.. previously initialised variables seem to be spontaneously un-initiating themselves. I’m not sure exactly how I messed it up but I plan to re-implement the algorithm from scratch tomorrow, and if that doesn’t solve it, I’ll put that on the back burner and use a library to achieve that functionality for now.

I don’t actually need back-propagation working for my project (the main focus is genetic algorithms), but for a more apples to apples comparison I want back-propagation running on the same network code as the genetic algorithm. Plus implementing it gave me a bit more of a practical take on delta rule learning, which I’ll need for my planned implementation of Cascade Correlation.

Anyways – an in-depth analysis of my final year project will have to happen another night.. I’m rambling now because I’m sleepy.. so I’m going to go sleep.