17 Replies - 730 Views - Last Post: 26 November 2011 - 10:30 PM
#1
Terrain and Vertex/Index buffer question / help
Posted 24 November 2011 - 04:02 PM
Now that I have accomplished the camera and its math. I have moved onto terrain. I can say that I have successfully rendered it via a .RAW greyscale height map. The question is:
(dont hold me to It) A triangle is Made up of 3 verticies, that are draw clockwise? But when I load the height map, it is getting the data diagonally. So in my raw file, I have to draw a diagonal line, if I want a straight hill/mountain in my render. I was wondering how I go about fixing this, or providing a method to satisfy the problem at hand. My code is gathered off of
http://www.two-kings.../terrain01.html
Thanks in advance,
Replies To: Terrain and Vertex/Index buffer question / help
#2
Re: Terrain and Vertex/Index buffer question / help
Posted 24 November 2011 - 07:09 PM
#3
Re: Terrain and Vertex/Index buffer question / help
Posted 25 November 2011 - 09:20 AM
#4
Re: Terrain and Vertex/Index buffer question / help
Posted 25 November 2011 - 04:41 PM
//lock vertex buffer X = Vertex[2000].fX //Unlock vertex buffer return X;
It gives me a very larger number. This is probably not the right way to do it, but I cannot find another way.
#5
Re: Terrain and Vertex/Index buffer question / help
Posted 25 November 2011 - 07:35 PM
#6
Re: Terrain and Vertex/Index buffer question / help
Posted 25 November 2011 - 08:08 PM
The way it works is this. It opens the .raw file in binary mode and is stored in a jaggared? Or multi-dimensional array? through inside 2 for loops.
We create our vertex buffer, lock it, then fill it with the data collected from the greyscale or 'height' map. We then unlock it, then create and fill an index buffer. We draw it with DrawIndexPrimitives*
Is this not the correct way to create terrain?
P.S I think the values range from 0 to 1
This post has been edited by aaron1178: 25 November 2011 - 08:09 PM
#7
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 03:33 AM
Are you able to point out the code that does this?
#8
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 05:07 AM
Terrain.h
#include "Main.h"
#d e f i n e T E R R A I N _ X 6 4 / / x d i m
#d e f i n e T E R R A I N _ Y 6 4 / / y d i m
c l a s s C T e r r a i n
{
p u b l i c :
C T e r r a i n ( v o i d ) ; / / c o n s t r u c t o r
v o i d C r e a t e T e r r a i n ( v o i d ) ; / / l o a d f i l e a n d
v o i d D r aw ( v o i d ) ;
p r i v a t e :
L P D I R E C T 3 D V E R T E X B U F F E R 9 m _ p T e r r a i n VB ; / / v e r t e
L P D I R E C T 3 D I ND E X B U F F E R 9 m _ p T e r r a i n I B ; / / i n d e x
DWO R D m _ dwT e r r a i n V e r t i c e s , m _ dwT e r r a i n P r i m i t i v e s
} ;
Terrain.cpp
CTerrain::CTerrain(void)
{
m_pTerrainVB = NULL;
m_pTerrainIB = NULL; m
_dwTerrainVertices = (TERRAIN_X + 1) * (TERRAIN_Y + 1); m
_dwTerrainPrimitives = TERRAIN_X * TERRAIN_Y * 2; Cr
eateTerrain();
} //CTerrain
void CTerrain::CreateTerrain(void)
{
unsigned char aHeightMap[TERRAIN_X + 1] [TERRAIN_Y + 1]; //height array
D3DVERTEX* pVertexData; //pointer to vertex
short* pIndexData; //pointer to index
ifstream File; //file object
File.open("heightmap.raw",ios::binary); //open file in binary mode
for(unsigned y = 0;y < (TERRAIN_Y + 1);++y)
{
for(unsigned x = 0;x < (TERRAIN_X + 1);++x)
{
aHeightMap[x][y] = File.get(); //get height }
}
File.close(); //close file
g_App.GetDevice()->CreateVertexBuffer( sizeof (D3DVERTEX)*m_dwTerrainVertices,
D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED,
&m_pTerrainVB,
NULL); m_pT
errainVB->Lock( 0,0,(void**)&pVertexData, 0); for(y = 0;y < (TERRAIN_Y + 1);++y) { fo
r(unsigned x = 0;x < (TERRAIN_X + 1);++x) {
pVe
rtexData[x + y * (TERRAIN_X + 1)].fX = (float)x; pVertexData[x + y * (TERRAIN_X + 1)].fY = (float)y; pVert
exData[x + y * (TERRAIN_X + 1)].fZ = (float) aHeightMap[x][y]/ 15.0f; pVertexData[x + y * (TERRAIN_X + 1)].dwColor = 0xffffffff; //color } } m_
p
TerrainVB->Unlock();
g_App.GetDevice()->CreateIndexBuffer( sizeof (short)*m_dwTerrainPrimitives*3, D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pTerrainIB,
NULL); m_pTerrainIB->Lock(0,0,(void**)&pIndexData,0); for(
y = 0;y < TERRAIN_Y;++y) { fo
r
(unsigned x = 0;x < TERRAIN_X;++x) {
*p
I
ndexData++ = x + y * (TERRAIN_X + 1); //v1 *pIndexData++ = x + 1 + y * (TERRAIN_X + 1); // v2 *pIndexData++ = x + 1 + (y + 1) * (TERRAIN_X + 1); //v4 *pIn
dexData++ = x + y * (TERRAIN_X + 1); //v1 *pIndexData++ = x + 1 + (y + 1) * (TERRAIN_X + 1); //v4 *pIndexData++ = x + (y + 1) * (TERRAIN_X + 1); // v3 }
}
m_pTer
rainIB->Unlock(); } //Cr
eateTerrain
}
void CTerrain::Draw(void) {
g_App.GetDevice()->SetStreamSource(0,m_ pTerrainVB,0,sizeof(D3DVERTEX)); g_App.GetDevice()->SetIndices(m_pTerrainIB);
g_App.GetDevice()->DrawIndexedPrimitive(D3DPT_
TRIANGLELIST, 0, 0, m_dwTerrainVertices, 0, m_dwTerrainPrimitives); } //Draw
And there are no errors in this code, its just because I am posting via phone.
#9
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 06:35 AM
#10
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 03:16 PM
Ok, so I open the .raw file in binary mode. I then go through 2 for loops, one for x and one for y. I then fill my custom vertex array variable with the x and y for the array indicies, and fill it with the contents of the file, using file.get() when file is the io stream.
F i l e . o p e n ( " h e i g h t ma p . r aw" , i o s : : b i n a r y ) ; / / o p e n f i l e i n b i n a r y mo d e
f o r ( u n s i g n e d y = 0 ; y < ( T E R R A I N _ Y + 1 ) ; + + y )
{
f o r ( u n s i g n e d x = 0 ; x < ( T E R R A I N _ X + 1 ) ; + + x )
{
a H e i g h t M a p [ x ] [ y ] = F i l e . g e t ( ) ; / / g e t h e i g h t
}
}
F i l e . c l o s e ( ) ; / / c l o s e f i l e
#11
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 03:33 PM
#12
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 03:56 PM
float x, y, z; X = aHeightData[200][200] / TERRAIN_X
As the HeightMap ranges from 0 to 255, it will not give us something like 5.0, -10.0, 0.0, but rather something like 5, 60, 200, so I think we need to divide it by the width of the terrain.
#13
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 05:22 PM
#14
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 05:33 PM
P.S would I have to change it from unsigned to an int?
This post has been edited by aaron1178: 26 November 2011 - 05:35 PM
#15
Re: Terrain and Vertex/Index buffer question / help
Posted 26 November 2011 - 05:38 PM
|
|

New Topic/Question
Reply




MultiQuote




|