Geometry & Shader Basics - Geometry, what is it?

In computer graphics, our games are made up of various on screen elements, floors, walls, crates, oil drums, cars, people and many, many more objects. All of these objects are normally rendered as a number of triangles, yep, even spheres are actually just lots of triangles making up a smooth curved surface, the more detail you have in an object, the more triangles you need.

We can fake detail with bump/normal maps and with other techniques, but the bottom line is that all these objects tend to be made up of triangles.



There are other ways to render items on the screen, for example you can render water, even oceans without a single triangle being used by using a post processing techniques, but here, we are going to focus on regular vertex geometry.

Vertex

A vertex is a point in space, so to create one of the triangles in one of the scene objects, we would need three points, or vertices, in space to tell the computer where that triangle needs to be in the game world. This is just one aspect of what the Vertex can store though, as well as position, it may have a colour, the direction it’s pointing/facing, also known as it’s Normal. The vertex may also store texture mapping information so that we can render a texture on the final triangle.

In code, our Vertex will be a structure describing the elements we need at a given point for our geometry.

MonoGame gives us a few Vertex structures we can use without us having to write any code.

VertexPosition

This vertex structure will give us the most basic information for our points in space, for a vertex, just a position. The position is stored as a Vector3, so three floating point numbers representing a position in the three axes, X, Y and Z.

VertexPositionColor

This gives us a little more information, as well as position, we can store a colour in these vertices. The colour is stored in a Color structure, R (Red), G (Green), B (Blue), A (Alpha) and can be as bits or floats. If bits, the values range from 0 to 255, if a float from 0 to 1.

VertexPositionColorTexture

Same as the previous structure but we can store texture mapping data. This is in a Vector2 structure, X and Y but are commonly called U and V. U and V are the two variable names used for the texture coordinate system used in computer graphics, we will cover these in more detail later. In simple terms think of the U, V as the X, Y coordinates of the texture to be applied to the geometry.

VertexPositionNormalTexture

This vertex structure disposes of the Color property and replaces it with a Vector3 for the Normal, or direction the vertex is facing, this is useful to know when we start writing shaders to light the geometry, as we will need to know what angle the light source is hitting the surface of our geometry.

VertexPositionTexture

This structure is just storing the position and the U, V texture coordinates for the vertex.

Custom Vertex Structures

As well as these very useful vertex structures, we can actually create our own, we will have a look at that later, but for now, it’s enough to know that the structures we have just covered all implement the IVertexType interface. When creating our own vertex structures we will also implement this interface.

As we can see, the most important characteristic of a vertex, is its position. This position is in relation to the object we are rendering, and this is commonly called Object or Model Space.


Space

The notion of Space is quite important when it comes to graphics programming, there are a number of different spaces, and we will cover a few of them in this blog. As we have just seen, vertex data starts off in Object Space, that is to say, this data is in relation to other vertex elements within the object. In order to render these vertices, we need to move them through a number of spaces before they can then be seen in our scene, typically by a camera.

This is done through transformations, the first will be to move these values into World space, this will translate the vertex values so that they correspond to the position, scale and rotation of our object. 

So, for (a simple) example, if the object's position in the world is at [10,0,0] and the triangle we want to draw has the following vertex positions, [0,.5,0], [-.5,-.5,0], [.5,-.5,0]. We need offset the vertex positions by the objects position making them,  [10,.5,0], [9.5,-.5,0], [10.5,0], our triangle will now be rendered where ever we place our object in the world.

The next is to transform the vertex data from World space into View space, this will move the vertex data into the viewers space (typically the camera) and finally the vertex data will be translated into Screen Space, this will place the vertex data on the screen.

Object Space

Any vertex data, which is relative to the object we want to render.

World Space

Object data transformed into world space is now relative to other objects in that world.

View Space

World space data transformed to be relative to the viewer of the object, in our case, and in most cases, the camera.

Screen Space

View space data transformed to be relative to objects on the screen, in order for any data to be rendered, it must be in Screen Space.

The three key transforms:


These transforms will all be done on the GPU when we write our shaders. There is nothing stopping you doing this calculation on the CPU, but, there will be a lot of vertices to transform and the GPU is specifically designed to do this heavy lifting floating point calculations, all you are going to achieve by doing these calculations on the CPU is slowing your render and so your game down.

Each of these transforms are done with different matrices which we will cover as we come across them. The first of which will be our World Matrix, we will store and evaluate this in a Transform class. For now, think of a matrix as a mathematical device we can use to move data from one space to another, we will look at this in more detail in the maths section of the book.


Last Chapter: Preface 

Next Chapter: Geometry in Code

As ever, thanks again for your time here, please feel free to make comments on thise post.

Comments

Popular Posts