Terrain and Vertex/Index buffer question / help

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 997 Views - Last Post: 26 November 2011 - 10:30 PM Rate Topic: -----

#1 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Terrain and Vertex/Index buffer question / help

Posted 24 November 2011 - 04:02 PM

Hey all,
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,

Is This A Good Question/Topic? 0
  • +

Replies To: Terrain and Vertex/Index buffer question / help

#2 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 24 November 2011 - 07:09 PM

PS: How would I go about getting each vertecies vector location? As in physics ect. I know I would have to lock the vertex buffer, but after that, I am completley lost. Is this even the right way to go about terrain collision?
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 25 November 2011 - 09:20 AM

This is a lot easier then you think. Analyze the data structure containing the vertices for your terrain. You will wonder how you didn't see it before. ;)
Was This Post Helpful? 0
  • +
  • -

#4 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 25 November 2011 - 04:41 PM

When I get the vertex info like bellow, all I get is a larger number?

//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.
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 25 November 2011 - 07:35 PM

Can you explain what a heightmap is and why it is called a 'heightmap'?
Was This Post Helpful? 0
  • +
  • -

#6 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 25 November 2011 - 08:08 PM

I cannot say why they named it a height map. But what I can say is this. A greyscale images is save as a .raw file. Black is flat and more greyish to white is the heighest peak.

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

Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 03:33 AM

Well, you kind of explained it even though you said you couldn't. First off, you should really know what a 'map' is, and a map is simply a defined relationship between the entities of two or more groups. So, a height map 'maps' the height of your terrain to the greyscales in your .raw file.

Are you able to point out the code that does this?
Was This Post Helpful? 0
  • +
  • -

#8 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 05:07 AM

Yes, here it is.

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.
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 06:35 AM

No, you have just given me a wall of code. Where is the code in there that will five you the height?
Was This Post Helpful? 0
  • +
  • -

#10 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 03:16 PM

Sorry Butch :/

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 


Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 03:33 PM

Okay, so you know that the aHeightMap 2D array contains element values in the closed interval [0..255] since it is of type unsigned char. Taking the simplest problem of getting the terrain height at any point, how do you think that can be achieved?
Was This Post Helpful? 0
  • +
  • -

#12 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 03:56 PM

Hmmm, I think this could be achieved via something like this:

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.
Was This Post Helpful? 0
  • +
  • -

#13 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 05:22 PM

Really? You need to carefully analyze the code, you're not understanding it.
Was This Post Helpful? 0
  • +
  • -

#14 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 05:33 PM

Wait, I think you gave me the answer in your previous post. You said that it was a 2D array, except I am working in 3D, so I need to make it a 3D array (for x, y, z) and fill it the same way?

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

Was This Post Helpful? 0
  • +
  • -

#15 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Terrain and Vertex/Index buffer question / help

Posted 26 November 2011 - 05:38 PM

No.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2