1

I have a 3D world with a cube inside it. I understand that the cube's vertices are defined as coordinates about the cube's "centre of mass".

I want to place the cube at a position away from me, to the right and up above so I want to place the cube at (1,1,1). I don't have any 'camera' code at the moment so assume my viewing position is (0,0,0).

What do I need to translate to move the cube to a new location?

I am confused by the View and Model matrices. I thought View was the camera view/position and Model was the position of the vertices in the 3D world.

My code below works as I expect for x and y positions but z is negative going into the screen (further away) and positive moving towards/out of the screen. I would like z to be positive the further away an object is from the viewpoint.

What am I doing wrong?

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

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

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

  // Move the object within the 3D space
  Model = glm::translate( Model, glm::vec3( m_X, m_Y, m_Z ) );

  glm::mat4 MVP;
  MVP = Projection * View * Model;

MVP is passed into the transform uniform.

My shader:

char *vs3DShader  = 

"#version 140\n"

"#extension GL_ARB_explicit_attrib_location : enable\n"

"layout (location = 0) in vec3 Position;"
"layout (location = 1) in vec4 color;"
"layout (location = 2) in vec3 normal;"

"out vec4 frag_color;"

"uniform mat4 transform;"

"void main()"
"{"
"  gl_Position = transform * vec4( Position.x, Position.y, Position.z, 1.0 );"

"  // Pass vertex color to fragment shader.. \n"
"  frag_color = color;"
"}"

;
genpfault
  • 51,148
  • 11
  • 85
  • 139
SparkyNZ
  • 6,266
  • 7
  • 39
  • 80

1 Answers1

2

My code below works as I expect for x and y positions but z is negative going into the screen (further away) and positive moving towards/out of the screen. I would like z to be positive the further away an object is from the viewpoint.

You can achieve this by setting up a view matrix, which mirrors the Z-Axis:

glm::mat4 View = glm::scale(glm::mat4(1.0), glm::vec3(1.0f, 1.0f, -1.0f));


The view matrix transforms from world space to view space. Your view matrix is the identity matrix:

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

This means the world space is equal the view space. Note, in general a view matrix is set by glm::lookAt.

The Z-Axis is the cross product of the X-Axis and the Y-Axis. This means, if the X-axis points to the left, and the Y-axis points up, in a right handed system, then the Z-axis points out of the viewport.
See Understanding OpenGL’s Matrices

enter image description here


The coordinates I described above, are in general the view coordinates in a right handed system, because glm::glm::perspective sets up a right handed projection matrix, which transforms this cooridnates to clip sapce, in that way that the z-axis is inverted and becomes the depth.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks heaps for that. That scale with Z=-1 made all the difference. I'm not sure why right-handed systems are preferred - it feels counter-intuitive to me but this is good to know. Excellent answer. – SparkyNZ Mar 20 '18 at 12:05
  • 1
    Maybe [Is OpenGL coordinate system left-handed or right-handed?](https://stackoverflow.com/questions/4124041/is-opengl-coordinate-system-left-handed-or-right-handed) can bring further clarification – Rabbid76 Mar 20 '18 at 12:09