Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:38:03+00:00 2026-06-14T11:38:03+00:00

I am implementing a Z-buffer to determine which pixels should be drawn in a

  • 0

I am implementing a Z-buffer to determine which pixels should be drawn in a simple scene filled with triangles. I have structural representations of a triangle, a vertex, a vector (the mathematical (x, y, z) kind, of course), as well as a function that draws an individual pixel to the screen. Here are the structures I have:

struct vertex{
  float x, y, z; 
  ... //other members for lighting, etc. that will be used later and are not relevant here 
}; 

struct myVector{
  float x, y, z; 
}; 

struct triangle{
  ... //other stuff 
  vertex v[3]; 
}; 

Unfortunately, as I scan convert my triangles to the screen, which relies on calculating depths to determine what is visible and gets to be drawn, I am getting incorrect/unrealistic Z values (e.g., the depth at a point in the triangle is out of bounds of the depths of all 3 of its vertices)! I have been looking through my code over and over and cannot figure out whether my math is off or I have a careless mistake somewhere, so I will try to present exactly what I am trying to do in the hopes that someone else can see something that I don’t. (And I have looked carefully at making sure that floating point values remain floating point values, that I am passing in arguments correctly, etc., so this is really baffling!)

Overall, my scan conversion algorithm fills pixels across a scan line like this (pseudocode):

for all triangles{  
  ... //Do edge-related sorting stuff, etc...get ready to fill pixels 

  float zInit;      //the very first z-value, with a longer calculation 
  float zPrev;      //the "zk" needed when interpolating "zk+1" across a scan line 

  for(xPos = currentX at left side edge; xPos != currentX at right side edge; currentX++){

    *if this is first pixel acorss scan line, calculate zInit and draw pixel/store z if depth is less   
     than current zBuffer value at this point. Then set zPrev = zInit. 

    *otherwise, interpolate zNext using zPrev. Draw pixel/store z if depth < current zBuffer value at 
     this point. Then set zPrev = zNext. 

  }
... //other scan conversion stuff...update x values, etc. 
}

To get the value of zInit for each scan line, I consider the plane equation Ax + By + Cz + D = 0 and rearrange it to get z = -1*(Ax + By + D)/C, where x and y are plugged in as the current x value across a scan line and the current scan line value itself, respectively.

For subsequent z values across a scan line, I interpolate as zk+1 = zk – A/C, where A and C come from the plane equation.

To get the A, B and C for these z calculations, I need the normal vector of the plane defined by the 3 vertices (the array vertex v[3]) of the current triangle. To get this normal (which I named planeNormal in the code), I defined a cross product function:

 myVector cross(float x1, float y1, float z1, float x2, float y2, float z2)
 {
   float crX = (y1*z2) - (z1*y2);
   float crY = (z1*x2) - (x1*z2);
   float crZ = (x1*y2) - (y1*x2);

   myVector res;
   res.x = crX;
   res.y = crY;
   res.z = crZ;
   return res;   
 }

To get the D value for the plane equation/my z calculations, I use the plane equation A(x-x1) + B(y-y1) + C(z-z1) = 0, where (x1, y1, z1) is just a reference point in the plane. I just chose the triangle vertex v[0] for the reference point and rearranged:

Ax + By + Cz = Ax1 + By1 + Cz1
Thus, D = Ax1 + By1 + Cz1

So, finally, to get the A, B, C, and D for the z calculations, I did this for each triangle, where trianglelist[nt] is the triangle at current index nt in the overall triangle array for the scene:

float pA = planeNormal.x;
float pB = planeNormal.y;
float pC = planeNormal.z;
float pD = (pA*trianglelist[nt].v[0].x)+(pB*trianglelist[nt].v[0].y)+(pC*trianglelist[nt].v[0].z);

From here, within the scan conversion algorithm I described, I calculated the zs:

zInit = -1*((pA*cx)+(pB*scanLine)+(pD))/(pC);  //cx is current x value; scanLine is current y value
...
...
float zNext = zPrev - (pA/pC);

Alas, after all that careful work, something is off! In some triangles, the depth values come out realistic (except for the sign). With triangle given by the vertices (200, 10, 75), (75, 200, 75) and (15, 60, 75), all depths come out as -75. The same happened for other triangles with all vertices at the same depth. But with the vertices (390, 300, 105), (170, 360, 80), (190, 240, 25), all of the z values are over 300! The very first one comes out as 310.5, and the rest just get bigger, with a max around 365. This should not happen when the deepest vertex is at z = 105!!! So, after all of the rambling, can anyone see what might have caused this? I wouldn’t be surprised if it’s a sign-related thing, but where (after all, the absolute values are right in the constant depth cases)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:38:05+00:00Added an answer on June 14, 2026 at 11:38 am

    The correct equations are:

    n = cross (v[2] - v[0], v[1] - v[0]);
    D = - dot (n, v[0]);
    

    Note the minus sign.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I was recently tasked on implementing a buffer which will be used as a
Is there any standard way of implementing some sort of a write-through buffer for
I have a form, which sets these styles in constructor: this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true);
I'm implementing some custom serialization (to byte array), and have run into a problem
I was implementing a circular buffer to store fixed-sized data structures like a queue.
I need a python script implementing a circular buffer for rows in a text
I am implementing both server and client side of a simple file download program.
I'm implementing a objective C wrapper for Box2d (which is written in c++). The
I have been working on implementing the Ancestry gem in my rails app, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.