[Opengl] Sorting vertices( further to nearest)
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));
}
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
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
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).
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);
( I was setting it true after i updated camera position )