Information Technology Sample Assignment Part 2

IT Assignment Help

Graphics For Games And Dynamics Modelling

Practical 1: Communication In The Shader System

Objective.

The purpose of the mechanisms used to transfer code and data into the GPU.

Provided Code

Information Technology Assignment Help Order Now

You are provided with a Visual Studio 2005 project. The key files in this project are:

main.cpp, CPU program loads GPU code, passes a simple box structure to the GPU together with uniform variables and attributes.

Glsl.vs, Vertex Shader program works with both pixel shaders demonstrates use of attributes

Glsluniformdemo.ps, Fragment shader demonstrates use of uniform variables

Glslvaryingdemo.ps, Fragment shader demonstrates use of varying variables

Things To Do Running The Program

A window should appear with a grey cube in it. You should be able to move it around with the mouse.

Try pressing the 'a' or 's' keys. One face should get lighter or darker. Try pressing the 'd' key. This should change the face that is affected.

Now press '2'. Everything should be similar – except that – instead of whole polygons only the region around a particular vertex will be affected – with a gradual tail off away from the vertex. Pressing '1' will restore the previous set up.

Looking at the code

Setting up the shaders.

Find the function bInitialiseGLSL(). Inside this function there is a switch statement. Look at case 0: This case has been carefully commented so that you can see what is going on. Note the mechanisms by which the code is retrieved, compiled and loaded. Note that the shader code is recompiled every time you press '1' or '2'.

Setting up the variables.

Immediately after the section which compiles and loads the shaders you will find statements that retrieve the integer values used to identify the uniform and varying variables within the pixel and vertex shaders. Note that the names of the variables – to be found in the pixel and vertex shader programs – appear as string constants in the CPU code.

Drawing the shape

The only actual drawing commands to be found in the program are the calls to glvertex3f(). These are grouped in 4’s and enclosed between the commands glBegin(GL_QUADS); and glEnd();

The command glBegin(GL_QUADS); actually allows any number of quadrilaterals to be drawn – the vertices will be automatically handled in 4’s – but we have put them in separate glBegin-glEnd structures so that the uniform variables can be different for each one.

Setting values to uniforms and attributes

The commands that set each attribute variable are located immediately before the appropriate vertex call. Ignore the texture commands for now.

Note the parameters used in each case – the index that was retrieved when the shaders were set up and the value that is to be transmitted. Knowing the variables that are being used – you should now be able to find the keyboard commands easily.

The commands that set up the uniform variables are similar – except that they are located outside the glBegin-glEnd blocks. It is forbidden to put them inside since uniform variables must be constant across each primitive.

The shader programs

The file glsl.vs is the vertex shader. The line gl_Position=ftransform(); performs the mandatory task of carrying out the required geometric transformations. The line may be modified/replaced if desired (not recommended until you know what you are doing).

As similar line sets up the texture co-ordinates on the vertex.

The final line copies the attribute vshade into the varying variable cshade – making it visible to the pixel shader.

The two '.ps' programs are the pixel shaders. At present they simply pass the uniform variable shade or the varying variable cshade to the output. gl_Fragcolor is the function that does this

Challenge

Visually identify the face/vertex that has been 'selected' for change. In the case of the vertex the edge cut off must be sharp. In doing this you may not ALTER any existing line of main.cpp although you may ADD new lines. You can do what you like with the shaders…

Practical 2: Texture In The Shader System

Objective.

The purpose of this session is to familiarize students with the mechanisms used to load and use texture in the GPU.

Provided code

New pixel shader A third pixel shader has been added together with a third context and associated variables, set up code and so on. These are labelled glcontext2 etc. A few other minor changes have been made for this version – in particular the uniform variable shade  is available in all three shaders

The shader programs are now:

Glsl.vs Vertex Shader program – works with all three pixel shaders – demonstrates use of attributes.

Glsluniformdemo.ps Fragment shader demonstrates use of uniform variables

Glslvaryingdemo.ps Fragment shader demonstrates use of varying variables

Glsltexcoordemo.ps Fragment shader demonstrates texture- mip mapping etc. variables

Highlights of the texture demonstration code main program

Loading the textures

This is done in the function bInitialiseGLTextures. Note that in this system the textures are loaded from ascii text files so you can see more clearly what is happening.

Setting up the sampler variables

This is done in a similar way to the Uniform variables. Note the function glUniform1iARB that sets a value for the sampler – and hence selects the textures that will be available for each primitive.

Setting the texture co-ordinates

Texture co-ordinates are vertex attributes and are hence passed through in a similar way to the vshade (vertex shade) attribute used before in the second shader. However a special function is used to do this as texture co-ordinates are part of the built in functionality. Of course there is no reason why you should not use user-defined attributes for this – although the built in functionality would then be wasted.

Things to do

Running the program

Should behave as before except that pressing 3 will now activate the third shader.

As supplied this will give a red-green wash to the cube – because the red and green channels are being used to display the texture co-ordinates.

Looking at the shader code

The vertex shader has not changed – but note that we are now making use of the line that sets up the texture co-ordinates on the vertex.

The new pixel shader has three versions of 'main'. You can select which one is active using the #defines at the top.

First Version

Displays texture co-ordinates as colours.

Second Version

Displays the texture. Note that the uniform variable 'shade' has been hijacked to rescale the texture – under control of the 'a', 's' and 'd' keys. Note the aliasing artefacts that occur at large and small scales.

You can fix this by changing 'NEAREST' to 'LINEAR' in bInitialiseGLTextures. This invokes the built in mip map interpolation functionality

Third Version

Contains explicit code for tri-linear mip mapping. Note that we have to work out which level to use ourselves and then select the mip map level explicitly using the texture2Dlod function. Note the use of the derivative functions for this. Note also the way that texture co-ordinates have to be scaled up and down to deal with the different resolutions.

Challenge

Make the texture appear to 'swirl' in a circular motion by modifying the texture co-ordinates. (If you feel ill when looking at it then you have got the right idea.)

Resources

PDF files containing the relevant versions of the open gl and glsl spec documents are available.
Please note that the actual syntax for open gl commands has an added prefix gl and suffix ARB (mostly) when compared to the document.

Information Technology Assignment Help | Programming Assignment Help | Computer Program Assignment Help | Homework Tutoring | Algorithm Help | System Design Help | Web Design | IT Tutor | Online Tutoring | Homework Help | System Architecture Help | Software Design Help | Science Project Help | Multithreading Help | Data Processing Help | Software Design Help | Data Analysis Help