Archived

This topic is now archived and is closed to further replies.

Solarbeam

More terrain blues...

Recommended Posts

What follows is for d3d
VOID GenerateTerrain(terrain &plane, D3DVECTOR origin, 
					 DWORD dwGridSize,FLOAT fScale, 
					 float pheight)
{
	int i, j, total, z;

	D3DVECTOR *grid;
	grid = new D3DVECTOR[dwGridSize];
	grid = new D3DVECTOR[dwGridSize];

	for (i = 0; i[j] = ((D3DVECTOR)(D3DVECTOR(i*fScale, j*fScale, z) + origin)); // error right here!
		}
	}

	int index, pos;

	plane.TerrainVertices = new D3DVERTEX[((dwGridSize-1)^2)*6];

	while (index < dwGridSize * 5)
	{
		plane.TerrainVertices[index] = D3DVERTEX(grid[pos][pos], D3DVECTOR(0, 0, 1), index, index);
		plane.TerrainVertices[index+1] = D3DVERTEX(grid[pos][pos+1], D3DVECTOR(0, 0, 1), index, index+1);
		plane.TerrainVertices[index+2] = D3DVERTEX(grid[pos+1][pos], D3DVECTOR(0, 0, 1), index+1, index);
		plane.TerrainVertices[index+3] = D3DVERTEX(grid[pos+1][pos+1], D3DVECTOR(0, 0, 1), index+1, index+1);
		plane.TerrainVertices[index+4] = D3DVERTEX(grid[pos+1][pos], D3DVECTOR(0, 0, 1), index+1, index);
		plane.TerrainVertices[index+5] = D3DVERTEX(grid[pos][pos+1], D3DVECTOR(0, 0, 1), index, index+1);
		index += 6;
		pos++;
	}

	plane.NumTerrainVertices = index;
}
 
Ok, it gives me this error on the line where i say grid[i][j] = This is the error: C:\mssdk\samples\Multimedia\D3dim\Arena\CamArena\Utility.cpp(182) : error C2679: binary ''='' : no operator defined which takes a right-hand operand of type ''struct _D3DVECTOR'' (or there is no acceptable conversion) I know what this is caused by - there isnt an overloaded = for doing D3DVECTOR[] =...is there an easy way to fix this, or am i gonna have to rip apart my dx headers?

Share this post


Link to post
Share on other sites
Don''t blame me if I''m wrong, but I think you have to put the following line before you include

#define D3D_OVERLOADS

Share this post


Link to post
Share on other sites
Um, ok, i used d3dxvector instead. Odd, now its giving me 8 errors...

Its saying "operator ''['' is ambiguous" on each of my uses of arrays. For instance, it says that grid[j] is ambiguous and that plane.TerrainVertices[index] is also ambiguous. What does this mean?

Share this post


Link to post
Share on other sites
I had a similar problem. I never figured it out (I know C very well, but not a lot of C++), but I got it working without the macro.

Look at the header file (d3dtypes.h, I believe?). Copy the body of the macro D3DVECTOR directly into your code. So instead of calling the macro, you are doing what the macro does manually in your code.

If you figure it out, let me know.

CP

"Can't you see it's only life! We can laugh about it!" - Seal

Share this post


Link to post
Share on other sites
Um, ??? I dont exactly get what you're saying. Whats all this about a macro? You mean the preprocessor types (#define)? Or are you talking about overloaded operators?

[MODIFIED]
Ok, I see what you're saying. It still doesnt fix my problem, though. Im currently trying to implement terrain as a class...anyone who has any input feel free to help...

Edited by - Solarbeam on October 5, 2000 7:13:31 PM

Share this post


Link to post
Share on other sites
Here''s how microsoft does it, why do what''s already been done?

D3DVERTEX* g_pTerrainVertices = NULL;
DWORD g_dwNumTerrainVertices = 0L;
WORD* g_pTerrainIndices = NULL;
DWORD g_dwNumTerrainIndices = 0L;

VOID GenerateTerrainGrid( DWORD dwGridSize, FLOAT fScale )
{
DWORD i, j, ind;

g_dwNumTerrainVertices = dwGridSize * dwGridSize;
g_dwNumTerrainIndices = (dwGridSize-1) * (dwGridSize-1) * 6;
g_pTerrainVertices = new D3DVERTEX[ g_dwNumTerrainVertices ];
g_pTerrainIndices = new WORD[ g_dwNumTerrainIndices ];

for( i=0; i {
for( j=0; j {
FLOAT u = i / (FLOAT)(dwGridSize-1);
FLOAT v = j / (FLOAT)(dwGridSize-1);
FLOAT x = fScale * ( 2*u - 1 );
FLOAT z = fScale * ( 2*v - 1 );

g_pTerrainVertices[i*dwGridSize+j] =
D3DVERTEX( D3DVECTOR(x,0,z), D3DVECTOR(0,1,0), u, v );
}
}

for( i=ind=0; i {
for( j=0; j {
g_pTerrainIndices[ind++] = (WORD)( (i+0)*dwGridSize + (j+0) );
g_pTerrainIndices[ind++] = (WORD)( (i+0)*dwGridSize + (j+1) );
g_pTerrainIndices[ind++] = (WORD)( (i+1)*dwGridSize + (j+0) );
g_pTerrainIndices[ind++] = (WORD)( (i+1)*dwGridSize + (j+0) );
g_pTerrainIndices[ind++] = (WORD)( (i+0)*dwGridSize + (j+1) );
g_pTerrainIndices[ind++] = (WORD)( (i+1)*dwGridSize + (j+1) );
}
}
}

Share this post


Link to post
Share on other sites
I tried to use that, but it didnt seem to work...What the heck are Indices, anyway? The SDK assumes that you know what they are. Also, it seems to require global vars. I tried to adapt it to use this struct:

struct terrain {
D3DVERTEX* g_pTerrainVertices;
DWORD g_dwNumTerrainVertices;
WORD* g_pTerrainIndices;
DWORD g_dwNumTerrainIndices;
}

And i changed the argument list of GenerateTerrainGrid to this:

VOID GenerateTerrainGrid( terrain &plane, DWORD dwGridSize, FLOAT fScale )

I replaced all the references to the global vars to point to plane, but it didnt seem to work. Yes, I used DrawIndexedPrimitive, but it wouldnt draw anything at all.

I really want a routine that can make rough ground; that one cant.


Share this post


Link to post
Share on other sites
Hey the code I posted above CAN make rough terrain you just apply random heights to the .y value (height value on the grid for each point) It should also work with that struct. The code creates the grid about 0,0,0 so be sure your camera is around there and also make sure your camer isn''t below it or it won''t show. And draw it like this:

pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, D3DFVF_VERTEX,plane.pTerrainVertices,plane.dwNumTerrainVertices,plane.pTerrainIndices, plane.dwNumTerrainIndices, 0 );}

you don''t need the g_ prefix if you''re using a struct.(not that it matters)

Also what error are you getting?

Share this post


Link to post
Share on other sites
I''m not getting an error - it just doesnt display...Also, if i supply a random y value, it''ll just end up making the ground disjointed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Solarbeam,

I haven''t checked your code deeply, but I noticed this:
You declare your grid array like:
D3DVECTOR *grid;
this is a one-dimensional array.
Then you ''new'' it twice. I don''t know what happens if you do this, but it won''t make it 2-dimensional.
But when you access the array later you use grid[pos][pos] as it were 2-dim. I think the ''ambiguous bla, bla'' - error lies here.

Either do:
D3DVECTOR **grid;
grid = new (D3DVECTOR *)[dwGridSize];
for(i=0; i = new D3DVECTOR[dwGridSize];
and access it via grid[x][y]

or (better IMHO):
D3DVECTOR *grid;
grid = new D3DVECTOR[dwGridSize * dwGridSize];
and access it via grid[x + y * dwGridSize]

About Indices:
They are used with DrawIndexedPrimitive and allow you to share vertices between triangles. In this way there are less vertices to be transformed.
For example, if you have a quad made of two triangles. If you draw it without indices you need to pass an array of 6 vertices to DrawPrimitive. Using indices, you pass an array of 4 vertices and the 6 indices to this array to DrawIndexedPrimitive. D3D only has to transform 4 vertices now instead of 6. If you do this in a smart way you can save a lot of time transforming duplicate vertices.
I''m also trying a terrain engine now (using binary triangle trees and LOD) and I''ve managed to only transform slightly more than half the vertices as there are triangles in a patch of terrain (if they use the same texture, of course) using indices. If I''d used DrawPrimitive, there were nearly six times as much vertices to be transformed.

Peter




Share this post


Link to post
Share on other sites
YES!!!! THX THX THX!!!! IT WORKS!!! AND ITS NOT DISJOINTED!!!!!! One prob, tho...the normals...but I can fix that easily...ANYWAY!! THX ALL!!! (*phew* after a week, ive finally solved it..)

Share this post


Link to post
Share on other sites