Why won't my grid appear

Started by
2 comments, last by ankhd 11 years, 4 months ago
Hey there,

recently tried to implement a 100x100 grid to my 3D Scene, although I believe I've done everything correctly the grid isn't appearing?

Land.h

//--------------------------------------------------\\
// Land.h \\
// Encapsulates the model data and rendering \\
// functionality to draw a 100x100 grid \\
//--------------------------------------------------\\
#ifndef _LAND_H
#define _LAND_H
#include <D3D11.h>
#include <D3DX10math.h>
#define LAND_WIDTH 100;
#define LAND_HEIGHT 100;
class Land
{
private:
struct VertexType //Struct to hold variables for the grid
{
D3DXVECTOR3 position;
D3DXVECTOR4 color;
};
public:
Land();
~Land();
bool Init(ID3D11Device*);
void Render(ID3D11DeviceContext*);
void Shutdown();
int GetIndexCount();
private:
bool InitBuffers(ID3D11Device*);
void RenderBuffers(ID3D11DeviceContext*);
void ShutdownBuffers();
int g_landWidth;
int g_landHeight;
int g_vertexCount;
int g_indexCount;
ID3D11Buffer *g_vertexBuffer, *g_indexBuffer;
};
#endif


Land.cpp

//--------------------------------------------------\\
// Land.cpp \\
// Encapsulates the model data and rendering \\
// functionality to draw a 100x100 grid \\
//--------------------------------------------------\\
#include "Land.h"
/* Constructor & Deconstructor */
Land::Land()
{
g_vertexBuffer = 0;
g_indexBuffer = 0;
}
Land::~Land(){}
/* Initialize the land class, set the width and height of the land and initialize the buffers */
bool Land::Init(ID3D11Device* device)
{
bool r;
g_landWidth = LAND_WIDTH;
g_landHeight = LAND_HEIGHT;
r = InitBuffers(device);
if(!r)
{
return false;
}
return true;
}
/* Initialize the vertex and index buffers used to hold the grid data, forming 4 lines */
bool Land::InitBuffers(ID3D11Device* device)
{
VertexType* vertices;
unsigned long* indices;
int index, i, j;
float posX, posZ;
D3D11_BUFFER_DESC vertexBD, indexBD;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT r;
//Calculate vertices in grid
g_vertexCount = (g_landWidth - 1) * (g_landHeight - 1) * 8;
//Set index count
g_indexCount = g_vertexCount;
//Create vertex array
vertices = new VertexType[g_vertexCount];
if(!vertices)
{return false;}
//Create the index array
indices = new unsigned long[g_indexCount];
if(!indices)
{return false;}
//Load index into the vertex array
index = 0;
//Load the vertex and index arrays with the grid data
for(j = 0; j<(g_landHeight-1); j++)
{
for(i = 0; i < (g_landWidth-1); i++)
{
// LINE 1
// Upper left.
posX = (float)i;
posZ = (float)(j+1);
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// Upper right.
posX = (float)(i+1);
posZ = (float)(j+1);
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// LINE 2
// Upper right.
posX = (float)(i+1);
posZ = (float)(j+1);
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// Bottom right.
posX = (float)(i+1);
posZ = (float)j;
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// LINE 3
// Bottom right.
posX = (float)(i+1);
posZ = (float)j;
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// Bottom left.
posX = (float)i;
posZ = (float)j;
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// LINE 4
// Bottom left.
posX = (float)i;
posZ = (float)j;
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
// Upper left.
posX = (float)i;
posZ = (float)(j+1);
vertices[index].position = D3DXVECTOR3(posX, 0.0f, posZ);
vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
indices[index] = index;
index++;
}
}
//Setup the description of the vertex buffer
vertexBD.Usage = D3D11_USAGE_DEFAULT;
vertexBD.ByteWidth = sizeof(VertexType) * g_vertexCount;
vertexBD.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBD.CPUAccessFlags = 0;
vertexBD.MiscFlags = 0;
vertexBD.StructureByteStride = 0;
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
//Create the vertex buffer
r = device->CreateBuffer(&vertexBD, &vertexData, &g_vertexBuffer);
if(FAILED(r))
{return false;}
//Setup the descrption of the index buffer
indexBD.Usage = D3D11_USAGE_DEFAULT;
indexBD.ByteWidth = sizeof(unsigned long) * g_indexCount;
indexBD.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBD.CPUAccessFlags = 0;
indexBD.MiscFlags = 0;
indexBD.StructureByteStride = 0;
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
//Create index buffer
r = device->CreateBuffer(&indexBD, &indexData, &g_indexBuffer);
if(FAILED(r))
{return false;}
//Release the arrays
delete [] vertices;
vertices = 0;
delete [] indices;
indices = 0;
return true;
}
/* Call the RenderBuffer function and put on the graphics pipeline */
void Land::Render(ID3D11DeviceContext* deviceContext)
{
RenderBuffers(deviceContext);
return;
}
/* Render Buffers lists the grid onto the graphics pipeline for rendering */
void Land::RenderBuffers(ID3D11DeviceContext* deviceContext)
{
unsigned int stride;
unsigned int offset;
stride = sizeof(VertexType);
offset = 0;
deviceContext->IASetVertexBuffers(0, 1, &g_vertexBuffer, &stride, &offset);
deviceContext->IASetIndexBuffer(g_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); //Make the grid a list of lines from each vertex
return;
}
/* Return the variables we are using */
int Land::GetIndexCount()
{return g_indexCount;}
/* Shutdown and release the objects we have used */
void Land::ShutdownBuffers()
{
if(g_indexBuffer)
{
g_indexBuffer->Release();
g_indexBuffer = 0;
}
if(g_vertexBuffer)
{
g_vertexBuffer->Release();
g_vertexBuffer = 0;
}
return;
}
void Land::Shutdown()
{
ShutdownBuffers();
return;
}


Graphics.h

Land* land;


Graphics::Init()

/* LAND */
land = new Land;
if(!land)
{return false;}
r = land->Init(d3d->GetDevice());
if(!r)
{MessageBox(hwnd, "Couldn't load Land Object!", "Error!", MB_OK);
return false;}
return true;


Start of Graphics::Render()

/* BEGIN SCENE */
d3d->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); //Clear buffer - RGBA
/* CAMERA */
camera->Render(); //Render Camera
d3d->GetWorldMatrix(worldMatrix); //...
camera->GetViewMatrix(viewMatrix); //Setup Camera & Matrixes
d3d->GetProjectionMatrix(projectionMatrix); //...
d3d->GetOrthoMatrix(orthoMatrix); //...
/* LAND */
land->Render(d3d->GetDeviceContext());


I believe I've done everything right, there isn't any errors, so why is it not appearing?

Thanks
Advertisement
It doesn't look like you're issuing a draw call in your RenderBuffers() method. You need to add:


deviceContext->DrawIndexed(indexbufferdesc.ByteWidth/sizeof(indexbuffersizetype),0,0);


At the end of your RenderBuffers method (obviously you'll also need a reference to a descriptor for your index buffer that you can get from indexbuffer->GetDesc()).
Thanks for the quick reply, but that wasn't it :( my other classes which render have very similar functions and they seem to work. I'm going to keep looking, probably something in the Syntax
Hi there.
Do you scale your grid before you render it looks small to me.

This topic is closed to new replies.

Advertisement