Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

(CS:DE) Simulated Lighting system


tslusny
 Share

Recommended Posts

For now only Player(MyIndex) is emmiting light becouse when they are other sources of light they just overlays and its not good.

CLIENT SIDE

At bottom of **modGlobals** add this:
```
Public LIGHT_MAXSTEPS As Long
Public LIGHT_BEGIN As Long
Public LIGHT_END As Long
```
Now add under Dims in **sub Main**:
```
LIGHT_MAXSTEPS = 8
    LIGHT_BEGIN = 255
    LIGHT_END = 50
```
Now replace **Sub RenderTexture** with this:
```
Public Sub RenderTexture(ByVal Texture As Long, ByVal dX As Long, ByVal dY As Long, ByVal sX As Long, ByVal sY As Long, ByVal dW As Long, ByVal dH As Long, ByVal sW As Long, ByVal sH As Long, Optional ByVal colour As Long = -1, Optional ByVal Light As Boolean = True)
Dim Box(0 To 3) As TLVERTEX, X As Long, textureWidth As Long, textureHeight As Long
Dim lVertexBrightness As Long, i As Long

    ' set the texture
    Call SetTexture(Texture)

    ' set the texture size
    textureWidth = D3DT_TEXTURE(Texture).width
    textureHeight = D3DT_TEXTURE(Texture).height

    If Light = True Then
        If Not InMapEditor Then
            If Not inMenu Then
                lVertexBrightness = CalcVertexBrightness(GetPlayerX(MyIndex), GetPlayerY(MyIndex), dX, dY)
                colour = D3DColorARGB(255, lVertexBrightness, lVertexBrightness, lVertexBrightness)
            End If
        End If
    End If

    ' exit out if we need to
    If Texture <= 0 Or textureWidth <= 0 Or textureHeight <= 0 Then Exit Sub

    For X = 0 To 3
        Box(X).RHW = 1
        Box(X).Color = colour
    Next

    Box(0).X = dX
    Box(0).Y = dY
    Box(0).tu = (sX / textureWidth)
    Box(0).tv = (sY / textureHeight)
    Box(1).X = dX + dW
    Box(1).tu = (sX + sW + 1) / textureWidth
    Box(2).X = Box(0).X
    Box(3).X = Box(1).X

    Box(2).Y = dY + dH
    Box(2).tv = (sY + sH + 1) / textureHeight

    Box(1).Y = Box(0).Y
    Box(1).tv = Box(0).tv
    Box(2).tu = Box(0).tu
    Box(3).Y = Box(2).Y
    Box(3).tu = Box(1).tu
    Box(3).tv = Box(2).tv

    Call D3DDevice8.DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Box(0), FVF_Size)
    D3DT_TEXTURE(Texture).UnloadTimer = GetTickCount
End Sub
```
Now add at bottom of **modGameLogic** this:
```
Public Function CalcVertexBrightness(LightX As Long, LightY As Long, VertexX As Long, VertexY As Long)
Dim x1 As Long, x2 As Long, y1 As Long, y2 As Long, distance As Double, LightSteps As Long
    Dim lXDistance As Long
    Dim lYDistance As Long
    Dim lDistance As Long
    Dim lVertexDistance As Long
    Dim lDistanceNormal As Long
    Dim lLightSteps As Long

    x1 = ConvertMapX(LightX * 32) + 8
    y1 = ConvertMapY(LightY * 32) + 8
    x2 = VertexX
    y2 = VertexY

    lDistanceNormal = CLng(Sqr((PIC_X / 2) ^ 2 + (PIC_Y / 2) ^ 2))
    '// Calculate distance from vertex
    lXDistance = CLng(Abs(x1 - x2))
    lYDistance = CLng(Abs(y1 - y2))
    lVertexDistance = CLng(Sqr(lXDistance ^ 2 + lYDistance ^ 2) / lDistanceNormal)

    If lVertexDistance <= LIGHT_MAXSTEPS Then
        '// Calculate light steps
        lLightSteps = CLng(Abs((LIGHT_BEGIN - LIGHT_END)) / (LIGHT_MAXSTEPS))

        If lLightSteps = 0 Then
                lLightSteps = 1
        End If

        '// Calculate vertex brightness
        CalcVertexBrightness = CLng(LIGHT_BEGIN - (lVertexDistance * lLightSteps))
    Else
        '// Return lowest value
        CalcVertexBrightness = LIGHT_END
    End If
End Function
```

Screenshot:
![](http://i.imgur.com/Zlvdh.png)
Link to comment
Share on other sites

Man thats friggen sweet! i was thinking about this a few days ago but i knew i wouldnt be able to figre it out, thanks a bunch for the tutorial, im gonna try it out now :)

Im getting a mismatch type error, in public sub rendertexture its highlighing dx and saying ref type mismatch. if you want  i can post a screenshot.  Most likely its not a bug, probibly  i did something wrong  but i figured ya may be able to help lol
Link to comment
Share on other sites

i forget ya might not notice when i edit my posts but im getting an error i described, do ya think ya can figure out what im doing wrong?

i might be putting these in the wrong place but im not exactly sure
LIGHT_MAXSTEPS = 8
    LIGHT_BEGIN = 255
    LIGHT_END = 50
Link to comment
Share on other sites

@or3o:

> i forget ya might not notice when i edit my posts but im getting an error i described, do ya think ya can figure out what im doing wrong?
>
> i might be putting these in the wrong place but im not exactly sure
> LIGHT_MAXSTEPS = 8
>     LIGHT_BEGIN = 255
>     LIGHT_END = 50

No i fixed it in tutorial replace your Public Function CalcVertexBrightness with that new in tutorial
Link to comment
Share on other sites

that fixed the error and thank you for the help, but i must have put that other chunk of code in the wrong place because my whole picture box is grey now lol. can you specify in detail where to put it for noobs like me? :) thanks in advance
Link to comment
Share on other sites

@or3o:

> that fixed the error and thank you for the help, but i must have put that other chunk of code in the wrong place because my whole picture box is grey now lol. can you specify in detail where to put it for noobs like me? :) thanks in advance

I have same issue in EO3.0 but in CS:DE it works like a charm im fixing it now. I just dont tried this tutorial with EO 3.0 becouse i thinked its almost same as CS:DE

Ok i think rendered things positiong in EO 3.0 is different than in CS:DE for example dx and dy is stored in single and in CS:DE they are stored in logs so i changed this tutorial to its original behavior ( **i change it to CS:DE tutorial** )
Link to comment
Share on other sites

@zerohero:

> Tried to get this working in 3.0 but to no current success. I am sure its possible but obviously I'm doing something wrong. If anyone does manage to get it working with the same results please let me know.

Do you see only grey screen when implemented this to EO 3.0?
Link to comment
Share on other sites

~~Initially nothing, I converted it to 3.0 drawing method standards and I was getting draw textures with colour changing but could not figure out how to convert them to colours.~~

*edit* got it working in 3.0, now just need to find a way to stop GUI from being 'darkened' will post back when I have.

*edit 2* I now have it working fully within 3.0, the changes where relatively straight forward however your system did have a few holes that needed filling.

The code will now check map moral to make sure the area is not indoors, and if it is react differently. I have also added a threshold so that the light source will only take effect when the alpha on the map is at a user set level, meaning during the day the light is not emitted.

It took literally an hour for me to figure out where exactly the 'light' was being drawn from haha, what a fun time that was. Anyway I will try and compile the revised version and either make a new thread or merge it here with Deathbeams if he does not mind.
Link to comment
Share on other sites

@zerohero:

> ~~Initially nothing, I converted it to 3.0 drawing method standards and I was getting draw textures with colour changing but could not figure out how to convert them to colours.~~
>
> *edit* got it working in 3.0, now just need to find a way to stop GUI from being 'darkened' will post back when I have.
>
> *edit 2* I now have it working fully within 3.0, the changes where relatively straight forward however your system did have a few holes that needed filling.
>
> The code will now check map moral to make sure the area is not indoors, and if it is react differently. I have also added a threshold so that the light source will only take effect when the alpha on the map is at a user set level, meaning during the day the light is not emitted.
>
> It took literally an hour for me to figure out where exactly the 'light' was being drawn from haha, what a fun time that was. Anyway I will try and compile the revised version and either make a new thread or merge it here with Deathbeams if he does not mind.

I merged this system with Day/Night system ripped from Eclipse Dawn, so i dont need any alpha levels to check if its day or what do you say becouse i dont 100% understanded what you said but :P And if you can figure out more than 1 light sources that will be great becouse it just overlays when more lightsources are there. I figured out how to cache that lights becouse otherwise its just infinite loop (almost :P ) and game just dont load.
Link to comment
Share on other sites

  • 2 weeks later...
you messed this up a little bit…
Look at the movement...
It's tile-based when it should be sprite-based so it moves **WITH** the player and not just teleport to the next tile…
Look at how to sprite is moved and go from there.

EDIT:
Or a better example, look at the shadow system
Link to comment
Share on other sites

@ZeroX8:

> you messed this up a little bit…
> Look at the movement...
> It's tile-based when it should be sprite-based so it moves **WITH** the player and not just teleport to the next tile…
> Look at how to sprite is moved and go from there.
>
> EDIT:
> Or a better example, look at the shadow system

Shadow is texture based. My lighting system is texture based, which makes it fake.

If you want real lighting you need pixel based movement, or at least if you want halfway decent lighting.
Link to comment
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
 Share

×
×
  • Create New...