一些OpenGL和GLM的解释 [英] some opengl and glm explanation

查看:315
本文介绍了一些OpenGL和GLM的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释以下几行吗?

Can somebody explain me what the following lines do ?

glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);
angle = (GLfloat) (i % 360);
glm::mat4 View = glm::mat4(1.);
View = glm::translate(View, glm::vec3(0.f, 0.f, -5.0f));
View = glm::rotate(View, angle * -1.0f, glm::vec3(1.f, 0.f, 0.f));
View = glm::rotate(View, angle * 0.5f, glm::vec3(0.f, 1.f, 0.f));
View = glm::rotate(View, angle * 0.5f, glm::vec3(0.f, 0.f, 1.f));
glm::mat4 Model = glm::mat4(1.0);
glm::mat4 MVP = Projection * View * Model;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "mvpmatrix"), 1, GL_FALSE, glm::value_ptr(MVP));

它们将单位转换为像素,但是我不确定这是否是像素.我还有一个更笼统的问题是,我如何表示一个数字,即sin(90)= 1、10像素或40或任何数字?以及如何指定(0,0)在屏幕中间? glm库是否已对上述所有内容进行了照顾?

They translate units to pixels but I am not sure if that is what they do. Another question I have, more general one, is how do I represent a number , i.e sin(90) = 1, to 10 pixels, or 40 or any number? And how do I specify that (0,0) will be in the middle of the screen ? Are all the above taken care by glm library?

推荐答案

第一行创建一个透视图投影,该透视图投影等效于调用gluPerspective(45.0f,1.0f,0.1f,100.0f).如果您不知道gluPerspective做什么,请检查此链接

The first line create a perspective projection that is equivalent to calling gluPerspective(45.0f, 1.0f, 0.1f, 100.0f). If you don't know what gluPerspective do, check this link

 glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);

下一行只是将旋转角度取模为360,以确保我们的投影角度小于360度.

The next row just modulo the rotation angle to 360 to make sure our projection angle is less than 360 degree.

angle = (GLfloat) (i % 360);

接下来的几行定义了我们的View矩阵.这基本上是您的相机视口,即您在监视器上看到的视口.平移和旋转功能调用是转换功能调用,用于将相机移动到适当位置

The next few line define our View matrix. This is basically your camera view-port i.e. what you see on the monitor. The translate and rotate function call are transformation functions call to move our camera into position

glm::mat4 View = glm::mat4(1.);
View = glm::translate(View, glm::vec3(0.f, 0.f, -5.0f));
View = glm::rotate(View, angle * -1.0f, glm::vec3(1.f, 0.f, 0.f));
View = glm::rotate(View, angle * 0.5f, glm::vec3(0.f, 1.f, 0.f));
View = glm::rotate(View, angle * 0.5f, glm::vec3(0.f, 0.f, 1.f));

下一行定义模型的位置.在这种情况下,它将是(1.0f,1.0f,1.0f,1.0f).如果您想知道为什么有4个参数而不是3个参数,请阅读OpenGL橙皮书或在同质坐标上查看Wikipedia

The next line define the position of our model. In this case it would be (1.0f, 1.0f, 1.0f, 1.0f). If you wonder why there are 4 parameters instead of 3, read the OpenGL orange book or check Wikipedia on homogeneous coordinates

glm::mat4 Model = glm::mat4(1.0);

最后两行通过计算模型视图投影矩阵并将其传递给OpenGL,完成了我们的场景设置.

The last two line finish setting up our scene by calculate the model view projection matrix and passing it to OpenGL.

glm::mat4 MVP = Projection * View * Model;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "mvpmatrix"), 1, GL_FALSE,     glm::value_ptr(MVP));

您的代码块总体上是在模拟OpenGL的绘制管道,将模型坐标转换为视图坐标.

Overall what your code block does is simulating the drawing pipeline of OpenGL from translating model coordinates into view coordinate.

关于您提出的第二个问题.我不了解第一部分,将数字转换为像素是什么意思?您是否正在尝试将一维矩阵中的点映射到二维矩阵中?如果是这种情况,则只需执行标准映射,例如pixel [index/rows] [index%rows],其中pixel是您的屏幕像素对象,index是您的数组索引,row是您的屏幕宽度.对于第二部分(如何将(0,0)设置到屏幕中间),我认为您需要做的就是向屏幕原点添加一个偏移量,因为OpenGL使用左手坐标系,即屏幕原点即(0,0)点将在屏幕的左下方.因此,如果您希望系统位于屏幕中间,则只需添加偏移量(-width/2,-height/2)即可将点转换为OpenGL空间,或者将偏移量转换为(width/2,height/2)投影.但是不建议使用自定义的标准.

About the second question you ask. I don't understand the first part, what do you mean by translating a number into a pixel? Are you trying to map a point in 1D matrix into a 2D matrix? if that is the case just do standard mapping, something like pixel[index/rows][index%rows] where pixel is your screen pixel object, index is your array index, row is width of your screen. For the second part (how to set (0,0) to middle of the screen) I think what you need to do is just add an offset to the origin of your screen since OpenGL use left-hand coordinate system, the screen origin point i.e. (0,0) point would be on the bottom left of your screen. So if you want your system to be at the middle of the screen just add an offset of (-width/2, -height/2) to translate your point to OpenGL space or (width/2, height/2) for vice verse projection. But using a self established standard is not advised thought.

这篇关于一些OpenGL和GLM的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆