Sign in to follow this  

OpenGL Drawing pixel's depth instead of colour

Recommended Posts

fau    103
Hi To begin with - I'm working on a movie about computer game and I have everything to "postprocess" some realistic and precise camera-like effects excluding depth of each pixel. Unfortunately I have a little knowledge of opengl but I understand in general how does pc, os and libraries like opengl work (used to write in assembler) so you can know that probably I'll understand your explanations :) . My problem is that I need an opengl hook (or other method, I don't have source code) that will make the game I'm working on (jedi knight 2 on q3 engine) to alter each pixel's 24 bit colour with it's depth. Accuracy is very important to me. I've heard that opengl uses non-linear depth buffer but maps that I use aren't very big. However if it's going to be non-linear precision then I need to know how to convert it to real depth. I would be very grateful if someone could write such program for me or help me solving this problem in any other way :) Regards and best Christmas wishes Fau [Edited by - fau on December 20, 2008 6:14:49 AM]

Share this post

Link to post
Share on other sites
idinev    236
Here are pointers:
- make and draw on a RGBA32f (4 floats per pixel) FBO (Frame-buffer-object). FBOs are like drawing in a texture, not the limited back-buffer
- use shaders
- put "gl_FragColor = vec(gl_FragCoord.z);" or "gl_FragColor = vec(1.0/gl_FragCoord.w);"
- maybe glReadPixels(...,GL_RGBA,GL_FLOAT);
- done

If you simply copy that FBO's texture onto the backbuffer, to see the color(depth), you'll discard any HDR info and make the RGBA32f rendering useless.

The nonlinearity of the depth-buffer comes only from that 1/depth. (this makes near-to-the-camera pixels get much more precision).

Share this post

Link to post
Share on other sites
idinev    236
Also, since you're using quake3's engine, you can compile your shader with nVidia's Cg's cgc.exe into ARB asm, and follow tutorials on quake-net or something about how to apply that shader on everything. (I've seen there's a tute like that there).

Share this post

Link to post
Share on other sites
fau    103
I understand your concept but as I said getting to know how to write it by myself would take me days. I can compile it and make minor changes if needed but could you possibly write it for me? Also I'd like to ask you for link to site you've mentioned since quake-net is rather common sentence.

Share this post

Link to post
Share on other sites
idinev    236
Just re-read your first post, mentioning "q3" - which I initially understood as Doom3. This is a problem, Quake3 doesn't use GPU shaders. A quote from a forum: "Quake3's "shaders" are not the same as whats being discussed here (GLSL). In Quake3, a "shader" is a strictly texture only language for compositing (sometimes procedural) textures and rendered utilizing multitexturing."
So, the effect will not work. Unless you suffice with 8-bit color output and just dump the depth-buffer onscreen.

I can write a small app in a framework of mine, to show how to visualize depth via a shader, and how to create a RGBA32f FBO, but it'll be useless as you're limited by the q3 engine. (and manually adding the necessary code requires knowledge in opengl).

The fragment-shader is, as I wrote:

void main(){
gl_FragColor = vec4(gl_FragCoord.z); // vec4(1.0/gl_FragCoord.w);

Share this post

Link to post
Share on other sites
fau    103
Well the code looks simpler than I thought I supposed that there will be some "surrounding" needed. :P
Anyway I still hope that someone might find a solution. Don't you think that my first thought - an opengl hook could make it?

Share this post

Link to post
Share on other sites
idinev    236
Hook - no. Adding 350 lines of code to the engine - yes.
Anyway, it's like trying to fit an electric motor to a 1980's car. You need knowledge of both techs.
Unless somehow this works for you:

Share this post

Link to post
Share on other sites
fau    103
There is no way I could write something into jk2 source since There isn't such thing accessible. I can only make changes using q3 virtual machine which won't let me do things like that. How do I use these open gl extension? It's written that it doesn't add any new procedures or functions does it mean that only thing I have to do is to load it? From Inside of program or outside of it?

edit: Ok I see now that I have to call glCopyPixels with DEPTH_STENCIL_TO_RGBA_NV as type. However as I wrote I can't add anything to game engine. Maybe I repeat myself but aren't we in a place where hooking an opengl function and adding this line instead of adding it to the game code will make it work?

[Edited by - fau on December 20, 2008 3:15:12 PM]

Share this post

Link to post
Share on other sites
V-man    813

There is a tool there called GLIntercept. It is basically a opengl32.dll that you drop where you exe is and it intercepts calls.
You can modify the source code to call glCopyPixels at the right moment. I guess that would be when SwapBuffers is called.

PS : GL_NV_copy_depth_to_color is a nvidia extension. I think it is not available in ATI.

Share this post

Link to post
Share on other sites
fau    103
It's exactly thing I've been looking for. Thank you very much, both of you are in the end credits :)
I'm trying to make it work right now by writing a plugin but it seems that putting it before or after wglSwapbuffers doesn't work. I've also tried before and after rendering call. The extension is supported by my card however I'm not sure if I have to load it somehow? When I try "IsGLExtensionSupported" I get "
GLDriver::IsExtensionSupported - Unable to determine if a extension is supported". Do you have any suggestions where exactly put glCopyPixels?

edit: Now I see that I was calling IsGLExtensionSupported in wrong place. It returns true. I got "some" effect one moment ago but it isn't exactly what I want. I'll have to look into it. Should I put glCopyPixels before or after wglSwapBuffers?

edit 2:I've found out that it works when I turn in-game menu on (It may stop rendering frames in the background - I'm not sure) and stops working when I turn it of.
Here is part of GLIntercept logfile with gl calls made by the game with menu turned off and here with turned on (working one).

[Edited by - fau on December 21, 2008 6:55:15 PM]

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  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
    • By Picpenguin
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now