Terrain texture problem

Started by
7 comments, last by lyzerk 11 years, 7 months ago
Hello,

I have problem with terrain texture. there is my codes;

[source lang="cpp"]void gameTerrain::SetIndexBuffer(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
short s_Indices[(WIDTH-1)*(HEIGHT-1)*6];


for (int x=0;x< WIDTH-1;x++) {

for (int y=0; y< HEIGHT-1;y++) {
s_Indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH;
s_Indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH;
s_Indices[(x+y*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH;

s_Indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH;
s_Indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH;
s_Indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH;

}
}

if (FAILED(g_pd3dDevice->CreateIndexBuffer((WIDTH-1)*(HEIGHT-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&Indexs,NULL)))
{
MessageBox(NULL,L"Error while creating IndexBuffer",L"FillIndices()",MB_OK);
}

VOID* p_Indices;
if (FAILED(Indexs->Lock(0, (WIDTH-1)*(HEIGHT-1)*6*sizeof(short), (void**)&p_Indices, 0)))
{
MessageBox(NULL,L"Error trying to lock",L"FillIndices()",MB_OK);
}else{
memcpy(p_Indices, s_Indices, (WIDTH-1)*(HEIGHT-1)*6*sizeof(short));
Indexs->Unlock();
}


D3DXCreateTextureFromFile(g_pd3dDevice,

L"map.jpg",

&g_texture);
}

void gameTerrain::SetVertex(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
for (int x=0; x < WIDTH;x++)
{
for (int y=0; y < HEIGHT;y++)
{
Vertex[y * WIDTH + x].x = -x;
Vertex[y * WIDTH + x].y = y;
Vertex[y * WIDTH + x].z = HeightData[x][y];
Vertex[y * WIDTH + x].color = 0xffffffff;
}
}

if (FAILED(g_pd3dDevice->CreateVertexBuffer(WIDTH*HEIGHT*sizeof(VertexType), D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_TEX1, D3DPOOL_DEFAULT, &VertexBuffer, NULL)))
{
MessageBox(NULL,L"Error while creating VertexBuffer",L"FillVertices()",MB_OK);
}

VOID* p_Vertices;
if (FAILED(VertexBuffer->Lock(0, WIDTH*HEIGHT*sizeof(VertexType), (void**)&p_Vertices, 0)))
{
MessageBox(NULL,L"Error trying to lock",L"FillVertices()",MB_OK);
}
else
{
memcpy(p_Vertices, Vertex, WIDTH*HEIGHT*sizeof(VertexType));
VertexBuffer->Unlock();
}

}

void gameTerrain::Render(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, 0, 0, 0);
D3DXMatrixRotationX(&matWorld, 90);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

g_pd3dDevice->SetTexture(0, g_texture);
g_pd3dDevice->SetStreamSource( 0, VertexBuffer, 0, sizeof(VertexType) );
g_pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1 );


g_pd3dDevice->SetIndices(Indexs);



g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,WIDTH*HEIGHT,0,(WIDTH-1)*(HEIGHT-1)*2);

/*g_pd3dDevice->SetIndices(NULL);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, (WIDTH-1)*(HEIGHT-1)*2);*/
}[/source]

and result is ; http://prntscr.com/eobr8

its something like just taking one pixel of image. what i did wrong ?
Advertisement
Your vertex structure doesn't contain texture coordinates. As a matter of fact, your vertex structure doesn't match the vertex type declaration.

Your vertex structure is:

class VertexType
{
float x,y,z;
unsigned int color;
};

while it should be:

class VertexType
{
float x,y,z;
float u,v; // <- these members contain the texture coordinates.
};

Cheers!
ok i did it now like that

[source lang="cpp"] for (int x=0; x < WIDTH;x++)
{
for (int y=0; y < HEIGHT;y++)
{
Vertex[y * WIDTH + x].x = -x;
Vertex[y * WIDTH + x].y = y;
Vertex[y * WIDTH + x].z = HeightData[x][y];
Vertex[y * WIDTH + x].u = x;
Vertex[y * WIDTH + x].v = y;
}
}[/source]

ok its working but, its working weird like;
http://prntscr.com/eoiqy

i want to do one terrain for one texture not like that, i want paint my map in jpg/png/bmp, its possible ?
Well, you are practically repeating your texture once for each vertex. You'll need to scale your texture coordinates values by some value (I let you to find out the correct scale value) in order to have the texture repeat once over the terrain.

float ScaleX = ?;
float ScaleY = ?;

for (int x=0; x < WIDTH;x++)
{
for (int y=0; y < HEIGHT;y++)
{
Vertex[y * WIDTH + x].x = -x;
Vertex[y * WIDTH + x].y = y;
Vertex[y * WIDTH + x].z = HeightData[x][y];
Vertex[y * WIDTH + x].u = x * SCALEX;
Vertex[y * WIDTH + x].v = y * SCALEY;
}
}

cheers!
hmm its a good point but i have some problem still, if Scales bigger texture gonna small. i tried my jpg resolution and more thing, 1024,80,-1024

here is result : http://prntscr.com/eojz5

sorry for my english, its too bad :/
Well did you try to put scales smaller than 1 ?

Let me give you a hint :

ScaleX = 1.0f / (float)Width;

Changing your texture resolution isn't the key to the problem.
ok now result is : http://prntscr.com/eom4h

its looks like weird
Check your scale variable type.

The correct type is given in my earlier post.

Cheers!

Check your scale variable type.

The correct type is given in my earlier post.

Cheers!


yes it was my mistake, thanks so much smile.png

This topic is closed to new replies.

Advertisement