Thread: Creating a 3D grid using direct3d 10

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Creating a 3D grid using direct3d 10

    I'm reading the book "Beginning DirectX 10 Game Programming" and as I go along I'm
    writing a simple game engine using what I learned.

    I am trying to make a simple 3D grid using that engine, but the program is not drawing what I expected (there's a below)

    Here is my code

    Code:
    //GameStart Function
    void GameStart()
    {
    	g_pGame->CreateRasterizer(D3D10_FILL_WIREFRAME, D3D10_CULL_NONE, true);
    
    	D3D10_INPUT_ELEMENT_DESC layout[] = {
    		{"Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
    		{"Color", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
    	};
    
    	int numElements = (sizeof(layout) / sizeof(layout[0]));
    	//Declare the vertices
    	VertexStruct vertices[NUM_VERTSX * NUM_VERTSY];
    
    	// Fill the vertices array with the terrain values
    	for(int z=0; z < NUM_VERTSY; ++z)
    	{
    		for(int x=0; x < NUM_VERTSX; ++x)
    		{
    			vertices[x + z * NUM_VERTSX].Pos.x = (float)x * CELL_WIDTH;
    			vertices[x + z * NUM_VERTSX].Pos.z = (float)z * CELL_HEIGHT;
    
    			// Allow the height of the cell to be randomly decided
    			vertices[x + z * NUM_VERTSX].Pos.y = 1.0f;
    
    			// Create the default color
    			vertices[x + z * NUM_VERTSX].Color = D3DXVECTOR4(1.0, 0.0f, 0.0f, 0.0f);
    		}
    	}
    
    	//Create the indices array, six vertices for each cell
    	DWORD indices[NUM_VERTSX * NUM_VERTSY * 6];
    
    	//the index counter
    	int curIndex = 0;
    
    	//Fill the indices array to create the triangles needed for the terrain
    	for (int z=0; z < NUM_ROWS; z++)	
    	{
    		for (int x=0; x < NUM_COLS; x++)
    		{
    			// The current vertex to build off of
    			int curVertex = x + (z * NUM_VERTSX);
    
    			// Create the indices for the first triangle
    			indices[curIndex]   = curVertex;
    			indices[curIndex+1] = curVertex + NUM_VERTSX;
    			indices[curIndex+2] = curVertex + 1;
    		
    			// Create the indices for the second triangle
    			indices[curIndex+3] = curVertex + 1;
    			indices[curIndex+4] = curVertex + NUM_VERTSX;
    			indices[curIndex+5] = curVertex + NUM_VERTSX + 1;
    
    			// increment curIndex by the number of vertices for the two triangles
    			curIndex += 6;
    		}
    	}
    
    	int i = sizeof(indices) / sizeof(DWORD);
    
    	ID3DBlob* pError;
    
    	D3DX10CreateEffectFromFile(TEXT("Effect.fx"), NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, g_pGame->GetDevice(),
    		NULL, NULL, &g_pEffect, &pError, NULL);
    
    	LPVOID l_pError = NULL;
    	if(pError)
    		l_pError = pError->GetBufferPointer();
    
    	g_pModel = new ModelObject(g_pGame->GetDevice(), numElements, layout, i, g_pEffect->GetTechniqueByName("Render"));
    	
    	//Set the indices to be put in the index buffer
    	D3D10_SUBRESOURCE_DATA initData;
    	initData.pSysMem = indices;
    
    	g_pModel->CreateIndexBuffer(initData);
    
    	initData.pSysMem = vertices;
    	i = sizeof(vertices) / sizeof(VertexStruct);
    	g_pModel->CreateVertexBuffer(initData, i);
    
    	g_pGame->CreateProjectionMatrix(g_pEffect);
    
    	g_pGame->SetCamera(D3DXVECTOR3(0.0f, 95.0f, -300.0f), D3DXVECTOR3(0.0f, 0.0f, 0.0f));
    }
    
    //CreateRasterizer function
    void BimanEngine::CreateRasterizer(D3D10_FILL_MODE FillMode, D3D10_CULL_MODE CullMode, bool Antialiased)
    {
    	//Fill out the rasterizer description
    	D3D10_RASTERIZER_DESC desc;
    	//How to draw objects
    	desc.FillMode = FillMode;
    	//What parts of the object to draw
    	desc.CullMode = CullMode;
    	desc.FrontCounterClockwise = true;
    	desc.DepthBias = false;
    	desc.DepthBiasClamp = 0;
    	desc.SlopeScaledDepthBias = 0;
    	desc.DepthClipEnable = false;
    	desc.ScissorEnable = false;
    	desc.MultisampleEnable = false;
    	desc.AntialiasedLineEnable = Antialiased;
    
    	//Create the rasterizer state
    	ID3D10RasterizerState* pRasterizer;
    	m_pD3DDevice->CreateRasterizerState(&desc, &pRasterizer);
    
    	//Set rasterizer state
    	m_pD3DDevice->RSSetState(pRasterizer);
    }
    
    //SetCamera function
    void BimanEngine::SetCamera(D3DXVECTOR3 Eye, D3DXVECTOR3 At)
    {
    	D3DXMatrixLookAtLH(&m_mxView, &Eye, &At, new D3DXVECTOR3(0.0f, 1.0f, 0.0f));
    }
    Thank you

    Creating a 3D grid using direct3d 10-enginetest-png

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    			// Allow the height of the cell to be randomly decided
    			vertices[x + z * NUM_VERTSX].Pos.y = 1.0f;
    //Comment unrelated.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    That was when the code was
    Code:
    vertices[x + z * NUM_VERTSX].Pos.y = rand() % CELL_HEIGHT;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a grid
    By karimoftos in forum C Programming
    Replies: 6
    Last Post: 04-19-2011, 11:32 AM
  2. Grid
    By mortalc in forum C Programming
    Replies: 7
    Last Post: 03-12-2011, 06:23 AM
  3. Why use Direct3D?
    By m3rk in forum Tech Board
    Replies: 42
    Last Post: 05-22-2009, 09:08 AM
  4. Need some help with Direct3D help
    By darknite135 in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2008, 07:32 PM
  5. Creating a 2D GUI using Direct3D
    By codec in forum Game Programming
    Replies: 8
    Last Post: 09-23-2004, 11:57 AM