Jump to content
  • Advertisement
Sign in to follow this  
exextatic

Texture mapping over an array of vertices?

This topic is 2528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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);

Share this post


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

Share this post


Link to post

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!