Trying to make a Grid in DirectX

I am trying to make a grid in DirectX, but I keep getting the error "Access violation writing location" for some reason.

This is my code for defining the grid:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void CDirectXFramework::DefineGrid(int Rows, int Cols, float dx, float dz, const D3DXVECTOR3& center) {

/* precalculations */
int CellRows = Rows - 1;
int CellCols = Cols - 1;

float width = (float)CellCols * dx;
float depth = (float)CellRows * dz;

//offsets to translate grid to center of coordinate system
float xOffset = -width * 0.5f;
float zOffset = depth * 0.5f;

/* build vertices */
vertices3 = 0;
VertexBuffer3->Lock(0, 0, (void**)&vertices3, 0);


int k = 0;

for(float i = 0; i < Rows; ++i) {
	for(float j = 0; j < Cols; ++j) {

		//negate the depth coordinates to put in Quadrant 4, then offset about center of coordinate system

		vertices3[k].position = D3DXVECTOR3(j * dx + xOffset, 0.0f, -i * dz + zOffset); /* this line in particular is where the error occurs once i = 2, j = 58, and k = 258 */

		//translate center of grid to the specified center
		D3DXMATRIX T;
		D3DXMatrixTranslation(&T, center.x, center.y, center.z);
		D3DXVec3TransformCoord(&vertices3[k].position, &vertices3[k].position, &T);

		D3DXVec3Normalize(&vertices3[k].normal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
		vertices3[k].uv = D3DXVECTOR2((vertices3[k].position.x + (0.5f * width)) / width, (vertices3[k].position.z - (0.5f * depth)) / -depth);

		++k; //next vertex
	}
}

VertexBuffer3->Unlock();


/* build indices */
indices3 = 0;
IndexBuffer3->Lock(0, 0, (void**)&indices3, 0);

//generate indices for each quad
k = 0;
for(DWORD i = 0; i < (DWORD)Rows; ++i) {
	for(DWORD j = 0; j < (DWORD)Cols; ++j) {

		//1st triangle
		indices3[k] = i * Cols + j;
		indices3[k + 1] = i * Cols + j + 1;
		indices3[k + 2] = (i + 1) * Cols + j;

		//2nd triangle
		indices3[k + 3] = (i + 1) * Cols + j;
		indices3[k + 4] = i * Cols + j + 1;
		indices3[k + 5] = (i + 1) * Cols + j + 1;

		k += 6; //next quad
	}
}

IndexBuffer3->Unlock();
}


What does it look like the problem might be?

btw, the values I am using are:

1
2
3
4
5
Rows = 100;
Cols = 100;
dx = 1.0f;
dz = 1.0f;
center = D3DXVECTOR3(0.0f, 0.0f, 0.0f);


I found that the program works when using these values for the rows and columns:

1
2
Rows = 2;
Cols = 2;


Using any other value gives the error mentioned above. It displays a small flat surface where I want it to, but I am trying to have a large enough surface to use as a ground (like 100 x 100).
Last edited on
What does it look like the problem might be?
I keep getting the error "Access violation writing location" for some reason.


It looks like the problem might be writing to memory you have no business writing to. Your code lacks a significant amount of context, so the exact reason for this happening is not clear, but it seems likely that k is not a valid index for vertices3 on line 26, given that's where you say the error occurs.
Is there a way I can ensure that the array of vertices do not get a NULL pointer in them, without declaring a huge amount of them beforehand?

Right now this how I have it set in the header file:
1
2
3
4
5
6
7
8
struct Vertex {
	D3DXVECTOR3 position;
	D3DXVECTOR3 normal;
	D3DXVECTOR2 uv;
};

Vertex*		vertices3; //defines position of vertices for grid
WORD*		indices3;


I do not have a set amount for the vertices3 array, because the program sets the array size automatically (I am not really sure why it works that way).
Last edited on
I do not have a set amount for the vertices3 array, because the program sets the array size automatically (I am not really sure why it works that way).


This sounds suspicious. How are you initializing vertices3?
I pretty much only initialize it inside the DefineGrid() function right before Lock():
1
2
3
/* build vertices */
vertices3 = 0;
VertexBuffer3->Lock(0, 0, (void**)&vertices3, 0);


It worked when using it for creating primitives such as a cube and pyramid, but I am guessing it won't work with creating a grid because of the huge amount of vertices that get made?
I just figured it out thanks to this website: http://www.toymaker.info/Games/html/terrain.html

The problem was how I had:
1
2
3
/* precalculations */
int CellRows = Rows - 1;
int CellCols = Cols - 1;


and from my init code:
1
2
gridVertices = gridRows + gridCols;
gridTriangles = (gridRows - 1) * (gridCols - 1) * 2;


gridVertices and gridTriangles were not being calculated properly, which explains why I had that "Access violation writing location" error. The same thing is true for the CellRows and CellCols variables.

On a side note, I was following the example in "Introduction to 3D Game Programming with DirectX 9.0c" which has the wrong formulas for those items. Everything else was pretty solid.
Topic archived. No new replies allowed.