[Opengl] Sorting vertices( further to nearest)

Started by
6 comments, last by Meitii Metsla 10 years ago
Hello everyone!
Recently i discovered that in order to prevent blending overlapping we have to draw furthest vertices first and then the nearest ones.
By overlapping i meant weird thing that sometimes transparent objectid lost their drawing order.
For example, there is quad before the one quad and it seems like that the quad behind the ohter one is rendered to screen first.
Giving really unrealistic results.
I have heard about vertex buffer objects what the latest opengl support im really not familiar with that, i dont know if the sorting is needed there.
Yet i have not seen any sorting function in tutorials. I guess the tutorials are meant for basic stuff only.
I have used glBegin(GL_QUADS) and GL_TRIANGLES to draw things but im moving soon to the modern version.
I think im just not bright enough to come up with the good solution but right now what im having in
my mind is that i run a loop where i check all vertices origin relatively to camera and render the further first but the problem is that
insead of checking all vertices once i need to check them much more.
Finding the furthest then deleting it from list. Running again the same loop again finding furthest and so on...
Alot of checking and i believe there much be better solution.
I would be very grateful if someone can help me with this.
Thank you in advance!
-Gyiove.
Advertisement
You need to sort models/sprites, not vertices.

Sorting is as easy as calling std::sort on your list of models with a comparison functor that compares distance from the camera.

Something like the following untested code:

class CompareDepth
{
  Camera& _camera;

public:
  CompareDepth(Camera& camera) : _camera(camera) {}

  bool operator()(const Sprite* lhs, const Sprite* rhs)
  {
    return camera.DistanceTo(lhs->GetPosition()) < camera.DistanceTo(rhs->GetPosition());
  }
};

void sort_sprites(vector<Sprite*>& sprites, Camera& camera)
{
  std::sort(sprites.begin(), sprites.end(), CompareDepth(camera));
}

Sean Middleditch – Game Systems Engineer – Join my team!

Im little bit confused now.
This is how i see the model in my game engine:
The model can contain either opaque and transparent triangles.
There can be more than just the one transparent triangle and because they are basically in random order this problem will appear even when i just draw 1 model.
Well at least this is happening when i use glBegin(GL_QUADS) and GL_TRIANGLES.
The models i have used right now are from counter strike 1.6, smd format.
Also if there is any other information needed please ask.
I think there have been simple misunderstanding because my lack of information given in first post.
The problem is that im pretty new at this and dont know exacly where what is needed.
My apologies.

The need to sort triangles is extremely rare, and considering Counter Strike doesn’t do it at all indicates you are just approaching the problem wrong.

First-off, you need to draw opaque triangles and translucent triangles in separate passes. Draw opaque first, disable depth writing, enable blending, and draw the translucent triangles.

You don’t sort the triangles, you sort the objects back-to-front for the translucent pass.

If you feel the need to sort triangles, you are probably doing it wrong, and you need to provide a screenshot as proof after implementing a proper render loop.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

2014-04-10_082648.png


	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);
	glDepthFunc(GL_LEQUAL);
	glDepthRange(0.0f, 1.0f);
	
	glDisable(GL_BLEND);
	shader[0].use();

	glUniform1i(shader_param[0][0], 0);
	glBindTexture(GL_TEXTURE_2D, textures[0].map[0].gl_pixels);
	glActiveTexture(GL_TEXTURE0);

	// render opaque quad (That dark textured quad)
	render_quad( 10.0, -99.0, 10.0, 30.0);
	glDisable(GL_DEPTH_TEST);

	if( textures[1].alphatype == 0 ) shader[0].use();
	else shader[1].use();
	glEnable (GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE);

	// render transparent quads
	glUniform1i(shader_param[1][0], 0);
	glBindTexture(GL_TEXTURE_2D, textures[1].map[0].gl_pixels);
	glActiveTexture(GL_TEXTURE0);
	render_quad( 0.0, -60.0, 0.0, 30.0);

	glUniform1i(shader_param[1][0], 0);
	glBindTexture(GL_TEXTURE_2D, textures[1].map[0].gl_pixels);
	glActiveTexture(GL_TEXTURE0);
	render_quad( -10.0, -131.0, -10.0, 30.0);

That sounded logic to me too first yet after trying i get this.

Don't disable depth test, you just want to disable writing to the depth buffer glDepthMask(false);

Well I think that is the correct function, opengl is a little rusty smile.png

Im little bit confused now.
This is how i see the model in my game engine:
The model can contain either opaque and transparent triangles.


To add some generality to the other answers: a single game object can be made of multiple models and a single model may be made of multiple meshes using different materials. Whether to use opaque or translucent rendering should be up to the material. When you draw your scene, push each model/mesh into a render queue with its associated material. Draw all the meshes with opaque materials then all the meshes with translucent materials.

Your game loop should look something like:

 for each object in world:
  if not cull object:
   for each mesh in object:
    add mesh,mesh.material to queue named mesh.material.queue_name

 sort front-to-back opaque_queue
 sort back-to-front translucent_queue

 draw opaque_queue
 draw translucent_queue
 draw hud
You very well might have more than two render queues depending on the effects you're trying to pull off. Even in some simple games I've worked on I've had a need for four or more with slightly different properties. The render queues aren't just differented by opaque or translucent, but also by the sorting needs (front-to-back, back-to-front, none), depth flags (read enabled, write enabled), and other state that affects more than just the material being drawn. The queues may also be groups by other material properties in order to achieve better material batching.

Note also how I sorted opaque geometry front-to-back. You generally want to do that if you're going to be sorting, especially if you don't have any kind of multi-pass rendering with a depth pre-pass. Sorting front-to-back is a performance gain; it should have zero visible impact on your scene since drawing opaque objects in random order with z-buffering enabled will give you the right output, it just might have a lot of over-draw that the sorting avoids. The sorting has to be compared with batching and the trade-offs need to be tuned to your specific game and the specific scenes and objects you're rendering (suffering some extra overdraw to get better batching may be a win, or it may not).

Sean Middleditch – Game Systems Engineer – Join my team!

Thank you all!
code:


glDepthMask(true);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cammove.update(); // Check keys pressed and mouse movements ( Calculating velocity direction and angles )
camera.update();// update camera position

glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);

shader[0].use();

glUniform1i(shader_param[0][0], 0);
glBindTexture(GL_TEXTURE_2D, textures[0].map[0].gl_pixels);
glActiveTexture(GL_TEXTURE0);

// render dark quad
render_quad( 10.0, -99.0, 10.0, 30.0);


if( textures[1].alphatype == 0 ) shader[0].use();
else shader[1].use();
glEnable (GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glDepthMask(false);

glUniform1i(shader_param[1][0], 0);
glBindTexture(GL_TEXTURE_2D, textures[1].map[0].gl_pixels);
glActiveTexture(GL_TEXTURE0);
render_quad( 0.0, -60.0, 0.0, 30.0);

glUniform1i(shader_param[1][0], 0);
glBindTexture(GL_TEXTURE_2D, textures[1].map[0].gl_pixels);
glActiveTexture(GL_TEXTURE0);
render_quad( -10.0, -131.0, -10.0, 30.0);
The problem was all along that i enabled glDepthMask in the wrong place.
( I was setting it true after i updated camera position )
Now everything is in right order and working fine.

This topic is closed to new replies.

Advertisement