Sign in to follow this  
deathwearer

Trouble to texture a square

Recommended Posts

I'm curently learning how to texture but I'm having problem. The code compile but the texture just doens't appear. And i can't find why. Here is the source.
#include <windows.h>
#include <d3d9.h>
#include <d3dx9tex.h>

// global variables
HINSTANCE hInst;
HWND wndHandle;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DVERTEXBUFFER9 vBuffer = NULL;
LPDIRECT3DTEXTURE9 WallTexture = NULL;

D3DXMATRIX matView;					// the view matrix
D3DXMATRIX matProj;					// the projection matrix
D3DXVECTOR3 cameraPosition;			// the position of the camera
D3DXVECTOR3 cameraLook;				// where the camera is pointing

struct CUSTOMVERTEX
{
	FLOAT x, y, z;
	FLOAT u, v;
};

// forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool initDirect3D();
void Clear_Screen();
void render();
HRESULT Setup_VB();
HRESULT Lock_VBuffer(LPDIRECT3DVERTEXBUFFER9 vBuffer);
void Unlock_VBuffer(LPDIRECT3DVERTEXBUFFER9 vBufferx);
void createCamera(float nearClip, float farClip);
void moveCamera(D3DXVECTOR3 vec);
void pointCamera(D3DXVECTOR3 vec);
void Load_Texture();

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)

int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance))
		return false;
	
	if (!initDirect3D())
		return false;
	Setup_VB();
	Load_Texture();

	createCamera(1.0f, 500.0f);		// near clip plane, far clip plane
	moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f));
	pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
		

	// Main message loop:
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		else
		{
			render();
		}
    }
		
	return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	// create the window
	wndHandle = CreateWindow("DirectXExample", 
							 "DirectXExample", 
							 WS_OVERLAPPEDWINDOW,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

bool initDirect3D()
{
	pD3D = NULL;
	pd3dDevice = NULL;

	if ( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		return false;

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	d3dpp.BackBufferWidth = 640;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount  = 1;
	d3dpp.MultiSampleType  = D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality = 0;
	d3dpp.SwapEffect         = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow      = wndHandle;
	d3dpp.Windowed			 = true;
	

	pD3D->CreateDevice( D3DADAPTER_DEFAULT,
						D3DDEVTYPE_HAL,
						wndHandle,
						D3DCREATE_SOFTWARE_VERTEXPROCESSING,
						&d3dpp,
						&pd3dDevice );

	pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
	pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	
	return true;
}

void Clear_Screen()
{
	if ( NULL != pd3dDevice )
	{
		pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0);
	}
}

void render()
{
	Clear_Screen();
	pd3dDevice->BeginScene();
	pd3dDevice->SetTexture( 0, WallTexture );
	pd3dDevice->SetStreamSource( 0, vBuffer, 0, sizeof(CUSTOMVERTEX) );
	pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
	pd3dDevice->EndScene();
	pd3dDevice->Present( NULL, NULL, NULL, NULL);
	
}

HRESULT Setup_VB()
{
	HRESULT hr;

	CUSTOMVERTEX g_Vertices[]=
	{
		{100.0f, 100.0f, 0.5f, 0.0f, 0.0f },
		{200.0f, 100.0f, 0.5f, 1.0f, 0.0f },
		{100.0f, 200.0f, 0.5f, 0.0f, 1.0f },
		{200.0f, 200.0f, 0.5f, 1.0f, 1.0f },

     };
	
	hr = pd3dDevice->CreateVertexBuffer( sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),
									0,
									D3DFVF_CUSTOMVERTEX,
									D3DPOOL_DEFAULT,
									&vBuffer,
									NULL );
	
	VOID* pVertices;

	vBuffer->Lock( 0, 0, ( void** ) &pVertices, 0 );

	memcpy(pVertices, g_Vertices, sizeof(g_Vertices) );

	vBuffer->Unlock();

	return S_OK;
}

void createCamera(float nearClip, float farClip)
{
	//Here we specify the field of view, aspect ration and near and far clipping planes.
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

}

/*************************************************************************
* moveCamera
* moves the camera to a position specified by the vector passed as a 
* parameter
*************************************************************************/
void moveCamera(D3DXVECTOR3 vec)
{
	cameraPosition = vec;
}

/*************************************************************************
* pointCamera
* points the camera a location specified by the passed vector
*************************************************************************/
void pointCamera(D3DXVECTOR3 vec)
{
	cameraLook = vec;

	D3DXMatrixLookAtLH(&matView, &cameraPosition,		//Camera Position
                                 &cameraLook,		//Look At Position
                                 &D3DXVECTOR3(0.0f, 1.0f, 0.0f));		//Up Direction

	pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
}

void Load_Texture()
{
	D3DXCreateTextureFromFile( pd3dDevice, "textures/wall.jpg", &WallTexture );
}

Share this post


Link to post
Share on other sites
is it possible that your texture is not successfully being loaded? I noticed you didn't have any error checking in your texture loader and that could be your problem. If the texture is not loaded correctly directx will not error out of your program, it just wont use the texture. Instead of what you have in Load_Texture() try



BOOL Load_Texture(void)
{
if(FAILED(D3DXCreateTextureFromFile( pd3dDevice, "textures/wall.jpg", &WallTexture ))
return FALSE;

return TRUE;
}





This is particularly problematic if you're using Visual Studio since it has issues with paths that haven't been explicitely defined. I wont go into the specifics, but give that a try... and when you call Load_Texture make sure you throw an error (or just quit) or something to let you know the texture wasn't loaded.

Share this post


Link to post
Share on other sites
My Fill mode is now set to SOLID and still doens't show anything. now I'm completly lost, i just followed everthing in the book ( and also from the source which is working )

Share this post


Link to post
Share on other sites
Oh updates, when i change that line

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

to

pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 4);

Which will now try to draw 4 triangles, it now show something... which is not really normal since i wana draw a square and i only have 2 triangle to draw.

Here is what i get not, there is obviously something wrong I'm doing, but what :S

http://img395.imageshack.us/img395/8483/bugtexture1rv.jpg

Share this post


Link to post
Share on other sites
I notice that you're defining your vertices counter-clockwise, while DirectX dafaults to culling triangles that wind counter-clockwise. This might be the problem. Try
pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

Share this post


Link to post
Share on other sites
Hi there DeathWearer,
How are you doing?

The Problem
Defining your vertices in a clockwise order.

The Solution
(1)|------|(3)
|\ |
| \ |
| \ |
(0)|------|(2)

0) (-1.0f, 0.0f, 0.0f)
1) (-1.0f, 2.0f, 0.0f)
2) ( 1.0f, 0.0f, 0.0f)
3) ( 1.0f, 2.0f, 0.0f)

I hope this helps. This is specifying your vertices in a clockwise order.

Take care bud.

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