Archived

This topic is now archived and is closed to further replies.

shakazed

Texture won´t show

Recommended Posts

I´m working on a Asteroids game and I want to use "background.bmp" as background texture. The problem is that it won´t show- I´ve run debug and it shows nothing. This is the code I use.
  

////////////////////////////////////////////////////////////////////////////////////////////

//DirectX Graphics Helper Functions//////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////

#include<d3d8.h>
#include<d3dx8.h>
#include<windows.h>
#include"D3D.h"

#define MY_FVF (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)


IDirect3D8 *iD3D8 = NULL;
IDirect3DDevice8 *iD3DDevice8 = NULL;
IDirect3DVertexBuffer8 *iD3DVertexBuffer8 =NULL;
IDirect3DTexture8 *iD3DTexture8 = NULL;

//Vertex structure 

struct CUSTOMVERTEX 
{
	float x,y,z;
	float tU, tV; //Texture coordinates

	D3DCOLOR diffuse;
};


bool D3DInitialize(HWND hWnd)
{

	iD3D8 = Direct3DCreate8(D3D_SDK_VERSION);

	D3DDISPLAYMODE d3ddm;
	
	if(FAILED(iD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
		return false;

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.Windowed = true;
	d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;


	if(FAILED(iD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &iD3DDevice8)))
		return false;
	
	// Turn off culling, so we see the front and back of the triangle

    iD3DDevice8->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting, since we are providing our own vertex colors

    iD3DDevice8->SetRenderState( D3DRS_LIGHTING, FALSE );

	 // Turn on the zbuffer

    iD3DDevice8->SetRenderState( D3DRS_ZENABLE, TRUE );

	if(FAILED(D3DXCreateTextureFromFile(iD3DDevice8, "Background.bmp", &iD3DTexture8)))
		return false;

	return true;

}

void D3DRender(void)
{
	iD3DDevice8->Clear(NULL,0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255),1.0f,0);

 
	D3DSetupMatrices();

	//Set the texture

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

	//Start rendering

	iD3DDevice8->BeginScene(),
	
	iD3DDevice8->SetStreamSource(0, iD3DVertexBuffer8, sizeof(CUSTOMVERTEX));
	iD3DDevice8->SetVertexShader(MY_FVF);

	//Paint two polygons using a trianglelist

	iD3DDevice8->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);

	//Stop rendering

	iD3DDevice8->EndScene();

	iD3DDevice8->Present(NULL,NULL,NULL,NULL);

}

//Release all pointers to  COM objects 

void D3DRelease(void)
{	
	if(iD3DDevice8 != NULL)
	{
		iD3DDevice8->Release();
		iD3DDevice8 = NULL;
	}

	if(iD3D8 != NULL)
	{
		iD3D8->Release();
		iD3D8 = NULL;
	}

	if(iD3DVertexBuffer8 != NULL)
	{
		iD3DVertexBuffer8->Release();
		iD3DVertexBuffer8 = NULL;
	}

	if(iD3DTexture8 != NULL)
	{
		iD3DTexture8->Release();
		iD3DTexture8  = NULL;
	}
}

void D3DSetupMatrices(void)
{

	//D3DXMATRIX matWorld;

	//Identity

	//iD3DDevice8->SetTransform(D3DTS_WORLD, &matWorld);


	//Set up the viev

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, -50.0f),
		&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
		&D3DXVECTOR3(0.0f, 1.0f,0.0f));
	iD3DDevice8->SetTransform(D3DTS_VIEW, &matView);

	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
	iD3DDevice8->SetTransform(D3DTS_PROJECTION, &matProj);
	
}

//Make a rectangle sending the apporpriate coordinates in a vector

bool D3DMakeRectangle(void)
{
	CUSTOMVERTEX sVertex[] = {
		{-25.0f,-25.0f,0.0f,0.0f, 1.0f, D3DCOLOR_XRGB(255,0,0),},
		{-25.0f,25.0f,0.0f,0.0f,0.0f, D3DCOLOR_XRGB(0,255,0),},
		{25.0f,-25.0f,0.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255,0,0),},
		{25.0f,25.0f,0.0f, 1.0f,0.0f, D3DCOLOR_XRGB(0,255,0),},
		};

	if(FAILED(iD3DDevice8->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, MY_FVF,
		D3DPOOL_DEFAULT, &iD3DVertexBuffer8)))
		return false;
	
	VOID *pVertices;

	if(FAILED(iD3DVertexBuffer8->Lock(0, sizeof(sVertex), (BYTE**)&pVertices, 0)))
		return false;

	memcpy( pVertices, sVertex, sizeof(sVertex) );

	if(FAILED(iD3DVertexBuffer8->Unlock()))
		return false;

	pVertices = NULL; //Let go of the pointer

	



	return 0;
}


[source]

Now I knwo there´s a stupid error that I´ve missed somewhere in there, but today my brain wont recognize it. Any help appreciated.  

http://www.shakazed.tk

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t see an index buffer, which would allow you only to define four points, but since this is lacking I suggest you look into creating two more points in your vertex buffer to define your square.


-David

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your custom vertex format should have the texture coordinates as the last members of the structure. Instead, you have them located between the position and diffuse information.

The best way to find stupid errors is to at least add error checking for each call you make into the D3D8 API, and better still install the debug D3D8 runtime and look for error messages in the debugger.

Share this post


Link to post
Share on other sites
Your custom vertex format should have the texture coordinates as the last members of the structure. Instead, you have them located between the position and diffuse information.

The best way to find stupid errors is to at least add error checking for each call you make into the D3D8 API, and better still install the debug D3D8 runtime and look for error messages in the debugger.

Share this post


Link to post
Share on other sites