title: OpenGL: All vertices disappear when multiplied by identity matrix
body: <p>I managed to set up a basic window and draw a triangle shape in it.</p>
  <p>This is how it looks like:</p>
  <p><a href="https://i.stack.imgur.com/VUha8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VUha8.png" alt="Single red triangle" /></a></p>
  <p>But the next step was to use glm::translate to create a translation matrix and set it as a uniform variable for the shader to multiply all triangle vertices by, to make the triangle move from side to side.  But the result was the triangle disappearing leaving the window completely empty.</p>
  <p>Here's all the code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;cmath&gt;
  
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  
  #include &lt;glm/glm.hpp&gt;
  #include &lt;glm/gtc/matrix_transform.hpp&gt;
  #include &lt;glm/gtc/type_ptr.hpp&gt;
  
  // Window dimensions
  const GLint WIDTH = 800, HEIGHT = 600;
  
  GLuint VAO, VBO, shader, uniformModel;
  
  bool direction = true;
  float triOffset = 0.0f;
  float triMaxoffset = 0.7f;
  float triIncrement = 0.0005f;
  
  // Vertex shader
  static const char* vShader = &quot;      \n\
      #version 330                        \n\
                                          \n\
      layout (location = 0) in vec3 pos;  \n\
                                          \n\
      uniform mat4 model;                 \n\
                                          \n\
      void main()                         \n\
      {                                   \n\
          gl_Position = model * vec4(0.4 * pos.x, 0.4 * pos.y, pos.z, 1.0); \n\
      }
      &quot;;
  
  // Fragment shader
  static const char* fShader = &quot;      \n\
      #version 330                        \n\
                                          \n\
      out vec4 colour;                    \n\
                                          \n\
      void main()                         \n\
      {                                   \n\
          colour = vec4(1.0, 0.0, 0.0, 1.0); \n\
      }
      &quot;;
  
  void CreateTriangle()
  {
      GLfloat vertices[] = {
          -1.0f, -1.0f, 0.0f,
          1.0f, -1.0f, 0.0f,
          0.0f, 1.0f, 0.0f
      };
  
      glGenVertexArrays(1, &amp;VAO);
      glBindVertexArray(VAO);
  
      glGenBuffers(1, &amp;VBO);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
      glEnableVertexAttribArray(0);
  
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  
      glBindVertexArray(0);
  }
  
  void AddShader(GLuint theProgram, const char* shaderCode, GLenum shaderType)
  {
      GLuint theShader = glCreateShader(shaderType);
  
      const GLchar* theCode[1];
      theCode[0] = shaderCode;
  
      GLint codeLength[1];
      codeLength[0] = strlen(shaderCode);
  
      glShaderSource(theShader, 1, theCode, codeLength);
      glCompileShader(theShader);
  
      GLint result = 0;
      GLchar eLog[1024] = { 0 };
  
      glGetShaderiv(theShader, GL_COMPILE_STATUS, &amp;result);
      if (!result)
      {
          glGetShaderInfoLog(theShader, sizeof(eLog), NULL, eLog);
          printf(&quot;Error compiling the %d shader: '%s'\n&quot;, shaderType, eLog);
          return;
      }
  
      glAttachShader(theProgram, theShader);
  }
  
  void CompileShaders()
  {
      shader = glCreateProgram();
  
      if (!shader)
      {
          printf(&quot;Error creating shader program!\n&quot;);
          return;
      }
  
      AddShader(shader, vShader, GL_VERTEX_SHADER);
      AddShader(shader, fShader, GL_FRAGMENT_SHADER);
  
      GLint result = 0;
      GLchar eLog[1024] = { 0 };
  
      glLinkProgram(shader);
      glGetProgramiv(shader, GL_LINK_STATUS, &amp;result);
      if (!result)
      {
          glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
          printf(&quot;Error linking program: '%s'\n&quot;, eLog);
          return;
      }
  
      glValidateProgram(shader);
      glGetProgramiv(shader, GL_VALIDATE_STATUS, &amp;result);
      if (!result)
      {
          glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
          printf(&quot;Error validating program: '%s'\n&quot;, eLog);
          return;
      }
  
      uniformModel = glGetUniformLocation(shader, &quot;model&quot;);
      if (uniformModel == -1)
      {
          printf(&quot;Failed to get uniform location of \&quot;model\&quot; or the variable is not used&quot;);
      }
  }
  
  int main()
  {
      // Initialize GLFW
      if (!glfwInit())
      {
          printf(&quot;GLFW initialization failed!&quot;);
          glfwTerminate();
          return 1;
      }
  
      // Set up GLFW window properties
      // OpenGL version
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      // Core profile = No backwards compatibility
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  
      GLFWwindow *mainWindow = glfwCreateWindow(WIDTH, HEIGHT, &quot;Test Window&quot;, NULL, NULL);
      if (!mainWindow)
      {
          printf(&quot;GLFW window creation failed!&quot;);
          glfwTerminate();
          return 1;
      }
      // Get buffer size information
      int bufferWidth, bufferHeight;
      glfwGetFramebufferSize(mainWindow, &amp;bufferWidth, &amp;bufferHeight);
  
      // Set the context for GLEW to use
      glfwMakeContextCurrent(mainWindow);
  
      // Allow modern extension features
      glewExperimental = GL_TRUE;
  
      if (glewInit() != GLEW_OK)
      {
          printf(&quot;GLEW initialization failed!&quot;);
          glfwDestroyWindow(mainWindow);
          glfwTerminate();
          return 1;
      }
  
      // Setup viewport size
      glViewport(0, 0, bufferWidth, bufferHeight);
  
      CreateTriangle();
      CompileShaders();
  
      // Loop until window closed
      while (!glfwWindowShouldClose(mainWindow))
      {
          // Get + handle user input events
          glfwPollEvents();
  
          if (direction)
              triOffset += triIncrement;
          else
              triOffset -= triIncrement;
  
          if (abs(triOffset) &gt;= triMaxoffset)
              direction = !direction;
  
          // Clear the window
          glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
  
          glUseProgram(shader);
  
          glm::mat4 model;
          model = glm::translate(model, glm::vec3(triOffset, 0.0f, 0.0f));
  
          glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
  
          glBindVertexArray(VAO);
          glDrawArrays(GL_TRIANGLES, 0, 3);
  
          glBindVertexArray(0);
  
          glUseProgram(0);
  
          glfwSwapBuffers(mainWindow);
      }
  
      return 0;
  }
  </code></pre>
  <p>The triangle appears back if I just remove the &quot;model * &quot; part before vec4 in vShader code, but what is wrong here with that &quot;model&quot; matrix?</p>