Jump to content

  • Log In with Google      Sign In   
  • Create Account

Terrain texture problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 30 August 2012 - 09:25 AM

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 ?

Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2508

Like
0Likes
Like

Posted 30 August 2012 - 10:41 AM

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!

#3 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 30 August 2012 - 11:17 AM

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 ?

#4 kauna   Crossbones+   -  Reputation: 2508

Like
1Likes
Like

Posted 30 August 2012 - 11:27 AM

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!

Edited by kauna, 30 August 2012 - 11:28 AM.


#5 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 30 August 2012 - 11:38 AM

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 :/

#6 kauna   Crossbones+   -  Reputation: 2508

Like
1Likes
Like

Posted 30 August 2012 - 11:53 AM

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.

#7 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 30 August 2012 - 12:11 PM

ok now result is : http://prntscr.com/eom4h

its looks like weird

#8 kauna   Crossbones+   -  Reputation: 2508

Like
0Likes
Like

Posted 30 August 2012 - 12:12 PM

Check your scale variable type.

The correct type is given in my earlier post.

Cheers!

#9 lyzerk   Members   -  Reputation: 120

Like
1Likes
Like

Posted 30 August 2012 - 12:17 PM

Check your scale variable type.

The correct type is given in my earlier post.

Cheers!


yes it was my mistake, thanks so much Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS