Original Post
Im trying to create 100 squares in a 10x10 layout. I render these onto the screen and i apply the texture. This is so that once i accomplish this i only have to raise and lower the z values to make a simple terrain. Anyway, my program executes fine but it doesnt display anything on the screen. Im assuming there is something wrong with my vertex buffer. Could someone please help me.
Here is the code
InitialiseVertexBuffer()
struct CUSTOMVERTEX
{
FLOAT x, y, z;
FLOAT tu, tv;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)
HRESULT InitialiseVertexBuffer()
{
VOID* pVertices;
CUSTOMVERTEX pcvVertices[600];
int SquareSize = 10;
int SquareNumber = 10;
//Loop through for each sqaure
//100 squares total (10x10)
int i=0;
for(int loopA=0; loopA < SquareNumber; loopA++) {
for(int loopB=0; loopB <SquareNumber; loopB++) {
//Vertex 1
pcvVertices[i].x = loopB*10;
pcvVertices[i].y = loopA*10;
pcvVertices[i].z = 0;
pcvVertices[i].tu = 0;
pcvVertices[i].tv = 0;
i += 1;
//Vertex 2
pcvVertices[i].x = loopB*10+10;
pcvVertices[i].y = loopA*10+10;
pcvVertices[i].z = 0;
pcvVertices[i].tu = 1;
pcvVertices[i].tv = 1;
i += 1;
//Vertex 3
pcvVertices[i].x = loopB*10;
pcvVertices[i].y = loopA*10+10;
pcvVertices[i].z = 0;
pcvVertices[i].tu = 0;
pcvVertices[i].tv = 1;
i += 1;
//Vertex 4
pcvVertices[i].x = loopB*10;
pcvVertices[i].y = loopA*10;
pcvVertices[i].z = 0;
pcvVertices[i].tu = 0;
pcvVertices[i].tv = 0;
i += 1;
//Vertex 5
pcvVertices[i].x = loopB*10+10;
pcvVertices[i].y = loopA*10;
pcvVertices[i].z = 0;
pcvVertices[i].tu = 1;
pcvVertices[i].tv = 0;
i += 1;
//Vertex 6
pcvVertices[i].x = loopB*10+10;
pcvVertices[i].y = loopA*10+10;
pcvVertices[i].z = 0;
pcvVertices[i].tu = 1;
pcvVertices[i].tv = 1;
i += 1;
}
}
//Terrain will be "flat"along xy plane, z value will be adjusted to obtain hills
/*CUSTOMVERTEX cvVertices[] =
{
//Square
{0.0f, 0.0f, 0.0f , 0.0f, 0.0f,}, //Vertex 0 - Blue
{0.0f, -10.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 1 - Red
{-10.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 2 - Red
{0.0f, -10.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 1 - Red
{-10.0f, -10.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 0 - Blue
{-10.0f, 0.0f, 0.0f, 0.0f, 1.0f,},//Vertex 2 - Red
};*/
//Create the vertex buffer from our device.
if(FAILED(g_pD3DDevice->CreateVertexBuffer(600 * sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL)))
{
WriteToLog("Failed to create Vertex Buffer");
return E_FAIL;
}
//Get a pointer to the vertex buffer vertices and lock the vertex buffer
if(FAILED(g_pVertexBuffer->Lock(0, sizeof(pcvVertices), &pVertices, 0)))
{
WriteToLog("Failed to lock vertex buffer");
return E_FAIL;
}
//Copy our stored vertices values into the vertex buffer
memcpy(pVertices, pcvVertices, sizeof(pcvVertices));
//Unlock the vertex buffer
g_pVertexBuffer->Unlock();
WriteToLog("Vertex Buffer Initilized");
WriteToLog(" ");
return S_OK;
}
Render()
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
//Clear the backbuffer to black
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
//Begin the scene
g_pD3DDevice->BeginScene();
//Setup the camera and perspective matrices
SetupCamera();
SetupPerspective();
//Rendering our objects
g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pD3DDevice->SetTexture(0, g_pTexture);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 200);
//End the scene
g_pD3DDevice->EndScene();
//Filp the back and front buffers so that whatever has been rendered on the back buffer
//will now be visible on screen (front buffer).
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
SetupCamera() and SetupPerspective()
void SetupCamera()
{
//Here we will setup the camera.
//The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"
//We have set the following:
//Camera Position: (0, 0, -30)
//Look at Position: (0, 0, 0)
//Up direction: Y-Axis.
D3DXMATRIX matView;
D3DXVECTOR3 camera, look, up;
camera = D3DXVECTOR3(0.0f, 0.0f,-300.0f);
look = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView, &camera, //Camera Position
&look, //Look At Position
&up); //Up Direction
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}
void SetupPerspective()
{
//Here we specify the field of view, aspect ration and near and far clipping planes.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
Thank you in advance