Drawing a Grid(Terrainlike)

Started by
17 comments, last by m4gnus 18 years, 10 months ago
Hi Finally the Inputsystem for my engine works so now i just want to render a simple grid with an indexed Trianglelist...I tried to keep everything as clear as possible so i first wrote a basemsh class which just contains an Index-,a Vertexbuffer and a DWORD for the FVF...every class that will be renders should be a child of this class so i thought i'll make a very basic terrain class which just creates a grid of variable size with a variable number of subdivions... but something is worng with it..it definitly has something to do with the creation of the Vertex and Indexbuffer because if i fill 'em manually it works fine... My terrain Class:

#include "stdafx.h"
#include ".\terrain.h"
#include "ngEngine.h"

Terrain::Terrain(void)
{
FVF=D3DFVF_XYZ|D3DFVF_DIFFUSE;
}

Terrain::~Terrain(void)
{
}

void Terrain::Create(ngEngine *engine,D3DXVECTOR3 StartPoint,float xSize,float zSize,int xRes,int zRes)
{
	m_pEngine=engine;
	float RowHeight=zSize/zRes;
	float ColWidth=xSize/xRes;
	CreateVertices(StartPoint,RowHeight,ColWidth,xRes,zRes);
	CreateIndices(xRes,zRes);
	ConstructBuffers();

}
void Terrain::CreateVertices(D3DXVECTOR3 StartPoint,float RowHeight,float ColWidth,int xCount,int zCount) 
{
	for(int j=0;j<=zCount;j++)   //<--for every Row do:
	{
		for(int i=0;i<=xCount;i++) //<--for every Vert do:
		{
			TerrainVert Vertex;
			Vertex.x=StartPoint.x+i*ColWidth;
			Vertex.y=StartPoint.y;            //<---Terrain Height
			Vertex.z=StartPoint.z+j*RowHeight;
			Vertex.color=0xff00ff00;
			Verts.push_back(Vertex);


		}
	}


}

void Terrain::CreateIndices(int xRes, int yRes) 
{
for(int j=0;j<=yRes-1;j++) //<--For Every Row do:
{
	for(int i=0;i<=xRes-1;i++) //<--For Every QUAD(!) do:
	{
		int Row=j*xRes;
		//First Triangle
		Indices.push_back(xRes*j+i+Row);
		Indices.push_back(i+Row);
		Indices.push_back(i+Row+1);

		//Indices.push_back(xRes*j+i+1+Row);
		//Second Triangle
		Indices.push_back(i+Row);
		Indices.push_back(i+1+Row);
		Indices.push_back(xRes*j+i+1+Row);
	}

}

}

void Terrain::ConstructBuffers() 
{

//Create VertexBuffer:

	if( FAILED( m_pEngine->g_pD3DDevice->CreateVertexBuffer(Verts.size()*sizeof(std::vector<TerrainVert>::value_type),
                                                       0,
                                                       FVF,
                                                       D3DPOOL_DEFAULT,
                                                       &pVBuffer,   //<---inherited from BaseMesh
                                                       NULL ) ) )
    {
        MessageBox(0,"Fetter Error1!","Error",0);
        PostQuitMessage(1);
    }
VOID *pVData;
pVBuffer->Lock(0,sizeof(std::vector<TerrainVert>::value_type),(void**)&pVData,0);	//lock buffer 
std::vector<TerrainVert>::pointer Vptr = &Verts[0];
memcpy(pVData,Vptr,Verts.size()*sizeof(std::vector<TerrainVert>::value_type));						//copy data to buffer
pVBuffer->Unlock();	

//Create IndexBuffer:
if(FAILED(m_pEngine->g_pD3DDevice->CreateIndexBuffer(Indices.size()*sizeof(std::vector<short>::value_type),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&pIBuffer,NULL))) 
{
	MessageBox(0,"Fetter Error2!","Error",0);
	PostQuitMessage(1);
}

VOID *pIData;
pIBuffer->Lock(0,sizeof(std::vector<short>::value_type),(void**)&pIData,0);	//lock buffer 
std::vector<short>::pointer Iptr = &Indices[0];
memcpy(pIData,Iptr,Indices.size()*sizeof(std::vector<short>::value_type));	//copy data to buffer
pIBuffer->Unlock();	
}

void Terrain::render() 
{
	m_pEngine->g_pD3DDevice->SetFVF(FVF); //<---inherited from BaseMesh
	m_pEngine->g_pD3DDevice->SetStreamSource(0,pVBuffer,0,sizeof(std::vector<TerrainVert>::value_type));
	m_pEngine->g_pD3DDevice->SetIndices(pIBuffer);
	m_pEngine->g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,Verts.size(),0,(xDivs-1)*(zDivs-1)*2);

}

regards, m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
Advertisement
It would help if you posted the actual problem, too. Does it render nothing? Does it crash? Do you get an error message?

Also, whatever the problem:
- use the DirectX Debug runtime
- turn the Debug Ouput Level up to full
- look at the debug spew
Kippesoep
it renders nothing...
"There are 10 types of people in the world... those who understand binary and those who don't."
Have you set the projection and view matricies?
Assuming the matrices are correct (considering your said it works if you fill the buffers manually): I don't see any set up of xDivs and zDivs in your code. Perhaps these are 0?
Kippesoep
Never mind that, you're creating degenerate triangles in your index buffer. Take a look at the contents of your index buffer.

You want something like:
for (int j = 0; j < yRes; ++j){	for (int i = 0; i < xRes; ++i)	{		int Row = j * xRes;		//First Triangle		Indices.push_back(Row + i);		Indices.push_back(Row + i + 1);		Indices.push_back(Row + i + xRes);		//Second Triangle		Indices.push_back(Row + i + 1);		Indices.push_back(Row + i + 1 + xRes);		Indices.push_back(Row + i + xRes);	}}
Kippesoep
ok thx i was also thinking it is a mistake like this but i calculated the indices of some traingle on paper and it seemed alright... i too tired to fix it now so i'll post tomorrow if it has helped..

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
Ok it finally draws something but just 1 single trinagle... the rest of the terrain is missing...

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
That brings me back to the former question: how are xDivs and zDivs set up?
Set through your program with the debugger and check the debug spew.
Kippesoep
what do you mean by set up? they are both set to 4 in my main cpp..
do you really think a debugger can help me here? i would use a debugger if i had a runtime error

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."

This topic is closed to new replies.

Advertisement