title: GL_INVALID_OPERATION when attempting to sample cubemap texture
body: <p>I'm working on shadow casting using <a href="http://learnopengl.com/#!Advanced-Lighting/Shadows/Point-Shadows" rel="nofollow noreferrer">this lovely tutorial.</a> The process is, we render the scene to a frame buffer, attached to which is a cubemap to hold the depth values. Then, we pass this cubemap to a fragment shader which samples it and gets the depth values from there. </p>
  
  <p>I took a slight deviation from the tutorial in that instead of using a geometry shader to render the entire cubemap at once, I instead render the scene six times to get the same effect - largely because my current shader system doesn't support geometry shaders and for now I'm not too concerned about the performance hit.</p>
  
  <p>The depth cubemap is being drawn to fine, here's a screenshot from gDEBugger: </p>
  
  <p><a href="https://i.stack.imgur.com/zPM0K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zPM0K.png" alt="Depth cubemap"></a></p>
  
  <p>Everything seems to be in order here.</p>
  
  <p>However, I'm having issues in my fragment shader when I attempt to sample this cubemap. After the call to <code>glDrawArrays</code>, a call to <code>glGetError</code> returns <code>GL_INVALID_OPERATION</code>, and as best as I can tell, it's coming from here: (The offending line has been commented)</p>
  
  <pre><code>struct PointLight
  {
    vec3 Position;
    float ConstantRolloff;
    float LinearRolloff;
    float QuadraticRolloff;
    vec4 Color;
    samplerCube DepthMap;
    float FarPlane;
  };
  uniform PointLight PointLights[NUM_POINT_LIGHTS];
  
  [...]
  
  float CalculateShadow(int lindex)
  {
    // Calculate vector between fragment and light
    vec3 fragToLight = FragPos - PointLights[lindex].Position;
  
    // Sample from the depth map (Comment this out and everything works fine!)
    float closestDepth = texture(PointLights[lindex].DepthMap, vec3(1.0, 1.0, 1.0)).r;
  
    // Transform to original value
    closestDepth *= PointLights[lindex].FarPlane;
  
    // Get current depth
    float currDepth = length(fragToLight);
  
    // Test for shadow
    float bias = 0.05;
    float shadow = currDepth - bias &gt; closestDepth ? 1.0 : 0.0;
    return shadow;
  }
  </code></pre>
  
  <p>Commenting out the aforementioned line seems to make everything work fine - so I'm assuming it's the call to the texture sampler that's causing issues. <a href="https://stackoverflow.com/questions/31561598/opengl-cube-map-texture-doesnt-work">I saw that this can be attributed to using two textures of different types in the same texture unit</a> - but according to gDEBugger this isn't the case:</p>
  
  <p><a href="https://i.stack.imgur.com/oUAfP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oUAfP.png" alt="Texture list"></a></p>
  
  <p>Texture 16 is the depth cube map.</p>
  
  <p>In case it's relevant, here's how I'm setting up the FBO: (called only once)</p>
  
  <pre><code>// Generate frame buffer
  glGenFramebuffers(1, &amp;depthMapFrameBuffer);
  
  // Generate depth maps
  glGenTextures(1, &amp;depthMap);
  
  // Set up textures
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_CUBE_MAP, depthMap);
  for (int i = 0; i &lt; 6; ++i)
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT,
      ShadowmapSize, ShadowmapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  
  // Set texture parameters
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  
  // Attach cubemap to FBO
  glBindFramebuffer(GL_FRAMEBUFFER, depthMapFrameBuffer);
  glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthMap, 0);
  glDrawBuffer(GL_NONE);
  glReadBuffer(GL_NONE);
  if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    ERROR_LOG("PointLight created an incomplete frame buffer!\n");
  glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
  </code></pre>
  
  <p>Here's how I'm drawing with it: (called every frame)</p>
  
  <pre><code>// Set up viewport
  glViewport(0, 0, ShadowmapSize, ShadowmapSize);
  
  // Bind frame buffer
  glBindFramebuffer(GL_FRAMEBUFFER, depthMapFrameBuffer);
  
  // Clear depth buffer
  glClear(GL_DEPTH_BUFFER_BIT);
  
  // Render scene
  for(int i = 0; i &lt; 6; ++i)
  {
    sh-&gt;SetUniform("ShadowMatrix", lightSpaceTransforms[i]);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
      GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, depthMap, 0);
    Space()-&gt;Get&lt;Renderer&gt;()-&gt;RenderScene(sh);
  }
  
  // Unbind frame buffer
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
  </code></pre>
  
  <p>And here's how I'm binding it before drawing:</p>
  
  <pre><code>std::stringstream ssD;
  ssD &lt;&lt; "PointLights[" &lt;&lt; i &lt;&lt; "].DepthMap";
  glActiveTexture(GL_TEXTURE4 + i);
  glBindTexture(GL_TEXTURE_CUBE_MAP, pointlights[i]-&gt;DepthMap()); // just returns the ID of the light's depth map
  shader-&gt;SetUniform(ssD.str().c_str(), i + 4); // just a wrapper around glSetUniform1i
  </code></pre>
  
  <p>Thank you for reading, and please let me know if I can supply more information!</p>