Jump to content

  • Log In with Google      Sign In   
  • Create Account


[Opengl] Sorting vertices( further to nearest)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Gyiove   Members   -  Reputation: 141

Like
1Likes
Like

Posted 09 April 2014 - 12:28 PM

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.
 


Sponsor:

#2 SeanMiddleditch   Members   -  Reputation: 4776

Like
2Likes
Like

Posted 09 April 2014 - 04:00 PM

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));
}


#3 Gyiove   Members   -  Reputation: 141

Like
1Likes
Like

Posted 09 April 2014 - 11:04 PM

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.


#4 L. Spiro   Crossbones+   -  Reputation: 12900

Like
4Likes
Like

Posted 09 April 2014 - 11:13 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Gyiove   Members   -  Reputation: 141

Like
0Likes
Like

Posted 09 April 2014 - 11:37 PM

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.
 


Edited by Gyiove, 09 April 2014 - 11:39 PM.


#6 Stainless   Members   -  Reputation: 805

Like
1Likes
Like

Posted 10 April 2014 - 08:47 AM

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



#7 SeanMiddleditch   Members   -  Reputation: 4776

Like
2Likes
Like

Posted 10 April 2014 - 10:15 AM

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).

#8 Gyiove   Members   -  Reputation: 141

Like
0Likes
Like

Posted 11 April 2014 - 05:11 AM

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.
 

Edited by Gyiove, 11 April 2014 - 05:12 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS