there are fourth code we need to understand when you learn
opengl. they are mesh model camera shader
today i will learn about the mesh class. because mesh is
small cell that is can be used to create model.
we define a micro to make sure this file can be only
included just once.
#ifndef MESH_H #define MESH_Ha mesh can be composed by triangles. one triangle can be
composed by three points. so now we should define a struct named Vertex. what
attributes will appear in one point.
the first one attribute is position which has three
dimentions. just like this: glm::vec3 Position;
how to get normal of the point. to learn this knowledge we can reference the website of: https://www.cnblogs.com/z12603/p/6860730.html
we can learn from the the website
http://www.waitingfy.com/archives/425 , there are two method for calculating vertex normal. just
use one face normal as the vertex normal. or using
averaging of all faces normals that share the the points.
now we konw how to calculate the vertex normal, but we do
not understand why we need vertex normal.
why we need the vertex normal? we calculate the lighting effect of each points in opengl
or d3d. so so we need to know the surface orientation for
per vertex.
so now we add the normal attribute to one point.
tangent and bitangent of the point. to learn this we can see this blog: http://www.opengl-tutorial.org/cn/intermediate-
tutorials/tutorial-13-normal-mapping/ in the segment of Tangent and Bitangent, there is a
picture clearly shows what does the tangent and bitangent
means. in my opition, we know the the normal of the point,
we know the uv of the point, we then choose the tangent
aling with the u direction. then we also know the tangent
space is composed by three orthogonal vector. so we can
use normal vector and tangent vector to get the bitangent
vector.
we now have five attributes. 1/ position 2/ normal 3/ uv 4/ tangent 5/ bitangent im some books we do not need give the bitangent. because
we can calculate it by normal and uv. so now the struct of the vertex can be defined to this: struct Vertex { glm::vec3 Position; glm::vec3 Normal; glm::vec2 TexCoords; glm::vec3 Tangent; glm::vec3 Bitangent; };
then we will see the second struct named Texture. we recall why we introduce a image to opengl. that is
because we can make our object more colorful. we can paste
a image to the obejct’s face or mesh. yep. but how to
define a texture in opengl. we load a image by stb_image. and in opengl we create a buffer to store the bytes. there
is pointer which points the buffer where the image the
locates. so we just only know the the address of the
buffer. glGenTextures(1, &texture); the return value is unsigned int. so we put it in the struct.
what we also know is the type of the texture. that denotes
this a normal texture or diffuse texture. so the texture’s
struct is like this: struct Texture { unsigned int id; string type; string path; // learn later. };
now we start to define the Mesh class.
one mesh should have a list of vertex. and in order to
define a complex mesh without cost many saving space. we
shold give a list of point index. so we give a list of
index. then we also need a list textures. so you can see there three members in Mesh class.
Class Mesh { public: vector vertices; vector indices; vector textures; }