Some opengl and glm descriptions

Can someone explain to 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)); 

They convert units to pixels, but I'm not sure if this is what they do. Another question that I have is more general - how do I represent a number, i.e. Sin (90) = 1, up to 10 pixels or 40 or any number? And how can I indicate that (0,0) will be in the middle of the screen? Are all of the above questions taken from the glm library?

+1
opengl glm-math
source share
2 answers

The first line creates a perspective projection equivalent to calling gluPerspective (45.0f, 1.0f, 0.1f, 100.0f). If you do not know what gluPerspective does, check this link

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

The next line is only modulo an angle of rotation of 360 to ensure that the projection angle is less than 360 degrees.

 angle = (GLfloat) (i % 360); 

The next few lines define our View matrix. This is basically your camera viewing port, that is, what you see on the monitor. The call to the translation and rotation functions are conversion functions that cause our camera to move to

 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)); 

The next line defines the position of our model. In this case it will be (1.0f, 1.0f, 1.0f, 1.0f). If you are wondering why there are 4 options instead of 3, read the OpenGL orange book or check Wikipedia for uniform coordinates

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

The last two lines complete the setup of our scene by calculating the projection matrix of the model and passing it to OpenGL.

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

In general, what your code block does is simulates an OpenGL drawing pipeline from translating model coordinates into presentation coordinates.

About the second question you ask. I do not understand the first part, what do you understand when translating a number into a pixel? Are you trying to map a point in a 1D matrix to a 2D matrix? if so, just do a standard mapping, something like a pixel [index / rows] [index% rows], where the pixel is your display pixel object, the index is your array index, the row is the width of your screen. For the second part (how to set (0,0) to the middle of the screen), I think that you just need to add an offset to the beginning of the screen, since OpenGL uses the left coordinate system, the starting point of the screen i.e. (0,0) the dot will be in the lower left corner of the screen. Therefore, if you want your system to be in the middle of the screen, just add an offset (-width / 2, -height / 2) to translate your point into OpenGL space or (width / 2, height / 2) for the opposite projection. But using a self-prepared standard is not recommended.

+7
source share

Set up a perspective camera conversion:

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

Moving the camera back down the Z axis a bit:

 View = glm::translate(View, glm::vec3(0.f, 0.f, -5.0f)); 

Camera rotation around the X axis back:

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

The rotation of the camera around the Y and Z axes is twice as fast as the X axis:

 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)); 
0
source share

All Articles