Applying light to a texture (DirectX 9)

Hey guys. You're probably going to laugh at me and tell me to read a book on DirectX(which I probably *should*), because I'm developing a game engine and all is going well -- except for one crucial aspect -- lighting. Now in DirectX we all know lighting is one of the easiest aspects -- I've implemented physics, my own mesh loader, etc, now, I've always been an OpenGL programmer, but it seems I've committed myself to DirectX 9 for my engine for a multitude of reasons -- I love being able to pass D3D device pointers into other functions, I love the entire built-in .X mesh concept, but I absolutely hate vertex/index buffers(which I don't have a complete grasp on as yet), and I'm not using DX11 because I want older PC's to be able to run my crappy games. Anyway, part of my engine borrows code from xbdev.net's BSP header file to read BSP tree maps(The guy is a genius in my eyes).
I've uploaded bsp.h here: https://drive.google.com/open?id=16Q6pKQAPGZ_K4_K5lomSEsxZuwBhhcNV

Screenshot of my engine thus far: https://drive.google.com/open?id=189hiJJKe8UXu0Q-a2gxilmtQG-0uzGE9

Screenshot of the same thing but with a mesh rotation, and in an futile attempt to influence lighting, I've changed the FVF format for the texture: https://drive.google.com/open?id=1o-YJ4YTeO5KqCAxkYtZ_T2hil4UynVdm

Within this header file is DrawFace(), where the textures are applied and polygons rendered according to a certain point. Now, I've been able to do just about everything within my engine, except *lighting*( in the traditional sense, at least, I could be cheap with this and just draw a large texture over the screen, apply alpha and use that texture to darken/lighten the screen, as you can see with my debug console), but I want to be able to influence lighting on the BSP map that is rendered.

Point is, there is *nowhere* in my source code where I have implemented an actual lightsource. I've disabled using materials, my light function is commented out, I've been trying *everything* to somehow influence lighting on the textures being rendered on the map, even screwing with the FVF as you can see in the second screenshot. I've put code for directional, spotlight, any kind of lighting you can think of *right before* the primitive is drawn for each polygon, within the DrawFace() function, that's how desperate I've become.

I concede to the fact that I am in over my head here, that if I want a real game engine I should understand the API a lot better than I do, but I'm one of those sink-or-swim people and it's beyond help at this point, I drive myself mad. So when it comes to DirectX, I am retard-dumb. Caveman grunt, jaw dropping on the ground knuckle-dragging idiot knocking an engine together. So I need answers from you fine gentlemen in ways I can understand.

I know that in the vertex format for textured primitives there are normals, perpendicular to the face itself to reflect light, and I should somehow use this to influence lighting -- I've tried everything here folks. I wouldn't be coming here admitting defeat if I wasn't at my wits end.

Code such as the following I've tinkered with a thousand times, in my render() function, and even in the BSP's ::DrawFace() and ::RenderAllFace() functions:


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
D3DLIGHT9 light;
	D3DMATERIAL9 material;

	ZeroMemory(&light, sizeof(light));
	ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_SPOT;    // make the light type 'spot light'
	light.Diffuse = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
	light.Position = g_vCamPosition;
	light.Direction = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
	light.Range = 1000.0f;    // a range of 100
	light.Attenuation0 = 0.0f;    // no constant inverse attenuation
	light.Attenuation1 = 0.125f;    // only .125 inverse attenuation
	light.Attenuation2 = 0.0f;    // no square inverse attenuation
	light.Phi = D3DXToRadian(40.0f);    // set the outer cone to 30 degrees
	light.Theta = D3DXToRadian(20.0f);    // set the inner cone to 10 degrees
	light.Falloff = 1.0f;    // use the typical falloff

	g_pd3dDevice->SetLight(0, &light);
	g_pd3dDevice->LightEnable(0, TRUE);

	ZeroMemory(&material, sizeof(D3DMATERIAL9));
	material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

	g_pd3dDevice->SetMaterial(&material);


I'm not saying that the above is even the appropriate code, it's just a quick example of a copy-paste of one of my failed attempts. It does *not* matter what variables, flags, or functions I use for my material/light, it does not influence my game whatsoever, because my game is 100% being lit by textures being read, and that is the light itself. My 3D meshes have jpeg/png's for textures, so they're not influenced, and nor is the map -- the light I get is the light that is emitted from the textures themselves. The only time my light/material's are influencing my game is when I'm at the end of my map looking out into space, and they change colors. That's it.

Look within DrawFace(), or bsp.h as a whole -- what can I do to influence lighting of these textures? Could someone provide me with example code? I'd really, really, appreciate it. I've been up 48 hours trying everything.

Please. Help. I have no idea what to do at this point and I am at a loss, and I'm not going to resort to blanketing my game with alpha'd primitives to try and give the illusion of lighting. I really need your guys help with this one, you people are brilliant.

Thanks so much!
Last edited on
Nevermind! After sleeping and taking a break, I simple added an extra texture for DrawFace() and enabled alpha blending, and creating a light variable that multiples by 0x11111111 from 0x00000000 (Extremely dark) to 0xFFFFFFFF(Very light). This is what happens when I don't get any sleep and go insane -- Please take it from me, people, if you're on a serious project and you hit a brick wall, TAKE A BREAK AND GET SOME SLEEP. I was up for 48 hours trying the dumbest things you can imagine, but sleep and a simple break allowed me to find an intelligent solution quickly. Thanks anyways though!

(This is the second thread I've started asking for help and then finding the solution myself, perhaps the next time I consider posting a question on this forum I'll sleep, take a break and go at it all over again and again unless I'm *really* in a bind).

~TomTheFox / Xterria from cprogramming.com
~YiffClan.org
Topic archived. No new replies allowed.