• Advertisement
Sign in to follow this  

(Long) Help (UMDX9 C++) Vertex Buffer Interference?

This topic is 4367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What's going on in my program? The idea was to render two different large flat grids of polygons to the screen in wireframe, however, I seem to have somehow messed even that up. The grid only produces "messed-up" results as shown in the below picture when I enable the second (upper-most) layer. The odd thing is, the second layer is somehow messing up the original(bottom-most) layer even when I'm not rendering the second layer. Here's a picture of what the grid is supposed to look like (taken from an alternate corner of the grids): Link Here's what the grid looks like in wireframe at a bad portion of the grid - note that the wireframe layers are color-differentiated and how the top layer is drawn in white wireframe, while the bottom layer is drawn in blue-gray: Link And here's what the grid looks like in filled-polygon mode using a cloud texture - note the differentiation of layers, and how the layer with textures tiled 4x more frequently is the bottom layer: Link Code is shown below - can someone help me here? Object declarations:

// Direct3D objects.
LPDIRECT3D9 Direct3D_Object = NULL;
LPDIRECT3DDEVICE9 D3D_Device = NULL;
LPDIRECT3DVERTEXBUFFER9 Object_Buffer[64][64];
LPDIRECT3DVERTEXBUFFER9 Terrain_Buffer[64 / 4][64 / 4];
LPDIRECT3DTEXTURE9 Texture_Array[64*64];
LPDIRECT3DTEXTURE9 Texture_One = NULL;
LPD3DXFONT Font = NULL; // DirectX font object.
RECT FontPosition = {0, 0, 0, 0}; // RECT used to position the font
bool wireframe = false;
bool pointframe = false;






The vertex buffers' creation (custom function source shown below). Note that the custom functions are used to fill the vertex buffer arrays with vertices ( to create the drawn grid ):

if (created_verts == false)
	{
		float x = 0.0;
		float z = 0.0;

		for (z = 0.0f; z < 64.0; z++)
		{
			for (x = 0.0f; x < 64.0; x++)
			{
				// Syntax is:
				// Build_Huge_Vertices(float left_side_coordinate_x, float right_side_coordinate_x,
				//    float y_coordinate, float top_side_coordinate_z, float bottom_side_coordinate_z)
				Build_Huge_Vertices_Sky((x), (x + 1), (-1), (z), (z + 1), int(x), int(z));
			}
		}

		x = 0.0f;
		z = 0.0f;
		
		for (z = 0.0f; z < 64.0; z+=4)
		{
			for (x = 0.0f; x < 64.0; x+=4)
			{
				Build_Huge_Vertices_Terrain((x), (x + 4), (-0.75), (z), (z + 4), int(x), int(z));
			}
		}
	}

	// flag this variable so we only have to create vertices once per program execution - not once per frame - that would get sticky
	created_verts = true;






Source of Build_Huge_Vertices_Sky (sky meaning the bottom-most layer):

void Build_Huge_Vertices_Sky(float x, float x2, float y, float z, float z2, int a, int b)
// the purpose of this function was to create a single quad made of two polygons in this fashion:
/*     z
     -----
	 |  /|
	x| / |x2
	 |/  |
	 -----
      z2
	
	
	
	x,z  x2,z
	 -----
	 |  /|
	 | / |
	 |/  |
	 -----
    x,z2  x2,z2

*/
{
	// makes a quad with upperleft coordinate x,z, and bottomright x2,z2
	Vertex DataVerts[4] =
   {
		{x2, y, z2, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f},
      {x, y, z2, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f},
	   {x, y, z, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},
	   {x2, y, z, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f}
	};

   // Create the vertex buffer that will hold the two polygons
	D3D_Device->CreateVertexBuffer(sizeof(DataVerts), 0, D3DFVF_D3DVertex, D3DPOOL_DEFAULT, &Object_Buffer[a], NULL);
	
   // Lock the buffer we can write to it.
Object_Buffer[a]->Lock(0, sizeof(DataVerts), (void**)&Vertices, 0);

   // Here we copy the triangle's data into the vertex buffer.
   memcpy(Vertices, DataVerts, sizeof(DataVerts));

   // Unlock when your done coping data into the buffer.
   Object_Buffer[a]->Unlock();
}






The source of the other function (which probably should have been created in a more object-oriented manner) is nearly identical to the first, except it uses different variables and operates on different vertex buffers:

void Build_Huge_Vertices_Terrain(float x, float x2, float y, float z, float z2, int a, int b)
{	// makes a quad with upperleft coordinate x,z, and bottomright x2,z2
	Vertex DataVertsT[4] =
   {
		{x2, y, z2, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f},
      {x, y, z2, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f},
	   {x, y, z, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},
	   {x2, y, z, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f}
	};
   // Create the vertex buffer that will hold the two polygons
	D3D_Device->CreateVertexBuffer(sizeof(DataVertsT), 0, D3DFVF_D3DVertex, D3DPOOL_DEFAULT, &Terrain_Buffer[a], NULL);
   // Lock the buffer we can write to it.
Terrain_Buffer[a]->Lock(0, sizeof(DataVertsT), (void**)&VerticesT, 0);
   // Here we copy the triangle's data into the vertex buffer.
   memcpy(VerticesT, DataVertsT, sizeof(DataVertsT));
   // Unlock when your done coping data into the buffer.
   Terrain_Buffer[a]->Unlock();
}






Finally, the renderloop (copy and pasted here to show that I'm not crazy and I'm not messing up the grids here) - Sorry that it's long:

void RenderScene()
{
	if (wireframe)
         D3D_Device->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
	else if (pointframe)
		D3D_Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT);
     else
         D3D_Device->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
   
	// Clear the screen from whatever was left on the screen from last frame
   D3D_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                     D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
   // Tell Direct 3D we are now going to start drawing.
   D3D_Device->BeginScene();

      // Set the view matrix (camera position).
      SetCamera();

	   // This will bind the vertex data in the buffer to the Direct3D device.
	  D3D_Device->SetFVF(D3DFVF_D3DVertex);
	  
	  // Uses a clean TextureState each frame
	  D3D_Device->SetTexture(0,NULL);

	  	  // The main render loop
	  // texnum holds the place of the texture that is currently being used, then it
	  // increments through all 4096 textures in the texturebuffer or Texture_Array[]
int texnum = 0;

// RENDER SKYLAYER

	  for (int x = 0; x < 64; x++) // 64 textured quads in the X direction
	  {
		 for (int z = 0; z < 64; z++) // and 64 more in the Z direction
		  {
			if ((Texture_Array[/*texnum*/0]) != NULL) // If a texture WAS found, then use the texture at Texture_Array[texnum] for the current
				// tile's texture
			{
			  D3D_Device->SetTexture(0,(Texture_Array[/*texnum*/0]));
			D3D_Device->SetStreamSource(0, Object_Buffer[x][z], 0, sizeof(Vertex));
			D3D_Device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
			}
			else // if the dynamic texture loader could not find a file (or
				// could not load one for any reason), the texture is left blank (white texture) or NULL, and 
				// the program goes on its merry way
			{
				//D3D_Device->SetTexture(0,Texture_Array[texnum]);
				D3D_Device->SetTexture(0,/*NULL*/Texture_Array[0]); // No Texture
				if (wireframe)
				{
					D3D_Device->SetStreamSource(0,Object_Buffer[x][z],0,sizeof(Vertex));
					D3D_Device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
				}
				
			}
			texnum++;
		  }
	  }
		  // RENDER TERRAINLAYER
texnum = 1;

	for (int x = 0; x < 64; x++) // 64 textured quads in the X direction
	  {
		 for (int z = 0; z < 64; z++) // and 64 more in the Z direction
		  {
			if ((Texture_Array[texnum]) != NULL) // If a texture WAS found, then use the texture at Texture_Array[texnum] for the current
				// tile's texture
			{
			  D3D_Device->SetTexture(0,(Texture_Array[texnum]));
			D3D_Device->SetStreamSource(0, Terrain_Buffer[x][z], 0, sizeof(Vertex));
			D3D_Device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
			}
			else // if the dynamic texture loader could not find a file (or
				// could not load one for any reason), the texture is left blank (white texture) or NULL, and 
				// the program goes on its merry way
			{
				//D3D_Device->SetTexture(0,Texture_Array[texnum]);
				D3D_Device->SetTexture(0,NULL); // No Texture
				if (wireframe)
				{
					D3D_Device->SetStreamSource(0,Terrain_Buffer[x][z],0,sizeof(Vertex));
					D3D_Device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
				}
				
			}
			//texnum++;
		  }
	  }

	FontPosition.top = 25;
      Font->DrawText(NULL, "C08 aka NSRM \n \n By Tom!", -1, &FontPosition, DT_CENTER, D3DCOLOR_XRGB(255,255,255)); // Draws the Program Title, and my name in the top middle of the screen in black
	  // Syntax is DrawText(Sprite sprite, String str, Integer index, Rect positionRectangle, DWORD alignment, Color col);

	  ControlsPosition.top = 0;
	  ControlsPosition.left = 0;
	  ControlsPosition.right = 640;
	  Controls->DrawText(NULL, "Contols for this demo: \n Up = Forwards, Down = Backwards \n Left = Strafe Left, Right = Strafe Right \n Spacebar = Up, Control = Down \n Mouse = Mouse Look \n W = Wireframe Toggle (OMG Extremely Sensitive!)", -1, &FontPosition, DT_LEFT, D3DCOLOR_XRGB(255,0,0)); // Displays the controls for the demo in the topleft in black
	  // Syntax is DrawText(Sprite sprite, String str, Integer index, Rect positionRectangle, DWORD alignment, Color col);
	//GetFPS();
	  //sDebugText = "   TESTING 1!"; - To test the debugging text area
	  // Draws the DebugData to the screen in its own little box in the bottom left
	  DebugText->DrawTextA(NULL, (sDebugText.c_str()), -1, &DebugTextPosition, DT_LEFT, D3DCOLOR_XRGB(255,255,0));


   // We are done drawing.
   D3D_Device->EndScene();
    
   // Swap the old frame with the new one.
   D3D_Device->Present(NULL, NULL, NULL, NULL);
   
   // Next frame!
}

Edit : Fixed many gramatical mistakes and made the hyperlinks at the top work again. [Edited by - DragonGeo2 on May 1, 2006 5:55:37 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement