title: Inverted X Axis OpenGL
body: <p>So I watched a little intro course on youtube to learn the basics of OpenGL and learnt things like making a triangle and a simple camera class, etc. I've wanted to try and work towards making a voxel engine so obviously the first thing that I thought to make was a simple cube that I could eventually replicate. My problem is though that when I go to render the vertices and triangles they seem to be in a mess that doesn't resemble what I hard coded in the cube class. I know that 0,0 is the centre of the screen; 1 in the x axis is the right; -1 is the left; 1 in the y axis is the top and -1 is the bottom. Yet when I send through my vertices and triangles to the vertex buffer it seems to be doing something completely different. It's most likely a really stupid mistake on my part.</p>
  
  <pre><code>Cube::Cube()
  {
  m_vertices[0] = Vertex(glm::vec3(-0.5, -0.5, 0));
  m_vertices[1] = Vertex(glm::vec3(-0.5, 0.5, 0));
  m_vertices[2] = Vertex(glm::vec3(0.5, 0.5, 0));
  m_vertices[3] = Vertex(glm::vec3(0.5, -0.5, 0));
  m_vertices[4] = Vertex(glm::vec3(-0.5, -0.5, 1));
  m_vertices[5] = Vertex(glm::vec3(-0.5, 0.5, 1));
  m_vertices[6] = Vertex(glm::vec3(0.5, 0.5, 1)); 
  m_vertices[7] = Vertex(glm::vec3(0.5, -0.5, 1));
  
  m_triangles[0] = Triangle(0, 1, 2); //Front
  //m_triangles[1] = Triangle(0, 2, 3); //Front
  
  //m_triangles[2] = Triangle(1, 5, 6); //Top
  //m_triangles[3] = Triangle(1, 6, 2); //Top
  
  //m_triangles[4] = Triangle(3, 5, 4); //Left
  //m_triangles[5] = Triangle(3, 5, 4); //Left
  
  //m_triangles[6] = Triangle(3, 2, 7); //Right
  //m_triangles[7] = Triangle(3, 3, 7); //Right
  
  //m_triangles[8] = Triangle(7, 6, 4); //Back
  //m_triangles[9] = Triangle(5, 6, 7); //Back
  
  //m_triangles[10] = Triangle(0, 4, 7); //Bottom
  //m_triangles[11] = Triangle(0, 3, 7); //Bottom
  }
  
  void Cube::Render()
  {
     Draw(m_vertices, sizeof(m_vertices) / sizeof(m_vertices[0]), m_triangles, (sizeof(m_triangles) / sizeof(m_triangles[0])));
  }
  </code></pre>
  
  <p>The draw function inherited from my mesh class</p>
  
  <pre><code>void Mesh::Draw(Vertex* vertices, unsigned int numVertices, Triangle* triangles, unsigned int numTriangles)
  {
  //Array of indices
  std::vector&lt;unsigned int&gt; indices;
  for (int i = 0; i &lt; numTriangles; i++)
  {
      indices.push_back(triangles[i].GetTriangle()[0]);
      indices.push_back(triangles[i].GetTriangle()[1]);
      indices.push_back(triangles[i].GetTriangle()[2]);
  }
  
  //How many vertices to draw
  m_drawCount = indices.size();
  
  //Generate and bind vertex array
  glGenVertexArrays(1, &amp;m_vertexArrayObject);
  glBindVertexArray(m_vertexArrayObject);
  
  //Generate and bind buffers
  glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
  glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
  //Write vertex data to the buffer
  glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vertices[0]), &amp;vertices[0], GL_STATIC_DRAW);
  
  //Only one attribute for the vertex data
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[INDEX_VB]);
  //Write vertex data to the buffer
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), &amp;indices[0], GL_STATIC_DRAW);
  
  //Unbind vertex array
  glBindVertexArray(0);
  
  glBindVertexArray(m_vertexArrayObject);
  
  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  glDrawElements(GL_TRIANGLES, m_drawCount, GL_UNSIGNED_INT, 0);
  
  glBindVertexArray(0);
  }
  </code></pre>
  
  <p>Vertex and Triangle structs in mesh.h</p>
  
  <pre><code>struct Vertex
  {
  public:
  //Constructor
  Vertex()
  {
  
  }
  //Constructor
  Vertex(const glm::vec3&amp; pos)
  {
      //Set vertex position
      this-&gt;m_pos = pos;
  }
  protected:
  
  private:
  //Vertex position
  glm::vec3 m_pos;
  };
  
  struct Triangle
  {
  public:
  //Constructor
  Triangle()
  {
  
  }
  
  //Constructor
  Triangle(int point1, int point2, int point3)
  {
      SetTriangle(point1, point2, point3);
  }
  
  int* GetTriangle()
  {
      return m_points;
  }
  
  void SetTriangle(int point1, int point2, int point3)
  {
      m_points[0] = point1;
      m_points[1] = point2;
      m_points[2] = point3;
  }
  protected:
  
  private:
  int m_points[3];
  };
  </code></pre>
  
  <p>Camera functions</p>
  
  <pre><code>Camera::Camera(const glm::vec3 pos, float fov, float aspect, float zNear, float zFar)
  {
  m_perspectiveMatrix = glm::perspective(fov, aspect, zNear, zFar);
  m_pos = pos;
  m_forward = glm::vec3(0, 0, 1);
  m_up = glm::vec3(0, 1, 0);
  }
  
  glm::mat4 Camera::GetViewProjection() const
  {
   return m_perspectiveMatrix * glm::lookAt(m_pos, m_pos + m_forward, m_up);
  }
  </code></pre>
  
  <p>Note that in the cube constructor I'm only creating one triangle which should be bottom left, top left, top right yet this is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/V12Rw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V12Rw.png" alt="The triangle isn&#39;t where I specified it"></a></p>
  
  <p>Another note is that my camera rotation seems to be off as well. Changing the y rotation actually rotates it on the x axis and the changing the x rotation rotates on the y axis.</p>
  
  <p>Also if anyone had a better way of creating and rendering the cube I would be grateful. Once I can do that I'll most likely look into letsmakeavoxelengine tutorials.</p>
  
  <p>Edit: It feels like the x and y axis are inverted. I could just invert all my  functions to counter that but that's kind of a hacky way around it and it still doesn't fix the underlying issue which could cause more trouble later on.</p>
  
  <p>Edit2: Transform.h</p>
  
  <pre><code>#pragma once
  #include &lt;glm\glm.hpp&gt;
  #include &lt;glm\gtx\transform.hpp&gt;
  #include "Camera.h"
  
  struct Transform
  {
  public:
  //Constructor
  Transform(const glm::vec3&amp; pos = glm::vec3(), const glm::vec3&amp; rot = glm::vec3(), const glm::vec3&amp; scale = glm::vec3(1.0f, 1.0f, 1.0f))
  {
      this-&gt;m_pos = pos;
      this-&gt;m_rot = rot;
      this-&gt;m_scale = scale;
  }
  
  //Get the model matrix
  inline glm::mat4 GetModelMatrix() const
  {
      //Create all the transform matrices
  
      //Position matrix
      glm::mat4 posMatrix = glm::translate(m_pos);
      //Scale matrix
      glm::mat4 scaleMatrix = glm::scale(m_scale);
      //Rotation matrix X
      glm::mat4 rotXMatrix = glm::rotate(m_rot.x, glm::vec3(1.0f, 0.0f, 0.0f));
      //Rotation matrix Y
      glm::mat4 rotYMatrix = glm::rotate(m_rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
      //Rotation matrix Z
      glm::mat4 rotZMatrix = glm::rotate(m_rot.z, glm::vec3(0.0f, 0.0f, 1.0f));
      //Combined rotation matrix
      glm::mat4 rotMatrix = rotXMatrix * rotYMatrix * rotZMatrix;
  
      return posMatrix * rotMatrix * scaleMatrix;
  }
  
  inline glm::mat4 GetMVP(const Camera&amp; camera) const
  {
      glm::mat4 ViewProjection = camera.GetViewProjection();
      glm::mat4 ModelMatrix = GetModelMatrix();
  
      return ViewProjection * ModelMatrix;//camera.GetViewProjection() * GetModel();
  }
  
  //Get position
  inline glm::vec3* GetPosition() { return &amp;m_pos; }
  //Get rotation
  inline glm::vec3* GetRotation() { return &amp;m_rot; }
  //Get scale
  inline glm::vec3* GetScale() { return &amp;m_scale; }
  
  //Set Position
  inline void SetPosition(const glm::vec3&amp; pos) { this-&gt;m_pos = pos; }
  //Set Rotation
  inline void SetRotation(const glm::vec3&amp; rot) { this-&gt;m_rot = rot; }
  //Set Scale
  inline void SetScale(const glm::vec3&amp; scale) { this-&gt;m_scale = scale; }
  
  private:
  //Transform position
  glm::vec3 m_pos;
  //Transform rotation
  glm::vec3 m_rot;
  //Transform scale
  glm::vec3 m_scale;
  };
  </code></pre>
  
  <p>Cube, Transform and Camera calls in main.cpp:</p>
  
  <pre><code>Cube cube;
  Transform transform;
  Camera camera(glm::vec3(0.0f, 0.0f, -3.0f), 70.0f, (float)display.GetWidth()/(float)display.GetHeight(), 0.01f, 100.0f);
  </code></pre>
  
  <p>Edit3: 100% inverted on X Axis. New cube.cpp code:</p>
  
  <pre><code>m_vertices[0] = Vertex(glm::vec3(-0.5, -0.5, 0)); //BottomLeftFront
  m_vertices[1] = Vertex(glm::vec3(-0.5, 0.5, 0)); //TopLeftFront
  m_vertices[2] = Vertex(glm::vec3(0.5, 0.5, 0)); //TopRightFront
  m_vertices[3] = Vertex(glm::vec3(0.5, -0.5, 0)); //BottomRightFront
  m_vertices[4] = Vertex(glm::vec3(-0.5, -0.5, 1)); //BottomLeftBack
  m_vertices[5] = Vertex(glm::vec3(-0.5, 0.5, 1)); //TopLeftBack
  m_vertices[6] = Vertex(glm::vec3(0.5, 0.5, 1)); //TopRightBack
  m_vertices[7] = Vertex(glm::vec3(0.5, -0.5, 1)); //BottomRightBack
  
  m_triangles[0] = Triangle(0, 1, 2); //Front
  m_triangles[1] = Triangle(0, 2, 3); //Front
  
  //m_triangles[2] = Triangle(1, 5, 6); //Top
  //m_triangles[3] = Triangle(1, 6, 2); //Top
  
  m_triangles[4] = Triangle(3, 5, 4); //Left //BottomLeftFront, TopRightBack, BottomRightBack
  //m_triangles[5] = Triangle(3, 5, 4); //Left
  
  //m_triangles[6] = Triangle(3, 2, 7); //Right
  //m_triangles[7] = Triangle(3, 3, 7); //Right
  
  //m_triangles[8] = Triangle(7, 6, 4); //Back
  //m_triangles[9] = Triangle(5, 6, 7); //Back
  
  //m_triangles[10] = Triangle(0, 4, 7); //Bottom
  //m_triangles[11] = Triangle(0, 3, 7); //Bottom
  </code></pre>
  
  <p>I've put a comment next to the new triangle that tells you what the actual resulting triangle points were. The triangle that I set should have been BottomRightFront, TopLeftBack, BottomLeftBack according to the code. I'll also add a screenshot of what it looks like.</p>
  
  <p><a href="https://i.stack.imgur.com/ffG0C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ffG0C.png" alt="Triangle was supposed to be BottomRightFront, TopLeftBack, BottomLeftBack"></a></p>