Shader fun

Lately I updated the engine renderer to use core OpenGL 3 only. While there are still some things to work out the engine is now running completely on shaders. This allowed me to do implement some amazing things.

1. Completly flexible vertex formats.

Adding a new vertex definition is pretty easy now. First you need to typedef a struct for storing the vertices:

typedef struct myVertexType
  F32V3 position;
  F32V3 normal;
  F32V2 texCoord0;
  F32 blendFactor;
} myVertexType_t;

The next step is to create a definition for the vertex type:

nxVertexAttribute_t myVertexDef[] = {
  { nxVertex::Position, 3 },
  { nxVertex::Normal, 3 },
  { nxVertex::TexCoord0, 2 },
  { SID("BlendFactor"), 1 },
  { nxVertex::End, sizeof(myVertexType_t) }

And that’s it. This can be done completly seperated from the engine. So no engine recompiles needed!

The first 3 attribute names (nxVertex::Position, …) are predefined in the engine and automatically linked to all shaders. “BlendFactor” is a custom attribute name which must match to the corresponding name and type in the shader (“in float BlendFactor”). The numbers after the names are the size of the attribute in 4 byte steps. The last element “nxVertex::End” is to tell the engine that the definition is complete. “sizeof(…)” is the stride of the vertex type.

2. Framebuffer Objects

To get the real power out of shaders, framebuffer objects are a must have. So I implemented them into the engine and started to play around with them for a bit. Up to now I can create FBO’s and attach textures to them, which are used as render buffers. Then the FBO can be activated and used for rendering. After that the attached texture can be used as input for further shading passes or used for normal texture mapping. This allows some very interesting effects like mirror surfaces, enivormental mapping or procedural texture generation.

I created a basic demo which first renders the scene into a FBO. In the next pass the textures from the FBO are blended into the scene. The two textures used as buffers in the FBO are color and depth:

3. Next: Deferred Shading

The whole thing of this OpenGL 3, shader, FBO thing is that it allows the implementation of deferred shading. I really liked the idea behind this technique when I first read about it and now it’s the time to implement it by myself. Some links if you’re interested in the topic:

Deferred Shading Tutorial (PDF)

Deferred Shading – Wikipedia

That’s it for now. I’ll post some more pics if I have something nice to look at.

Leave a Reply

You must be logged in to post a comment.