source: trunk/src/rogue/fov_recursive_shadowcasting.cc @ 319

Last change on this file since 319 was 319, checked in by epyon, 11 years ago
  • created core module and moved all free source files there
  • took the opportunity to update all copyright lines and minor cleanup
  • minor fixes
  • not all examples are up to date
File size: 2.2 KB
RevLine 
[319]1// Copyright (C) 2012-2014 ChaosForge Ltd
[276]2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/rogue/fov_recursive_shadowcasting.hh"
8
[319]9#include "nv/core/math.hh"
[276]10
11static int nv_rogue_rs_mult[4][8] = {
12        { 1, 0, 0,-1,-1, 0, 0, 1},
13        { 0, 1,-1, 0, 0,-1, 1, 0},
14        { 0, 1, 1, 0, 0,-1,-1, 0},
15        { 1, 0, 0, 1,-1, 0, 0,-1},
16};
17
18void nv::rogue::fov_recursive_shadowcasting::run( const position& p, uint16 radius )
19{
20        m_position = p;
21        m_radius   = radius;
22
23        if ( m_radius == 0 )
24        {
25                position max_radius = m_size-m_position;
26                max_radius.x=glm::max(max_radius.x,m_position.x);
27                max_radius.y=glm::max(max_radius.y,m_position.y);
28                m_radius = (int)glm::length((glm::vec2)max_radius)+1;
29        }
30        m_radius2 = m_radius * m_radius;
31        for ( int oct=0; oct < 8; oct++ ) cast_light( 1, 1.0, 0.0, oct );
32        m_map->set_visible( p, true );
33}
34
35void nv::rogue::fov_recursive_shadowcasting::cast_light( int row, float start, float end, int oct )
36{
37        if ( start < end ) return;
38
39        int xx = nv_rogue_rs_mult[0][oct];
40        int xy = nv_rogue_rs_mult[1][oct];
41        int yx = nv_rogue_rs_mult[2][oct];
42        int yy = nv_rogue_rs_mult[3][oct];
43
44        float new_start = 0.0f;
45        for ( int j = row; j < m_radius + 1; j++ )
46        {
47                int dx=-j-1;
48                int dy=-j;
49                bool blocked = false;
50                while ( dx++ <= 0 )
51                {
52                        ivec2 c( dx*xx+dy*xy, dx*yx+dy*yy );
53                        c += m_position;
54                        if ( c.x >= 0 && c.x < m_size.x && c.y >= 0 && c.y < m_size.y )
55                        {
56                                float l_slope = ( dx - 0.5f ) / ( dy + 0.5f );
57                                float r_slope = ( dx + 0.5f ) / ( dy - 0.5f );
58                                if ( start < r_slope ) continue;
59                                if ( end   > l_slope ) break;
60                                bool transparent = m_map->is_transparent( c );
61
62                                if ( dx*dx+dy*dy <= m_radius2 && (m_light_walls || transparent))
63                                {
64                                        m_map->set_visible( c, true );
65                                }
66                                if ( blocked )
67                                {
68                                        if ( !transparent )
69                                        {
70                                                new_start = r_slope;
71                                                continue;
72                                        }
73                                        blocked = false;
74                                        start = new_start;
75                                }
76                                else
77                                {
78                                        if ( !transparent && j < m_radius )
79                                        {
80                                                blocked = true;
81                                                cast_light( j+1, start, l_slope, oct );
82                                                new_start = r_slope;
83                                        }
84                                }
85                        }
86                }
87                if ( blocked ) break;
88        }
89
90}
91
Note: See TracBrowser for help on using the repository browser.