Jump to content
  • Advertisement
Sign in to follow this  
Vincent_M

OpenGL Depth Issues with Instanced Rendering

This topic is 1255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm learning about instanced rendering, and I just rendered 10,000 cubes to the scene with a single call to glDrawElementsInstanced(). All of my cubes rendered with their randomly generated transform matrices and solid colors, however the depth for all of these boxes weren't correct. At first, I thought it was due to instanced rendering. Then, I drew 2 cubes with their own draw call. The cube furthest away was drawn last per frame, and it still drew over the cube in front of it when it was overlapping my front cube in the framebuffer. I made sure GL_DEPTH_TEST was enabled, and glDepthFunc() was set to GL_LEQUAL. I didn't change the defaults for depth writing, masking or range. I'm also using GLFW to generate my windows and context. I even explicitly set GLFW_DEPTH_BITS to 24 using glfwWindowHint() before I called glfwCreateWindow():

int main(int argc, char** argv)
{
	float lastTime = 0.0f;
	GLFWwindow *window = nullptr;

	// setup error callback, and initialize GLFW
	glfwSetErrorCallback(error_callback);
	if (!glfwInit())
	{
		printf("GLFW wasn't initialized");
		exit(EXIT_FAILURE);
	}

	// set the context
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	//glfwWindowHint(GLFW_SAMPLES, 4);
	glfwWindowHint(GLFW_DEPTH_BITS, 24);

	// create the window
	window = glfwCreateWindow(800, 600, "GLFW Test", nullptr, nullptr);
	if (!window)
	{
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	// setup window callbacks
	glfwSetKeyCallback(window, key_callback);
	glfwSetFramebufferSizeCallback(window, resize_callback);

	// set the current context and swap interval
	glfwMakeContextCurrent(window);
	glfwSwapInterval(1);

	// initialize GLEW
	glewExperimental = 1;
	GLenum err = glewInit();
	if (err != GLEW_OK)
	{
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		return -1;
	}
	glGetError(); // flush out possible GL_INVALID_ENUM error that glewExperimental causes upon glewInit()

	// output the OpenGL version, renderer and number of samples
	printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
	printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER));

	// enable initial states
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glEnable(GL_BLEND);

	// configure initial parameters
	glFrontFace(GL_CCW);
	glDepthFunc(GL_LEQUAL);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glActiveTexture(GL_TEXTURE0);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	InitCommon(); // initialize

	// install states
	//states.push_back(new ModelState("Model Processor", window));
	states.push_back(new OculusState("Oculus State", window));
	states.push_back(new IndirectRenderingState("Indirect Rendering State", window));
	states.push_back(new BasicInstancingState("Basic Instancing", window));
	states.push_back(new WireCubeState("Cube State", window));
	states.push_back(new SpritePackerState("Sprite Packer State", window));
	states.push_back(new BinPackerState("Bin Packer State", window));
	states.push_back(new BasicTextureState("Basic Texture State", window));
	states.push_back(new TriangleState("Triangle State", window));
	SetState(0);

	// main window loop
	lastTime = (float)glfwGetTime();
	while(!glfwWindowShouldClose(window))
	{
		GamepadManager::Update();

		// update and render the current state
		if(currentState)
		{
			currentState->Update((float)glfwGetTime() - lastTime);
			currentState->Render();
		} else
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// swap buffers and poll for events
		lastTime = (float)glfwGetTime();
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	// release all states
	for (State *state : states)
	{
		if (state != nullptr)
		{
			state->UnloadContent();
			delete state;
		}
	}

	// cleanup
	states.clear();
	ShutdownCommon();
	glfwTerminate();
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

I finally had a chance to try this, and I still got the same results. Even though blending is enabled, all of my objects are completely opaque.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!