Texture mapping over an array of vertices?

Started by
12 comments, last by exextatic 12 years, 7 months ago
Hi all, first post here :D

Slight problem trying to map a texture over an array of vertices. My vertices shape a terrain but I cant get the texture to map over them.
My code:
LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;
OURCUSTOMVERTEX cv_Vertices[WIDTH * HEIGHT];
float flt_HeightData[WIDTH][HEIGHT];

for(int i = 0; i < WIDTH; i++)
{
for(int j = 0; j < HEIGHT; j++)
{
flt_HeightData[j] = noise->smoothNoise(i, j);
}
}


float temp1 = 0, temp2 = 0;
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
cv_Vertices[y * WIDTH + x].x = -x;
cv_Vertices[y * WIDTH + x].z = y;
cv_Vertices[y * WIDTH + x].y = flt_HeightData[x][y];
cv_Vertices[y * WIDTH + x].color = 0xffffffff;
cv_Vertices[y * WIDTH + x].U = temp1;
cv_Vertices[y * WIDTH + x].V = temp2;
temp2 += (1.0f/WIDTH);
}
temp1 += (1.0f/HEIGHT);
}

if (FAILED(d3ddev->CreateVertexBuffer(WIDTH * HEIGHT * sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))
{
MessageBox(hWnd, "Error while creating VertexBuffer", "FillVertices()", MB_OK);
}


VOID* p_Vertices;
if (FAILED(p_dx_VertexBuffer->Lock(0, WIDTH * HEIGHT * sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0)))
{
MessageBox(hWnd,"Error trying to lock", "FillVertices()", MB_OK);
}
else
{
memcpy(p_Vertices, cv_Vertices, WIDTH*HEIGHT*sizeof(OURCUSTOMVERTEX));
p_dx_VertexBuffer->Unlock();
}

That code is from the function that initialises my terrain. noise is my Perlin Noise class.
struct OURCUSTOMVERTEX
{
float x,y,z;
DWORD color;
FLOAT U, V;
};

Is the vertex struct.

Any ideas?
Thanks

James Warner
Advertisement
One thing that looks suspicious is this:


int temp1 = 0, temp2 = 0;

for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
//...
temp2 += (1/WIDTH);
}
temp1 += (1/HEIGHT);
}


You are probably not storing the values in temp that you expect. Try using float variables for that and (1.f/WIDTH).

One thing that looks suspicious is this:


int temp1 = 0, temp2 = 0;

for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
//...
temp2 += (1/WIDTH);
}
temp1 += (1/HEIGHT);
}


You are probably not storing the values in temp that you expect. Try using float variables for that and (1.f/WIDTH).

Using your code I got:
help_1.jpg
This is what I got before
By the way, the texture Im using is:
texture.jpg
Hi OCN.FiX.

If the texture is not loading, be sure the texture file is within the folder or specified place, another thing is to change FVF declaration in the line if (FAILED(d3ddev->CreateVertexBuffer(WIDTH * HEIGHT * sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL))) from
D3DFVF_XYZ|D3DFVF_DIFFUSE to D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1

Hi OCN.FiX.

If the texture is not loading, be sure the texture file is within the folder or specified place, another thing is to change FVF declaration in the line if (FAILED(d3ddev->CreateVertexBuffer(WIDTH * HEIGHT * sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL))) from
D3DFVF_XYZ|D3DFVF_DIFFUSE to D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1

Yes, Im sure the texture is loading. I changed what you said but it didnt do anything.
Here is the code in my render function if it helps:
// Render my terrain:
d3ddev->SetStreamSource(0, tVertexBuffer, 0, sizeof(OURCUSTOMVERTEX));
d3ddev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
d3ddev->SetIndices(tIndexBuffer);
d3ddev->SetTexture(0, texture);
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, WIDTH * HEIGHT, 0, (WIDTH - 1) * (HEIGHT - 1) * 2);
Sort of fixed, I changed int temp1.. ,temp2 to float temp1.. temp2 and weve got the texture stretched horizontally across it :D
I found the line i specified as suspicious. I wanted you to try on that line. I dont feel anything wrong in Render part.:)

I found the line i specified as suspicious. I wanted you to try on that line. I dont feel anything wrong in Render part.:)


Thanks :) Just a little more tweaking now to get it to stretch properly :P
help_2.JPG
New problem :P Any ideas?
Are you using float for both temp1 and temp2 and [color=#1C2837][size=2](1.f/WIDTH) and [color=#1C2837][size=2](1.f/HEIGHT) as I suggested?

This topic is closed to new replies.

Advertisement