Skip to main content
gamedev.net
🔒 Locked

problem reading .raw file

Started by danong 5 replies 900+ views
Original Post
danong
danong
I have problem reading .raw file data into heightmap terrain. I've created a 8-bpp based grayscale image in photoshop cs, and i saved it as .raw file with a header size of 2513. 128*128 width-height. however, when i read into my program, it generated a weird terrain. here, i present the outcome and the original raw image that i'd created. like what's shown in the pic, it seems like one quad of the image was generated in a different order. I'm sure my reading to the data is right and have no problem during execution, just wondering why it gave me such a weird-formed terrain in this way. My guess is that , arrangement of the data of the raw file structure that i creat with PS may be in different way or i may be using the wrong software to convert image into .raw file. Any remedy up to this? thanks =) btw, i'm reading in this way : heightmapY = fileHeightmap[ i * elementPerRow + j ]; EDIT: GDNet uses the HTML 'img' tag to embed images. [Edited by - jollyjeffers on January 5, 2007 3:27:21 PM]
Technical ..? then Master Non-Technical first ...
jollyjeffers
jollyjeffers
I'm going to move this over to GP&T (although it could belong in GP as well) as this doesn't seem to be anything specific to the DirectX API.

From the look of your screenshot you've probably got some offsets wrong - it seems the heights are just shifted/wrapped horizontally by a constant amount.

I'd get friendly with your debugger of choice and see whats going on. Putting assertions/breakpoints in, printing out debug information is always a good idea. Can also help to run with an 8x8 (or similar) heightmap so you've got a more manageable set of data to step-through in the debugger.

Without any code theres little else that anyone here can do [smile]

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
danong
danong
Thank you jolly. :)
But i still stuck into this, think perhaps present codes is better. :/

void cTerrain::Initialize(){

//assign size
m_Size = 128;
//image byte per pixel
int byteperpixel = 1;

//load heightmap
FILE *fp;

//allocate memory , load heightmap
unsigned char * m_hmap = new unsigned char [ m_Size * m_Size ];

//clear memory
memset(m_hmap, 0, sizeof(unsigned char) * m_Size * m_Size );

//open heightmap , read from binary
fp = fopen("nintendo.raw","rb");
if(fp == NULL){
MessageBox(NULL,"Failed to Load HeightMap!", "Error", MB_OK);
PostQuitMessage(0);
}

//read data into m_hmap
fread(m_hmap,1 , m_Size * m_Size * byteperpixel, fp);

//dump fp
fclose(fp);

//assign number of vertices
m_NumVertices = m_Size * m_Size;

//allocate memory to vertex buffer and clear memory

//create vertex buffer
createVertexBuffer();

//lock vertex buffer and copy buffer memory
VOID * pVertices;
if( FAILED( m_pVertexBuffer->Lock( 0, 0, (BYTE **) & pVertices, 0) ) )
PostQuitMessage(0);

//create index buffer
createIndexBuffer();

//prepare to copy indices into index buffer
VOID * pIndex;

//lock the index buffer
if(FAILED(m_pIndexBuffer->Lock(0, sizeof(g_Indices), (BYTE **) &pIndex, 0)))
PostQuitMessage(0);

//calculate height
calculateHeight(m_hmap);

//copy indices into the buffer
//unlock the index buffer
memcpy( pIndex, g_Indices, sizeof(g_Indices) );
m_pIndexBuffer->Unlock();

//copy into vertex buffer
//unlock the vertex buffer
memcpy( pVertices, g_Vertices, sizeof(g_Vertices));
m_pVertexBuffer->Unlock();

//dump heightmap as no longer in need
if(m_hmap){
delete m_hmap;
m_hmap = NULL;
}
};
//end of initialize


//calculate height
void cTerrain::calculateHeight(unsigned char *pmap){

int curvertex = 0; //point at first vertex
for(int m = 0; m != m_Size; m++){
for(int n = 0; n != m_Size; n++){
//get current vertex
curvertex = m * m_Size + n;

g_Vertices[curvertex].x = (m - m_Size / 2);
g_Vertices[curvertex].y = (float)pmap[curvertex] / 5.0f;
g_Vertices[curvertex].z = (n - m_Size / 2);

//set color
g_Vertices[curvertex].color = D3DCOLOR_XRGB((int)g_Vertices[curvertex].y * 5, (int)g_Vertices[curvertex].y * 5, (int)g_Vertices[curvertex].y * 5);

//set texture coordinate ( don't need in this stage )
g_Vertices[curvertex].u = 0;
g_Vertices[curvertex].v = 0;

}
}

//assign indices
int add_index = 0;
for(int i = 0; i != m_Size - 1; i++){
for(int j = 0; j != m_Size - 1; j++){
//get current vertex
curvertex = i * m_Size + j;

//save index
g_Indices[add_index++] = curvertex;
g_Indices[add_index++] = curvertex + 1;
g_Indices[add_index++] = curvertex + m_Size;

g_Indices[add_index++] = curvertex + m_Size + 1;
g_Indices[add_index++] = curvertex + m_Size;
g_Indices[add_index++] = curvertex + 1;
}
}

};

//end of calculate height


//here's the render function
void cTerrain::render(){

D3DXMATRIX matWorld, matRotY, matRotZ;

D3DXMatrixIdentity(&matWorld);
D3DXMatrixIdentity(&matRotY);
D3DXMatrixRotationY(&matRotY, timeGetTime()/1500.0f);
D3DXMatrixMultiply(&matWorld,&matWorld,&matRotY);

//transform to world
(*m_pDevice)->SetTransform(D3DTS_WORLD, &matWorld);


//render terrain
(*m_pDevice)->SetStreamSource(0, m_pVertexBuffer, sizeof(CUSTOMVERTEX));

(*m_pDevice)->SetIndices(m_pIndexBuffer, 0);

(*m_pDevice)->SetVertexShader( D3DFVF_CUSTOMVERTEX );

//draw indices
(*m_pDevice)->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, m_NumVertices, 0, sizeof(g_Indices) / 6);

};

//end of render

Technical ..? then Master Non-Technical first ...
danong
danong
OH BRAVO!
Nice buddy!! >=)
I got it work right now.
Okay , i actually put a header that's because it told me to do so in the first time. silly of me , thanks anyway =)
Technical ..? then Master Non-Technical first ...
danong
danong
OH BRAVO!
Nice buddy!! >=)
I got it work right now.
Okay , i actually put a header that's because it told me to do so in the first time. silly of me , thanks anyway =)
Technical ..? then Master Non-Technical first ...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.