Question

I'm actually working on making AR with the HMD oculus rift. I'm not a pro on openGL and I'm sure it is the source of my problem.

I get this error:

Unhandled exception at 0x064DBD07 (nvoglv32.dll) in THING.exe: 0xC0000005: Access violation reading location 0x00000000.

It hapens during the drawing in quadGeometry_supcam->draw(); that is in renderCamera(eye); :

  glDrawElements(elementType, elements * verticesPerElement,
    GL_UNSIGNED_INT, (void*) 0);// Stop at this line in the debuger

Here's the drawing code

  frameBuffer.activate();
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  gl::Stacks::with_push(pr, mv, [&]{
    mv.preMultiply(eyeArgs.modelviewOffset);
    pr.preMultiply(eyeArgs.projectionOffset);
      //renderCamera(eye); //If I uncomment this part, it crash
    renderScene(eye);
      //renderCamera(eye); //If I uncomment this part, it works but It's not what I want
  });
  frameBuffer.deactivate();
  glDisable(GL_DEPTH_TEST);

  viewport(eye);
  distortProgram->use();
  glActiveTexture(GL_TEXTURE1);
  eyeArgs.distortionTexture->bind();
  glActiveTexture(GL_TEXTURE0);
  frameBuffer.color->bind();
  quadGeometry->bindVertexArray();
  quadGeometry->draw();
  gl::VertexArray::unbind();
  gl::Program::clear();

//////////////////////////////////////////////////////////////

void renderScene(StereoEye eye) {
  GlUtils::renderSkybox(Resource::IMAGES_SKY_CITY_XNEG_PNG);
  GlUtils::renderFloorGrid(player);
  gl::MatrixStack & mv = gl::Stacks::modelview();
  gl::Stacks::with_push(mv, [&]{
  mv.translate(glm::vec3(0, eyeHeight, 0)).scale(ipd);
  GlUtils::drawColorCube(true); // Before this call renderCamera crash after it works
});
}

void renderCamera(StereoEye eye) {
  gl::ProgramPtr program;
  program = GlUtils::getProgram(
      Resource::SHADERS_TEXTURED_VS,
      Resource::SHADERS_TEXTURED_FS);
  program->use();
  textures[eye]->bind();
  quadGeometry_supcam->bindVertexArray();
  quadGeometry_supcam->draw();
 }

If I call renderCamera before GlUtils::drawColorCube(true); it crash but after it works. But I need to draw the camera before the rest. I'm not going to precise drawColorCube because it use an other shader. I suppose that the problem comes from something missing for the shader program. So here's the fragment and vertex shader.

Vertex:

uniform mat4 Projection = mat4(1);
uniform mat4 ModelView = mat4(1);

layout(location = 0) in vec4 Position;
layout(location = 1) in vec2 TexCoord0;

out vec2 vTexCoord;

void main() {
  gl_Position = Projection * ModelView * Position;
  vTexCoord = TexCoord0;
}

Fragment:

uniform sampler2D sampler;

in vec2 vTexCoord;
out vec4 vFragColor;

void main() {
  vFragColor = texture(sampler, vTexCoord);
}

(I want to draw the scene on the image of the camera). Any idea?

Was it helpful?

Solution 2

(Edited)

I thought the problem was in NULL being passed as indices parameter to glDrawElements() here:

 glDrawElements(elementType, elements * verticesPerElement,
                GL_UNSIGNED_INT, (void*) 0);

The last parameter, indices, used to be mandatory and passing in 0 or NULL wouldn't work, because it would then try to read the indices from that address and would produce that error Access violation reading location 0x00000000.

But as Jherico pointed out, it is possible to have a buffer with indices bound to GL_ELEMENT_ARRAY and then the indices parameter is an offset into that buffer, not a pointer. Passing 0 then simply means 'no offset'. And Oculus Rift SDK apparently uses this method.

The program crashing with that invalid access to address 0x0 at the line with glDrawElements() therefore does not indicate that 0 should not be used, but that the GL_ELEMENT_ARRAY buffer is not initialized or enabled correctly. Probably because of an improper cleanup, as Jherico pointed out in his better answer.

OTHER TIPS

You aren't cleaning up the GL state after you call quadGeometry_supcam->draw();. Try adding these lines after that call

gl::VertexArray::unbind();
gl::Program::clear();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top