LearnOpenGL

Translation in progress of learnopengl.com.
git clone https://git.mtkn.jp/LearnOpenGL
Log | Files | Refs

Mesh.html (11976B)


      1     <div id="content">
      2     <h1 id="content-title">Mesh</h1>
      3 <h1 id="content-url" style='display:none;'>Model-Loading/Mesh</h1>
      4 <p>
      5   With Assimp we can load many different models into the application, but once loaded they're all stored in Assimp's data structures. What we eventually want is to transform that data to a format that OpenGL understands so that we can render the objects. We learned from the previous chapter that a mesh represents a single drawable entity, so let's start by defining a mesh class of our own.
      6 </p>
      7 
      8 <p>
      9   Let's review a bit of what we've learned so far to think about what a mesh should minimally have as its data. A mesh should at least need a set of vertices, where each vertex contains a position vector, a normal vector, and a texture coordinate vector. A mesh should also contain indices for indexed drawing, and material data in the form of textures (diffuse/specular maps).
     10 </p>
     11 
     12 <p>
     13   Now that we set the minimal requirements for a mesh class we can define a vertex in OpenGL:
     14 </p>
     15 
     16 <pre><code>
     17 struct Vertex {
     18     glm::vec3 Position;
     19     glm::vec3 Normal;
     20     glm::vec2 TexCoords;
     21 };
     22 </code></pre>
     23 
     24 <p>
     25   We store each of the required vertex attributes in a struct called <fun>Vertex</fun>. Next to a <fun>Vertex</fun> struct we also want to organize the texture data in a <fun>Texture</fun> struct:
     26 </p>
     27 
     28 <pre><code>
     29 struct Texture {
     30     unsigned int id;
     31     string type;
     32 };  
     33 </code></pre>
     34 
     35 <p>
     36   We store the id of the texture and its type e.g. a diffuse or specular texture.
     37 </p>
     38 
     39 <p>
     40   Knowing the actual representation of a vertex and a texture we can start defining the structure of the mesh class:
     41 </p>
     42 
     43 <pre><code>
     44 class Mesh {
     45     public:
     46         // mesh data
     47         vector&lt;Vertex&gt;       vertices;
     48         vector&lt;unsigned int&gt; indices;
     49         vector&lt;Texture&gt;      textures;
     50 
     51         Mesh(vector&lt;Vertex&gt; vertices, vector&lt;unsigned int&gt; indices, vector&lt;Texture&gt; textures);
     52         void Draw(Shader &shader);
     53     private:
     54         //  render data
     55         unsigned int VAO, VBO, EBO;
     56 
     57         void setupMesh();
     58 };  
     59 </code></pre>
     60 
     61 <p>
     62   As you can see, the class isn't too complicated. In the constructor we give the mesh all the necessary data, we initialize the buffers in the <fun>setupMesh</fun> function, and finally draw the mesh via the <fun>Draw</fun> function. Note that we give a shader to the <fun>Draw</fun> function; by passing the shader to the mesh we can set several uniforms before drawing (like linking samplers to texture units).
     63 </p>
     64 
     65 <p>
     66   The function content of the constructor is pretty straightforward. We simply set the class's public variables with the constructor's corresponding argument variables. We also call the <fun>setupMesh</fun> function in the constructor:
     67 </p>
     68 
     69 <pre><code>
     70 Mesh(vector&lt;Vertex&gt; vertices, vector&lt;unsigned int&gt; indices, vector&lt;Texture&gt; textures)
     71 {
     72     this-&gt;vertices = vertices;
     73     this-&gt;indices = indices;
     74     this-&gt;textures = textures;
     75 
     76     setupMesh();
     77 }
     78 </code></pre>
     79 
     80 <p>
     81   Nothing special going on here. Let's delve right into the <fun>setupMesh</fun> function now.
     82 </p>
     83 
     84 <h2>Initialization</h2>
     85 <p>
     86   Thanks to the constructor we now have large lists of mesh data that we can use for rendering. We do need to setup the appropriate buffers and specify the vertex shader layout via vertex attribute pointers. By now you should have no trouble with these concepts, but we've spiced it up a bit this time with the introduction of vertex data in structs:
     87 </p>
     88 
     89 <pre><code>
     90 void setupMesh()
     91 {
     92     <function id='33'>glGenVertexArrays</function>(1, &VAO);
     93     <function id='12'>glGenBuffers</function>(1, &VBO);
     94     <function id='12'>glGenBuffers</function>(1, &EBO);
     95   
     96     <function id='27'>glBindVertexArray</function>(VAO);
     97     <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, VBO);
     98 
     99     <function id='31'>glBufferData</function>(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);  
    100 
    101     <function id='32'>glBindBuffer</function>(GL_ELEMENT_ARRAY_BUFFER, EBO);
    102     <function id='31'>glBufferData</function>(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), 
    103                  &indices[0], GL_STATIC_DRAW);
    104 
    105     // vertex positions
    106     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(0);	
    107     <function id='30'>glVertexAttribPointer</function>(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
    108     // vertex normals
    109     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(1);	
    110     <function id='30'>glVertexAttribPointer</function>(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
    111     // vertex texture coords
    112     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(2);	
    113     <function id='30'>glVertexAttribPointer</function>(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
    114 
    115     <function id='27'>glBindVertexArray</function>(0);
    116 }  
    117 </code></pre>
    118 
    119 <p>
    120   The code is not much different from what you'd expect, but a few little tricks were used with the help of the <fun>Vertex</fun> struct.
    121 </p>
    122 
    123 <p>
    124   Structs have a great property in C++ that their memory layout is sequential. That is, if we were to represent a struct as an array of data, it would only contain the struct's variables in sequential order which directly translates to a float (actually byte) array that we want for an array buffer. For example, if we have a filled <fun>Vertex</fun> struct, its memory layout would be equal to:
    125 </p>
    126 
    127 <pre><code>
    128 Vertex vertex;
    129 vertex.Position  = glm::vec3(0.2f, 0.4f, 0.6f);
    130 vertex.Normal    = glm::vec3(0.0f, 1.0f, 0.0f);
    131 vertex.TexCoords = glm::vec2(1.0f, 0.0f);
    132 // = [0.2f, 0.4f, 0.6f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f];
    133 </code></pre>
    134 
    135 <p>
    136   Thanks to this useful property we can directly pass a pointer to a large list of <fun>Vertex</fun> structs as the buffer's data and they translate perfectly to what <fun><function id='31'>glBufferData</function></fun> expects as its argument: 
    137 </p>
    138 
    139 <pre><code>
    140 <function id='31'>glBufferData</function>(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices[0], GL_STATIC_DRAW);    
    141 </code></pre>
    142 
    143 <p>
    144   Naturally the <code>sizeof</code> operator can also be used on the struct for the appropriate size in bytes. This should be <code>32</code> bytes (<code>8</code> floats * <code>4</code> bytes each).
    145 </p>
    146 
    147 <p>
    148   Another great use of structs is a preprocessor directive called <code>offsetof(s,m)</code> that takes as its first argument a struct and as its second argument a variable name of the struct. The macro returns the byte offset of that variable from the start of the struct. This is perfect for defining the offset parameter of the <fun><function id='30'>glVertexAttribPointer</function></fun> function:
    149 </p>
    150 
    151 <pre><code>
    152 <function id='30'>glVertexAttribPointer</function>(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));  
    153 </code></pre>
    154 
    155 <p>
    156   The offset is now defined using the <fun>offsetof</fun> macro that, in this case, sets the byte offset of the normal vector equal to the byte offset of the normal attribute in the struct which is <code>3</code> floats and thus <code>12</code> bytes.
    157 </p>
    158 
    159 <p>
    160   Using a struct like this doesn't only get us more readable code, but also allows us to easily extend the structure. If we want another vertex attribute we can simply add it to the struct and due to its flexible nature, the rendering code won't break.
    161 </p>
    162 
    163 <h2>Rendering</h2>
    164 <p>
    165   The last function we need to define for the <fun>Mesh</fun> class to be complete is its <fun>Draw</fun> function. Before rendering the mesh, we first want to bind the appropriate textures before calling <fun><function id='2'>glDrawElements</function></fun>. However, this is somewhat difficult since we don't know from the start how many (if any) textures the mesh has and what type they may have. So how do we set the texture units and samplers in the shaders?
    166 </p>
    167 
    168 <p>
    169   To solve the issue we're going to assume a certain naming convention: each diffuse texture is named <code>texture_diffuseN</code>, and each specular texture should be named <code>texture_specularN</code> where <code>N</code> is any number ranging from <code>1</code> to the maximum number of texture samplers allowed. Let's say we have 3 diffuse textures and 2 specular textures for a particular mesh, their texture samplers should then be called: 
    170 </p>
    171 
    172 <pre><code>
    173 uniform sampler2D texture_diffuse1;
    174 uniform sampler2D texture_diffuse2;
    175 uniform sampler2D texture_diffuse3;
    176 uniform sampler2D texture_specular1;
    177 uniform sampler2D texture_specular2;
    178 </code></pre>
    179 
    180 <p>
    181   By this convention we can define as many texture samplers as we want in the shaders (up to OpenGL's maximum) and if a mesh actually does contain (so many) textures, we know what their names are going to be. By this convention we can process any amount of textures on a single mesh and the shader developer is free to use as many of those as he wants by defining the proper samplers.
    182 </p>
    183 
    184 <note>
    185   There are many solutions to problems like this and if you don't like this particular solution it is up to you to get creative and come up with your own approach.
    186 </note>
    187 
    188 <p>
    189   The resulting drawing code then becomes:
    190 </p>
    191 
    192 <pre><code>
    193 void Draw(Shader &shader) 
    194 {
    195     unsigned int diffuseNr = 1;
    196     unsigned int specularNr = 1;
    197     for(unsigned int i = 0; i &lt; textures.size(); i++)
    198     {
    199         <function id='49'>glActiveTexture</function>(GL_TEXTURE0 + i); // activate proper texture unit before binding
    200         // retrieve texture number (the N in diffuse_textureN)
    201         string number;
    202         string name = textures[i].type;
    203         if(name == "texture_diffuse")
    204             number = std::to_string(diffuseNr++);
    205         else if(name == "texture_specular")
    206             number = std::to_string(specularNr++);
    207 
    208         shader.setFloat(("material." + name + number).c_str(), i);
    209         <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, textures[i].id);
    210     }
    211     <function id='49'>glActiveTexture</function>(GL_TEXTURE0);
    212 
    213     // draw mesh
    214     <function id='27'>glBindVertexArray</function>(VAO);
    215     <function id='2'>glDrawElements</function>(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
    216     <function id='27'>glBindVertexArray</function>(0);
    217 }  
    218 </code></pre>
    219 
    220 <p>
    221   We first calculate the N-component per texture type and concatenate it to the texture's type string to get the appropriate uniform name. We then locate the appropriate sampler, give it the location value to correspond with the currently active texture unit, and bind the texture. This is also the reason we need the shader in the <fun>Draw</fun> function. 
    222 </p>
    223 
    224 <p>
    225  We also added <code>"material."</code> to the resulting uniform name because we usually store the textures in a material struct (this may differ per implementation).
    226 </p>
    227 
    228 <note>
    229   Note that we increment the diffuse and specular counters the moment we convert them to <code>string</code>. In C++ the increment call: <code>variable++</code> returns the variable as is and <strong>then</strong> increments the variable while <code>++variable</code> <strong>first</strong> increments the variable and <strong>then</strong> returns it. In our case the value passed to <code>std::string</code> is the original counter value. After that the value is incremented for the next round.
    230 </note>
    231 
    232 <p>
    233   You can find the full source code of the <fun>Mesh</fun> class <a href="/code_viewer_gh.php?code=includes/learnopengl/mesh.h" target="_blank">here</a>.
    234 </p>
    235 
    236 <p>
    237   The <fun>Mesh</fun> class we just defined is an abstraction for many of the topics we've discussed in the early chapters. In the <a href="https://learnopengl.com/Model-Loading/Model" target="_blank">next</a> chapter we'll create a model that acts as a container for several mesh objects and implements Assimp's loading interface.
    238 </p>
    239        
    240 
    241     </div>
    242