1、One of the biggest parts of graphics programming is: MATH.
What is math?
Math in general is used in pretty much every area of graphics programming and as this learning OpenGL continues and we start dealing with things we’ll actually have to start implementing our own kind of algorithms that involve math and doing calculations and all that.
There are 2 different kind of areas of math in graphics programming: matrices and vectors:
Matrices: a matrix is essentially an array of numbers that we can manipulate by multiplying or we can set up in various ways, it is literally an array of numbers and when we multiply those numbers by something else we get a new set of numbers, and those calculations can be useful for things like positioning objects in a 3D world.
Vectors: there are 2 types of vectors that we deal with in graphics programming, directional vectors and positional vectors. A vector is really seen as like a direction or a magnitude or a length, vectors in graphics programming are positions in either 2D or 3D or 4D space, for example we have a vector: (200,100), it’s a point on the screen 200 pixels in from left and 100 pixels up from the bottom.

Why do we use it? Why do we even need it?
The most common usage and most important usage of vectors and matrices is for transformation, a transformation is essentially a way that we can get our vertex buffer with all of it’s points into some kind of form that we see on our screen. The reason that we need transformation is for a number of reasons, first of all let’s say we have a massive 3D world and we have a ball we need to position that all somewhere in the 3D world, what if we have a camera moving around maybe it’s orbiting around the ball, the way of that works is, well there is no such thing as a camera, so what we need to do really is just move the world and the ball around so we change the position of the world and the ball and that kind of creates the illusion of a “camera” circling or orbiting our subject, also we might want the positions and vertices in a way that isn’t just a translation, and might be something like scale or we want to rotate things all of that stuff requires maps to accomplish.
在这里插入图片描述
By default OpenGL gives us a projection matrix:-1 to 1.

2、How we can use math?
GLM is OpenGL specific, is a header only library so we don’t need to compile it after we included it, and we don’t have to link against the library.
MathsInOpenGL
解压
MathsInOpenGL
复制文件夹里的glm文件夹到与stb_image同目录下
MathsInOpenGL
MathsInOpenGL
回到VS,增加附加包含目录:src/vendor。
MathsInOpenGL
刷新解决方案资源管理器,右键glm选择包含在项目中,dummy.cpp右键选择从项目中排除。
MathsInOpenGL
3、代码实现:
使用GLM,Application包含glm头文件。
在这里插入图片描述
Projection Matrix is a way for us to tell our window how we want to map all of our different vertices to. We have this concept of having vertex buffer filled with vertex positions, however we need to transform that into a 2D plane bc when we draw it on our laptop screen or computer monitor we need it to drawn in a 2D way(picture this: we have this mathematical representation of a 3D world but we need to draw it on a 2D surface).

How do we go from having this 3D geometry to having a kind of 2D drawing?
That’s what a projection matrix is useful, the way that we need to fix this problem is by basically telling all of our vertex positions that make up that ChernoLogo:“hey the window that we’re drawing onto, it isn’t actually a sqaure, it’s 4:3, so maybe do some math to make that work.”

Basic.shader里创建u_MVP并使用。
在这里插入图片描述
Shader.h包含glm头文件并增加SetUniformMat4f声明。
在这里插入图片描述
Shader.cpp增加SetUniformMat4f定义。
在这里插入图片描述
最后,Application.cpp增加如下代码。
在这里插入图片描述
Orthographic matrix: essentially a way to map all of our coordinates into a 2D plane where objects that are further away do not actually get smaller, this is opposed to like a perspective projection which is what we’re kind of used to seeing if we take a photograph in real like, where objects that are further away are actually smaller, that’s something we use for 3D rendering.

4、运行效果:
在这里插入图片描述
改变下ortho()的值后:

glm::mat4 proj = glm::ortho(-4.0f, 4.0f, -3.0f, 3.0f, -1.0f, 1.0f);

运行效果:
在这里插入图片描述


版权声明:本文为AlexiaDong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/AlexiaDong/article/details/126593865