SparkyNZ
SparkyNZ

Reputation: 6676

OpenGL - Why are my Z coords 2x scale?

enter image description here

This is my cube? I have set all vertice coordinates to -1 or +1, so all distances should be 2.0 for each edge. However, it only appears as a cube if I halve the Z coordinate values.

Do I have a bug in the MVP code that I've missed?

  glEnableVertexAttribArray( 0 );
  glEnableVertexAttribArray( 1 );

  glBindBuffer( GL_ARRAY_BUFFER, m_VBO );

  // First attributes are the x,y,z coords..
  glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, CVertex3DC::m_Stride, 0 );

  // Second attribute will be the RGBA
  glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, CVertex3DC::m_Stride, (void *) g_XYZFloatLen );

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

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

  dist = -5.0f;

  View = glm::translate( View, glm::vec3( 0.0f, 0.0f, dist ) );

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

  rot = 0;

  Model = glm::rotate( Model, rot, glm::vec3( 0.0f, 1.0f, 1.0f ) ); // where x, y, z is axis of rotation (e.g. 0 1 0)

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

  GLuint transformLoc;

  transformLoc = glGetUniformLocation( g_ShaderProgram, "transform" );
  glUniformMatrix4fv( transformLoc, 1, GL_FALSE, glm::value_ptr( MVP ) );

  int nOffset = 0;

  // Draw each face..
  for( int f = 0; f < (int) m_Faces.size(); f ++ )
  {
    nOffset = m_Faces[ f ].Render( nOffset );
  }

  glDisableVertexAttribArray( 0 );
  glDisableVertexAttribArray( 1 );

UPDATE: Here's the vertex shader - I'm not using 'normal' at the moment.

#version 330

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(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);

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

Upvotes: 1

Views: 84

Answers (1)

derhass
derhass

Reputation: 45322

Transformation matrices are only half of the truth. In modern GL, the vertex shader (or more precisely: all programmable stages before the rasterizer) are responsible for the transformation of object space coordinates into clip space.

So the shader might use the matrices, or it might completely ignore them, or it might apply additional transformations. In your case, the VS scales x and y by 0.5 before applying the transformation matrix, hence your object appears streched in z.

Note that the vertex shader might not even use the input attributes at all. It can work completely attribute-less, by generating some surface parameterized by the gl_VertexID alone. So when one only knows the data in the vertex arrays and the state of all uniforms, one still has no clue of how the rendered look will look (although in most cases, the data and uniforms will be used, otherwise it would be a waste of ressources to specify them).

Upvotes: 1

Related Questions