title: Texture Array access not working Opengl 3.3, GLSL 330
body: <p>I am trying to use a 2D texture array as a texture atlas. I have the following code to render a single square. The square renders if I replace the texture lookup in the fragment shader with a simple colour output, and it renders one of the textures properly if I use and set up a simple 2D texture instead of a 2d Texture Array. However, when trying to create and use the 2d Texture array, I simply get a black screen. What gives?</p>
  
  <pre><code>//Shader Sources
  const GLchar* vertexsource =
      "#version 330\n"
      "in vec2 position;"
      "in vec2 texcoord;"
      "out vec2 f_texcoord;"
      "void main() {"
      "   gl_Position = vec4(position, 0.0, 1.0);"
      "   f_texcoord = texcoord;"
      "}";
  const GLchar* fragmentsource =
      "#version 330\n"
      "in vec2 f_texcoord;"
      "out vec4 color;"
      "uniform float time;"
      "uniform sampler2DArray sample;"
      "void main() {"
      "   float layer = step(sin(time), 0.0);"
      "   color = texture(sample, vec3(f_texcoord, 0.0));\n"
      "   //color = vec4(1.0,1.0,1.0,1.0);\n"
      "}";
  
  //Vertex Data
  GLfloat vertices[] = {
      //Verts         Texture verts
      -0.5f, 0.5f,    0.0f, 0.0f,
      0.5f, 0.5f,     1.0f, 0.0f,
      0.5f, -0.5f,    1.0f, 1.0f,
      -0.5f, -0.5f,   0.0f, 1.0f
  };
  
  GLuint elements[] = {
      0, 1, 2,
      2, 3, 0
  };
  
  Shaders::SetLogger(log_);
  VertexShader vertexshader("D3 Vertex Shader", vertexsource);
  FragmentShader fragshader("D3 Frag Shader", fragmentsource);
  program = new ShaderProgram("D3 Shader Program");
  program-&gt;AttachShader(vertexshader);
  program-&gt;AttachShader(fragshader);
  program-&gt;LinkProgram();
  glUseProgram(program-&gt;glIdentifier());
  
  GLint posattrib = program-&gt;GetAttributeLocation("position");
  GLint texattrib = program-&gt;GetAttributeLocation("texcoord");
  //Dont actually need this because only one Texture unit in use
  GLint coloruniform = program-&gt;GetUniformLocation("sample");
  
  GLuint vbo;
  glGenBuffers(1, &amp;vbo);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
  GLuint ebo;
  glGenBuffers(1, &amp;ebo);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
  
  glEnableVertexAttribArray(posattrib);
  glVertexAttribPointer(posattrib, 2, GL_FLOAT, GL_FALSE,
                        4 * sizeof(GLfloat),
                        (GLvoid*)0);
  
  glEnableVertexAttribArray(texattrib);
  glVertexAttribPointer(texattrib, 2, GL_FLOAT, GL_FALSE,
                        4 * sizeof(GLfloat),
                        (GLvoid*)(2 * sizeof(GLfloat)));
  
  //glUniform1i(coloruniform, 0);
  
  int width, height;
  unsigned char* image = SOIL_load_image("sample.png", &amp;width, &amp;height,
                                         0, SOIL_LOAD_RGBA);
  
  int width2, height2;
  unsigned char* image2 = SOIL_load_image("sample2.png", &amp;width2, &amp;height2,
                                         0, SOIL_LOAD_RGBA);
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  
  glActiveTexture(GL_TEXTURE0);
  GLuint texture;
  glGenTextures(1, &amp;texture);
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  glBindTexture(GL_TEXTURE_2D_ARRAY, texture);    
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  
  glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 
               width, height, 2,
               0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)0);
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  log_-&gt;Debug("Requested memory for texture");
  
  
  glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 
                  0, 0, 0, 
                  width, height, 1,
                  GL_RGBA, GL_UNSIGNED_BYTE, image);
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 
                  0, 0, 1, 
                  width2, height2, 1, 
                  GL_RGBA, GL_UNSIGNED_BYTE, image2);
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  
  
  glUniform1i(coloruniform, 0);
  errorstream &lt;&lt; glGetError() &lt;&lt; " ";
  log_-&gt;Debug(errorstream.str());
  </code></pre>
  
  <p>And the actual drawing code:</p>
  
  <pre><code>GLint timeuniform = program-&gt;GetUniformLocation("time");
  GLint sampleuniform = program-&gt;GetUniformLocation("sample");
  glActiveTexture(GL_TEXTURE0);
  glUniform1f(timeuniform, frametimer_.RunningTime().asSeconds());
  glUniform1i(sampleuniform, 0);
  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  window_-&gt;display();
  </code></pre>
  
  <p>Edit: Edited in suggested changes suggested by jozxyqk</p>