1

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

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Trt Trt
  • 5,330
  • 13
  • 53
  • 86

2 Answers2

7

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

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

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

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

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

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

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.

Tri Chu
  • 110
  • 8
0

Set up a perspective camera transform:

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

Rotating the camera around the X axis backwards:

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

Rotating the camera around the Y and Z axes half 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));
genpfault
  • 51,148
  • 11
  • 85
  • 139