Drawing a Grid(Terrainlike)

Started by
17 comments, last by m4gnus 18 years, 11 months ago
ok the error has to be in the vertex creation because if i create them by hand for a grid with xRes=2 and zRes=2 (a simple quad) and let the CreateIndices function create the indices everything works fine!

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
Advertisement
By "set up" I mean "given a value".

That's the thing about the debugger: you can inspect the value of anything in memory. You'd be able to see what the coordinates are of the vectors being generated. What you have is a runtime error. Non-fatal, but a runtime error nonetheless.

Another thing you may want to check out: you're only locking one vertex in ConstructBuffers:
pVBuffer->Lock(0,sizeof(std::vector<TerrainVert>::value_type),&pVData,0);

You probably want to lock the entire buffer:
pVBuffer->Lock(0,0,&pVData,0);
Kippesoep
oops..thx i fixed that but my prob is still there and i don't really know why this:
	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);		}	}


does something different than:
TerrainVert vertex;vertex.x=0.0f;vertex.y=-10.0f;vertex.z=100.0f;vertex.color=0xff00ff00;TerrainVert vertex2;vertex2.x=400.0f;vertex2.y=-10.0f;vertex2.z=100.0f;vertex2.color=0xff00ff00;TerrainVert vertex3;vertex3.x=0.0f;vertex3.y=-10.0f;vertex3.z=-300.0f;vertex3.color=0xff0000ff;TerrainVert vertex4;vertex4.x=400.0f;vertex4.y=-10.0f;vertex4.z=-300.0f;vertex4.color=0xffff0000;Verts.push_back(vertex);Verts.push_back(vertex2);Verts.push_back(vertex3);Verts.push_back(vertex4);


if StartPoint is set to 0,-10,100, xSize and zSize to 400 and xRes+zRes to 2...if i do it by hand it create a large wuad like i want but the double loop just create 1 strange triangle. Can somebody tell me what's the difference betweem these too?


regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
It does something different because that's what you're telling it to do. To get those 4 vertices, you'd have to set xSize to 400, zSize to -400 (negative!!), xRes and zRes to 1.

It's probably the negative zSize that is causing the problem. If you want z to go along the negative axis, but specify a positive zSive, all your triangles are reversed and will be backface-culled.
Kippesoep
no backface culling is turned off...and are you sure that i have to set xRes+zRes to 1? these 2 values are not the number of quad-columns(/rows) but the number of vertex-columns(/rows)

btw it would it help if i post a screenshot like it should be(with manual filled vertexbuffer) and a screenshot with that strange triangle when generating the vertices.

if i change zRes to -zRes it draws nothing...

regards,
m4gnus

"There are 10 types of people in the world... those who understand binary and those who don't."
The numbers I gave you are the way to generate the four vertices you created manually. Step through it with the debugger and you'll see.

BTW, instead of xDivs and zDivs, I meant to use those values for xRes and zRes (why do you have two different variables refering to the same concept with slightly different semantics without any good link in the class that uses them?).
Kippesoep
i mean that for a quad you have to use xRes,zRes=2 because these values are the vertices per row/column not the quads per row/column and because a simple quad consists of 4 vertices(2 in 1 row, 2 rows) i need to set xRes,zRes to 2. if i would set 'em to 1 i would have a point...i know that you would ussually expect it to be the number of quads per row, so i'll change it as soon as everything works so far...

btw i changed xDivs,zDivs to xRes,zRes now...
"There are 10 types of people in the world... those who understand binary and those who don't."
In the code you gave, to get one quad, you need to set xDivs and zDivs to 2, and xRes and yRes to 1. (Always: xDivs = xRes + 1; zDivs = zRes + 1;). Again, in the code shown, there is nothing that shows where xDivs and zDivs come from. It is not set anywhere, so it's likely to be a global variable. Bad idea to use global variables like that inside your class. Basically: get rid of that. What I'd expect to see (and recommend you do) is something like:

void Terrain::Create (ngEngine *engine, D3DXVECTOR3 StartPoint, float xSize, float zSize, int xRes, int zRes){    //Store values    m_pEngine = engine;    m_xRes = xRes;    m_zRes = zRes;        //Create vertices    CreateVertices (StartPoint, xSize/m_xRes, zSize/m_zRes);    //Create indices    CreateIndices ();}void Terrain::CreateVertices (D3DXVECTOR3 StartPoint, float RowHeight, float ColWidth){    for (int j = 0; j <= m_zRes; ++j)    {        for (int i = 0; i <= m_xRes; ++i)        {            TerrainVert Vertex;            Vertex.x = StartPoint.x + i * ColWidth;            Vertex.y = StartPoint.y;            Vertex.z = StartPoint.z + j * RowHeight;            Vertex.color = 0xDD00DD00;            Verts.push_back (Vertex);        }    }}void Terrain::CreateIndices (){    for (int j = 0; j < m_zRes; ++j)    {        for (int i = 0; i < m_xRes; ++i)        {            /* ... */        }    }}void Terrain::render() {    m_pEngine->g_pD3DDevice->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, Verts.size (), 0, m_zRes * m_xRes * 2);}


This places the terrain resolution in the class, where it belongs, and doesn't cause the confusion.

If the numbers I gave you don't work, then something is wrong elsewhere. They will generate the exact same vertices you listed manually. Let me spell out the program flow for you:

terrain->Create (g_Engine, D3DXVECTOR3 (0, -10, 100), 400, -400, 1, 1);//In terrain::Createfloat RowHeight=-400/*zSize/zRes*/;float ColWidth=400/*xSize/xRes*/;CreateVertices(D3DXVECTOR3 (0, -10, 100), -400, 400, 1, 1);//In CreateVertices/* j = 0, zCount == 1 *//* i = 0; xCount == 1 */Vertex.x = 0; //(StartPoint.x+i*ColWidth) = 0 + 0 * 400 = 0Vertex.y = -10;Vertex.z = 100; //(StartPoint.z+j*RowHeight) = 100 + 0 * -400 = 100/* j = 0, zCount == 1 *//* i = 1; xCount == 1 */Vertex.x = 400; //(StartPoint.x+i*ColWidth) = 0 + 1 * 400 = 400Vertex.y = -10;Vertex.z = 100; //(StartPoint.z+j*RowHeight) = 100 + 0 * -400 = 100/* j = 1, zCount == 1 *//* i = 0; xCount == 1 */Vertex.x = 0; //(StartPoint.x+i*ColWidth) = 0 + 0 * 400 = 0Vertex.y = -10;Vertex.z = -300; //(StartPoint.z+j*RowHeight) = 100 + 1 * -400 = -300/* j = 1, zCount == 1 *//* i = 1; xCount == 1 */Vertex.x = 400; //(StartPoint.x+i*ColWidth) = 0 + 1 * 400 = 400Vertex.y = -10;Vertex.z = -300; //(StartPoint.z+j*RowHeight) = 100 + 1 * -400 = -300


So, you get (0,-10,100) (400,-10,100) (0,-10,300) (400,-10,-300), which is exactly what you wanted.
Kippesoep
everything works fine now...the xRes and zRes variables now really are the fertex rows+columns...just had to use for(int i=0;i<xCount;i++) instead of for(int i=0;i<=xCount;i++)

thank you

Edit: oops it only works for xRes,zRes=2 but if i set it to higher values one quad is missing..

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