Sign in to follow this  
nlraley

Trying to LoadTextureFrom file into Quad

Recommended Posts

I've been going through the DirectX Tutorials and have made it to the Texture part, which is what I have been attempting to accomplish and am having some troubles.

My devices have been initialized and I have been able to draw both a colored triangle as well as a square. However, trying to follow the tutorial to get a texture ( a 256x256 PNG ) has thus far resulted in failure. I see the square, which is rendering as a darker blue than the background; however, no image is being loaded.

I am trying to load this into a 2d space, so I'm not needing the 3d environment as the tutorial uses, which may be causing some of my issues. Right now I'm just trying to create the texture, HRESULT is passing this stage, and then send that into the vertex buffer.

Here is what I have so far, maybe someone can point me in the right direction now:
I call this function to finish setting up my Device after it has been created:
	//-------------------
D3DXMATRIX matOrtho;
D3DXMATRIX matIdentity;
//------------------- Setup rendering states
if (FAILED(d3d.hr = d3d.d3d9Device->SetRenderState(D3DRS_LIGHTING, FALSE))) return d3d.hr;
if (FAILED(d3d.hr = d3d.d3d9Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE))) return d3d.hr;
if (FAILED(d3d.hr = d3d.d3d9Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA))) return d3d.hr;
if (FAILED(d3d.hr = d3d.d3d9Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA))) return d3d.hr;
if (FAILED(d3d.hr = d3d.d3d9Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE))) return d3d.hr;
//------------------- Setup orthographic projection matrix
D3DXMatrixOrthoLH (&matOrtho, 256, 256, 1.0f, 10.0f);
D3DXMatrixIdentity (&matIdentity);
if (FAILED(d3d.hr = d3d.d3d9Device->SetTransform (D3DTS_PROJECTION, &matOrtho))) return d3d.hr;
if (FAILED(d3d.hr = d3d.d3d9Device->SetTransform (D3DTS_WORLD, &matIdentity))) return d3d.hr;
if (FAILED(d3d.hr = d3d.d3d9Device->SetTransform (D3DTS_VIEW, &matIdentity))) return d3d.hr;
//-------------------


This is followed by a InitGeometry call which creates my texture, and my quad:
//-------------------
VOID* pVertices;
RECT tRect;
LPCSTR file;
D3DXIMAGE_INFO SrcInfo;
//-------------------
file = "G:\\MGMapsCache\\GoogleMap\\18\\108341\\61585.png";
//-------------------
if (FAILED( d3d.hr = D3DXCreateTextureFromFile(
d3d.d3d9Device,
file,
&d3d.d3dTexture
)))
return E_FAIL;
//-------------------
Vertex vertices[] = // x, y, z, rhw, tu, ty, color
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0, 0, 0xffff0000 }, // Top Left
{ 256.0f, 0.0f, 0.5f, 1.0f, 1, 0, 0xff00ffff }, // Top Right
{ 0.0f, 256.0f, 0.5f, 1.0f, 0, 1, 0xff00ffff }, // Bottom Left
{ 256.0f, 256.0f, 0.5f, 1.0f, 1, 1, 0xff00ff00 } // Bottom Right
};


//-------------------
// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
//-------------------
if( FAILED( d3d.d3d9Device->CreateVertexBuffer(
4 * sizeof( Vertex ), // Length
0, // Usage -
D3DFVF_TLVERTEX, // Specifies Vertex Format
D3DPOOL_DEFAULT, // D3DPool
&d3d.d3d9VertexBuffer, // Pointer to the Direct3D Vertex Buffer
NULL // HANDLE
) ) )
{
//-------------------
return E_FAIL;
//-------------------
}
//-------------------
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
//-------------------
if( FAILED( d3d.d3d9VertexBuffer->Lock( 0, sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof( vertices ) );
d3d.d3d9VertexBuffer->Unlock();
//-------------------
return S_OK;


After this is done I now have a texture loaded and my quad set up. The coordinates for the position of the triangles are set at:
Top Left: 0, 0, 0.5, 1, 1, 0, 0
Top Right: 256, 0.5, 0, 1, 1, 0
Bottom Left: 0, 256, 0.5, 1, 0, 1
Bottom Right: 256, 256, 0.5, 1, 1, 1

Which should be setting up the appropriate X,Y,Z coordinates, an hr of 1 which is what the original tutorial set for the triangle, and then a tu and ty with respect to where they should be located in the window.

Now I Clear the Device and call:
d3d.d3d9Device->SetStreamSource( 0, d3d.d3d9VertexBuffer, 0, sizeof( Vertex ) );
d3d.d3d9Device->SetTexture(0, d3d.d3dTexture);
d3d.d3d9Device->SetFVF( D3DFVF_TLVERTEX );
d3d.d3d9Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
//------------------- End the Scene
d3d.d3d9Device->EndScene();


Then I call Present. However, like I said, the texture doesn't load into the quad. What am I doing wrong here?

Share this post


Link to post
Share on other sites
Been playing with it all afternoon, and for the better part of last week, and still can't figure out what's wrong.

If anyone has any advice I'd greatly appreciate it, if what I've provided is not enough information then let me know what else you need to know, or if your willing to take a look at my source code I've zipped it up here:
http://www.filefactory.com/file/b3fcc39/n/Direct3D_2.rar

Share this post


Link to post
Share on other sites
I've tried loading a sprite now instead of drawing the texture to the quad.

I have this:
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
d3d.Cleanup();
PostQuitMessage( 0 );
return 0;

case WM_PAINT:
d3d.Render();
ValidateRect( hWnd, NULL );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}


Which calls Render on WM_PAINT. Now in my Render I have:
void Direct3D::Render()
{
//-------------------
if ( NULL == d3d.d3d9Device)
return;
//-------------------
d3d.d3d9Device->Clear(
0, // Number of Rectangles in Array - Set to 0 since Rects is NULL
NULL, // Pointer to an Array of D3DRect to Clear - NULL indicates entire viewport
D3DCLEAR_TARGET, // Surface to Clear
D3DCOLOR_XRGB( 0, 0, 0 ), // Clear to this Color
1.0f, // Depth Buffer to Clear to
0 // Stencil Buffer to Clear to
);
//-------------------
if ( SUCCEEDED( d3d.d3d9Device->BeginScene() ) )
{
//-------------------
// Scene Rendering takes place Here
//-------------------
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
//-------------------
D3DCOLOR vertexColour = 0xFFFFFFFF;
d3d.d3d9Sprite->Begin(D3DFILL_SOLID);
d3d.d3d9Sprite->Draw(
d3d.d3dTexture,
NULL,
new D3DXVECTOR3(128.0f, 128.0f, 0.5f),
NULL,
vertexColour
);
d3d.d3d9Sprite->End();

/*
d3d.d3d9Device->SetStreamSource( 0, d3d.d3d9VertexBuffer, 0, sizeof( Vertex ) );
d3d.d3d9Device->SetTexture(0, d3d.d3dTexture);
d3d.d3d9Device->SetFVF( D3DFVF_TLVERTEX );
d3d.d3d9Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); */
//------------------- End the Scene
d3d.d3d9Device->EndScene();
}
//------------------- Present the Backbuffer contents to the display
d3d.d3d9Device->Present( NULL, NULL, NULL, NULL);
//-------------------
}
//-------------------


However, once I call d3d.d3d9Sprite->Begin it enters back into Render again leaving me constantly looping into Render. Why is this so?

Share this post


Link to post
Share on other sites
I figured it out finally. What I did instead of trying to draw the texture into the quad, I created a Sprite, initialized it, and then created a texture from file and called:
//-------------------
D3DCOLOR vertexColour = 0xFFFFFFFF;
//-------------------
if ( NULL == d3d.d3d9Device)
return;
//-------------------
d3d.d3d9Device->Clear(
0, // Number of Rectangles in Array - Set to 0 since Rects is NULL
NULL, // Pointer to an Array of D3DRect to Clear - NULL indicates entire viewport
D3DCLEAR_TARGET, // Surface to Clear
D3DCOLOR_XRGB( 0, 0, 0 ), // Clear to this Color
1.0f, // Depth Buffer to Clear to
0 // Stencil Buffer to Clear to
);
//-------------------
if ( SUCCEEDED( d3d.hr = d3d.d3d9Device->BeginScene() ) )
{
//-------------------
// Scene Rendering takes place Here
//-------------------
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
//-------------------

d3d.d3d9Sprite->Begin(NULL);
d3d.d3d9Sprite->Draw(
d3d.d3dTexture,
NULL,
new D3DXVECTOR3(0.0f, 0.0f, 0.0f),
new D3DXVECTOR3(0.0f, 0.0f, 0.0f),
D3DCOLOR_ARGB(127, 255, 255, 255)
);
d3d.d3d9Sprite->Draw(
d3d.d3dTexture2,
NULL,
new D3DXVECTOR3(0.0f, 0.0f, 0.0f),
new D3DXVECTOR3(256.0f, 0.0f, 0.0f),
D3DCOLOR_ARGB(127, 255, 255, 255)
);
d3d.d3d9Sprite->End();

/*
d3d.d3d9Device->SetStreamSource( 0, d3d.d3d9VertexBuffer, 0, sizeof( Vertex ) );
d3d.d3d9Device->SetTexture(0, d3d.d3dTexture);
d3d.d3d9Device->SetFVF( D3DFVF_TLVERTEX );
d3d.d3d9Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); */
//------------------- End the Scene
d3d.d3d9Device->EndScene();
}
else
return;
//------------------- Present the Backbuffer contents to the display
d3d.d3d9Device->Present( NULL, NULL, NULL, NULL);
//-------------------

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this