Changeset 539

Show
Ignore:
Timestamp:
05/16/08 17:10:24 (8 months ago)
Author:
LeoD
Message:

config caching, this was some hard work but it seems pretty useless now :P

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/run/data/shaders/model-pixel.glsl

    r538 r539  
    55#   include "shadowmap.glsl" 
    66 
    7 uniform sampler2D shadow
     7uniform sampler2D shadowTexture
    88 
    99varying vec4 lightSpacePos; 
    1010#endif 
    1111 
     12// The model's team color 
    1213uniform vec3 color;  
    1314 
     
    1920    gl_FragColor = vec4(tex.rgb * tex.a + color * (1 - tex.a), 1.0) * (light * 2); 
    2021     
    21 /*#ifdef SHADOWMAPPING 
    22     gl_FragColor *= shadowMap(shadow, lightSpacePos); 
     22#ifdef SHADOWMAPPING 
     23    gl_FragColor *= shadowMap(shadowTexture, lightSpacePos); 
    2324#else 
    2425    gl_FragColor *= gl_Color; 
    25 #endif*/ 
     26#endif 
    2627} 
  • trunk/run/data/shaders/shadowmap.glsl

    r538 r539  
    55    float shadowMapDistance = texture2D(sampler, shadowMapCoord).r; 
    66 
    7     return (shadowMapCoord.x >= 0 && shadowMapCoord.x <= 1 && shadowMapCoord.y >= 0 && shadowMapCoord.y <= 1 && 
    8            lightDistance >= shadowMapDistance && shadowMapDistance != 1.0f) ? 0.35f : 1.0f; 
     7    return (shadowMapCoord.x >= 0 && 
     8            shadowMapCoord.x <= 1 && 
     9            shadowMapCoord.y >= 0 && 
     10            shadowMapCoord.y <= 1 && 
     11             
     12            lightDistance >= shadowMapDistance && 
     13            shadowMapDistance != 1.0f) ? 0.4f : 1.0f; 
    914} 
    1015 
     
    1217{ 
    1318    vec2 shadowMapCoord = 0.5f * lightSpacePos.xy / lightSpacePos.w + vec2(0.5f, 0.5f); 
    14     float bias = -0.0008f; // avoid shadow aliasing by offseting the shadow a bit 
     19    const float bias = -0.0008f; // avoid shadow aliasing by offseting the shadow a bit 
    1520    float lightDistance = 0.5f + bias + lightSpacePos.z / lightSpacePos.w * 0.5f; 
    1621 
    1722    float val = 0.1f; 
    18     float o1 = 0.001f; 
    19     float o2 = 0.0005f; 
     23    const float o1 = 0.001f; 
     24    const float o2 = 0.0005f; 
    2025 
    2126#if SHADOWMAPPING_SAMPLES > 0 
  • trunk/run/data/shaders/terrain-pixel.glsl

    r537 r539  
    1 uniform sampler2D alpha
     1uniform sampler2D alphaMap
    22uniform sampler2D texture1; 
    33uniform sampler2D texture2; 
    44uniform sampler2D texture3; 
    5 uniform sampler2D lightmap; 
     5uniform sampler2D lightMap; 
    66 
    77#ifdef SHADOWMAPPING 
    88#   include "shadowmap.glsl" 
    99 
    10 uniform sampler2D shadow
     10uniform sampler2D shadowTexture
    1111 
    1212varying vec4 lightSpacePos; 
     
    1818    vec2 coord1 = vec2(gl_TexCoord[1]); 
    1919     
    20     vec4 a = texture2D(alpha, coord0); 
     20    vec4 a = texture2D(alphaMap, coord0); 
    2121    vec4 c1 = texture2D(texture1, coord1); 
    2222    vec4 c2 = texture2D(texture2, coord1); 
    2323    vec4 c3 = texture2D(texture3, coord1); 
    24     vec4 l = texture2D(lightmap, coord0); 
     24    vec4 l = texture2D(lightMap, coord0); 
    2525     
    2626    float inverse = 1.0f / (a.r + a.g + a.b); 
     
    3232     
    3333#ifdef SHADOWMAPPING 
    34     gl_FragColor *= shadowMap(shadow, lightSpacePos); 
     34    gl_FragColor *= shadowMap(shadowTexture, lightSpacePos); 
    3535#endif 
    3636} 
  • trunk/run/defend.cfg

    r538 r539  
    2222    // port for multiplayer 
    2323    port = 31337 
    24      
     24 
    2525[graphics] 
    2626    // shadow mapping 
    27     shadowmapping = 1 
    28      
    2927    [shadowmapping] 
     28        enable = 1 
     29         
    3030        size = 2048 
    3131        samples = 1 
  • trunk/src/defend/Config.d

    r535 r539  
    22 
    33import tango.io.FilePath; 
    4 //import tango.text.Properties; 
    5 //import tango.util.Convert; 
    64 
    75import gen.math.Vector; 
     
    5250} 
    5351 
    54 Config defendConfig; 
    55  
    56 version(none) class Config  
    57 
    58     static: 
    59  
    60     void load(char[] file) 
    61     { 
    62         Properties!(char).load(new FilePath(file), (char[] name, char[] value) 
    63         { 
    64             entries[name] = value; 
    65         }); 
    66     } 
    67  
    68     Unstatic!(T) opCall(T)(char[] name, T def) 
    69     { 
    70         if(auto entry = name in entries) 
    71             return to!(Unstatic!(T))(*entry); 
    72              
    73         return def; 
    74     } 
    75      
    76     private 
    77     { 
    78         char[][char[]] entries; 
    79     } 
    80 
     52CachedConfig!("int graphics.shadowmapping.enable;" 
     53              "int graphics.shadowmapping.size;" 
     54              "int graphics.shadowmapping.samples;" 
     55              "int graphics.lighting;") 
     56               
     57              defendConfig; 
  • trunk/src/defend/Main.d

    r537 r539  
    7878         
    7979        // Load the config 
    80         defendConfig = new Config(file); 
     80        defendConfig = new typeof(defendConfig)(file); 
    8181    } 
    8282 
     
    244244 
    245245    // Mirror config settings to shader defines 
    246     if(defendConfig("graphics").integer("shadowmapping")
     246    if(defendConfig.graphics.shadowmapping.enable
    247247    { 
    248248        Shader.define("SHADOWMAPPING"); 
  • trunk/src/defend/game/Game.d

    r537 r539  
    275275    sceneGraph.addCamera("main", mainCamera, vec3(0, 0, 0.2)); 
    276276 
    277     if(defendConfig("graphics").integer("shadowmapping")
     277    if(defendConfig.graphics.shadowmapping.enable
    278278        initShadowMapping(); 
    279279 
  • trunk/src/defend/objects/SceneNode.d

    r538 r539  
    7979    Mesh mesh; 
    8080     
     81    CameraData shadowCamera; 
     82     
    8183public: 
    8284    mixin MAllocator; 
     
    119121            shader.setUniform("lightTexture", 1); 
    120122 
    121             if(defendConfig("graphics").integer("shadowmapping")
     123            if(defendConfig.graphics.shadowmapping.enable
    122124            { 
    123                 auto shadow = sceneGraph.getCamera("shadow"); 
    124                 assert(shadow.core !is null); 
    125                  
    126                 shader.setUniform("shadow", 2); 
    127                 shader.setUniform("lightTransform", shadow.core.projection * shadow.core.modelview); 
     125                shadowCamera = sceneGraph.getCamera("shadow"); 
     126             
     127                shader.setUniform("shadowTexture", 2); 
     128                shader.setUniform("lightTransform", shadowCamera.core.projection * shadowCamera.core.modelview); 
    128129                shader.setUniform("modelTransform", absoluteModelview); 
    129130                 
    130                 renderer.setTexture(2, shadow.framebuffer.texture); 
     131                renderer.setTexture(2, shadowCamera.framebuffer.texture); 
    131132            } 
    132133             
  • trunk/src/defend/terrain/Terrain.d

    r538 r539  
    398398            renderer.setTexture(4, lightmapTexture); 
    399399             
    400             shader.setUniform("alpha", 0); 
     400            shader.setUniform("alphaMap", 0); 
    401401            shader.setUniform("texture1", 1); 
    402402            shader.setUniform("texture2", 2); 
    403403            shader.setUniform("texture3", 3); 
    404             shader.setUniform("lightmap", 4); 
    405              
    406             if(defendConfig("graphics").integer("shadowmapping")
     404            shader.setUniform("lightMap", 4); 
     405             
     406            if(defendConfig.graphics.shadowmapping.enable
    407407            { 
    408408                auto shadow = sceneGraph.getCamera("shadow"); 
     
    410410                 
    411411                renderer.setTexture(5, shadow.framebuffer.texture); 
    412                 shader.setUniform("shadow", 5); 
     412                shader.setUniform("shadowTexture", 5); 
    413413 
    414414                shader.setUniform("lightTransform", shadow.core.projection * shadow.core.modelview); 
     
    458458        renderer.setTexture(4, lightmapTexture); 
    459459         
    460         localShader.setUniform("alpha", 0); 
     460        localShader.setUniform("alphaMap", 0); 
    461461        localShader.setUniform("texture1", 1); 
    462462        localShader.setUniform("texture2", 2); 
    463463        localShader.setUniform("texture3", 3); 
    464         localShader.setUniform("lightmap", 4); 
     464        localShader.setUniform("lightMap", 4); 
    465465 
    466466        foreach(patch; patches) 
  • trunk/src/gen/util/Config.d

    r537 r539  
    66         
    77        - structure by indentation (tabs!) 
    8         - "section" creates a new section 
     8        - [section] creates a new section 
    99        - sections may be nested 
    1010        - a = b defines a variable 
     
    1717        Example: 
    1818     
    19         section name 
     19        [name] 
    2020            a = "b" 
    2121            c = 37 
    2222         
    23             section blubber 
     23            [blubber] 
    2424                foo = vec3(1, 2, 3) 
    2525                blablabla = "blabl\nabla?" 
     
    5050} 
    5151 
    52 final class Config : ConfigSection 
     52class Config : ConfigSection 
    5353{ 
    5454private: 
     
    7878        return path.dup.pop.toString() ~ "/"; 
    7979    } 
     80} 
     81 
     82class CachedConfig(char[] names) : Config 
     83{ 
     84private: 
     85    static char[] extractNames(out char[][] types, out char[][] ids) 
     86    { 
     87        char[] result; 
     88         
     89        int position; 
     90     
     91        do 
     92        { 
     93            char[] name; 
     94            char[] type; 
     95            char[] id; 
     96             
     97            while(names[position] == ' ' || names[position] == '\n' || names[position] == '\r') 
     98                  position++; 
     99         
     100            while(names[position] != ';' && position < names.length) 
     101                name ~= names[position++]; 
     102 
     103            position++; 
     104             
     105            { 
     106                int i; 
     107                 
     108                while(name[i] != ' ' && i < name.length) 
     109                    type ~= name[i++]; 
     110                 
     111                i++; 
     112                 
     113                while(i < name.length) 
     114                    id ~= name[i++]; 
     115            } 
     116             
     117            types ~= type; 
     118            ids ~= id; 
     119        } 
     120        while(position != names.length); 
     121         
     122        return result; 
     123    } 
     124 
     125    static char[][] splitParts(char[] id) 
     126    { 
     127        char[][] result; 
     128     
     129        { 
     130            char[] part; 
     131             
     132            foreach(c; id) 
     133            { 
     134                if(c != '.') 
     135                    part ~= c; 
     136                else 
     137                { 
     138                    result ~= part; 
     139                    part = ""; 
     140                } 
     141            } 
     142             
     143            result ~= part; 
     144        } 
     145         
     146        return result; 
     147    } 
     148 
     149    static char[] genAssign(char[] type, char[] id) 
     150    { 
     151        char[] result = "\t" ~ id ~ " = this"; 
     152         
     153        { 
     154            char[][] parts = splitParts(id); 
     155             
     156            for(uint i = 0; i < parts.length; i++) 
     157            { 
     158                auto part = parts[i]; 
     159                 
     160                if(i == parts.length - 1) 
     161                { 
     162                    result ~= "."; 
     163                 
     164                    if(type == "int") 
     165                        result ~= "integer"; 
     166                    else if(type == "string") 
     167                        result ~= "string"; 
     168                    else 
     169                        assert(false, "unknown type: " ~ type); 
     170                } 
     171                else 
     172                    result ~= ".child"; 
     173                 
     174                result ~= "(\"" ~ part ~ "\")"; 
     175            } 
     176        }    
     177         
     178        return result ~ ";\n"; 
     179    } 
     180 
     181    static char[] genStructs(char[][] types, char[][] ids, char[] level = "", uint depth = 0) 
     182    { 
     183        char[] result = ""; 
     184        char[][] done = []; 
     185         
     186        foreach(i, id; ids) 
     187        { 
     188            char[][] parts = splitParts(id); 
     189         
     190            if(id.length > level.length && id[0 .. level.length] == level) 
     191            { 
     192                if(parts.length == depth + 1) 
     193                    result ~= types[i] ~ " " ~ parts[$ - 1] ~ ";\n"; 
     194                else 
     195                { 
     196                    char[] newLevel = level ~ (depth ? "." : "") ~ parts[depth]; 
     197                     
     198                    bool found = false; 
     199                     
     200                    foreach(d; done) 
     201                    { 
     202                        if(d == newLevel) 
     203                        { 
     204                            found = true; 
     205                            break; 
     206                        } 
     207                    } 
     208                     
     209                    if(found) 
     210                        continue; 
     211 
     212                    done ~= newLevel; 
     213                     
     214                    result ~= "struct _" ~ parts[depth] ~ "\n{\n"; 
     215                    result ~= genStructs(types, ids, newLevel, depth + 1); 
     216                    result ~= "}\n_" ~ parts[depth] ~ " " ~ parts[depth] ~ ";\n"; 
     217                } 
     218            } 
     219        } 
     220         
     221        return result; 
     222    } 
     223 
     224    static char[] genCode() 
     225    { 
     226        char[] result = ""; 
     227 
     228        { 
     229            char[][] types; 
     230            char[][] ids; 
     231             
     232            extractNames(types, ids); 
     233             
     234            result ~= genStructs(types, ids); 
     235             
     236            result ~= "this(char[] c)\n{\n\tsuper(c);\n"; 
     237             
     238            for(uint i = 0; i < types.length; i++) 
     239                result ~= genAssign(types[i], ids[i]); 
     240             
     241            result ~= "}"; 
     242        } 
     243         
     244        return result; 
     245    } 
     246 
     247public: 
     248    //pragma(msg, genCode()); 
     249    mixin(genCode()); 
    80250} 
    81251