Advertisement

Z-Buffering in D3D8 - what the heck?

Started by February 24, 2001 03:19 PM
0 comments, last by Wierdo 23 years, 11 months ago
Hello, people. I have a problem with Z-Buffering in Direct3D 8 (duh). I have two polygons, one red and the other green. I have a rotation matrix that rotates the polygons around the x-axis. However, when the polygons rotate, the red polygon always appears! I''ve tried a lot of things, including switching to w-buffering and setting the testing mode of the z-buffer, and I know that the z-buffer is created successfully because when switching the mode of the z-buffer to D3DCMP_GREATER the green triangle always appears instead. If it helps you out, I have disabled culling. Here is my triangle init code:

	COLORED_VERTEX temp_tri[6];
	void *data;

	// set the triangles up
	temp_tri[0].x=0.5f;
	temp_tri[0].y=0.0f;
	temp_tri[0].z=-.433f;
	temp_tri[0].color=D3DCOLOR_XRGB(255,255,255);

	temp_tri[1].x=-0.5f;
	temp_tri[1].y=0.0f;
	temp_tri[1].z=-.433f;
	temp_tri[1].color=D3DCOLOR_XRGB(0,0,255);

	temp_tri[2].x=0.0f;
	temp_tri[2].y=0.0f;
	temp_tri[2].z=.433f;
	temp_tri[2].color=D3DCOLOR_XRGB(0,255,0);

	temp_tri[3].x=-0.5f;
	temp_tri[3].y=0.0f;
	temp_tri[3].z=-.433f;
	temp_tri[3].color=D3DCOLOR_XRGB(0,0,255);

	temp_tri[4].x=0.0f;
	temp_tri[4].y=0.0f;
	temp_tri[4].z=.433f;
	temp_tri[4].color=D3DCOLOR_XRGB(0,255,0);

	temp_tri[5].x=0.0f;
	temp_tri[5].y=-0.433f;
	temp_tri[5].z=0.0f;
	temp_tri[5].color=D3DCOLOR_XRGB(255,0,0);


	// now create the vertex buffer
	if(FAILED(d3d_device->CreateVertexBuffer(6*sizeof(COLORED_VERTEX),0,COLORED_VERTICES,D3DPOOL_DEFAULT,&temp_vertex_buffer)))
		return(0);

	// now fill the vertex buffer
	if(FAILED(temp_vertex_buffer->Lock(0,0,(BYTE**)&data,D3DLOCK_DISCARD)))
		return(0);
	memcpy(data,temp_tri,6*sizeof(COLORED_VERTEX));
	if(FAILED(temp_vertex_buffer->Unlock()))
		return(0);
 
Here is my rotation code:

	// zero out the world transform
	D3DMATRIX translate, rotate;
	SetToIdentityMatrix(&world_transform);
	SetToIdentityMatrix(&translate);
	SetToIdentityMatrix(&rotate);

	// set the rotate axis thing
	rotate._22=rotate._33=(float)cos(DEGREE_TO_RADIAN(x));
	rotate._23=(float)sin(DEGREE_TO_RADIAN(x));
	rotate._32=-rotate._23;

	// set the transform
	translate._43=2.0f;

	// now set the world transform
	MultiplyMatrices(&rotate,&translate,&world_transform);
	d3d_device->SetTransform(D3DTS_WORLD,&world_transform);
 
And finally, here is the rendering code:

	// clear the surfaces
	d3d_device->Clear(0,NULL,D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);

	// begin the scene!
	d3d_device->BeginScene();

	d3d_device->SetRenderState(D3DRS_LIGHTING,FALSE);
	d3d_device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
	d3d_device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	d3d_device->SetStreamSource(0,temp_vertex_buffer,sizeof(COLORED_VERTEX));
	d3d_device->SetVertexShader(COLORED_VERTICES);
	d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);

	// end the scene
	d3d_device->EndScene();

	// now flip the primary surface and the backbuffer
	d3d_device->Present(NULL,NULL,NULL,NULL);
 
Please help me as soon as possible! Thank you in advance.
It's like my grandfather used to say, "Never quote your elders."
Come on, there must have been SOMEONE to encounter this error!
It's like my grandfather used to say, "Never quote your elders."

This topic is closed to new replies.

Advertisement