title: Uniform mat4 has unknown values when created by glUniformMatrix4fv
body: <p>I have a very basic engine based on OpenGL that can render polygons in any color to a resizeable window. I'm now trying to implement matrices for my shaders by giving the model, perspective and view matrices as uniforms via my shader. Before adding the uniforms everything worked as it should, I could even pass in a uniform <code>vec2</code> to simulate a light source at my mouse position. The uniform <code>mat4</code>s doesn't work as well as the <code>vec2</code>s.</p>
  
  <p>For debugging purposes I'm only rendering one yellow square centered on the screen. When using no uniforms the square shows as expected. i now try passing in one <code>mat4</code>, set as an identity matrix. In the vertex shader I'm multiplying <code>gl_Position</code> by the identity matrix I uniformed. When I run the program it only shows a black window.</p>
  
  <p>I've tried manually creating an identity matrix in the vertex shader and multiplying <code>gl_Position</code> by that matrix instead of the uniform one. When I do that my yellow square shows as normal. This leads me to believe that the uniform <code>mat4</code> doesn't get the correct values, however, I don't know how to check the values of the matrix when it's being used in the shader.</p>
  
  <p>This is how my vertex shader looks:</p>
  
  <pre><code>#version 430 core
  
  layout (location = 0) in vec3 position;
  layout (location = 1) in vec4 color;
  
  out vec4 Color;
  
  uniform mat4 model;
  uniform mat4 view;
  uniform mat4 project;
  
  void main()
  {
      mat4 id;
      id[0] = vec4(1.0, 0.0, 0.0, 0.0);
      id[1] = vec4(0.0, 1.0, 0.0, 0.0);
      id[2] = vec4(0.0, 0.0, 1.0, 0.0);
      id[3] = vec4(0.0, 0.0, 0.0, 1.0);
      Color = color;
      gl_Position = id * vec4(position, 1.0);
  }
  </code></pre>
  
  <p>The <code>mat4 id</code> is the manually created identity matrix, when changing <code>id *</code> to <code>model *</code> I get the black window.</p>
  
  <p>This is how my fragment shader looks:</p>
  
  <pre><code>#version 430 core
  
  in vec4 Color;
  
  out vec4 outColor;
  
  void main()
  {
      outColor = Color;
  }
  </code></pre>
  
  <p>The shader is initialized by this code:</p>
  
  <pre><code>m_shaderID = glCreateProgram();
  
  const char* vertexSource = ReadFile::readFile(vertpath);
  const char* fragmentSource = ReadFile::readFile(fragpath);
  
  GLint status;
  
  // Vertex Shader
  GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertexShader, 1, &amp;vertexSource, NULL);
  glCompileShader(vertexShader);
  glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &amp;status);
  if (status != GL_TRUE)
  {
      std::cout &lt;&lt; "Failed to compile vertex shader!\nInfo log: " &lt;&lt; std::endl;
      char buffer[512];
      glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
      std::cout &lt;&lt; buffer &lt;&lt; std::endl;
      glDeleteShader(vertexShader);
  }
  
  // Fragment Shader
  GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragmentShader, 1, &amp;fragmentSource, NULL);
  glCompileShader(fragmentShader);
  glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &amp;status);
  if (status != GL_TRUE)
  {
      std::cout &lt;&lt; "Failed to compile fragment shader!\nInfo log: " &lt;&lt; std::endl;
      char buffer[512];
      glGetShaderInfoLog(fragmentShader, 512, NULL, buffer);
      std::cout &lt;&lt; buffer &lt;&lt; std::endl;
      glDeleteShader(fragmentShader);
  }
  
  // Shader program
  glAttachShader(m_shaderID, vertexShader);
  glAttachShader(m_shaderID, fragmentShader);
  glLinkProgram(m_shaderID);
  glValidateProgram(m_shaderID);
  
  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);
  </code></pre>
  
  <p>The matrix is created as a uniform by this code:</p>
  
  <pre><code>void Shader::setUniformmat4(const GLchar* name, glm::mat4 matrix)
  {
      for (int i = 0; i &lt;= 3; i++)
          printf("%f, %f, %f, %f\n", matrix[i].x, matrix[i].y, matrix[i].z, matrix[i].w);
      glUniformMatrix4fv(glGetUniformLocation(m_shaderID, name), 1, GL_FALSE, glm::value_ptr(matrix));
  }
  </code></pre>
  
  <p>The <code>printf</code> is for checking the values of the matrix as they are used to create the uniform, and they have the values of an identity matrix at that point.</p>
  
  <p>The function <code>setUniformmat4</code> is called by this code:</p>
  
  <pre><code>glm::mat4 model = glm::mat4(1.0);
  shader.setUniformmat4("model", model);
  </code></pre>
  
  <p>When creating a lighting effect I create the uniform by calling this function:</p>
  
  <pre><code>void Shader::setUniformvec2(const GLchar* name, glm::vec2 vector)
  {
      glUniform2f(glGetUniformLocation(m_shaderID, name), vector.x, vector.y);
  }
  </code></pre>
  
  <p>via this piece of code:</p>
  
  <pre><code>shader.setUniformvec2("light_pos", glm::vec2((x / window.getWidth()) * 2.0 - 1.0, 1.0 - 2.0 * (y / window.getHeight())));
  </code></pre>
  
  <p>Where <code>x</code> and <code>y</code> are the mouses coordinates. I then add the line </p>
  
  <pre><code>uniform vec2 light_pos;
  </code></pre>
  
  <p>To the fragment shader. This works no problem, and it traces the mouse perfectly. The function used for setting the uniform <code>mat4</code> looks the same as the function for setting the uniform <code>vec2</code>, only difference is the 4fv for the <code>mat4</code> and 2f for the <code>vec2</code>.</p>
  
  <p>As you can see, I'm using glm for the matrices and vectors.</p>
  
  <p>My main function looks like this:</p>
  
  <pre><code>    Window window(720, 720, "Window");
  
      Shader shader("shader.vert", "shader.frag");
  
      glm::mat4 model = glm::mat4(1.0);
      shader.setUniformmat4("model", model);
  
      Renderer* renderer = new Renderer();
  
      std::vector&lt;StaticSprite*&gt; sprites;
  
      sprites.push_back(new StaticSprite(-0.5, -0.5, 0.0, 1.0, 1.0, glm::vec4(1.0, 1.0, 0.0, 1.0), &amp;shader));
  
      while (!window.closed())
      {
          window.clear();
          shader.enable();
          double x, y;
          window.getMousePosition(x, y);
          shader.setUniformvec2("light_pos", glm::vec2((x / window.getWidth()) * 2.0 - 1.0, 1.0 - 2.0 * (y / window.getHeight())));
  
          for (StaticSprite* sprite : sprites)
              renderer-&gt;submit(sprite);
          renderer-&gt;flush();
  
          shader.disable();
          window.update();
      }
      return 0;
  </code></pre>
  
  <p>My question summarized is basically why are the values of the uniform mat4 not correct, is there any way to find out what those values are, and what should I change in the code to make the uniform mat4s work?</p>
  
  <p>Please ask for any additional information needed to give an answer, I will happily provide anything I forgot to include.</p>