title: OpenGL drawing Inverted Triangle
body: <p>My triangle vertices are in array vertices that are specified in such a way that I should get an upright triangle but my triangle is inverted around y axis.
  The code is below.</p>
  
  <p>This is some Global stuff</p>
  
  <pre><code>#include&lt;GL\glew.h&gt;
  #include&lt;GL\freeglut.h&gt;
  #include&lt;glm.hpp&gt;
  #include&lt;gtc\matrix_transform.hpp&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  #include&lt;iostream&gt;
  using namespace std;
  
  GLfloat vertices[] = {
      -1,-1,
      1,-1,
      0,1
  };
  
  GLuint vaoId;
  GLuint vboId;
  
  glm::mat4 projectionMatrix = glm::perspective(50.0f, 1.0f / 1.0f, 5.0f, 
  20.0f);
  glm::mat4 modelViewMatrix = glm::translate(glm::mat4(1), glm::vec3(0, 0, -20));
  
  string parseShader(string fileName) {
      string shaderCode;
      ifstream ifs;
      ifs.open(fileName, ios::in);
      string line;
      while (!ifs.eof()) {
          getline(ifs, line);
          shaderCode += line;
          shaderCode += "\n";
      }
      return shaderCode;
  }
  
  GLuint program;
  </code></pre>
  
  <p>This function just loads shaders</p>
  
  <pre><code>void loadShader() {
      string vShader = parseShader("vShader.txt");
      GLuint vShaderId = glCreateShader(GL_VERTEX_SHADER);
      const GLchar* vShaderSource = vShader.c_str();
      glShaderSource(vShaderId, 1, &amp;vShaderSource, NULL);
      glCompileShader(vShaderId);
      GLint compileStatus;
      glGetShaderiv(vShaderId, GL_COMPILE_STATUS, &amp;compileStatus);
      if (!compileStatus) {
          cout &lt;&lt; "Cannot compile shader\n";
          glDeleteShader(vShaderId);
          return;
      }
  
      string fShader = parseShader("fShader.txt");
      GLuint fShaderId = glCreateShader(GL_FRAGMENT_SHADER);
      const GLchar* fShaderSource = fShader.c_str();
      glShaderSource(fShaderId, 1, &amp;fShaderSource, NULL);
      glCompileShader(fShaderId);
      glGetShaderiv(fShaderId, GL_COMPILE_STATUS, &amp;compileStatus);
      if (!compileStatus) {
          cout &lt;&lt; "Cannot compile fragment Shader\n";
          glDeleteShader(fShaderId);
          return;
      }
  
      GLuint programId;
      programId = glCreateProgram();
      glAttachShader(programId, vShaderId);
      glAttachShader(programId, fShaderId);
      glLinkProgram(programId);
      GLint linkStatus;
      glGetProgramiv(programId, GL_LINK_STATUS, &amp;linkStatus);
      if (!linkStatus) {
          cout &lt;&lt; "Cannot link program\n";
          glDeleteShader(vShaderId);
          glDeleteShader(fShaderId);
          glDeleteProgram(programId);
          return;
      }
      glUseProgram(programId);
  }
  </code></pre>
  
  <p>This loads data in VBO</p>
  
  <pre><code>void loadDataInBuffers() {
      glGenBuffers(1, &amp;vboId);
      glBindBuffer(GL_ARRAY_BUFFER, vboId);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  }
  </code></pre>
  
  <p>This function is used to bind states to VAO</p>
  
  <pre><code>void loadVao() {
      glBindBuffer(GL_ARRAY_BUFFER, vboId);
      glGenVertexArrays(1, &amp;vaoId);
      glBindVertexArray(vaoId);
      glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      glBindVertexArray(0);
  }
  </code></pre>
  
  <p>This is rendering function</p>
  
  <pre><code>void display() {
      glClear(GL_COLOR_BUFFER_BIT);
      glBindVertexArray(vaoId);
      glDrawArrays(GL_TRIANGLES, 0, 3);
      glutSwapBuffers();
  }
  </code></pre>
  
  <p>This calls all other functions defined above</p>
  
  <pre><code>void init() {
      glClearColor(0, 0, 0, 0);
      loadDataInBuffers();
      loadShader();
      loadVao();
      glUniformMatrix4fv(1, 1, GL_FALSE, &amp;projectionMatrix[0][0]);
      glUniformMatrix4fv(2, 1, GL_FALSE, &amp;modelViewMatrix[0][0]);
  }
  </code></pre>
  
  <p>My main Method looks like this</p>
  
  <pre><code>int main(int argc, char** argv) {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
      glutInitWindowSize(500, 500);
      glutInitWindowPosition(100, 100);
      glutCreateWindow("Simple Window");
      glewInit();
      init();
      glutDisplayFunc(display);
      glutMainLoop();
      return 0;
  }
  </code></pre>
  
  <p>Below is the vertex Shader</p>
  
  <pre><code>#version 430
  
  layout(location = 0) in vec2 pos;
  layout(location = 1) uniform mat4 projectionMatrix;
  layout(location = 2) uniform mat4 modelViewMatrix;
  
  void main(){
      vec4 position = vec4(pos,0,1);
      gl_Position = projectionMatrix*modelViewMatrix*position;
  }
  </code></pre>
  
  <p>Below is the Fragment Shader</p>
  
  <pre><code>#version 430
  
  void main(){
      gl_FragColor = vec4(1,0,0,1);
  }
  </code></pre>
  
  <p>This is what I get
  <a href="https://i.stack.imgur.com/gH4SF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gH4SF.png" alt="THis is what I get when I draw the Triangle"></a></p>
  
  <p>Shouldn't I get something like this
  <a href="https://i.stack.imgur.com/Eot7q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Eot7q.png" alt="Shouldn&#39;t I get something like this"></a></p>