• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Meitii Metsla

[Opengl] Sorting vertices( further to nearest)

7 posts in this topic

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

Share this post


Link to post
Share on other sites
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));
}
2

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0