Jump to content
  • Advertisement
Sign in to follow this  
tendifo

problem with textures

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

For some reason I can't use textures in my Direct3d application. I set up my direct3d objects in the InitDX() function and I set up the textures in teh SetupTextures() function and I render the scene in the Render() function. I choose my texture in the render method by doing ddevice->SetTexture(0, tex1); but it doesn't work for some reason. Is there a render state I need to set for my device? Or maybe my vertex structure and definition is wrong? Any help would be greatly appreciated. Here's my code:
/* -------------------------------------------
   include all necessary libraries
   ------------------------------------------- */
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <math.h>

/* -------------------------------------------
   global variables
   ------------------------------------------- */
bool iswindowed = true;
HWND hwnd; // the window
bool keys[256]; // is a certain key pressed or not?

LPDIRECT3D9 d3dobj; // the d3d object

LPDIRECT3DDEVICE9 ddevice; // the d3d device
D3DPRESENT_PARAMETERS present_parameters; // the properties of the device

D3DXMATRIXA16 matWorld; // world matrix
D3DXMATRIXA16 matView; // view matrix (camera)
D3DXMATRIXA16 matProj; // projection matrix (the lens)

/* -------------------------------------------
   scene objects (textures, lights)
   ------------------------------------------- */
LPDIRECT3DTEXTURE9 tex1;
D3DMATERIAL9 mtrl;
D3DLIGHT9 light0;
D3DLIGHT9 light1;
D3DLIGHT9 light2;

/* -------------------------------------------
   vertex structure
   ------------------------------------------- */
struct LIGHTVERTEX
{
	float px, py, pz;
	float nx, ny, nz;
	float tu, tv;
};

// defenition of our vertex
#define D3DFVF_LIGHTVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

/* -------------------------------------------
   initializes direct x 9
   ------------------------------------------- */
bool InitDX() {

	// create direct3d object
	if((d3dobj = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) {
		MessageBox(NULL, "Could not create Direct3D Object", "D3D_OBJ ERR", MB_OK);
		return false;
	}

	// set up device properties
	ZeroMemory(&present_parameters, sizeof(present_parameters));
    present_parameters.Windowed = iswindowed;
    present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
    present_parameters.EnableAutoDepthStencil = true;
    present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
	if (iswindowed) { // true = windows, false = fullscreen
		present_parameters.BackBufferFormat = D3DFMT_UNKNOWN;
	} else {
		present_parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
		present_parameters.BackBufferWidth = 1024;
		present_parameters.BackBufferHeight = 768;
	}
	
	// create device
	if(FAILED(d3dobj->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
											hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
											&present_parameters, &ddevice)))
	{
		MessageBox(GetActiveWindow(), "Could not create Direct3D Device", "D3D_DEV ERR", MB_OK);
		return false;
	}

	// there will be lighting
	ddevice->SetRenderState(D3DRS_LIGHTING, true);

	// cull all trangles that go counter clockwise to save time
	ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	// we want a z-buffer so its 3d
	ddevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

	// world matrix
	D3DXMatrixIdentity(&matWorld);
    ddevice->SetTransform(D3DTS_WORLD, &matWorld);

	// view matrix
	D3DXMatrixLookAtLH(&matView, new D3DXVECTOR3(0, 5, -2), new D3DXVECTOR3(0, 0, 0), new D3DXVECTOR3(0, 1, 0));
	ddevice->SetTransform(D3DTS_VIEW, &matView);

	// projection matrix
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 4/3, 1, 100);
	ddevice->SetTransform(D3DTS_PROJECTION, &matProj);
	
	// the initialization succeeded
	return true;
}

/* -------------------------------------------
   set up the textures
   ------------------------------------------- */
void SetupTextures() {
	D3DXCreateTextureFromFile(ddevice, "C:\abc.bmp", &tex1);
}

/* -------------------------------------------
   set up the lights
   ------------------------------------------- */
void SetupLights() {

	// the material
    ZeroMemory(&mtrl, sizeof(D3DMATERIAL9));
    mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
    mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
    mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
    mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
    ddevice->SetMaterial(&mtrl);

	// light 1
	ZeroMemory(&light0, sizeof(D3DLIGHT9));
	light0.Type = D3DLIGHT_DIRECTIONAL;
	light0.Direction = D3DXVECTOR3((float)cos(1*(2*D3DX_PI / 3)), -1.0f, (float)sin(1*(2*D3DX_PI / 3)));
	light0.Diffuse.r = 1.0f;
	light0.Diffuse.g = 0.0f;
	light0.Diffuse.b = 0.0f;
	ddevice->SetLight(0, &light0);
	ddevice->LightEnable(0, true);

	// ligth 2
	ZeroMemory(&light1, sizeof(D3DLIGHT9));
	light1.Type = D3DLIGHT_DIRECTIONAL;
	light1.Direction = D3DXVECTOR3((float)cos(2*(2*D3DX_PI / 3)), -1.0f, (float)sin(2*(2*D3DX_PI / 3)));
	light1.Diffuse.r = 0.0f;
	light1.Diffuse.g = 1.0f;
	light1.Diffuse.b = 0.0f;
	ddevice->SetLight(1, &light1);
	ddevice->LightEnable(1, true);

    // light 3
	ZeroMemory(&light2, sizeof(D3DLIGHT9));
	light2.Type = D3DLIGHT_DIRECTIONAL;
	light2.Direction = D3DXVECTOR3((float)cos(3*(2*D3DX_PI / 3)), -1.0f, (float)sin(3*(2*D3DX_PI / 3)));
	light2.Diffuse.r = 0.0f;
	light2.Diffuse.g = 0.0f;
	light2.Diffuse.b = 1.0f;
	ddevice->SetLight(2, &light2);
	ddevice->LightEnable(2, true);

}

/* -------------------------------------------
   renders the scene
   ------------------------------------------- */
void Render() {

	LIGHTVERTEX tri[12] = {
		// front triangle
		{0.0, 1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0},
		{1.0, 0.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0},
		{-1.0, 0.0, -1.0, 0.0, 1.0, -1.0, 1.0, 0.0},
		// right triangle
		{0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0},
		{1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0},
		{1.0, 0.0, -1.0, 1.0, 1.0, 0.0, 1.0, 0.0},
		// left triangle
		{0.0, 1.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0},
		{-1.0, 0.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0},
		{-1.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, 0.0},
		// back triangle
		{0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0},
		{-1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0},
		{1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0}
	};

	// begin the scene
	ddevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
	ddevice->BeginScene();

	// use our vertex types
	ddevice->SetFVF(D3DFVF_LIGHTVERTEX);

	// set up the lights
	SetupLights();

	// set the texture
	ddevice->SetTexture(0, tex1);

	// rotate the scene
	static float abc = 0.0f;
	abc += 0.5f;
	D3DXMatrixIdentity(&matWorld);
	D3DXMatrixRotationY(&matWorld, (D3DX_PI / 32) * abc);
    ddevice->SetTransform(D3DTS_WORLD, &matWorld);
	
	// draw stuff
	ddevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 4, tri, sizeof(LIGHTVERTEX));

	// end the scene
	ddevice->EndScene();
	ddevice->Present(NULL, NULL, NULL, NULL);
}

/* -------------------------------------------
   windows procedure function to handle
   windows messages
   ------------------------------------------- */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
		case WM_KEYDOWN:
			keys[wParam] = true;
			return 0;
		case WM_KEYUP:
			keys[wParam] = false;
			return 0;
		case WM_CLOSE:
			PostQuitMessage(0);
			return 0;
	}
	return (DefWindowProc(hwnd,message,wParam,lParam));
}

/* -------------------------------------------
   entry point into the application
   ------------------------------------------- */
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
	
	WNDCLASS wincls;
	wincls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wincls.lpfnWndProc = WndProc;
	wincls.cbClsExtra = 0;
	wincls.cbWndExtra = 0;
	wincls.hInstance = GetModuleHandle(NULL);
	wincls.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	wincls.hCursor = LoadCursor(NULL, IDC_ARROW);
	wincls.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
	wincls.lpszMenuName = NULL;
	wincls.lpszClassName = "wincls";

	if (!RegisterClass(&wincls)) {
		MessageBox(NULL, "Unable to create windows class", "Windows Error", MB_OK);
		return 0;
	}

	if (!(hwnd = CreateWindowEx(0, "wincls", "Tree", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, HWND_DESKTOP, NULL, wincls.hInstance, NULL))) {
		MessageBox(NULL, "Unable to create window", "Windows Error", MB_OK);
		return 0;
	}
	
	if (!InitDX()) {
		MessageBox(NULL, "Unable to initialize Direct X 9", "Direct X 9 Error", MB_OK);
		return 0;
	}
	
	// set up the textures
	SetupTextures();

	// show the form
	ShowWindow(hwnd, true);
	
	// windows loop variables
	MSG msg;
	bool done = false;

	while (!done) {
		// check for a message
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
			// is it a quit message
			if (msg.message == WM_QUIT) {
				// end the windows loop
				done = true;
			} else {
				// process the message
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		} else {
			// render the scene
			Render();
			// check for an escape key
			if (keys[VK_ESCAPE]) {
				done = true;
			}
		}
	}
	
	// cleanup d3d objects
	if (tex1 != NULL) {
		tex1->Release();
	}
	if (ddevice != NULL) {
		ddevice->Release();
	}
	if (d3dobj != NULL) {
		d3dobj->Release();
	}

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
The problem was with the path of the texture.

You had:

"C:\abc.bmp"

when it should have been

"c:\\abc.bmp"

This is because \ is used as an escape clause. Like \n and \0.

ace

Share this post


Link to post
Share on other sites
An important thing to point out here is that D3DXCreateTextureFromFile was failing but you didn't notice because you're not checking for errors. Additionally you should use the Debug Runtime which will tell you why things fail. You can read about it in the Forum FAQ.

Share this post


Link to post
Share on other sites
Yes and even if you dont check for errors, the fact that after the call was made the texture object was still 0x00000000 indicates that you didnt step through either.

No worries though,

ace

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!