OpenGL Best way to test the depth?

Recommended Posts

I have a heightmap that i test the depth in a special + faster way. & now i need models on this heightmap, then i need to test the depth of all models only. But I think that OpenGL will test with all the triangles I've already drawn (the H-Map). & if i draw the H-Map last the models will get overlapped by it. Im I able to use my own way to test the depth & then use OpenGL's in a fast way? If not then: Is the depth tested in the Graphics card? (im thinking of calculating the depth myself in the CPU) Thanx!

Share on other sites
Not sure I follow. Wouldn't you want the heightmap to overlap models where occlusion occures? If a part of the heightmap is in front of a model, it should be obscured, no?

Share on other sites
Im not 100% sure what i want yet.. So it is hard to explain. Im testing a weird technique... But can someone please answer this: Is the depth tested in the Graphics card?

[Edited by - masterviking on April 29, 2006 6:26:55 AM]

Think so, yeah.

Share on other sites
Im sorry but a "think so" is not enough... I now know what I want. Question:
Can I calculate the depth of only a single soup of triangles? (Using OpenGL's built in system!?)
a_a _ a_ a_ a_
\_||_||_||_||_/
|\||_||_||_||/|
|_\|_|SOUP|/_|
|_|\_||_||_/|_|
|_||\||_||/||_|
aaaaa\aa/
aaaaaa\/
aaaaa_cam

The squares is heightmap squares :)

& then draw that soup at a position while drawing the heightmap sqares from the top to the bottom?

Share on other sites
EDIT: Oh wait, I think I misinterpreted your question.

If you want to draw a height map, you simply have to put the z coordinate of the heights further away from the screen than your models. The graphics card's rasteriser and OpenGL's vertex transformations will take care of your z-ordering for you. It will also run very fast. :) Do not try to calculate depth from the camera on the CPU, unless you want your program to run very slowly. The GPU does all that these days.

For reference, could you clarify, are you doing a 2D or 3D effect? It appears that you are talking about a 2D effect, in which case, even in orthographic mode, OpenGL uses a z coordinate for ordering polygons' distances from screen.

[Edited by - Leo_E_49 on May 7, 2006 6:12:50 PM]

Share on other sites
Thanx for trying to answer :)
But I really want a faster way to do this, because I see that it whould be possible... But its maybe alot of work.

\________/
a\_Soup_/
aa\____/
aaa\aa/
aaaa\/
aaa_cam

The line above the soup is a wall & also the line below.

If it whould be possible to render the scene in 3 steps then it whould work fine - first the wall above then the soup & then the wall below. [But only if it is possible to render it in 3 steps inthis way!]

Simple:
Then this became another question:
Are you able to send triangles to the graphics card & then tell it to render them & after that send in new triangles to be rendered over them?

Haha, yes! (know the answer) The power of intelligent questions have shown up!

To make other learn on this topic, i will answer the question: You can actually use the function "SwapBuffers([pointer to a hardware device context])", & not clear the screen in-between.
Like this:
DrawWall1();
SwapBuffers(hdc);
DrawSoup();
SwapBuffers(hdc);
DrawWall2();
SwapBuffers(hdc);

But now we can discuss if this way is faster :) ..hmm..

EDIT: & I will have to use the "Swap" function 100-1000 times... So that doesn't sound very good...

Share on other sites
Yes, that's what I meant.

Just call the SwapBuffers() function only once:
DrawWall1();DrawSoup();DrawWall2();SwapBuffers(hdc);

Everything is handled by OpenGL for you. :)

From what I understand, the SwapBuffers function is very slow compared to the rest of OpenGL, so you should only call it once per scene render.

OpenGL uses the graphics hardware so it is very much faster than processing on the CPU and sending the data to the graphics card so many times. OpenGL sends all the data from the scene at once to the graphics card to speed up the rendering (this is usually called batch processing).

I will give you a basic explanation of how this all works so you understand. The following is called the "Graphics Pipeline" (It's a general version, not an official OpenGL version):

1. You enter all your vertex data and matrices into the program.
1a. Your vertex data and matrices are sent to the graphics card.
2. Your vertices are transformed in the GPU by a world matrix into "world space".
3. Lighting is performed on all your vertices.
4. Your vertices are transformed by a view matrix into "view space".
5. Your vertices are projected by a projection matrix into "screen space".
6. Triangles are created from your vertices.
7. The triangles are clipped to the screen coordinates.
8. The triangles are rasterized to a graphical buffer by a rasterizer.
9. Textures and colours are applied to the buffer where your triangles are.
10. The buffer is copied to screen.

This happens once every frame. (P.S. If anyone sees anything wrong with this, please correct me)

2 to 5 can be replaced with a vertex shader. 9 can be replaced with a fragment shader.

A full description of this can be found here: clicky

Sorry to confuse you earlier. I made a mistake and didn't understand what you were talking about. :(

[Edited by - Leo_E_49 on May 7, 2006 6:19:49 PM]

Share on other sites
"A full description of this can be found here: clicky"
That is alot of text, but thank you. I will read it.

This text I didn't see:
"OpenGL uses a z coordinate for ordering polygons' distances from screen."
That's only if you enable GL_DEPTH_TEST... & that function is very undesirable to use when you can draw some triangles in the right order.. But what im talking about is that I can draw most triangles in the right order but some I cannot.

Ok - now I see that it whould be resource eating to use SwapBuffers(hdc); all the time... I know that there is no way to exactly do this with a simple function, but Im looking for some way to send blocks/soups of triangles that the graphics card should calculate the depth on (on each block you send [but not check what blocks is closest/furthest from cam]) & then I just give a 3D position where the block should be drawn, & in what order they should be drawn (in the order that i sent the blocks in).

So the graphics card should check the depth of only some triangles that I send in, not all. But I have heard that GL_DEPTH_TEST checks the depth on all tris. Or?

Share on other sites
Well, you can disable GL_DEPTH_TEST and test depths of objects yourself by sorting them. However, it's unlikely that your depth sorting algorithm will be able to match the speed of OpenGL anyway. Furthermore, using GL_DEPTH_TEST, depths are tested per pixel. So if an object appears infront of as well as behind another object, it will draw correctly. Where as, if the objects are just simply drawn in order without GL_DEPTH_TEST enabled, it will be impossible to do this (unless you do the test for every triangle, which will be very slow and also not as accurate).

GL_DEPTH_TEST will automatically handle your triangles so you do not need to sort them. It uses something called the "painter's algorithm" on every pixel to do this.

http://en.wikipedia.org/wiki/Painter's_algorithm

I'm not sure why you say that GL_DEPTH_TEST is very undesireable to use? What makes you say that? It works well for most people.

Share on other sites
"Why do you say that?" Because it whould be "undesirable" if you was able to do the kind of block drawing I tried to described in the same post. The painter's algorithm is only needed to be applied to the separate blocks.. If it where like that.. But im just dreaming...

Thank you for giving me inputs! :)

I will just stick with the GL_DEPTH_TEST

Create an account

Register a new account

• Forum Statistics

• Total Topics
627702
• Total Posts
2978712
• Similar Content

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• 21
• 14
• 12
• 10
• 12