Help with DirectX 9 Textures

so i have a IDirect3DTexture9 that is being displayed on the screen using shaders, I want to be able to display animations by loading in a texture that has each frame of the animation in like a spreadsheet type picture, and then only displaying party of the texture at a time. is there a way to traverse through a texture and only display part of it?
Thanks in advanced.
is there a way to traverse through a texture and only display part of it?

Yes, by setting the texture coordinates appropriately.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb206245(v=vs.85).aspx

So, let's say you have one texture that stores four frames in it, one in each quadrant. Let's say the first frame is in the top-left, the second frame is in the top-right, the third frame is in the bottom-left, and the fourth frame is in the bottom-right.

The texture coordinates for the first frame would be (0, 0) through (0.5, 0.5). The second frame would be (0.5, 0) through (1, 0.5). The third frame would be (0, 0.5) through (0.5, 1). The last frame would be (0.5, 0.5) through (1, 1).
how would I go about setting that?
or how would I go about just displaying a portion of it?
I get the concept I just don't know how to actually implement it.
is there a function to call?
thanks again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//This example draws a textured quad on a black background

//Our vertex structure: a point, a color and texture coordinates
struct TEXTURED_VERTEX
{ 
    float x, y, z; // Position in 3d space
    DWORD color;
    float tu, tv; // Texture coordinates
}; 

//This is a textured quad. Each point is colored white so that the color
//shown is the true texture color. This is set up such that the whole
//texture is displayed on the quad. Note the texture coordinates
TEXTURED_VERTEX sprite_frame[] = 
{ 
    { -1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), 0.0f, 1.0f}, // Bottom left vertex 
    { 1.0f , -1.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 1.0f}, // Bottom right vertex
    { 1.0f , 1.0f , 0.0f, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0.0f}  // Top right vertex
    { -1.0f, 1.0f , 0.0f, D3DCOLOR_XRGB(255, 255, 255), 0.0f, 0.0f}  // Top left vertex 
};

//On Reset Device

//Tell DirectX about our vertex structure
D3DVERTEXELEMENT9 vertexDeclaration[] = 
{ 
    {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
    {0, 3*sizeof(float), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
    {0, 3*sizeof(float)+sizeof(DWORD), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, 
    D3DDECL_END() 
}; 

//Assuming you have a LPDIRECT3DDEVICE9 called pDevice

LPDIRECT3DVERTEXDECLARATION9 vertexDeclarationToSet = 0; 
pDevice->CreateVertexDeclaration( vertexDeclaration, &vertexDeclarationToSet ); 
pDevice->SetVertexDeclaration( vertexDeclarationToSet );

//On Render

//Assuming you've loaded your texture, set the texture stage and set
//your matrices correctly

//Clear the scene. Make a black background
pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 ); 
pDevice->BeginScene(); 

//Draw our textured quad
pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, sprite_frame, sizeof(TEXTURED_VERTEX) ); 

pDevice->EndScene(); 
pDevice->Present( 0, 0, 0, 0 );


The above example draws the whole texture onto a quad. If you want to only show part of the texture then you need to modify the texture coordinates. The texture coordinates are given as part of the vertex structure. For more efficient rendering, use vertex buffers.
Last edited on
Topic archived. No new replies allowed.