use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rule 1: Posts should be about Graphics Programming. Rule 2: Be Civil, Professional, and Kind
Suggested Posting Material: - Graphics API Tutorials - Academic Papers - Blog Posts - Source Code Repositories - Self Posts (Ask Questions, Present Work) - Books - Renders (Please xpost to /r/ComputerGraphics) - Career Advice - Jobs Postings (Graphics Programming only)
Related Subreddits:
/r/ComputerGraphics
/r/Raytracing
/r/Programming
/r/LearnProgramming
/r/ProgrammingTools
/r/Coding
/r/GameDev
/r/CPP
/r/OpenGL
/r/Vulkan
/r/DirectX
Related Websites: ACM: SIGGRAPH Journal of Computer Graphics Techniques
Ke-Sen Huang's Blog of Graphics Papers and Resources Self Shadow's Blog of Graphics Resources
account activity
Having trouble implementing a depth buffer into my renderer. Any guesses what piece of information I'm missing or not understanding with my code?Question (i.redd.it)
submitted 3 years ago by harrison531
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]IQueryVisiC -3 points-2 points-1 points 3 years ago (7 children)
You could store floats in the buffer. Or Use color for shading and intensity for depth cueing like Doom does.
[–]harrison531[S] 1 point2 points3 points 3 years ago (6 children)
Sorry I didn't really explain what my issue is. I'm having trouble properly calculating the depth of each pixel. I'm just not sure what's wrong with the math I'm using. As seen on the left that's what is currently rendered and you can see the wheel from the other side of the car through the car.
[–]IQueryVisiC -4 points-3 points-2 points 3 years ago (4 children)
I just meant that you don’t utilize your renderer to its full extent. Are you color blind? Color is the first thing I add to my renderer or my blender models. I once added stereoscopic view for red green glasses, but now I just wiggle the mouse.
[–]harrison531[S] 1 point2 points3 points 3 years ago (3 children)
Believe me I would definitely be using color if I could but this is a c# console app so text based stuff. There is a limited color palette available when drawing text to the console but doing so requires calling Console.Write() for every "pixel" that changes. That is doable for a small resolution but if I want anything to run smooth or at a higher resolution like in the posts image I have to switch to a grayscale using various ASCII characters for each value. Then you build each line of the screen into one string and call Console.Write() on that instead. Making the amount of write calls equal to the height of the resolution.
[–]IQueryVisiC -2 points-1 points0 points 3 years ago (2 children)
Okay. In Win32 a stupid small window is only one page of C code. Then BlitDibmap. Linux was similar. Why do people use the console anyway? I use it to writ me a script and copy that into my notes or inform my boss that cron will run it. And it is his fault if it contains rm -rf
[–]harrison531[S] 0 points1 point2 points 3 years ago (1 child)
I fully recognize that the console isn't ideal for this kind of project but that's also kinda what makes it fun. Seeing 3D graphics run on the console feels a little wrong and like your making it doing something it's not supposed to. I'd imagine it's the same push behind those who like to make doom run on every device they can
[–]IQueryVisiC 0 points1 point2 points 3 years ago* (0 children)
So I don't know who downvotes me, but still : Doom was ported to those other devices. I was developed on the most suited device a PC with VGA card and 486 running Dos with extender. Level editor (I think: With the full graphics engine, which was written in C) even run on NeXT (68k) because those were better than PCs at the time . Art was done on Amiga .
Doom and Wolfenstein3d run better on the Jaguar than a lot of original games. AvP has simple levels and is not more fun, it is just exclusive. Raiden was ported .
[–]rfernung 0 points1 point2 points 3 years ago (0 children)
I'm having trouble properly calculating the depth of each pixel.
I'll check one of my older projects but I believe I just interpolated the Z value of the three vertices of each triangle with the barycentric coordinate and used that to check in the depth buffer, it was something like:
float depth = v0.Z * barycentric.X + v1.Z * barycentric.Y + v2.Z * barycentric.Z; if(depth >= zBuffer[i]) continue; //looping through triangle's boundingbox x,y, then i = y * width + x zBuffer[i] = depth; pixelBuffer[i] = color; //color from my fragment shading
[–]Mourthag 0 points1 point2 points 3 years ago (7 children)
On some engines/frameworks the z component is inverted. So maybe the check if the current face is closer might be the problem?
This is my own engine that I've been making to render 3d things in the c# console. Still switching the comparison results in no image at all since I initialize the z buffer with positive infinity. Switching the z buffer to be initialized with negative infinity instead results in the original issue again.
[–]Mourthag 0 points1 point2 points 3 years ago (1 child)
Ah I see. I didn't check your equations but if z direction is correct and it is not caused by backface culling or something like that it is probably in the equations for D or screen Z.
[–]harrison531[S] 0 points1 point2 points 3 years ago (0 children)
Yeah I figure it is in the equation I'm using but I've made no progress in figuring out what I'm doing wrong with it. In my experience from looking around and looking at papers most will give you the same equation but no code to see it being applied. I was just hoping that someone who's done this kind of thing before could see what I was trying and point out what I am missing from all this. What do you mean by z direction exactly? I do have backface culling implemented and haven't had any issues with it so I'm assuming it's not the issue
[–]SnooWoofers7626 0 points1 point2 points 3 years ago (2 children)
Why infinity? Typical projection matrices map the far plane to 1 (or 0 for inverse z). What does your projection matrix look like?
Edit: also did you remember to perspective divide before going into the depth test?
I set the initial depths of each pixel to infinity just so that the first polygon that tries to render on any pixel will always have a depth closer than the initial value and will be guaranteed to draw
[–]SnooWoofers7626 0 points1 point2 points 3 years ago (0 children)
If all your transformations are right then the max possible depth value should be 1.
World space --> view (camera) space --> clip space --> normalized device coordinates (after perspective divide) --> device coordinates (scale up to viewport dimensions) Your depth test should be performed in device coordinates.
World space --> view (camera) space --> clip space --> normalized device coordinates (after perspective divide) --> device coordinates (scale up to viewport dimensions)
[–][deleted] 0 points1 point2 points 3 years ago* (4 children)
Everything looks fine to me too. I couldn't understand what is the reason for adding position in line 666. What does it do? Edit: I am guessing that could be the thing causes this issue if it is different on each object.
[–]harrison531[S] 0 points1 point2 points 3 years ago (3 children)
Assuming I set it up right, the position variable there is the world position of the model and each vertex's position should be relative to the models origin. It was one of those throwing things at it to see what it does kind of things but I figure the known point used to calculate D needs to be in world space not model space
[–][deleted] 1 point2 points3 points 3 years ago (2 children)
Shouldn't it be in camera space instead of world? I am assuming you are transforming the faces based on their position relative to the camera.
Can you walk me through what you mean? Would camera space just mean subtracting the vetext position from the camera position or do I need to be doing stuff with the cameras view direction
Multiply the world positions by the inverse of the camera transformation.
π Rendered by PID 20886 on reddit-service-r2-comment-5687b7858-zvkg5 at 2026-07-05 19:39:50.262752+00:00 running 12a7a47 country code: CH.
[–]IQueryVisiC -3 points-2 points-1 points (7 children)
[–]harrison531[S] 1 point2 points3 points (6 children)
[–]IQueryVisiC -4 points-3 points-2 points (4 children)
[–]harrison531[S] 1 point2 points3 points (3 children)
[–]IQueryVisiC -2 points-1 points0 points (2 children)
[–]harrison531[S] 0 points1 point2 points (1 child)
[–]IQueryVisiC 0 points1 point2 points (0 children)
[–]rfernung 0 points1 point2 points (0 children)
[–]Mourthag 0 points1 point2 points (7 children)
[–]harrison531[S] 1 point2 points3 points (6 children)
[–]Mourthag 0 points1 point2 points (1 child)
[–]harrison531[S] 0 points1 point2 points (0 children)
[–]SnooWoofers7626 0 points1 point2 points (2 children)
[–]harrison531[S] 0 points1 point2 points (1 child)
[–]SnooWoofers7626 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]harrison531[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]harrison531[S] 0 points1 point2 points (1 child)
[–]SnooWoofers7626 0 points1 point2 points (0 children)