Z Buffering problems :-(

Sep 8, 2008 at 1:16pm
Hi everyone, wondering if you would please be able to help me on a few things,

I've got a Zbuffering assignment which I'm almost there on, but a few things are giving me problems/errors and I'd hugely appreciate if anyone could find the solution for me. I'll put each question, and then the relevant code I currently have for that section,


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. "Initialise the z buffer before every frame. You should initialise all the values of zbuf to zero at the start of DrawImage so that the z buffer is initialised at the start of every frame."

I currently have the zbuffer array declared as double zbuf[WINDOWWIDTH][WINDOWHEIGHT]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void DrawImage( )
{

	zbuf == 0;

	POLYGON polyTempP, polyTempQ, polyTempQT;	// Temporary polygons for copying transformed, projected / clipped
									// versions of scene polys before drawing them. 
	int iCurrentPoly;							// Current polygon in process
	TRANSFORM object_transformation=BuildTrans(m_dThetaX,m_dThetaY,m_dThetaZ,m_vDisp);
	VECTOR view_in_object_coordinates=MOnV(InverseRotationOnly(object_transformation),m_vDisp);
	if ( m_iNumOfPolys <= 0 )
		DrawSquare(Default_Colour );		// This draws the square you see, replace with Trapezium, and later Polygon
	else
		m_iOurFrameCount++;	// Increment frame counter if we have a polygon to draw 



------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. "add a z parameter to drawtrapezium. This will need to be a double. The formal parameter ( call it current_z) needs to be added to the function definition and to the prototype. Where the function is called in drawpolygon use the actual parameter

poly->vert[0].z

i.e. the z coordinate of polygon vertex zero. This is a 1/z value, calculated in project and copied through my versions of the clipping functions – beware if you are using your own clipping function."

And also included in the same section,

3. "add the test within drawtrapezium. Just before the call to Plotpix put in an if statement comparing current_z to the value of zbuf at the point where you will be plotting. If the current_z value is larger you want to proceed with the Plotpix AND update the zbuf value to be the same as the “winning” value of current_z."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void DrawTrapezium(COLOUR c,int current_y,int yfin,INCREMENTEE x)
{
	// current_y is the y coord of the trapezium top and is used as a 
	//running total through the routine
  // x_left and x_right are the x coordinates of the top of the trapezium
	//they are passed as pointers so that the values can be 
	//returned back to the polygon routine at the end to form the top of the next trapezium.
	//(A reference could have been used here 
	//but I am trying to keep this program compatible with C rather than C++)

	//Loop around line by line
  int current_x;

  

  
  if (current_y>yfin) return;
       while(1)
	{
   for (current_x= *x.left;current_x<= *x.right;current_x++)
   {
       Plotpix(current_x,current_y,c.r,c.g,c.b);// This line plots a pixel
   }
    if (current_y>=yfin) break;
    current_y++;

    *x.left+=x.slopeleft; *x.right+=x.sloperight;//increment endpoints  
    
	}

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks a lot in advance to anyone who can assist me with this :-)
Sep 8, 2008 at 1:33pm
Not sure if this helps at all but right now you have zbuff == 0; for problem one and that won't do anything. == is the comparison whereas = sets it to 0. I don't think you can just say zbuff = 0; though since zbuff is a 2 dimensional array. You would have to iterate through and set each element to 0.
Sep 8, 2008 at 1:54pm
Still having problems, although I missed the == issue so thanks for that, but having a single = causes even more errors for me :-(
Sep 8, 2008 at 2:00pm
Like jpeg said, zbuff is a pointer to a matrix. You can't just set it to zero. If you do, the array becomes stray.
You need to iterate through all elements to do it.
Sep 8, 2008 at 2:03pm
I see. would you mind giving an example of this please?
Thanks
Sep 8, 2008 at 2:14pm
1. When dealing with arrays you must handle them with array-handling functions. A simple fill loop should help (I don't know whether you are using C or C++):
1
2
3
4
//C++
#include <algorithm>
...
std::fill_n( zbuf, WINDOWWIDTH *WINDOWHEIGHT, 0.0 );
1
2
// C
for (double* d = zbuf, unsigned n = 0; n < WINDOWWIDTH *WINDOWHEIGHT; n++, d++) *d = 0.0;
2. Just add a new formal parameter named "current_z". I don't know what he means about the 1/z stuff. If you don't either you'll have to ask for some help understanding the project.
3. The whole point of a z-buffer is to avoid drawing pixels that are "farther away" than pixels that have already been drawn. So, before plotting a pixel (line 22), just check to see if zbuf[current_x][current_y] is "farther away" before actually drawing the pixel. If it is "closer" then don't draw the pixel. If you do draw it, don't forget to update the zbuf with the new pixel's z.

Hope this helps.
Last edited on Sep 8, 2008 at 2:15pm
Sep 8, 2008 at 2:48pm
Thanks very much for this, I'll give it a go!
Topic archived. No new replies allowed.