• Advertisement
Sign in to follow this  

Setting Texture

This topic is 2618 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

I was trying to set texture to a Box, I got the texture to apply but i end up lines of color.


void init_light(pD3DdeviceX d3ddev)
{
D3DLIGHT9 light; // create the light struct
D3DMATERIAL9 material; // create the material struct

ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light'
light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // set the light's color
light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);

d3ddev->SetLight(0, &light); // send the light struct properties to light #0
d3ddev->LightEnable(0, TRUE); // turn on light #0

ZeroMemory(&material, sizeof(D3DMATERIAL9)); // clear out the struct for use
material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set diffuse color to white
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set ambient color to white

d3ddev->SetMaterial(&material); // set the globably-used material to &material
}

struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL)

LPDIRECT3DTEXTURE9 MenuTexture;
static BOOL Create = TRUE;

BYTE Menu[] = { Image Bytes };

LPDIRECT3DVERTEXBUFFER9 v_buffer;
LPDIRECT3DINDEXBUFFER9 i_buffer;
void init_graphics(pD3DdeviceX pDevice)
{

D3DXCreateTextureFromFileInMemory(pDevice,&Menu,sizeof(Menu),&MenuTexture);

// create three vertices using the CUSTOMVERTEX struct built earlier
CUSTOMVERTEX vertices[] =
{
{ -3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, }, // side 1
{ 3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, },
{ -3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, },
{ 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, },

{ -3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, }, // side 2
{ -3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, },
{ 3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, },
{ 3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, },

{ -3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, }, // side 3
{ -3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, },

{ -3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, }, // side 4
{ 3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, },
{ -3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, },
{ 3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, },

{ 3.0f, -3.0f, -3.0f, 1.0f, 0.0f, 0.0f, }, // side 5
{ 3.0f, 3.0f, -3.0f, 1.0f, 0.0f, 0.0f, },
{ 3.0f, -3.0f, 3.0f, 1.0f, 0.0f, 0.0f, },
{ 3.0f, 3.0f, 3.0f, 1.0f, 0.0f, 0.0f, },

{ -3.0f, -3.0f, -3.0f, -1.0f, 0.0f, 0.0f, }, // side 6
{ -3.0f, -3.0f, 3.0f, -1.0f, 0.0f, 0.0f, },
{ -3.0f, 3.0f, -3.0f, -1.0f, 0.0f, 0.0f, },
{ -3.0f, 3.0f, 3.0f, -1.0f, 0.0f, 0.0f, },
};


// create the vertex and store the pointer into v_buffer, which is created globally
pDevice->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid; // the void pointer

v_buffer->Lock(0, 0, (void**)&pVoid, 0); // lock the vertex buffer
memcpy(pVoid, vertices, sizeof(vertices)); // copy the vertices to the locked buffer
v_buffer->Unlock(); // unlock the vertex buffer

// create the indices using an int array
short indices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};

// create an index buffer interface called i_buffer
pDevice->CreateIndexBuffer(36*sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);

// lock i_buffer and load the indices into it
i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
}

void render_frame(pD3DdeviceX d3ddev)
{
// d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);


// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);

D3DXMATRIX matRotateY; // a matrix to store the rotation information

static float index = 0.0f; index+=0.02f; // an ever-increasing float value

// build a matrix to rotate the model based on the increasing float value
D3DXMatrixRotationY(&matRotateY, index);

// tell Direct3D about our matrix
d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);

D3DXMATRIX matView; // the view transform matrix

D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, 10.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction

d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

D3DXMATRIX matProjection; // the projection transform matrix

D3DVIEWPORT9 viewP;
d3ddev->GetViewport( &viewP );//Screens Width and height

D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)viewP.Width / (FLOAT)viewP.Height, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane

d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

d3ddev->SetTexture( 0, MenuTexture );
d3ddev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
d3ddev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
d3ddev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
d3ddev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );


// select the vertex and index buffers to use
d3ddev->SetStreamSource( 0, v_buffer, 0, sizeof( CUSTOMVERTEX ) );
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetIndices(i_buffer);

// draw the cube
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);



}

i kno i code a little messy.
this is how it ended up

http://img651.imageshack.us/img651/2553/abox.png

Share this post


Link to post
Share on other sites
Advertisement
K I didnt think if i put UV cord in google i will not get UV protection and stuff ( there should be a DirectX SDK for kids I should not have to learn this and still stay in skool)

Share this post


Link to post
Share on other sites
Each vertex in a model has a position in 3D space, X, Y and Z as you have it now. Also they can optionally have a normal X, Y, and Z as you have it now.

To apply a texture to a polygon, you will also need UV coordinates per vertex that specify how you want the texture to appear.

So you need to change your vertex structure to include 2 additional floats U and V.

UV coordinates generally range between 0.0f and 1.0f (generally speaking for now).

Where 0.0f is the left most pixel for U and 1.0f is the right most pixel for U.

So to map the entire texture to a quad, top left vertex UV 0.0f, 0.0f and the top right vertex UV 1.0f 0.0f. Bottom right UV 0.0f 1.0f and bottom right 1.0f, 1.0f

Does that help ?

Share this post


Link to post
Share on other sites
Looks like you have removed the normal though which will mean it won't get lit correctly.

Don't you want...

#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

Share this post


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

  • Advertisement