Thread: Applying Light to Textures (DirectX 9)

  1. #1
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039

    Applying Light to Textures (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*...well I read one on DX6 when I was youngster whilst actively posting on this site, but forgot essentially everything), 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: bsp.h - Google Drive

    Screenshot of my engine thus far: game01.jpg - Google Drive

    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: game02.jpg - Google Drive

    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 :rawFace() and ::RenderAllFace() functions:

    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: bsp.h - Google Drive

    Screenshot of my engine thus far: game01.jpg - Google Drive

    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: game02.jpg - Google Drive

    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 :rawFace() and ::PVSRender() functions:

    Code:
    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!

    And yes, Salem, I have cross-posted on cplusplus.com, they don't have the proper forum for such a question and I'm looking for expediency =) (And *this* is where I truly belong anyway!) <3
    Last edited by Xterria; 12-20-2019 at 09:05 AM.

  2. #2
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    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

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    301
    TAKE A BREAK
    Definitely works a majority of the time.

    As far as lighting goes you are going to want to look at things like normals, and triangle builds in clockwise/counterclockwise manners.

    specifically NORMALS.

    although not directx, it's the idea that matters imo:

    LearnOpenGL - Basic Lighting
    Last edited by Structure; 01-10-2020 at 11:41 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX : Problem related to applying transformations
    By N Jagdish in forum Game Programming
    Replies: 4
    Last Post: 12-06-2011, 06:36 PM
  2. applying formula help please
    By farah0718 in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2008, 05:07 AM
  3. Applying windows to walls?
    By dxfoo in forum Game Programming
    Replies: 4
    Last Post: 09-29-2006, 11:59 PM
  4. DirectX 8 Transparent Textures
    By poopy_pants2 in forum Game Programming
    Replies: 14
    Last Post: 04-17-2003, 07:59 PM

Tags for this Thread