Grid Problem

Started by
1 comment, last by VISQI 13 years, 3 months ago
hey guys
I am having a problem with my grid algorithm. I checked out my reference. I made sure the problem is not concerning other elements of the demo (D3D, Shaders, etc.).
Anyways, here is the code for the vertex and index buffers


void App::CreateGrids(float Width, float Depth, float dx, float dz, D3DXVECTOR3 Center,D3DXVECTOR3 Normal,float plane, std::vector<Vertex> Vert, std::vector<DWORD> Indi)
{
//1- generate vertices
//2- generate indicies
//3- Fill the buffers
//4- Render

//1- Generating vertices

float NumColVert = Width + 1;
float NumRowVert = Depth + 1;
TotalVert = NumColVert * NumRowVert;
TotalPoly = Width * Depth * 2;

float Xtrans = -Width * 0.5f;
float Ztrans = Depth * 0.5f;
D3DXMATRIX MatTrans;
D3DXMatrixIdentity(&MatTrans);

Vert.resize(TotalVert);
float k;
//xz plane
if(plane == 1)
{
k = 0;
for(float i = 0; i < NumRowVert; i++)//Vertex generation and auto-translation to origin(0,0) then to "Center"
{
for(float j = 0; j < NumColVert; j++)
{
Vert[k].pos.x = j * dx + Xtrans;
Vert[k].pos.y = 0.0f;
Vert[k].pos.z = -i * dz + Ztrans;
Vert[k].normal = Normal;

D3DXMatrixTranslation(&MatTrans, Center.x, Center.y, Center.z);
D3DXVec3TransformCoord(&Vert[k].pos, &Vert[k].pos, &MatTrans);
k++;
}
}

}

//zy plane
if(plane == 2)
{
k = 0;
for(float i = 0; i < NumRowVert; i++)//Vertex generation and auto-translation to origin(0,0) then to "Center"
{
for(float j = 0; j < NumColVert; j++)
{
Vert[k].pos.x = 0.0f;
Vert[k].pos.y = -i * dz + Ztrans;
Vert[k].pos.z = j * dx + Xtrans;
Vert[k].normal = Normal;

D3DXMatrixTranslation(&MatTrans, Center.x, Center.y, Center.z);
D3DXVec3TransformCoord(&Vert[k].pos, &Vert[k].pos, &MatTrans);
k++;
}
}

}

Indi.resize(TotalPoly * 3);
k = 0;
for(float i = 0; i < Width; i++)//Index generation per grid
{
for(float j = 0; j < Depth; j++)
{
Indi[k] = (i+1) * NumColVert + j;
Indi[k + 1] = i * NumColVert + j;
Indi[k + 2] = i * NumColVert + j+1;
Indi[k + 3] = (i+1) * NumColVert + j;
Indi[k + 4] = i * NumColVert + j+1;
Indi[k + 5] = (i+1) * NumColVert + j+1;

// next quad
k += 6;
}
}


/*
//Simple Quad
Vert[0].pos.x = 0.0f - 0.5f;
Vert[0].pos.y = 0.0f;
Vert[0].pos.z = -0.0f + 0.5f;
Vert[1].pos.x = 1.0f - 0.5f;
Vert[1].pos.y = 0.0f;
Vert[1].pos.z = -0.0f + 0.5f;
Vert[2].pos.x = 0.0f - 0.5f;
Vert[2].pos.y = 0.0f;
Vert[2].pos.z = -1.0f + 0.5f;
Vert[3].pos.x = 1.0f - 0.5f;
Vert[3].pos.y = 0.0f;
Vert[3].pos.z = -1.0f + 0.5f;

Indi[0] = 2;
Indi[1] = 0;
Indi[2] = 1;
Indi[3] = 2;
Indi[4] = 1;
Indi[5] = 3;
*/

UINT VertexOffset = OI * sizeof(Vert);
UINT IndexOffset = OI * sizeof(Indi);

//3- Fill the Buffers
Vertex* V = 0;
DxHR((gd3dDev->CreateVertexBuffer(100*100, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &GridVB, NULL)), "CreateVertexBuffer()");
//Append to the Buffers, not overwrite
if(SUCCEEDED(GridVB->Lock(VertexOffset, sizeof(Vert), (void**)&V, 0)))
{
for(int i = 0; i < TotalVert; i++)
{
V.pos = Vert.pos;
V.normal = Vert.normal;
}
DxHR(GridVB->Unlock(), "Unlock() //GridVB");
Vert.clear();
}

DWORD* I = 0;
DxHR(gd3dDev->CreateIndexBuffer(100*100, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &GridIB, NULL), "CreateIndexBuffer()");
if(SUCCEEDED(GridIB->Lock(IndexOffset, sizeof(Indi), (void**)&I, 0)))
{
for(int i = 0; i < TotalPoly * 3; i++)
I = Indi;
DxHR(GridIB->Unlock(), "Unlock() //IB");
Indi.clear();
}
}

i think the algorithm is pretty self-explanatory, just create vertices at the 4th quadrant and then move them to be at the origin then just relocate them to any place using the D3DXVec3TransformCoord() function and the Center vector
the indices are according, they just connect the vertices in a simple and default manner;
The "Simple Quad" snippet is just there to draw a simple individual quad using the same algorithm. Unfortunately, it didn't work for some reason(there was nothing in the output but a blank window);
The Vertex and index Offset integers are just there so i can append to the same buffer over and over again to draw multiple grids with the same characteristics.
I checked that the shaders, D3D, WIN32, etc. was working good using D3DXCreate* function to create a teapot and applied full lighting and material effects on it with no problems with anything in debug runtime whatsoever.
Here is a picture of the massacre:
http://img340.imageshack.us/f/solidc.jpg/

Wireframe:
http://img534.imageshack.us/f/wireframez.jpg/

Thanks in advance
Advertisement
Just a quick look:

1. I don't think this has anything to do with the problem, but you shouldn't use a float ("float Plane") as a switch variable. Use an integer. In addition, you don't handle the situation where Plane is neither 1 nor 2.

2. You create an index buffer of size WORD (D3DFMT_INDEX16), but you cast the buffer pointer to DWORD.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

i have been digging up every possible element in this demo to see which is the problem, i seriously can't believe that it worked just by casting WORD instead of DWORD, i didn't think it was that easy
the Plane float was still in works, i used debug runtime to see the values of Plane myself.
Anyways, thanks again Buckeye

This topic is closed to new replies.

Advertisement