Design and implementation of a render engine using OpenGL Module Engine
Abstract
Module engine is meant to be an open-source library to create graphical programs written in C#. It can serve as a base to create larger tools, such as 3D editors or game engines. This library includes the basic structures to build a 3D scene: mesh, lighting & shadows, textures, hierarchies and more detailed along this document.
Full text
Oscar Sales Salvador Video games design and Development degree Final Project technical report: Design and implementation of a render engine using OpenGL Module Engine Description: Module engine is meant to be an open-source library to create graphical programs written in C#. It can serve as a base to create larger tools, such as 3D editors or game engines. This library includes the basic structures to build a 3D scene: mesh, lighting & shadows, textures, hierarchies and more detailed along this document. Keywords: render, engine, 3D graphics, OpenGL
Main index INTRODUCTION ................................................................................................................................... 1 IDEA DESCRIPTION ............................................................................................................................... 1 USED TOOLS ........................................................................................................................................ 2 GOALS AND REASONS .......................................................................................................................... 2 RELATED SUBJECTS ............................................................................................................................. 3 PLANNING ............................................................................................................................................ 3 DEVELOPMENT .................................................................................................................................... 4 1. OPENGL .......................................................................................................................................... 4 1.1 Shaders .................................................................................................................................... 4 1.2 GLSL ........................................................................................................................................ 5 1.3 Buffer Objects (VBO) .............................................................................................................. 6 1.4 Uniforms ................................................................................................................................. 12 2. 3D MESH ....................................................................................................................................... 14 2.1 Mesh structure ....................................................................................................................... 14 2.2 Mesh objects ......................................................................................................................... 16 2.3 Triangulate faces .................................................................................................................. 17 3. TRANSFORM & HIERARCHY ............................................................................................................ 20 3.1 Transformation matrices ....................................................................................................... 20 3.2 The view matrix ..................................................................................................................... 25 3.3 The projection matrix ............................................................................................................ 27 3.4 Hierarchy ................................................................................................................................ 30 4. RENDERING THE SCENE ................................................................................................................. 37 4.1 The shader program ............................................................................................................. 37 5. ILLUMINATION ................................................................................................................................. 40 5.1 Light types ............................................................................................................................. 40 5.2 Combine lights ....................................................................................................................... 43 5.3 Implementing lights in the shader ........................................................................................ 44 6. TEXTURES ...................................................................................................................................... 45 6.1 Create the texture ................................................................................................................. 45 6.2 Use of textures in OpenGL .................................................................................................. 47 6.3 Textures in the shader .......................................................................................................... 47 6.4 Skybox ................................................................................................................................... 49 7. SHADOWS ...................................................................................................................................... 51 7.1 Shadow mapping concept .................................................................................................... 51
7.2 Rendering the depth map ..................................................................................................... 54 7.3 Implementing shadow Mapping ........................................................................................... 57 7.4 Shadow mapping acne / bias ............................................................................................... 58 7.5 Shadow mapping anti-aliasing ............................................................................................. 59 8. POST-PROCESSING ........................................................................................................................ 63 8.1 Post-processing effects ........................................................................................................ 65 CONCLUSIONS ................................................................................................................................... 73 ACHIEVEMENTS .................................................................................................................................. 73 FINAL IMAGES ..................................................................................................................................... 74 WHAT HAVE I LEARNT?....................................................................................................................... 74 PROJECT OPINION .............................................................................................................................. 75 PROJECT APPLICATIONS .................................................................................................................... 75 POSSIBLE EXPANSION PATHS ............................................................................................................. 76 REFERENCES AND BIBLIOGRAPHY .............................................................................................. 76
TABLES INDEX chapter – number name Table 1: 0.0 - 1 Time planning ......................................................................................................3 Table 2: 1.1 - 1 Create a shader ....................................................................................................4 Table 3: 1.1 - 2 Vertex shader example ........................................................................................5 Table 4: 1.1 - 3 Fragment shader example ...................................................................................5 Table 5: 1.3 - 1 Render face by vertex ..........................................................................................6 Table 6: 1.3 - 2 VBO structure example ........................................................................................7 Table 7: 1.3 - 3 VBO Buffer example.............................................................................................7 Table 8: 1.3 - 4 VBO Buffer iteration ............................................................................................8 Table 9: 1.3 - 5 Get VBO values ....................................................................................................8 Table 10: 1.3 - 6 Create VBO buffers ..........................................................................................10 Table 11: 1.3 - 7 Specify VBO size ...............................................................................................10 Table 12: 1.3 - 8 Load values into a VBO ....................................................................................10 Table 13: 1.3 - 9 VBO Indices buffer ...........................................................................................10 Table 14: 1.3 - 10: Use VBO ........................................................................................................10 Table 15: 1.3 - 11 Set shader attributes .....................................................................................11 Table 16: 1.3 - 12 Draw loaded VBO ...........................................................................................11 Table 17: 1.4 - 1 Load uniforms ..................................................................................................13 Table 18: 2.2 - 1 Mesh constructor.............................................................................................16 Table 19: 2.2 - 2 Create plane manually .....................................................................................16 Table 20: 2.3 - 1 Triangulation algorithm ...................................................................................19 Table 21: 2.3 - 2 Algorithm to check convexity...........................................................................19 Table 22: 3.1.2 - 1 Euler to axis conversion ................................................................................23 Table 23: 3.2 - 2 Vectors to calculate the camera matrix ...........................................................26 Table 24: 3.4.1 - 1 Euler to quaternion .......................................................................................32 Table 25: 3.4.1 - 2 Inverse, norm & conjugate of a quaternion ..................................................33 Table 26: 3.4.1 - 3 Multiply quaternion ......................................................................................33 Table 27: 3.4.1 - 4 Rotate vector with Quaternion .....................................................................33 Table 28: 3.4 - 1 Update hierarchy .............................................................................................35 Table 29: 4.1.1 - 1 Vertex shader................................................................................................37 Table 30: 4.1.1 - 2 Shader program ............................................................................................38 Table 31: 4.1.2 - 1 The render loop ............................................................................................38 Table 32: 4.1.2 - 2 Draw hierarchy .............................................................................................39 Table 33: 5.1.1 - 1 Directional light calculation ..........................................................................41 Table 34: 5.1.2 - 1 Point light calculation ...................................................................................43 Table 35: 5.1.3 - 1 Ambient light calculation ..............................................................................43 Table 36: 5.2 - 1 Combine lights .................................................................................................43 Table 37: 5.3 - 1 Structs in the shader ........................................................................................44 Table 38: 5.3 - 2 Lights in shader ................................................................................................45 Table 39: 6.1 - 1 Create GL Texture ............................................................................................46 Table 40: 6.2 - 1 Bind textures....................................................................................................47 Table 41: 6.3 - 1 Textures in the vertex shader ..........................................................................48 Table 42: 6.3 - 2 Textures in the fragment shader......................................................................48 Table 43: 7.2.1 - 1 The depth vertex shader ...............................................................................54 Table 44: 7.2.1 - 2 The depth fragment shader ..........................................................................55 Table 45: 7.2.2 - 1 Create and change framebuffer ....................................................................56
Table 46: 7.2.3 - 1 Render loop with depth map ........................................................................56 Table 47: 7.3 - 1 Vertex shader with shadow mapping ..............................................................57 Table 48: 7.3 - 2 Fragment shader with shadow mapping..........................................................57 Table 49: 7.4 - 1 Solve shadow acne with bias ...........................................................................59 Table 50: 7.5 - 1 Shadow antialiasing .........................................................................................60 Table 51: 7.5 - 2 Reduce PCF cost: find borders .........................................................................61 Table 52: 7.5 - 3 Reduce PCF to a circle ......................................................................................62 Table 53: 8.1.1 - 1 Outline effect code .......................................................................................65 Table 54: 8.1.2 - 1 Blur effect code.............................................................................................67 Table 55: 8.1.4 - 1 Fog code .......................................................................................................69 Table 56: 8.1.5 - 1 color correction code ....................................................................................69 Table 57: 8.1.6 - 1 Desaturate code............................................................................................70 Table 58: 8.1.7 - 1 Intensify code ...............................................................................................71 Table 59: 8.1.8 - 1 Visualize depth map code .............................................................................72
FIGURES INDEX chapter – number name Figure 1: 2.1 - 1 Vertex structure................................................................................................14 Figure 2: 2.1 - 2 Face structure ...................................................................................................14 Figure 3: 2.1 - 3 Edge structure ..................................................................................................15 Figure 4: 2.3 - 1 Ear clipping triangulation ..................................................................................17 Figure 5: 2.3 - 2: Convex or concave corner ...............................................................................18 Figure 6: 3.1 - 1 Transformation matrix concept ........................................................................20 Figure 7: 3.1 - 2 Identity matrix ..................................................................................................21 Figure 8: 3.1.1 - 1 Matrix translation calculation ........................................................................21 Figure 9: 3.1.1 - 2 Simplified matrix translation calculation .......................................................22 Figure 10: 3.1.2 - 1 Rotation auxiliar matrix ...............................................................................23 Figure 11: 3.1.2 - 2 Rotation matrix calculation .........................................................................24 Figure 12: 3.1.2 - 3 Simplified rotation matrix calculation..........................................................24 Figure 13: 3.1.3 - 1 Scale matrix calculation ...............................................................................24 Figure 14: 3.1.4 Simplified formula of the transformation matrix ..............................................25 Figure 15: 3.2 - 1 Camera matrix parameters .............................................................................26 Figure 16: 3.2 - 2 The camera matrix ..........................................................................................26 Figure 17: 3.3 - 1 Difference between orthographic and perspective projection .......................27 Figure 18: 3.3.1 - 1 Perpective cube ...........................................................................................27 Figure 19: 3.3.1 - 2 Perspective matrix .......................................................................................27 Figure 20: 3.3.2 - 1 Orthographic cube .......................................................................................28 Figure 21: 3.3.2 - 2 Orthographic matrix ....................................................................................28 Figure 22: 3.3.2 - 3 Simplified orthographic matrix ....................................................................28 Figure 23: 3.3.2 - 4 Projection matrix difference for perspective and orthographic ..................29 Figure 24: 3.4 - 1 Render algorithm ............................................................................................30 Figure 25: 3.4 - 2 Hierarchy rotation problem ............................................................................31 Figure 26: 3.4 - 3 Hierarchy rotation problem solution ..............................................................31 Figure 27: 3.4 - 4 Hierarchy rotation problem solved .................................................................34 Figure 28: 3.4 - 5 Hierarchy scale problem .................................................................................34 Figure 29: 3.4 - 6 Forward, Up & Right vectors ..........................................................................36 Figure 30: 6.1 - 1 Texture relative coordinates ...........................................................................46 Figure 31: 6.4 - 1 Skybox example ..............................................................................................49 Figure 32: 6.4 - 2 Skybox graphical explanation .........................................................................49 Figure 33: 7.1 - 1 Depth map examples ......................................................................................51 Figure 34: 7.1 - 2 Fragment discarded ........................................................................................52 Figure 35: 7.1 - 3 Viewport relative coordinates ........................................................................53 Figure 36: 7.4 - 1 Shadow mapping acne ....................................................................................58 Figure 37: 7.5 - 1 Shadow mapping antialiasing .........................................................................59 Figure 38: 7.5 - 2 Orthographic size for depth map ....................................................................60 Figure 39: 7.5 - 3 PCF Shadow Mapping .....................................................................................60 Figure 40: 7.5 - 4 Reduce PCF cost: find borders ........................................................................61 Figure 41: 7.5 - 5 Reduce PCF to a circle.....................................................................................62 Figure 42: 8.1.1 - 1 Find borders in image ..................................................................................65 Figure 43: 8.1.1 - 2 Cartoon outline effect .................................................................................66 Figure 44: 8.1.2 - 1 Blur effect ....................................................................................................67 Figure 45: 8.1.2 - 1 Anti-aliasing post-processing .......................................................................67
Figure 46: 8.1.2 - 1 Anti-aliasing effect code ..............................................................................68 Figure 47: 8.1.3 Depth of field effect..........................................................................................68 Figure 48: 8.1.4 - 1 Fog effect .....................................................................................................69 Figure 49: 8.1.5 - 1 Color correction effect .................................................................................70 Figure 50: 8.1.6 - 1 Desaturate ...................................................................................................70 Figure 51: 8.1.7 - 1 Intensify effect .............................................................................................71 Figure 52: 8.1.8 - 1 Visualize depth map ....................................................................................72
FORMULAS INDEX Check if a corner of three vertices is convex or concave. Table 21: 2.3 - 2 Algorithm to check convexity Matrix translation Figure 8: 3.1.1 - 1 Matrix translation calculation Matrix translation on identity Figure 9: 3.1.1 - 2 Simplified matrix translation calculation Matrix rotation Figure 10: 3.1.2 - 1 Rotation auxiliary matrix Figure 11: 3.1.2 - 2 Rotation matrix calculation Euler to axis conversion Table 22: 3.1.2 - 1 Euler to axis conversion Matrix rotation on identity Figure 12: 3.1.2 - 3 Simplified rotation matrix calculation Matrix scale Figure 13: 3.1.3 - 1 Scale matrix calculation Matrix simplified full transformation (on identity) Figure 14: 3.1.4 Simplified formula of the transformation matrix Camera/View matrix Table 23: 3.2 - 2 Vectors to calculate the camera matrix Figure 16: 3.2 - 2 The camera matrix Perspective matrix Figure 19: 3.3.1 - 2 Perspective matrix Orthographic matrix Figure 21: 3.3.2 - 2 Orthographic matrix Euler into quaternion Table 24: 3.4.1 - 1 Euler to quaternion Quaternion operations Table 25: 3.4.1 - 2 Inverse, norm & conjugate of a quaternion Table 26: 3.4.1 - 3 Multiply quaternion Rotate vector Table 27: 3.4.1 - 4 Rotate vector with Quaternion Direcitonal light calculation Table 33: 5.1.1 - 1 Directional light calculation Point light calculation Table 35: 5.1.2 - 1 Point light calculation Ambient light calculation Table 36: 5.1.3 - 1 Ambient light calculation Combine lights Table 37: 5.2 - 1 Combine lights Shadow mapping 7.3 Implementing shadow Mapping
1 Introduction Idea description Nowadays every company dedicated to 3D graphics needs, sooner or later, to create its own working tools. Creating a full engine from the beginning may require a tough heavy work and effort and may take a huge amount of time which not everyone can spend. Therefore, most of companies prefer to acquire a tool, an engine or a library, from a third specialized company. ModuleEngine is a library that allows the programmer to easily render 3D scenes just by writing the scene using the methods in the library or importing models from outside. This tool may serve as a base to create larger 3D programs of any kind, such as 3D editors (like Blender or 3DsMax), game engines (like Unity3D or Unreal) or sculpting tools (like Sculptris or ZBrush). Since the project is meant to be open-source, if the code is not complete, the programmer can modify or improve it by adding new features, so he's not limited to the current code. The project is developed in Microsoft Visual Studio with the language C# using the library OpenTK, which translates OpenGL from C++ to C#. The code in C# can technically be used on any platform.
8 second vertex: vert_index = 2; { V1pos, V2pos, V3pos, V1norm, V2norm, V3norm, V1co, V2co, V3co } | | | Table 8: 1.3 - 4 VBO Buffer iteration The buffer object should be created only when the object is created, not every frame of the rendering. Then, every time the scene is rendered we only need to load that VBO into the GPU, not create it again. Let's see how to create a VBO: · Only when the object is created: Create list of values, later loaded into the buffer: // Init lists: we create separated arrays to be loaded later to the buffer Positions = new float [ vertices.Count * 3 ]; Normals = new float [vertices.Count * 3 ]; TexCoords = new float [vertices.Count * 2 ]; for (int i = 0; i< vertices.Count; ++i) Positions[i * 3] = vertex.x; Positions[i * 3 + 1] = vertex.y; Positions[i * 3 + 2] = vertex.z; // Same with normals and texCoords // List of faces, as a list of indices referring vertices indices = new int [faces.Count * 3]; for ( … ) indices [i * 3] = face.indexA; indices [i * 3 + 1] = face.indexB; indices [i * 3 + 2] = face.indexC; Table 9: 1.3 - 5 Get VBO values Once this is done, there is got all the information required stored in arrays. Now let’s initialize the VBO: int vertices_BO, indices_BO; // vertices and indices are two different buffers GL.GenBuffers ( 1, out vertices_BO ); // Create only 1 buffer and put the reference value (int) in vertices_BO GL.GenBuffers ( 1, out indices_BO ); // Same for the index buffer GL.BindBuffer ( BufferTarget.ArrayBuffer, vertices_BO); // Use this buffer. BufferTarget = vertices or indices.
9 In GL, Bind means "use this one". GL is always pointing one thing of each kind: one framebuffer, one buffer array, one texture... All we do is specify which one with the pointer, using the binding methods. int size = vertices.length * ( 3 + 3 + 2 ) * sizeof(float); // position (3 floats) + normal (3) + texCoords (2) GL.BufferData ( BufferTarget.ArrayBuffer, size, IntPtr.Zero, BufferUsageHint.StaticDraw); This method loads the data into the buffer. BufferTarget = vertices or indices. ArrayBuffer is vertices size is calculated before, size of the buffer IntPtr.Zero. Here the data for the buffer would be specified. However, only one array can be passed here, and there are several of them: position, normal, color, texCoords… So let’s pass “null” (in this case IntPtr.Zero because it can’t be null) and the different arrays will be loaded into the buffer using BufferSubData, instead of BufferData. // Size of the position array or the normal array int size3 = 3 * vertices.length * sizeof(float); // Size of the texture coordinates array: int size2 = 2 * vertices.length * sizeof(float); // GL.BufferSubData is used to set only a portion of the buffer GL.BufferSubData (BufferTarget.ArrayBuffer, IntPtr.Zero, size3, positions); GL.BufferSubData ( target, start, size, data ); IntPtr.Zero here is the position where this subdata starts, which is zero 0. size is the size of this array, size3 calculated before position is the array we created before. GL.BufferSubData (BufferTarget.ArrayBuffer, IntPtr (size3), size3, normals); GL.BufferSubData (BufferTarget.ArrayBuffer, IntPtr (size3 * 2), size2, texCoords); The texture coordinates, for instance, start in (size3 * 2), after the positions and the normals. GL.BindBuffer (BufferTarget.ElementArrayBuffer, indices_BO); // Now we point the indices buffer, instead of the vertex buffer // Target = ElementArrayBuffer, this means indices buffer
10 GL.BufferData (BufferTarget.ElementArrayBuffer, (IntPtr)(indices.length * sizeof(Int32)), indices, BufferUsageHint.StaticDraw); Here the data of the buffer is specified. In this case, the array “indices” is passed, and not IntPtr.zero, because it’s a single array. So BufferData can be used, instead of BufferSubData. And the size is the size of Int32 instead of floats, because the indices are integers. Once the data is dumped into the buffer, the arrays aren’t needed anymore, so we better remove them to save memory: Positions = null; // C# automatically collects trash delete ( Positions ); // In C++ we would do this. Then, to put it all together; This is how to create a VBO: // Create buffers int vertices_BO, indices_BO; GL.GenBuffers ( 1, out vertices_BO ); GL.GenBuffers ( 1, out indices_BO ); Table 10: 1.3 - 6 Create VBO buffers int size3 = 3 * vertices.length * sizeof(float); int size2 = 2 * vertices.length * sizeof(float); int size = size3 * 2 + size2; // Set buffer size GL.BindBuffer ( BufferTarget.ArrayBuffer, vertices_BO); GL.BufferData ( BufferTarget.ArrayBuffer, size, IntPtr.Zero, BufferUsageHint.StaticDraw); Table 11: 1.3 - 7 Specify VBO size // Pass positions, normals and texCoords. GL.BufferSubData (BufferTarget.ArrayBuffer, IntPtr.Zero, size3, positions); GL.BufferSubData (BufferTarget.ArrayBuffer, IntPtr (size3), size3, normals); GL.BufferSubData (BufferTarget.ArrayBuffer, IntPtr (size3 * 2), size2, texCoords); Table 12: 1.3 - 8 Load values into a VBO // Set Indices GL.BindBuffer (BufferTarget.ElementArrayBuffer, indices_BO); GL.BufferData (BufferTarget.ElementArrayBuffer, (IntPtr)(indices.length * sizeof(Int32)), indices, BufferUsageHint.StaticDraw); Table 13: 1.3 - 9 VBO Indices buffer Then, when the scene is going to be rendered, it’s only needed to bind the buffer and connect the variables in the shader with the values in the array: GL.EnableClientState( ArrayCap.VertexArray ); // enable buffer GL.BindBuffer (BufferTarget.ArrayBuffer, vertex_BO); GL.BindBuffer (BufferTarget.ElementArrayBuffer, vertex_BO); Table 14: 1.3 - 10: Use VBO
11 GL is always pointing to a buffer, this code is just telling it to point to our buffer, to use it when drawing. Now if the variables in the shader are: Attribute vec3 position; Attribute vec3 normal; Attribute vec2 texCoords; They can be set by doing this: int size = sizeof( float ) * 3; // size of a vector 3 // Position: int location = GL.GetAttribPointer ( shaderProgram, “position” ); GL.VertexAttribPointer (location, 3, VertexAttribPointerType.Float, false, size, 0); GL.EnableVertexAttribArray (location); // This is required // Normal: location = GL.GetAttribPointer (shaderProgram, “normal” ); GL.VertexAttribPointer (location, 3, VertexAttribPointerType.Float, false, size, size); GL.EnableVertexAttribArray (location); // TexCoord: location = GL.GetAttribPointer (shaderProgram, “TexCoord” ); GL.VertexAttribPointer (location, 2, VertexAttribPointerType.Float, false, sizeof(float)*2, size*2); GL.EnableVertexAttribArray (location); Table 15: 1.3 - 11 Set shader attributes The method is: GL.VertexAttribPointer (pointer, number of values to retrieve, type, Boolean (always false), size of the value, start position); The location of the attribute can be retrieved in the shader: int ID = GL.GetAttribPointer ( ShaderProgram, string name ); Example: int ID = GL.GetAttribePointer ( sp, “position” ); Now, once the buffer is loaded and pointed, to draw the object only one line required is: GL.DrawElements (BeginMode.Triangles, faces.length * 3, UnsignedInt, IntPtr.Zero); Table 16: 1.3 - 12 Draw loaded VBO After rendering the object, we should terminate the VBO to save memory: // Disable buffer GL.DisableClientState (ArrayCap.VertexArray); // Un-point the buffers GL.BindBuffer (BufferTarget.ArrayBuffer, 0); GL.BindBuffer (BufferTarget.ElementArrayBuffer, 0);
12 // Disable the attributes GL.DisableVertexAttribArray (0); // Repeat with 1, 2, 3… Then, total summary: 1 – When the object is created · Initialize lists (arrays) of values · Create the buffer · Pass data from the arrays to the buffer · Free arrays to save space (array = null;) 2 – When the object is going to be rendered · Enable · Bind buffers · Link attributes 3 – Draw 4 – End / Disable VBO and attributes 1.4 Uniforms As said previously, a uniform variable in the shader is a value that is constant for the whole model. For example, the transformation matrices. A uniform must be loaded just before drawing. First we retrieve the pointer to the variable, and then we pass the value: int loc = GL.GetUniformLocation ( ShaderProgram, “ModelMatrix”); GL.UnifromMatrix4 ( ShaderProgram, false, matrix ); * the boolean (false) is to automatically transpose the matrix if required. Only for matrices. To set the different kind of values: GL.UnifromMatrix4 for matrices GL.Uniform1 for integers, floats or booleans GL.Uniform2, GLUniform3, GLUniform4 for vectors, vec2, 3 or 4. If the uniform is an array: float[4] values; a value of the array is set like this: int loc = GL.GetUniformLocation ( ShaderProgram, “values[0]”); GL.Uniform1(loc, value);
13 Shader: uniform bool ask; uniform float[4] values; uniform vec3 direction; uniform mat4 matrix; Code: int loc = GL.GetUniformLocation ( sp, “ask” ); GL.Uniform1 (loc, true); loc = GL.GetUniformLocation ( sp, “values[0]” ); GL.Uniform1 (loc, 0.05f); loc = GL.UniformLocation ( sp, “direction” ); GL.Uniform3 (loc, new Vector3(1, 0, 2) ); loc = GL.UniformLocation ( sp, “matrix” ); GL.UniformMatrix4 (loc, mat); // having a variable called mat Table 17: 1.4 - 1 Load uniforms Initially, to draw the scene correctly, we’ll pass three uniform matrices: the view matrix (the camera perspective), the projection matrix and the model matrix (position, rotation and scale of the object). But to do any of this, first we need to build the matrices.
14 2. 3D Mesh 2.1 Mesh structure To implement the mesh structure, I chose the winged edge structure with some extensions I needed, so I prefer to call it the expanded winged edge: Figure 1: 2.1 - 1 Vertex structure Vertex The vertex has a list of incoming and outgoing edges, plus a list of faces. Figure 2: 2.1 - 2 Face structure Face The face knows its vertices and edges.
15 Figure 3: 2.1 - 3 Edge structure Edge Every edge knows its two vertices (head & tail), the next and the previous edges in the face (since all faces are triangles), its face and its brother. Each edge is duplicated, so that edge only belongs to a face and its brother belongs to the other face. That's necessary to calculate the normal of the face. To calculate the normal of a face (see Figure 2: 2.1 - 2 Face structure), calculate the cross product of any two of its edges, for example AB and AC. AB x AC = n The normal is very important, especially to avoid rendering back faces or light. Every edge would belong to two faces, since in a correct mesh any edge is always shared by two faces unless it's a border. However, in each face the edge must go in the opposite direction to calculate the normal. The edge AB for a face will be BA to the other. So, in order to calculate the normal correctly, every edge is duplicated and they are brothers to each other. Hence, the head vertex will be the tail vertex of its brother and vice versa. Finally, the class Mesh3D contains only a list of vertices and a list of faces as a list of indices referring the vertices. To draw the object we only have to access to each face of the list and retrieve the three vertices (A, B, C), which contain their positions (X, Y, Z), relative to (0,0) for now.
16 2.2 Mesh objects Now that a structure for 3D mesh is sustainable, let’s create some basic objects to be added to the scene, such as cubes, spheres, planes, cones... First of all the plane and the cube can be created manually, they are quite trivial. According to the structure described earlier, the idea is not repeating data: vertices, edges or faces. So the constructor of Mesh3D is made in a way that it receives a list of vertices and indices and it builds the edges and faces: public Mesh3D ( vertex[ ] vertices, int[ ] indices) this.vertices = vertices; this.faces = new Face [ indices.length / 3 ]; for(int i = 0; i<indices.length; i += 3) int vA = indices[i]; int vB = indices[i + 1]; int vC = indices[i + 2]; Face f = new Face ( this, vA, vB, vC ); // the Face constructor creates edges accessing the list of vertices with the indices vA, vB, vC this.faces[ i/3 ] = f; Table 18: 2.2 - 1 Mesh constructor This way, to create a mesh manually only the list of vertices and indices are needed. However, some things need to be remembered: · The indices must be in order, since every three vertices will create a face. · The order of the vertices in a face matters. The normal of the face will be automatically calculated with them as explained before. So the face A,B,C is inverse to C,B,A. So, to avoid repeating vertices, first the vertices and then the indices are declared. Code to create a plane manually: // faces are triangles, so we need two faces for a plane Vertex3D A = new Vertex3D(-0.5f, 0, -0.5f); Vertex3D B = new Vertex3D(0.5f, 0, -0.5f); Vertex3D C = new Vertex3D(-0.5f, 0, 0.5f); Vertex3D D = new Vertex3D(0.5f, 0, 0.5f); Mesh3D m = new Mesh3D( { A, B, C, D, C, B }, { 1, 2, 3, 4, 5, 6 } ); Table 19: 2.2 - 2 Create plane manually The cube is similar, but more complex. However, it can still be built manually. Nevertheless, the next figures, spheres or cones, are too complex, too many vertices and faces to write them like this.
17 So, to build them I used a tool, an .obj importer. Obj is just a 3D model format, just like jpg or png are image formats. An obj file stores the data of the model as text, it can be easily seen with NotePad. This external library from (https://github.com/chrisjansson/ObjLoader) allows to open an .obj file and retrieve its faces and vertices, but it’s written using a different structure to mine, so the structure created by this library needs to be converted. With the help of this tool, I created and exported the primitives from Blender (3D modeling program) to use them here. 2.3 Triangulate faces Now, with this tool, 3D objects written in .obj can also be imported to our scene. There's only one problem remaining: an obj may contain faces with more than three vertices. However, my program uses only three vertices per face. The model needs to be triangulated when it’s imported. To this end I chose the ear-clipping algorithm. Consists in checking if the corner formed every three vertices is concave or convex. If it's convex, a triangle can be cut. Else, check the next three vertices. Figure 4: 2.3 - 1 Ear clipping triangulation
24 · Having that 3x3 matrix and a previous matrix, to which rotation is applied: The resulting matrix will be: Figure 11: 3.1.2 - 2 Rotation matrix calculation The last column doesn't change. Each position is equal to the values (a, b, c) of that row multiplied by the values (e, f, g) of that column. But if this operation is applied to the identity matrix, even after a translation, the rotation will be: Figure 12: 3.1.2 - 3 Simplified rotation matrix calculation 3.1.3 Scale Scale is, in fact, very easy. Having a scale vector (x, y, z): Figure 13: 3.1.3 - 1 Scale matrix calculation
25 The first column is multiplied by x, the second by y and the third by z. The last column remains the same. 3.1.4 Simplified formula The full operation may seem very complicated, but if it begins with the identity matrix and only applies one translation, rotation and scale, we can know for sure that the resulting matrix will always be: Figure 14: 3.1.4 Simplified formula of the transformation matrix This way, the whole operation is not only easier to understand, but more efficient to calculate. X,Y,Z are the translation components, S is the scale vector, and e, f, g are the values calculated in Figure 10: 3.1.2 - 1 Rotation auxiliary matrix. 3.2 The view matrix Now that an object can already be placed, rotated, scaled and rendered, it would be nice to change the perspective of the point of view dynamically, so we can see the object from any angle and even zoom in and out. In other words, to have a camera. What the program will do, actually, is multiply a camera matrix to the current matrix. The GPU doesn't actually have a camera and can't change the point of view. What it will do is calculate the translation of the vertices to see the object as it would be seen from that point of view. So, instead of moving the camera, the whole scene will be deformed to fit the perspective. For now, the final Matrix = CameraMatrix x TransformationMatrix The order matters when multiplying matrices, so the camera matrix goes first. The next step is to calculate this camera matrix. This can be accomplished by doing the "LookAt" matrix operation. For it, three parameters are needed: eye (position of the camera), center (point to look at), and up (to orientate the camera):
26 Figure 15: 3.2 - 1 Camera matrix parameters As we can see, eye and center are points, while up is a vector. Having those three values the maths work as follows: Vector D (distance) = center – eye Normalize D (D = D / D.Magnitude) Vector U = up x distance Normalize U Vector V = distance x U Normalize V Table 23: 3.2 - 2 Vectors to calculate the camera matrix Knowing D, U, V and eye, the camera matrix is: Figure 16: 3.2 - 2 The camera matrix
27 3.3 The projection matrix Finally, once the object can be placed and seen from a specific point of view, we need a projection matrix, which handles the 3D perspective. It can be used either a perspective projection or an orthographic projection: Figure 17: 3.3 - 1 Difference between orthographic and perspective projection The perspective projection considers depth, while orthographic doesn’t. The matrix calculation is a bit different. Let's see first the perspective matrix, since it’s the most common of them: 3.3.1 Perspective Matrix The perspective view uses four parameters: - Near and far clipping planes. - Aspect: relation between width and height - Fovy: relation of size between the near and the far plane. Figure 18: 3.3.1 - 1 Perpective cube Figure 19: 3.3.1 - 2 Perspective matrix
28 3.3.2 Orthographic Matrix Figure 20: 3.3.2 - 1 Orthographic cube Figure 21: 3.3.2 - 2 Orthographic matrix The orthographic matrix uses a rectangle: left and right, top and bottom, far and near. Notwithstanding, considering that the cube of the projection is symmetric, then right = - left and top = - bottom, so only need four values: width, height, near and far. Figure 22: 3.3.2 - 3 Simplified orthographic matrix Unlike in the perspective view, the near and far values don't affect to the orthographic projection. The final view won't change by modifying those values, just nothing closer than near or further than far will be visible. However, unlike with the perspective view, the position of the camera isn't important, only the direction. With an orthographic view, zoom is useless. The only way to see more or less space is by changing the width and height values:
29 Figure 23: 3.3.2 - 4 Projection matrix difference for perspective and orthographic So, basically. To see more or less space of the scene, if using the perspective projection, we don’t need to update or change the projection matrix, only the camera matrix. However, if using the orthographic projection, it’s the projection matrix we need to change. Now these three matrices (transformation, view and projection) must be sent to the shader as a uniform. The projection matrix will be multiplied by the view matrix and the model matrix, and finally by the vertex position to get the final vertex position at the viewport (the screen). gl_Position = projection · viewMatrix · modelMatrix · vec4 (vpos, 1.0);
30 3.4 Hierarchy Now objects can be placed and transformed. Notwithstanding it would be even better to have a scene full of objects. In some cases we want some objects to suffer the same transformations as others: rotate, scale and move one single object and others must do the same. That's useful, for instance, if we import a 3D model and that model is composed of several separated meshes. In that case, we should be able to control the parts both together and separately. For many other reasons a hierarchy could be useful, but additionally it will serve to render the scene, using this structure and algorithm: Figure 24: 3.4 - 1 Render algorithm In order to draw a scene there’s a Root object and all the objects in the scene must be related to it, otherwise they won't be rendered. Every object will know its parent and contain a list of its children. When the parent of an object is changed, the old parent must remove it from its list of children, then the parent is changed and the new parent must add it to its list. The idea is that the transformations made to a parent must also be applied to the children. Translation is easy, just have to apply the same translation to the whole hierarchy. However, the rotation and scaling are more complicated, because those operations must be made using the parent's pivot position as origin (0,0).
31 Figure 25: 3.4 - 2 Hierarchy rotation problem When rotating, two things are needed: rotate and move. In fact, in the picture above (Figure 25: 3.4 - 2 Hierarchy rotation problem), the problem is the position of the small cube, not its rotation, that's fine. So the logic is, first we rotate each object, like in the second figure, which is wrong. And then the small cube is moved to the right position. The point can be calculated by rotating the vector from the parent position the same angle being used to rotate the object: Figure 26: 3.4 - 3 Hierarchy rotation problem solution ·1- Get the vector to the pivot: child.Position (blue) – parent.Position (red) ·2- Rotate vector: v = RotateVector (angle); ·3- Place object: child.Position = parent.Position + v;
32 If we were working with two dimensions (2D), rotating a vector would be much easier. Nevertheless, we are working with three dimensions (3D) and rotate a vector becomes much more difficult. To accomplish this we need Quaternions, and that's been the next step in the project. So let me make a break in this section to talk about Quaternions. 3.4.1 Quaternion A quaternion is just another way to express the rotation of an object, like the euler angles. A quaternion is much better for maths, however it's also much less intuitive and understandable, so it's not used for representation but it is for calculation. Rotating a vector with euler angles is, not only too much complicated and costly, but also very inexact, the steps between angles are too big and the rotation looks like framed, slow. The calculation with Quaternions is easier, faster and more accurate. A quaternion is expressed with four values: Q ( w, (x, y, z)) Given a vector V and a rotation expressed with euler anlges E. For example, if we want to rotate the vector forward (0, 0, 1) 20 degrees in the x-axis and 35 in the y-axis (20, 35, 0). The rotated vector is: rv = q · Q ( 0, v )·qi Q ( 0, v ) is the quaternion Q ( 0, (x, y, z) ) -> Q (0, (0,0,1)), this one is easy. 'q' is a quaternion built from the euler. Remember that euler angles and quaternions are both ways to represent a rotation, so we can pass from one to the other. 'qi' is the inverse of q. - Euler into quaternion This step is very similar to the calculation to get the axis of an euler, explained earlier in 3.1.2 Rotation. With an Euler (x, y, z) cx = cos(x/2), cy = cos(y/2), cz = cos(z/2), sx = sin(x/2), sy = sin(y/2), sz = sin(z/2) a = sx cy cz + cx sy sz; b = cx sy cz + sx cy sz; c = cx cy sz – sx sy cz w = cx cy cz – sx sy sz; Q ( w, (a,b,c) ) Table 24: 3.4.1 - 1 Euler to quaternion *But in this case, the angles in the cosines and sinus are divided by 2.
33 - The inverse of the quaternion - The inverse of a quaternion is Q ( w/norm , conjugate/norm ) - Norm = - Conjugate = Q ( w, -v ) Table 25: 3.4.1 - 2 Inverse, norm & conjugate of a quaternion For example, for the quaternion ( 1, (1, 2, -1) ), the conjugate is ( 1, (-1, -2, 1) ) and the norm is 1 + 1 + 4+ 1 = 7. So the inverse is Q ( 1 / 7, (-1, -2, 1)/7 ). - Multiply quaternions Having two quaternions A and B: w = A.w · B.w - A.v · B.v // Where A.v · B.v is the Dot product of them. v = A.v x B.v + A.v·B.w + B.v·A.w // A.v x B.v is the cross product. Table 26: 3.4.1 - 3 Multiply quaternion The quaternion is Q ( w, v ). Let's see a walkthrough to rotate the vector forward (0, 0, 1) with the euler (0, 20, 0): rotatedVector = q · Q ( 0, v )·qi Table 27: 3.4.1 - 4 Rotate vector with Quaternion Q ( 0, v ) = Q ( 0, (0, 0, 1) ) q, making the operation explained before: Q ( 0.94, ( 0, 0.342, 0) ) the inverse of q: the Norm value is 1 (1.000564 exactly) so the inverse is: Q ( 0.94 / 1, (0, -0.342 / 1, 0) = Q (0.94, (0, -0.342, 0) ) rotatedVector = Q (0.94, (0, 0.342, 0)) ·Q (0, (0, 0, 1))·Q(0.94, (0, -0.342, 0)) rotatedVector = Q (0, (0.342, 0, 0.94)) · Q(0.94, (0, -0.342, 0)) rotatedVector = Q (0, (0.643, 0, 0.766)) So the vector (0, 0, 1) rotated 20 degrees is (0.643, 0, 0.766)
40 5. Illumination In the future, textures and shadows will be implemented, so the objects in the scene will be easier to difference. However, right now the objects are shadeless, which means they are painted with a plane color that makes harder to distinguish their shape. Figure 30: 5.0 - 1 Shadeless cube So for now let’s add illumination, what will set a different brightness to each fragment of the object depending on the lights and the surface of the object will vary its brightness. 5.1 Light types Three kinds of lights are going to be implemented: ·Directional lights (suns): their position doesn't matter, only the direction ·Point lights / Spot lights (bulbs): The position matters. ·The Ambient light (environment light): adds light to the whole scene equally.
41 5.1.1 Directional lights For these lights only the forward vector of the light, its direction, needs to be considered. The forward vector is automatically updated when rotating the object. Every mesh has a color as an attribute, the object is painted with that color. Later on, objects will be painted with textures, but for now objects are painted with a single color. The idea is setting the brightness depending on the angle between the face and the light. Figure 31: 5.1.1 - 1 Directiona light concept If the face isn't facing the light (angle between the normal and the vector to the light greater than 90°), it will be completely dark. Otherwise, when the face is facing the light, the angle can be used to interpolate the brightness. A color in rgb is formed by three components red, green and blue (not considering alpha). So the following algorithm is used: float angle = angleBetween ( - normal, light.forward ); float factor; if (angle >= 90) factor = 0; else factor = 1 - (angle / 90) FinalColor = color * factor * intensity ; Table 33: 5.1.1 - 1 Directional light calculation Additionally, the light should be able to be controlled with an intensity value. This value is multiplied by the factor ( factor x intensity ). If the intensity is increased too much, the color should become white because, indeed, when there's too much light everything becomes white. If the intensity is 1, when the light incises directly it returns the natural color of the object. If the
42 intensity is smaller than 1, the color will always be darker. But if the intensity is greater than 1, there may be the problem that RGB multiplied by the factor is greater than 255. So, the color components (R, G, B) must be clamped, limited between 0 and 255. R *= factor; If (R > 255) R = 255; Same with G and B. For instance, if the intensity is 1.5 and the light incises directly and the color of the object is (35, 211, 242) it would become (53, 255, 255). Notwithstanding, in GLSL the color components are a value between 0 and 1, because they’re expressed like a percentage: 0.5 = 255/2, 1 = 255. So the clamping method would be: color *= intensity; float r = color.x; if (r > 1) r = 1; float g = color.y; if (g > 1) g = 1; float b = color.z; if (b > 1) b = 1; Table 34: 5.1.1 - 2 Clamp color 5.1.2 Point lights Normally point lights and spot lights are different lights. However I realized that they are quite the same, except the spot has an angle: *Images not from my program Therefore, I preferred to add an "angle" attribute to the point light, which by default will be 360° and the light will work as a normal point light. Adjusting the angle a point light can be turned into a spot. In addition to the intensity and angle, the point light uses two more values: range and function. Range is the distance that it reaches. Its light won't reach
43 any further than that. Function is a value that specifies how fast the light vanishes. That means, how the light decreases with distance within that range. The algorithm would be: // if the distance surpasses the range, stop float dist = distance (object, light); if (dist > Range) return; // if the angle surpasses the range, stop float angle = angleBetween (direction, lightToFragment); if (angle > Angle) return; // the factor from 0 to 1, depending on the distance and angle float factor = dist / Range; factor *= 1 – (angle / Angle); // elevate factor by the function value // since factor goes from 0 to 1, it can only become smaller to 0 // this produces the light to become more concentrated to the center factor = factor ^ function; FinalColor = color * factor * intensity; Table 35: 5.1.2 - 1 Point light calculation 5.1.3 Ambient light The ambient light adds the same light with the same color and intensity to the whole scene equally. It's the default environment light. So it’s pretty easy. It’s just a color and an intensity value. FinalColor = ambientColor * ambientIntensity; Table 36: 5.1.3 - 1 Ambient light calculation 5.2 Combine lights Finally, all lights must be combined on a fragment: directional lights + point lights + ambient light. If only intensities mattered, the solution would be adding them. However, as there are also colors, these need to be combined. If all colors had the same intensity, the mean of them would be used. But intensities work as weights. So the idea is calculating the mean of the colors multiplied by its intensity: for (int i = 0 …. lights.count ) FinalColor += lights[ i ].Calculate( ); // color * intensity FinalColor /= lights.count; FinalColor = (FinalColor + material or texture) / 2; Table 37: 5.2 - 1 Combine lights
44 To finish, the light resultant color is mixed with the material color or the texture. 5.3 Implementing lights in the shader Lights must be sent into the shader. The ambient light is not a problem, because there's only one and it's simple. However there may be several and undefined directional or point lights. Hence, we need an array of lights in the shader. In GLSL structs can be defined: struct DirectionalLight { vec3 Direction; float Intensity; vec3 Color; } DirectionalLight dlights[ ]; Table 38: 5.3 - 1 Structs in the shader But there’s another problem here, because in the shader arrays can't be dynamic. This means that the length of the array must be fixed and that there must be a specific number of lights: DirectionalLight dlights[ 4 ]; // For example A Boolean "active" can be added to the struct, so only some of the lights are used, not all of them: struct DirectionalLight { boolean Active; // if false, light isn’t computed This way, only some of the lights available would be used. The problem is yet, that the GPU is storing the space for those lights, even if they are not being used. That's why an array of... a hundred lights, for example, shouldn’t be defined. Furthermore, computing lights is costly, and even more later when shadows will be implemented. So, we’re going to use only one directional light, one sun, and up to 4 point lights. The sun isn’t important, normally a scene will only implement one or two suns at the same time. And with point lights there is a trick: if the array of point lights is a uniform variable, remember that uniforms are loaded before drawing each object unless they are always the same, like the projection matrix. All of this is explained in 1.4 Uniforms. So a partial but not perfect solution is, for each object, load the four (or x) closest point lights. So there may be unlimited point lights in the scene but each object would only use the four closest point lights in the shader. uniform float ambientIntensity; uniform vec3 ambientColor;
45 struct DirectionalLight{ float intensity; float color; float direction; } uniform DirectionalLight sun; struct PointLight{ float intensity; float color; float direction; } uniform PointLight sun[4]; Table 39: 5.3 - 2 Lights in shader 6. Textures 6.1 Create the texture Textures in OpenGL consist of two steps: loading the bitmap, the file or whatever from a specific source, and loading the bitmap into the GPU. The first part won’t be explained here because it’s not part of OpenGL. Depending on the environment or the libraries used, it will be different. In C# one can open a .jpg or .png file using the classes Image and Bitmap. Once that is done, this is the process to create a texture in OpenGL: // Create texture int pointer; pointer = GL.GenTexture( ); // Point the created texture for the next settings GL.BindTexture(TextureTarget.Texture2D, pointer); // This is specific for C#, not OpenGL Bitmap bitmap = new Bitmap (img); BitmapData data = bitmap.LockBits( new Rectange(0,0,width,height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // Now the pixels are stored in data // Configure the image GL.TexImage2D (TextureTarget.Textuer2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.scan0); // here the pixels are passed // TEXTURE SETTINGS GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureMagFilter.Linear);
46 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, TextureMagFilter.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, TextureMagFilter.Repeat); Table 40: 6.1 - 1 Create GL Texture GL.GenTexture( ) creates the texture and returns the pointer. GL.TexImage2D(…) configures the image and sets the data (the pixels, the image). GL.TexParameter is used to configure the texture. A possible setting for the texture is the min and mag filters. This is, the interpolation mode when changing the size of the texture (greater or smaller). In the previous Table 40: 6.1 - 1 Create GL Texture, the option is set as Linear, this is a linear interpolation. Another option is Nearest, which means no interpolation, it takes the closest pixel. The other option is the S/T Wrapping. That’s what happens when the texture coordinates are greater than 1 or smaller than 0, since GL uses relative coordinates: Figure 32: 6.1 - 1 Texture relative coordinates In the Table 40: 6.1 - 1 Create GL Texture, the wrapping setting is set as Repeat, this means that the coordinate is corrected. For example, 1.3 = 0.3, 2.6 = 0.6, - 0.7= 0.3. Another option is ClampToEdge, then the last closest pixel to the edge is taken: 1.3 = 1, 2.6 = 1, -0.7 = 0.
47 6.2 Use of textures in OpenGL Textures don’t work as the rest of uniform data types, although they are defined the same way: uniform Sampler2D texture; // this is a texture However, a texture is not sent and loaded to the shader like any other uniform. The GPU can use up to 32 textures at the same time. Those 32 values are in fact pointers to textures. What the programmer can do is set the pointers to those textures: int texture1, texture 2; // pointer to texture … create textures GL.ActiveTexture (0); GL.BindTexture (TextureTarget.Texture2D, texture1); GL.ActiveTexture (1); GL.BindTexture (TextureTarget.Texture2D, texture2); Table 41: 6.2 - 1 Bind textures After this, the texture unit 0 is pointing texture1 and the texture unit 1 is pointing texture2. In the shader there could be, for instance: uniform Sampler2D textureA; // 0 uniform Sampler2D textureB; // 1 Finally, the texture creation and the texture binding doesn’t need to happen at the same time. In fact, the texture should only be created at the beginning of the program, and the binding should be done every time before using the texture. 6.3 Textures in the shader First of all, a texture needs texture coordinates. Remember, as explained in 2.2 Mesh objects, that objects are being imported from .obj files, so the texture coordinates are taken from there. The texture coordinates go from 0 to 1, relative to the image. (See Figure 32: 6.1 - 1 Texture relative coordinates). Now the textures coordinates must be passed to the shader as an attribute (attribute vec2 texCords). How to do this is explained in 1.3 Buffer Objects (VBO).
48 In the vertex shader: attribute vec2 texCords; … out vec2 TexCords; // sent to the fragment shader and interpolated … void main( ) { … TexCords = texCords; } Table 42: 6.3 - 1 Textures in the vertex shader In the fragment shader in vec2 TexCords; uniform Sampler2D texture; … void main( ) { // calculate light color … // pixel of the texture vec4 tex = texture2D (texture, TexCords); // combine color and texture FinalColor = (lightColor + tex) /2; } Table 43: 6.3 - 2 Textures in the fragment shader Figure 33: 6.3 - 1 Render with textures
49 6.4 Skybox A skybox is a texture in the background simulating to be the horizon. Like this: Figure 34: 6.4 - 1 Skybox example The skybox is, indeed, a box, a cube. The skybox pretend to be very far, at the horizon. But in fact, it’s a small box around the camera, and the rest of objects in the scene can be seen through it: Figure 35: 6.4 - 2 Skybox graphical explanation To draw through an object, the OpenGL depth test must be disabled. The depth test discards the fragments behind another. GL.DepthMask (false);
56 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, TextureMagFilter.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, TextureMagFilter. ClampToEdge); The texture creation and setting is already explained in 6.1 Create the texture. Finally the texture needs to be connected to the framebuffer, indicate the framebuffer to draw on that texture: // The framebuffer must be bound before changing settings GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBPointer); GL.FramebufferTexture (FramebufferTarget.Texture2D, FramebufferAttachment.ColorAttachment0, texture, 0); And the depth framebuffer needs two more settings: GL.DrawBuffer (DrawBufferMode.None); GL.ReadBuffer (DrawBufferMode.None); Finally, whenever this framebuffer needs to be used it must be bound: GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBPointer); Table 46: 7.2.2 - 1 Create and change framebuffer 7.2.3 Render the scene GL.Viewport (1024, 1024); // size of the depth map Bind depth frambuffer and use Depth shader Render scene normally // Now the depth map is in the depth texture GL.Viewport (800, 600); // Size of the screen Bind normal framebuffer and normal shader Bind depthMap texture to use in shader Render scene normally Table 47: 7.2.3 - 1 Render loop with depth map The viewport is the size of the framebuffer. It must fit the size of the texture. The depth map was created 1024x1024. That was completely my decision, the depth map can have any size. The higher the resolution, the better the quality of the shadow. Next, the framebuffer and shader for the depth map must be activated, and then the scene can be rendered. But before, the uniform matrices and the VBO of the objects must be loaded, just like the normal render and as
57 explained along the document. Remember that the depth render must use the orthographic matrix, not the perspective projection. Then return to the normal viewport size, framebuffer and shader, and the depth map rendered previously is bound to be used as a uniform. 7.3 Implementing shadow Mapping Vertex shader: uniform mat4 lightMatrix; out vec4 lightFragPos; … void main ( ) { lightFragPos = perspective * lightMatrix * model * vec4(v, 1.0); } Table 48: 7.3 - 1 Vertex shader with shadow mapping Fragment shader: uniform Sampler2D depthMap; in vec4 lightFragPos; … float shadowMapping() { float depth_in_map = texture(depthMap, lightFragPos.xy).z; float depth_in_frag = lightFragPos.z; if (depth_in_frag > depth_in_map) return 0; return 1; } void main( ) { // calculate light color … lightColor *= shadowMapping(); FinalColor = (lightColor + texture2D(texture, texCords)) /2; } Table 49: 7.3 - 2 Fragment shader with shadow mapping
58 Figure 40: 7.3 - 1 Shadow mapping render 7.4 Shadow mapping acne / bias Once all of this is done, the resulting shadow shows the following problem: Figure 41: 7.4 - 1 Shadow mapping acne This problem is called acne. This is caused because of a lack of precision. In theory, the depth map stores the exact depth or distance. But in the practice,
59 the depth has a minimal ± error. Nevertheless this problem can be solved with a little offset value called bias: float BIAS = 0.005f; // for example if (depth_in_frag > depth_in_map - BIAS) Table 50: 7.4 - 1 Solve shadow acne with bias 7.5 Shadow mapping anti-aliasing Figure 42: 7.5 - 1 Shadow mapping antialiasing The resultant shadow of the shadow mapping can be a bit aliased. There are several solutions to this problem: a) Increase the resolution of the depth map. b) Adjust the orthographic view to fit the scene the most accurately as possible. c) PCF (Sample blurring) Increasing the resolution isn’t as costly as it could sound, because a depth map is quite easier to generate than a color map. So the resolution 1024x1024 could be increased to 4096x4096 even. The orthographic matrix indicates how much of the scene is visible by the depth map:
60 Figure 43: 7.5 - 2 Orthographic size for depth map If the dimensions of the orthographic view are greater but the resolution of the map is the same, more pixels are unused and the required zone has a low resolution. PCF is an acceptable technique, but costly. It simply consists in blurring the shadow by taking samples around and calculating the mean of them. Figure 44: 7.5 - 3 PCF Shadow Mapping int level = 2; // PCF x2 vec2 depthCords; // coordinates of the fragment in the depth map float mean = 0; for (int i = -level; i<level; ++i) for (int j = -level; j<level; ++j) mean += shadowAt(depthCords + vec2( i / a, j / aa ) ); mean /= level * level; Table 51: 7.5 - 1 Shadow antialiasing ‘a’ is a large value. Remember that the texture coordinates are relative, it’s a value between 0 and 1, while i and j vary between –level and +level. Therefore, the displacement vector (i, j) needs to be enormously reduced to
61 values like 0.001 or so. It’s not important to get the exact pixel next to the other, just one very close. This method, though it’s expensive, there are some ways to reduce the cost: a) Finding borders Right now PCF is applied to the whole depth map. That’s useless, since PCF isn’t needed there where everything is black or white, dark or not. Only in borders. A way to check that is to manually calculate the shadow at the center and two opposite corners of the area. If the three values are 0, there is not a shadow there , so return 0. If the three values are 1, there is a shadow, so return 1. Only when the three of them aren’t equal, the PCF must be calculated. Figure 45: 7.5 - 4 Reduce PCF cost: find borders float sc = shadowAt (cords); float sa = shadowAt (cords + vec2(level / a, level / a)); float sb = shadowAt (cords + vec2(-level / a, -level / a)); If (sc == sa && sc == sb) { if (sc == 0) return 0; return 1; } // Calculate PCF and avoid calculating those fields again. Table 52: 7.5 - 2 Reduce PCF cost: find borders
62 b) Reducing the area to a circle Figure 46: 7.5 - 5 Reduce PCF to a circle That loop is taking samples from a quadratic area. However that’s unnecessary. In fact, the result would be better using a circle instead. Considering the half width of the area as the radius, if the module / length of the vector (i, j) is greater than the radius, discard that field. int level = 2; // this is the radius vec2 depthCords; float mean = 0; float count = 0; for (int i = -level; i<level; ++i) for (int j = -level; j<level; ++j) if (length(vec2(i,j)) > level) continue; ++count; mean += shadowAt(depthCords + vec2( i / a, j / aa ) ); mean /= count; Table 53: 7.5 - 3 Reduce PCF to a circle Now (level x level) isn’t the amount of checked fields, so better use a counter.
63 8. Post-processing Post-processing is the process of applying effects to the final rendered screen, so it consists in working with a 2D texture instead of the 3D scene. With post-processing multiple effects can be achieved. Next some of them are explained, the ones implemented. However, the texture is already in the GPU, not the CPU. So it’s not affordable to bring it back to the CPU, work it at high level and send it back to the GPU. Instead, post-processing uses also a shader. If a shader is used, it can only work with fragments and vertices, but we need to work with a texture and pixels. What is done to solve this problem, is render a single plane right in front of the camera, using as a texture the scene rendered previously. So what the camera sees isn’t the real scene, but a plane “wearing” the view image, similar to watching a landscape in a TV. To accomplish this, we need another framebuffer, texture and shader. This is already explained in 7.2.2 Change the framebuffer, read that chapter for the details. But let’s make a review to understand better. A framebuffer renders into a texture. Until now there were two framebuffers, one to render into the depth map with the depth shader, and another to render into the screen with the normal shader. Now the idea is implementing another framebuffer to draw the scene into a color texture using the normal shader, and the framebuffer that renders into the screen would only render the plane with the post-processing shader. This would be the order: · Depth framebuffer renders depth map. Depth shader · Scene framebuffer renders the scene into the texture. Normal shader · Screen framebuffer renders a plane with the previous scene. Post-pro shader The post-processing shader will apply the effects. Besides that, geometrically it’s very simple. It doesn’t need shadows nor illumination or anything else. Just the vertex position and texture coordinates to display the plane and the image. Additionally, to apply some effects, we also need the depth map from the camera. Until now the depth map was only rendered from the light, for the shadow mapping. Now it’s also necessary to render the depth map from the camera, in order to accomplish some of the effects. For commodity, we won’t create another framebuffer and texture. The same framebuffer and texture used to render the light depth map can be used
64 here. Nonetheless the texture would be overwritten. Hence, this should be the order of the scene rendering: 1º Render light depth map 2º Render scene normally, using the depth map 3º Render camera depth map overwrite the depth map 4º Render plane and do post-processing This way, when the depth map texture is overwritten in the 3º step, it’s already been used in the 2º step to render the scene and it won’t matter. To render the camera depth map, the process is the same as for the light, as explained in scene , but using the camera matrix, very simple. Then, the 7.2.3 Render the post-processing shader will use two uniform samplers (textures): uniform Sampler2D sceneView, uniform Sampler2D sceneDepthMap. Later on, the full code will be explained, for now let’s see the different postprocessing effects and how to accomplish them. Remember: all of these effects are made in the post-processing shader. These effects are made in each fragment, working with the color. So the idea would be to apply effect over effect: void main() FragmentShader drawing the plane { vec4 color = texture2D (sceneTex, cords); Color = effec1(color); Color = effect2(color); Color = effect3(color); ClampColor(color); R,G,B between 0 and 1 (255) }
65 8.1 Post-processing effects 8.1.1 Cartoon outline The cartoon border is in fact very simple: using the depth map, we see the depth difference between a specific pixel and the ones around. If the difference is very small, that fragment is in a surface. However, if the depth difference is very big (or just bigger than a limit), the pixel is in a border: Figure 47: 8.1.1 - 1 Find borders in image The process is very important, not only to this effect but for most of them, it’s similar to the shadow antialiasing: check the pixels around: float depthAtCenter = texture(depthMap, cords).z; for (int i = -samples; i<samples; ++i) for (int j = -samples; j<samples; ++j) float depthHere = texture (depthMap, cords + displacement).z; float diff = depthAtCenter – depthHere; if (diff < 0) diff *= -1; if (diff > THRESHOLD) return BLACK; // or any border color return color; Table 54: 8.1.1 - 1 Outline effect code If a depth difference higher than THRESHOLD is found, black (the color of the border) is returned. If it’s found, the sampling is less expensive because the function is terminated before.
72 8.1.8 Visualize the depth map Figure 57: 8.1.8 - 1 Visualize depth map This is easy but the only disadvantage is that all the calculations for lights and shadows are finally unused and useless. vec4 c = texture2D (depthMap, cords); float v = c.x * c.x * c.x; return vec4(v, v, v, 1.0); Table 60: 8.1.8 - 1 Visualize depth map code If texture2D( ) is used on a depth map instead of texture( ), a color is obtained. Then the problem is still that the difference isn’t very big. That means that the difference of color between the front and the background is small and everything looks too white. Then, remembering that here the RGB components are a value between 0 and 1, by multiplying the component by itself it will be reduced. For example, a 0.2 color is very close to black (0). 0,2 x 0,2 = 0,04. It becomes a very small value. 0.9 is very close to white (1). 0,9 x 0,9 = 0,81. It decreases but not that much. This way the differences are increased, and there’s more contrast, more difference of brightness between the front (black) and the background (white).
73 Conclusions Achievements At the end of this project I’ve achieved a DLL library capable of rendering a scene, importing obj files that can be modeled somewhere else. The library includes: - Mesh structures - Connectivity with other libraries (OpenTK and OBJParser) - Lights and shadows - Textures and UV Coordinates - Post-processing The library is importable and usable from any other project in C#, and a scene can easily be defined and created. Apart from the project, I’m more interested in this memory where I’ve collected everything that I’ve learnt, and explained it quite simple. Furthermore, while I wrote this very same document I reflected about itself, about the algorithms and techniques and realized about certain improvements or mistakes. And, as I’m interested into this subject (GL and graphics), I’ve enjoyed writing. I see this document more like a book or a guide, rather than a memory. Besides, other works like games or modeling are more impressive to display. My work isn’t really interesting or attractive to watch. In fact, the interesting part of this project is in this document, not in the code.
74 Final images What have I learnt? My goal was to deepen into OpenGL and computer graphics. Partially I achieved that, of course, now my knowledge and understanding on the subject is much greater. But I’ve also learnt several things: - There’s still much more to learn about the subject. What I’ve learnt is just a little piece of the puzzle. - It’s hard to find information about advanced and professional algorithms on the subject. Most of the information found in internet is
75 about the most basic algorithms and they used to be explained conceptually, the code is vaguely a help. - When working with graphics, the problem isn’t achieving something, but making it efficient. Efficiency and speed are a continuous concern and threat. Project opinion I chose this subject for various reasons. First, to understand the basics and performance of computer graphics. Not only conceptually like we do at class, but also learn how to implement it. I’m quite happy about that. Secondly, because engine programming seems a very important and interesting sector to me, perhaps I’d like to work there some day. Most of people thought this project was too much work, but I think it’s been quite appropriate. I think it wasn’t too much or too few. Generally I’m happy I chose this subject, although along the semester some other ideas came to me and it’s a pity I couldn’t make them all. Deviation from the proposal There’s been important deviations from the original proposal. Mainly, some chapters I finally didn’t include or changed for others. For example, I wanted to include a chapter with physics, where I’d implement basic physics for my objects, like gravity and simple collisions. Of course, I realized that physics in a render engine doesn’t make much sense. So, instead, I preferred to develop the post-processing, which has been much more profitable and according to the subject. I also included a chapter for raycasts and bounding volumes. That part is actually implemented, I developed it and it’s done. But I decided not to include it in this document, not only because it’s already too long but also because, as said, it makes nothing to see with graphics. Talking about GL and rendering, raycasts and bounding volumes aren’t very relevant. Project applications The idea was to make a library that could serve as a base to develop other programs, like a game engine, a modeling program or so. That is partially achieved. The library can indeed be imported to another project and visualize scenes, plus some other features. However, there are still many other functionalities missing, and the library should be expanded.
76 Possible expansion paths First of all, efficiency must be greatly improved. I already identified some techniques to do so, that unfortunately I couldn’t implement: - Occlusion - Deferred shading Deferred shading is very interesting. It consists in, instead of processing the lights, shadows and other aspects of every fragment, first the scene is fully rendered very fast without any of that. But not only the color map, but also the depth map and any other necessary. Then, the lights and shadows are calculated over the image, more or less like post-processing, using the previous calculated maps. This method avoids processing discarded fragments. Regarding the capabilities of the library, I’d like to implement wireframe, to visualize edges and vertices, click detection, real-time mesh editing, for example, dragging vertices to edit the mesh and export a new obj. Anyway, there’s still much to be done, but generally I’m quite happy with the result of the project. References and bibliography http://www.opengl-tutorial.org https://www.opengl.org https://learnopengl.com/ https://github.com/opentk http://codeflow.org https://www.gamedev.net https://computergraphics.stackexchange.com https://stackoverflow.com https://www.khronos.org