Changeset 67

Show
Ignore:
Timestamp:
05/24/08 22:19:12 (3 months ago)
Author:
JoeCoder
Message:

Moved the Color class into its own file.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/demo1/gameobj.d

    r66 r67  
    1212import std.stdio; 
    1313import yage.core.timer; 
    14 import yage.core.types
     14import yage.core.color
    1515import yage.core.vector; 
    1616import yage.resource.resource; 
  • trunk/src/demo1/main.d

    r65 r67  
    2121// Current program entry point.  This may change in the future. 
    2222int main() 
    23 
    24  
     23{    
    2524    // Init (resolution, depth, fullscreen, aa-samples) 
    2625    Device.init(800, 600, 32, false, 1); 
     
    5352    Ship ship = new Ship(scene); 
    5453    ship.setPosition(Vec3f(0, 50, -950)); 
    55     ship.getCameraSpot().setPosition(0, 1000, 3000); 
     54    ship.getCameraSpot().setPosition(Vec3f(0, 1000, 3000)); 
    5655 
    5756    // Camera 
     
    8584        if (Input.keydown[SDLK_ESCAPE]) 
    8685            Device.exit(0); 
     86         
     87        if(key == SDLK_c){ 
     88            std.gc.fullCollect();  
     89            writefln("garbage collected"); 
     90        } 
    8791    } 
    8892     
     
    102106    l1.setDiffuse(Color(1, .85, .7)); 
    103107    l1.setLightRadius(7000); 
    104     l1.setPosition(0, 0, -6000); 
     108    l1.setPosition(Vec3f(0, 0, -6000)); 
    105109 
    106110    // Star 
     
    113117    planet.setModel("space/planet.ms3d"); 
    114118    planet.scale = Vec3f(60); 
    115     planet.setAngularVelocity(0, -0.01, 0); 
     119    planet.setAngularVelocity(Vec3f(0, -0.01, 0)); 
    116120     
    117121    //planet.getModel().clearAttribute("gl_Normal"); 
  • trunk/src/demo1/ship.d

    r66 r67  
    5959 
    6060    void update(float delta) 
    61     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    62  
    63         super.update(delta); 
     61    {   super.update(delta); 
    6462 
    6563        // Set the acceleration speed 
     
    8381            void fade(BaseNode self) 
    8482            {   SpriteNode node = cast(SpriteNode)self; 
    85                 node.setColor(1, 1, 1, node.getLifetime()/5); 
     83                node.setColor(Color(1, 1, 1, node.getLifetime()/5)); 
    8684                float scale = std.math.sqrt(5.0f)-std.math.sqrt(node.getLifetime()) + .4; 
    8785                node.setScale(scale); 
    88                 node.setVelocity(node.getVelocity().scale(max(1-delta*ldamp*2, 0.0f))); 
    8986            } 
    9087            puff.onUpdate(&fade); 
     
    9289            puff = new SpriteNode(ship.getScene(), puff); 
    9390            puff.setPosition(ship.getAbsolutePosition()+Vec3f(-.8, 0, 2.5).rotate(ship.getAbsoluteTransform())); 
    94              
    95              
    9691             
    9792             
  • trunk/src/demo2/main.d

    r65 r67  
    9696         
    9797        if(key == SDLK_c){ 
    98             std.gc.fullCollect(); //FIX! 
     98            std.gc.fullCollect();  
    9999            writefln("garbage collected"); 
    100100        } 
  • trunk/src/yage/core/all.d

    r51 r67  
    1515public 
    1616{   import yage.core.array; 
     17    import yage.core.color; 
    1718    import yage.core.freelist; 
    1819    import yage.core.math; 
  • trunk/src/yage/core/math.d

    r51 r67  
    2323 * compare equal.  This allows 0.00001 and 0 to be almost equal. */ 
    2424bool almostEqual(float a, float b, float fudge=0.0001) 
    25 {   if (fabs(a-b) <= fudge) 
     25{    
     26    if (fabs(a-b) <= fudge) 
    2627        return true; 
    2728 
     
    3334            return true; 
    3435    return false; 
     36     
     37    //return fdim(a, b) < fudge; 
    3538} 
    3639unittest 
  • trunk/src/yage/core/types.d

    r59 r67  
    1414import yage.core.vector:Vec4f; 
    1515private extern (C) void *memcpy(void *, void *, uint); 
    16  
    17  
    18  
    19 /** 
    20  * A struct used to represent a color. 
    21  * Colors are represented in RGBA format. 
    22  * Note that uints and dwords store the bytes in reverse, 
    23  * so Color(0x6633ff00).hex == "00FF3366" 
    24  * All Colors default to transparent black. 
    25  * Example: 
    26  * -------------------------------- 
    27  * uint  red  = Color("red").ui; 
    28  * Vec4f blue = Color("0000FF").vec4f; 
    29  * writefln(Color("blue"));      // outputs "0000FF00" 
    30  * writefln(Color(0x00FF0000));  // outputs "0000FF00" 
    31  * -------------------------------- 
    32  */ 
    33 struct Color 
    34 { 
    35     private static real frac = 1.0f/255; 
    36      
    37     union 
    38     {   ubyte[4] ub;/// Get the Color as an array of ubyte 
    39         uint ui;    /// Get the Color as a uint      
    40         dword dw;   /// Get the Color as a dword 
    41         struct { ubyte r, g, b, a; } /// Access each color component. 
    42     } 
    43      
    44     /// Initialize 
    45     static Color opCall(int r, int g,int b, int a=255) 
    46     {   Color res; 
    47         res.r=r; 
    48         res.b=b; 
    49         res.g=g; 
    50         res.a=a; 
    51         return res; 
    52     } 
    53     /// Ditto 
    54     static Color opCall(float r, float g, float b, float a=1) 
    55     {   Color res; 
    56         res.r=cast(ubyte)clamp(r*255, 0.0f, 255.0f); 
    57         res.b=cast(ubyte)clamp(b*255, 0.0f, 255.0f); 
    58         res.g=cast(ubyte)clamp(g*255, 0.0f, 255.0f); 
    59         res.a=cast(ubyte)clamp(a*255, 0.0f, 255.0f); 
    60         return res; 
    61     } 
    62  
    63  
    64     /// Convert dword to Color 
    65     static Color opCall(dword dw) 
    66     {   Color res; 
    67         res.dw = dw; 
    68         return res; 
    69     } 
    70      
    71     /// Convert uint to Color 
    72     static Color opCall(uint ui) 
    73     {   return Color(dword(ui)); 
    74     } 
    75      
    76     /// Convert ubyte[] to Color 
    77     static Color opCall(ubyte[] v) 
    78     {   Color res; 
    79         for (int i=0; i<max(v.length, 4); i++) 
    80             res.ub[i] = cast(ubyte)(v[i]); 
    81         return res; 
    82     }    
    83      
    84     /// Convert int[] to Color 
    85     static Color opCall(int[] v) 
    86     {   Color res; 
    87         for (int i=0; i<max(v.length, 4); i++) 
    88             res.ub[i] = cast(ubyte)(v[i]); 
    89         return res; 
    90     }    
    91      
    92     /// Convert float[] to Color 
    93     static Color opCall(float[] f) 
    94     {   Color res; 
    95         for (int i=0; i<min(f.length, 4); i++) 
    96             res.ub[i] = cast(ubyte)clamp(f[i]*255, 0.0f, 255.0f); 
    97         return res; 
    98     } 
    99      
    100     /// Convert Vec3f to Color 
    101     static Color opCall(Vec3f v) 
    102     {   return Color(v.v); 
    103     } 
    104      
    105     /// Convert Vec4f to Color 
    106     static Color opCall(Vec4f v) 
    107     {   return Color(v.v); 
    108     } 
    109      
    110     /** 
    111      * Convert a string to a color. 
    112      * The string can be a 6 or 8 digit hexadecimal or an English color name. 
    113      * Black, blue, brown, cyan, gold, gray/grey, green, indigo, magenta, orange,  
    114      * pink, purple, red, violet, white, and yellow are supported. 
    115      * See: <a href="http://www.w3schools.com/css/css_colornames.asp">CSS color names</a> 
    116      * Params: 
    117      * string = The string to convert.*/ 
    118     static Color opCall(char[] string) 
    119     {    
    120         // An english color name 
    121         switch (std.string.tolower(string)) 
    122         {   case "black":   return Color(0xFF000000); 
    123             case "blue":    return Color(0xFFFF0000); 
    124             case "brown":   return Color(0xFFA52A2A); 
    125             case "cyan":    return Color(0xFFFFFF00); 
    126             case "gold":    return Color(0xFF00D7FF); 
    127             case "gray":     
    128             case "grey":    return Color(0xFF808080); 
    129             case "green":   return Color(0xFF008000); 
    130             case "indigo":  return Color(0xFF82004B); 
    131             case "magenta": return Color(0xFFFF00FF); 
    132             case "orange":  return Color(0xFF00A5FF); 
    133             case "pink":    return Color(0xFFCBC0FF); 
    134             case "purple":  return Color(0xFF800080); 
    135             case "red":     return Color(0xFF0000FF); 
    136             case "violet":  return Color(0xFFEE82EE); 
    137             case "white":   return Color(0xFFFFFFFF); 
    138             case "yellow":  return Color(0xFF00FFFF); 
    139             default: break; 
    140         }        
    141      
    142         // Append alpha to 6-digit hex string. 
    143         if (string.length == 6) 
    144             string ~= "FF";      
    145          
    146         // Convert string one char at a a time. 
    147         Color result; 
    148         int digit; 
    149         foreach (int i, char h; string) 
    150         {   if (i>=8) 
    151                 break; 
    152          
    153             digit=0; // will be 0-15 
    154             if (47 < h && h < 58)   // 0-9 
    155                 digit = (h-48); 
    156             else if (64 < h && h < 71) // A-F 
    157                 digit = (h-55); 
    158             else if (96 < h && h < 103) // a-f 
    159                 digit = (h-87); 
    160             else 
    161                 throw new Exception("Invalid character '" ~ h ~"' for Color()"); 
    162             result.ub[i/2] += digit * (15*((i+1)%2)+1); // gets low or high nibble 
    163         } 
    164         return result; 
    165     } 
    166      
    167      
    168     /// Get the Color as an array of float. 
    169     float[] f() 
    170     {   float[4] res; 
    171         res[0] = r * frac; 
    172         res[1] = g * frac; 
    173         res[2] = b * frac; 
    174         res[3] = a * frac; 
    175         return res.dup; 
    176     } 
    177      
    178     /// Get the Color as a Vec3f. 
    179     Vec3f vec3f() 
    180     {   Vec3f res; 
    181         res.v[0] = r * frac; 
    182         res.v[1] = g * frac; 
    183         res.v[2] = b * frac; 
    184         return res; 
    185     } 
    186  
    187     /// Get the Color as a Vec4f. 
    188     Vec4f vec4f() 
    189     {   Vec4f res; 
    190         res.v[0] = r * frac; 
    191         res.v[1] = g * frac; 
    192         res.v[2] = b * frac; 
    193         res.v[3] = a * frac; 
    194         return res; 
    195     } 
    196      
    197     /** 
    198      * Get the color as a string. 
    199      * Params: 
    200      * lower = return lower case hexadecimal digits*/  
    201     char[] hex(bool lower=false) 
    202     {   if (lower) 
    203             return formatString("%.8x", bswap(ui)); 
    204         return formatString("%.8X", bswap(ui)); 
    205     } 
    206     /// ditto 
    207     char[] toString() 
    208     {   return hex(); 
    209     } 
    210      
    211     unittest 
    212     {   assert(Color.sizeof == 4); 
    213          
    214         // Test initializers 
    215         assert(Color([0, 102, 51, 255]).hex == "006633FF"); 
    216         assert(Color([0.0f, 0.4f, 0.2f, 1.0f]).hex == "006633FF"); 
    217         assert(Color(0xFF336600).hex == "006633FF"); 
    218          
    219         // Test converters 
    220         assert(Color("abcdef97").hex == "ABCDEF97"); 
    221         assert(Color("006633FF").vec4f == Vec4f(0.0f, 0.4f, 0.2f, 1.0f)); 
    222         assert(Color("006633FF").ui == 0xFF336600); 
    223     }    
    224 } 
    225  
    22616 
    22717 
  • trunk/src/yage/core/vector.d

    r59 r67  
    1313import yage.core.math; 
    1414import yage.core.quatrn; 
     15 
    1516/** 
    1617 * This is a template to create a vector of any type with any number of elements. 
     
    1920 * Example: 
    2021 * -------------------------------- 
    21  * Vec!(real, 4) a; // a is a four-component real vector. 
     22 * Vec!(4, real) a; // a is a four-component real vector. 
    2223 * --------------------------------*/ 
    23 struct Vec(T, int K
     24struct Vec(int S, T
    2425{ 
    25     alias Vec!(T, K) VTK
     26    alias Vec!(S, T) VST
    2627 
    2728    union 
    28     {   T v[K] = 0; // same as x, y, z, etc. 
    29         static if(K==2) 
     29    {   T v[S] = 0; // same as x, y, z, etc. 
     30        static if(S==2) 
    3031        {   struct { T x, y; } 
    3132        } 
    32         static if(K==3) 
     33        static if(S==3) 
    3334        {   struct { T x, y, z; } 
    3435            struct { T r, g, b; } 
    3536        } 
    36         static if(K==4) 
     37        static if(S==4) 
    3738        {   struct { T r, g, b, a; } 
    3839            struct { T x, y, z, w; } 
     
    4647 
    4748    /// Create a zero vector 
    48     static VTK opCall() 
    49     {   VTK res; 
     49    static VST opCall() 
     50    {   VST res; 
    5051        return res; 
    5152    } 
    5253 
    5354    /// Create a vector with all values as s. 
    54     static VTK opCall(T s) 
    55     {   VTK res; 
     55    static VST opCall(T s) 
     56    {   VST res; 
    5657        foreach(inout T e; res.v) 
    5758            e = s; 
     
    6061 
    6162    /// Create a new vector with the values s0, s1, s2, ... 
    62     static VTK opCall(T[K] s ...) 
    63     {   VTK res; 
    64         res.v[0..K] = s[0..K]; 
    65         return res; 
    66     } 
    67  
    68     /// Create a new vector with the values of s; s must be at least of length K
    69     static VTK opCall(T[] s) 
    70     {   assert(s.length>=K); 
    71         VTK res; 
    72         res.v[0..K] = s[0..K]; 
     63    static VST opCall(T[S] s ...) 
     64    {   VST res; 
     65        res.v[0..S] = s[0..S]; 
     66        return res; 
     67    } 
     68 
     69    /// Create a new vector with the values of s; s must be at least of length S
     70    static VST opCall(T[] s) 
     71    {   assert(s.length>=S); 
     72        VST res; 
     73        res.v[0..S] = s[0..S]; 
    7374        return res; 
    7475    } 
    7576 
    7677    /// 
    77     VTK add(VTK s) 
    78     {   VTK res; 
     78    VST add(VST s) 
     79    {   VST res; 
    7980        for (int i=0; i<v.length; i++) 
    8081            res.v[i] = v[i]+s.v[i]; 
     
    8384 
    8485    /// The _angle between this vector and s, in radians. 
    85     float angle(VTK s) 
     86    float angle(VST s) 
    8687    {   return acos(dot(s)/(length()*s.length())); 
    8788    } 
    8889 
    89     /// 
    90     VTK clamp(T min, T max) 
    91     {   VTK res; 
    92         for (int i=0; i<K; i++) 
     90    /// Clamp all values between min and max. 
     91    VST clamp(T min, T max) 
     92    {   VST res; 
     93        for (int i=0; i<S; i++) 
    9394        {   if (res.v[i]<min) res.v[i] = min; 
    9495            else if (res.v[i]>max) res.v[i] = max; 
     
    100101 
    101102    /// Return the _dot product of this vector and s. 
    102     float dot(VTK s) 
     103    float dot(VST s) 
    103104    {   float res=0; 
    104105        for (int i=0; i<v.length; i++) 
     
    131132 
    132133    /// Create a new vector with the values of s; s must be at least of length 3. 
    133     VTK projection(VTK s) 
     134    VST projection(VST s) 
    134135    {   return s.scale(dot(s)/s.length2()); 
    135136    } 
    136137 
     138    /// 
    137139    T* ptr() 
    138140    {   return v.ptr;        
     
    140142     
    141143    /// Scale (multiply) this vector. 
    142     VTK scale(float s) 
    143     {   VTK res = *this; 
     144    VST scale(float s) 
     145    {   VST res = *this; 
    144146        for (int i=0; i<v.length; i++) 
    145147            res.v[i] *= s; 
     
    147149    } 
    148150    /// ditto 
    149     VTK scale(VTK s) 
    150     {   VTK res = *this; 
     151    VST scale(VST s) 
     152    {   VST res = *this; 
    151153        for (int i=0; i<v.length; i++) 
    152154            res.v[i] *= s.v[i]; 
     
    160162    } 
    161163    /// ditto 
    162     void set(VTK s) 
    163     {   v[0..K] = s.v[0..K]; 
     164    void set(VST s) 
     165    {   v[0..S] = s.v[0..S]; 
    164166    } 
    165167 
    166168    /// ditto 
    167     void set(T[K] s ...) 
    168     {   v[0..K] = s[0..K]; 
     169    void set(T[S] s ...) 
     170    {   v[0..S] = s[0..S]; 
    169171    } 
    170172 
     
    172174    char[] toString() 
    173175    {   char[] result = "<"; 
    174         for (int i=0; i<K; i++) 
     176        for (int i=0; i<S; i++) 
    175177            result ~= formatString("%.4f ", v[i]); 
    176178        result ~= ">"; 
     
    179181} 
    180182 
    181 alias Vec!(int, 2) Vec2i;     /// A two-component int Vec 
    182 alias Vec!(int, 3) Vec3i;     /// A three-component int Vec 
    183 alias Vec!(int, 4) Vec4i;     /// A four-component int Vec 
    184 alias Vec!(float, 2) Vec2f;       /// A two-component float Vec 
     183alias Vec!(2, int) Vec2i;     /// A two-component int Vec 
     184alias Vec!(3, int) Vec3i;     /// A three-component int Vec 
     185alias Vec!(4, int) Vec4i;     /// A four-component int Vec 
     186alias Vec!(2, float) Vec2f;       /// A two-component float Vec 
    185187//alias Vec3f Vec3f;            // Defined below 
    186 alias Vec!(float, 4) Vec4f;       /// A four-component float Vec 
     188alias Vec!(4, float) Vec4f;       /// A four-component float Vec 
    187189 
    188190/** 
  • trunk/src/yage/gui/style.d

    r55 r67  
    44module yage.gui.style; 
    55 
    6 import yage.core.types
     6import yage.core.color
    77import yage.core.vector; 
    88import yage.resource.material; 
  • trunk/src/yage/node/base.d

    r66 r67  
    100100    char[] toString() { return toString(false); } 
    101101    char[] toString(bool recurse) /// Ditto 
    102     {   static int indent; 
     102    {    
     103         
     104        static int indent; 
    103105        char[] pad = new char[indent*3]; 
    104106        pad[0..length] = ' '; 
     
    129131     */  
    130132    void update(float delta) 
    131     {   // Cache the current relative and absolute position/rotation for rendering. 
     133    {    
     134        // Cache the current relative and absolute position/rotation for rendering. 
    132135        // This prevents rendering a halfway-updated scenegraph. 
    133136        cache[scene.transform_write].transform = transform; 
  • trunk/src/yage/node/light.d

    r66 r67  
    8484    /// Overridden to remove the light from the Scene's arary of lights 
    8585    void remove() 
    86     {   debug scope( failure ) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
     86    {    
    8787        scene.removeLight(light_index); 
    8888        super.remove(); 
  • trunk/src/yage/node/moveable.d

    r32 r67  
    3232    protected bool      velocity_dirty=true;    // The absolute velocity vectors need to be recalculated. 
    3333 
    34     // Suppose rendering and scene-graph updating are in different threads 
     34    // Rendering and scene-graph updates run in different threads. 
    3535    // If the scene is rendered halfway through updating, rendering glitches may occur. 
    3636    // Therefore, the scene-graph implements a sort of "triple buffering". 
     
    4444    protected Cache cache[3]; 
    4545 
    46     // Incomplete 
    47     void lookAt(Vec3f target, Vec3f up) 
    48     {   Vec3f f = target.normalize(); 
    49         up = up.normalize(); 
    50  
    51         Vec3f s = f.cross(up).normalize(); 
    52         Vec3f u = s.cross(f); 
    53  
    54         setRotation(0, 0, 0); 
    55         rotate(u); 
     46    /** 
     47     * Move and rotate by the transformation Matrix. 
     48     * In other words, apply t as a transformation Matrix. */ 
     49    void transformation(Matrix t) 
     50    {   transform.postMultiply(t); 
     51        setTransformDirty(); 
    5652    } 
    5753 
     
    6763        return &transform_abs; 
    6864    } 
    69  
     65     
    7066    /** 
    7167     * Return the relative transformation Matrix of this Node.  This Matrix stores the position 
     
    7975        return transform; 
    8076    } 
     77    void setTransform(Matrix transform) /// Ditto 
     78    {   this.transform = transform; 
     79        setTransformDirty(); 
     80    } 
     81     
    8182    /** 
    8283     * Get the absolute transformation Matrix of this Node, calculating it if necessary. 
     
    8586     * instead of the current version.  This can be used to avoid working with a half-updated scenegraph.*/ 
    8687    Matrix getAbsoluteTransform(bool cached = false) 
    87     {   debug scope(failure) writef("Backtrace xx ",__FILE__,"(",__LINE__,")\n"); 
    88  
    89         if (cached) // the transform_abs cache is never dirty 
     88    {   if (cached) // the transform_abs cache is never dirty 
    9089            return cache[scene.transform_read].transform_abs; 
    9190        if (transform_dirty) 
     
    9392        return transform_abs; 
    9493    } 
    95  
    96     /** 
    97      * Get the position of this Node relative to its parent's location. 
     94     
     95    /** 
     96     * Get / set the position of this Node relative to its parent's location. 
    9897     * Note that changing the values of the return vector will not affect the Node's position. */ 
    9998    Vec3f getPosition() 
    10099    {   return Vec3f(transform.v[12..15]); 
    101100    } 
     101    void setPosition(Vec3f position) 
     102    {   transform.v[12..15] = position.v[0..3]; 
     103    } 
     104     
    102105    /** 
    103106     * Get the absolute position of this Node, calculating it if necessary. 
     
    106109    {   return Vec3f(getAbsoluteTransform().v[12..15]); 
    107110    } 
    108  
    109  
     111     
    110112    /** 
    111113     * Get the rotation of this Node relative to its parent's rotation. 
     
    114116    {   return transform.toAxis(); 
    115117    } 
     118    void setRotation(Vec3f axis) /// Ditto 
     119    {   transform.set(axis); 
     120        setTransformDirty(); 
     121    } 
     122     
    116123    /** 
    117124     * Get the absolute rotation of this Node, calculating it if necessary. 
     
    121128    } 
    122129 
    123  
    124     /// Set this Node's relative transformation Matrix. 
    125     void setTransform(Matrix transform) 
    126     {   this.transform = transform; 
    127         setTransformDirty(); 
    128     } 
    129  
    130  
    131     /// Set the position of this node relative to its parent's position and rotation. 
    132     void setPosition(float x, float y, float z) 
    133     {   transform.v[12]=x; 
    134         transform.v[13]=y; 
    135         transform.v[14]=z; 
    136         setTransformDirty(); 
    137     } 
    138     /// Ditto 
    139     void setPosition(Vec3f position) 
    140     {   setPosition(position.x, position.y, position.z); 
    141     } 
    142  
    143  
    144     /// Set the rotation of this Node relative to its parent's rotation, using an axis angle. 
    145     void setRotation(float x, float y, float z) 
    146     {   setRotation(Vec3f(x, y, z)); 
    147     } 
    148     /// Ditto 
    149     void setRotation(Vec3f axis) 
    150     {   transform.set(axis); 
    151         setTransformDirty(); 
    152     } 
    153  
    154  
    155     /** 
    156      * Move and rotate by the transformation Matrix. 
    157      * In other words, apply t as a transformation Matrix. */ 
    158     void transformation(Matrix t) 
    159     {   transform.postMultiply(t); 
    160         setTransformDirty(); 
    161     } 
    162  
    163     /// Move this Node relative to its parent. 
    164     void move(float x, float y, float z) 
    165     {   move(Vec3f(x, y, z)); 
    166     } 
    167     /// Ditto 
    168     void move(Vec3f distance) 
    169     {   transform.v[12]+=distance.x; 
    170         transform.v[13]+=distance.y; 
    171         transform.v[14]+=distance.z; 
    172         setTransformDirty(); 
    173     } 
    174  
    175     /// Move this Node relative to the direction it's pointing (relative to its rotation). 
    176     void moveRelative(float x, float y, float z) 
    177     {   moveRelative(Vec3f(x, y, z)); 
    178     } 
    179     /// Ditto 
    180     void moveRelative(Vec3f direction) 
    181     {   transform = transform.moveRelative(direction); 
    182         setTransformDirty(); 
    183     } 
    184  
    185  
    186     /// Rotate this Node relative to its current rotation axis, using an axis angle 
    187     void rotate(float x, float y, float z) 
    188     {   rotate(Vec3f(x, y, z)); 
    189     } 
    190     /// Ditto 
    191     void rotate(Vec3f axis) 
    192     {   transform = transform.rotate(axis); 
    193         setTransformDirty(); 
    194     } 
    195  
    196  
    197     /// Rotate this Node around the absolute worldspace axis, using an axis angle. 
    198     void rotateAbsolute(float x, float y, float z) 
    199     {   rotateAbsolute(Vec3f(x, y, z)); 
    200     } 
    201     /// Ditto 
    202     void rotateAbsolute(Vec3f axis) 
    203     {   transform = transform.rotateAbsolute(axis); 
    204         setTransformDirty(); 
    205     } 
    206  
    207  
    208     /// Get the velocity of this Node relative to its parent. 
    209     Vec3f getVelocity() 
     130    /// Get / set the velocity of this Node relative to its parent's linear and angular velocity. 
     131    void setVelocity(Vec3f velocity) 
     132    {   linear_velocity = velocity;  
     133    }  
     134    Vec3f getVelocity() /// Ditto 
    210135    {   return linear_velocity; 
    211136    } 
     
    218143    } 
    219144 
    220     /// Set the velocity of this Node relative to its parent's linear and angular velocity. 
    221     void setVelocity(float x, float y, float z) 
    222     {   linear_velocity.set(x, y, z); 
    223     } 
    224     /// Ditto 
    225     void setVelocity(Vec3f velocity) 
    226     {   linear_velocity = velocity; 
    227     } 
    228  
    229     /** 
    230      * Return the angular velocity axis; the Node rotates around this axis and 
    231      * the length of this is the rotations per second in radians. */ 
    232     Vec3f getAngularVelocity() 
     145    /** 
     146     * Get/set this Node's angular velocity relative to it's parent's rotation and angular velocity. 
     147     * This is represented in an axis-angle vector where the direction is the axis of rotation and the  
     148     * length is the rotation in radians. */ 
     149    Vec3f getAngularVelocity()  
    233150    {   return angular_velocity; 
    234     } 
    235  
    236     /// Set the angular velocity axis relative to this Node's current rotation. 
    237     void setAngularVelocity(float x, float y, float z) 
    238     {   angular_velocity.set(x, y, z); 
    239     } 
    240     /// Ditto 
    241     void setAngularVelocity(Vec3f axis) 
    242     {   angular_velocity = axis; 
     151    }    
     152    void setAngularVelocity(Vec3f axis) /// Ditto 
     153    {   angular_velocity = axis;  
     154    } 
     155     
     156     
     157     
     158    // Incomplete 
     159    // Might also consider a function to return a rotation vector needed to rotate to look at target. 
     160    void lookAt(Vec3f target, Vec3f up) 
     161    {   Vec3f f = target.normalize(); 
     162        up = up.normalize(); 
     163 
     164        Vec3f s = f.cross(up).normalize(); 
     165        Vec3f u = s.cross(f); 
     166 
     167        setRotation(Vec3f(0, 0, 0)); 
     168        rotate(u); 
     169    } 
     170 
     171    /// Move this Node relative to its parent. 
     172    void move(Vec3f distance) 
     173    {   transform.v[12]+=distance.x; 
     174        transform.v[13]+=distance.y; 
     175        transform.v[14]+=distance.z; 
     176        setTransformDirty(); 
     177    } 
     178 
     179    /// Move this Node relative to the direction it's pointing (relative to its rotation). 
     180    void moveRelative(Vec3f direction)  
     181    {   transform = transform.moveRelative(direction); 
     182        setTransformDirty(); 
     183    } 
     184 
     185    /// Rotate this Node relative to its current rotation axis, using an axis angle 
     186    void rotate(Vec3f axis) 
     187    {   transform = transform.rotate(axis); 
     188        setTransformDirty(); 
     189    } 
     190 
     191    /// Rotate this Node around the absolute worldspace axis, using an axis angle. 
     192    void rotateAbsolute(Vec3f axis)  
     193    {   transform = transform.rotateAbsolute(axis); 
     194        setTransformDirty(); 
    243195    } 
    244196 
    245197    /// Accelerate the Node in the direction specified 
    246     void accelerate(float x, float y, float z) 
    247     {   accelerate(Vec3f(x, y, z)); 
    248     } 
    249     /// Ditto 
    250198    void accelerate(Vec3f v) 
    251     {   linear_velocity += v; 
     199    {   linear_velocity += v;  
    252200    } 
    253201 
    254202    /// Accelerate relative to the way this Node is rotated (pointed). 
    255     void accelerateRelative(float x, float y, float z) 
    256     {   accelerateRelative(Vec3f(x, y, z)); 
    257     } 
    258     /// Ditto 
    259203    void accelerateRelative(Vec3f v) 
    260     {   linear_velocity += v.rotate(transform); 
     204    {   linear_velocity += v.rotate(transform);  
    261205    } 
    262206 
    263207    /// Accelerate the angular velocity of the Node by this axis. 
    264     void angularAccelerate(float x, float y, float z) 
    265     {   angularAccelerate(Vec3f(x, y, z)); 
    266     } 
    267     /// Ditto 
    268208    void angularAccelerate(Vec3f axis) 
    269     {   angular_velocity += axis; 
     209    {   angular_velocity += axis;  
    270210    } 
    271211 
     
    273213     * Accelerate the rotation of this Node, interpreting the acceleration axis 
    274214     * in terms of absolute worldspace coordinates. */ 
    275     void angularAccelerateAbsolute(float x, float y, float z) 
    276     {   angularAccelerateAbsolute(Vec3f(x, y, z)); 
    277     } 
    278     /// Ditto 
    279215    void angularAccelerateAbsolute(Vec3f axis) 
    280     {   angular_velocity += axis.rotate(getAbsoluteTransform().inverse()); 
    281     } 
    282  
    283  
    284  
    285  
    286  
     216    {   angular_velocity += axis.rotate(getAbsoluteTransform().inverse());  
     217    } 
    287218} 
  • trunk/src/yage/node/node.d

    r66 r67  
    131131     * This provides an easy way to change the color of a Node without having to modify all materials individually. 
    132132     * Default color is white and opaque (all 1's).*/ 
    133     Color getColor() { return color; } 
    134     void setColor(Color color) { this.color = color; } /// Ditto 
    135     void setColor(float r, float g, float b, float a=1) { color = Color(r, g, b, a); }  /// Ditto 
     133    Color getColor()  
     134    {   return color; 
     135    } 
     136    void setColor(Color color) /// Ditto 
     137    {   this.color = color;  
     138    }    
    136139     
    137140    /** 
     
    139142     * The default value is float.infinity, but a lower number will cause the Node to be removed 
    140143     * from the scene graph after that amount of time, as its lifetime is decreased with every Scene update.*/   
    141     float getLifetime() { return lifetime; } 
    142     void setLifetime(float seconds) { lifetime = seconds; } /// Ditto 
    143  
     144    float getLifetime()  
     145    {   return lifetime;  
     146    } 
     147    void setLifetime(float seconds)  /// Ditto 
     148    {   lifetime = seconds;  
     149    } 
     150     
     151     
    144152    /// Get an array of lights that were enabled in the last call to enableLights(). 
    145    LightNode[] getLights() 
     153LightNode[] getLights() 
    146154    {   return lights; 
    147155    } 
     
    162170     * Get / set the scale of this Node in the x, y, and z directions. 
    163171     * The default is (1, 1, 1).  Unlike position and rotation, scale is not inherited. */   
    164     void setScale(float x, float y, float z) { scale.set(x, y, z); } 
    165     void setScale(Vec3f scale) { setScale(scale.x, scale.y, scale.z); } /// ditto 
    166     void setScale(float scale){ setScale(scale, scale, scale); } /// ditto 
    167     Vec3f getScale() { return scale; } /// ditto 
     172    void setScale(float x, float y, float z)  
     173    {   scale.set(x, y, z);  
     174    } 
     175    void setScale(Vec3f scale) /// ditto 
     176    {   setScale(scale.x, scale.y, scale.z);  
     177    } 
     178    void setScale(float scale) /// ditto 
     179    {   setScale(scale, scale, scale);  
     180    }  
     181    Vec3f getScale() /// ditto 
     182    {   return scale;  
     183    } 
    168184 
    169185    /**  
    170186     * Gt /set whether this Node will be rendered.  This has nothing to do with frustum culling. 
    171187     * Setting a Node as invisible will not make its children invisible also. */ 
    172     void setVisible(bool visible) { this.visible = visible; } 
    173     bool getVisible() { return visible; } /// ditto 
     188    void setVisible(bool visible)  
     189    {   this.visible = visible; 
     190    } 
     191    bool getVisible()  /// ditto 
     192    {   return visible; 
     193    } 
    174194     
    175195 
    176196    /// Remove this Node.  This function should be used instead of delete. 
    177197    void remove() 
    178     {   if (parent && this in parent.children) 
    179             parent.children.remove(this); 
    180          
     198    {    
    181199        // this needs to happen because some children (like lights) may need to do more in their remove() function. 
    182200        foreach(Node c; children) 
    183201            c.remove(); 
     202         
     203        if (parent && this in parent.children) 
     204            parent.children.remove(this); 
    184205    } 
    185206 
     
    191212    in { assert(_parent !is null); 
    192213    }body 
    193     {   if (parent && this in parent.children) 
     214    {            
     215        if (parent && this in parent.children) 
    194216            parent.children.remove(this); 
    195217         
     
    208230     * It normally doesn't need to be called manually.*/ 
    209231    void update(float delta) 
    210     {   lifetime-= delta; 
     232    {    
     233        lifetime-= delta; 
    211234 
    212235        // Move by linear velocity if not zero. 
  • trunk/src/yage/node/scene.d

    r57 r67  
    4949    protected bool  fog_enabled = false; 
    5050    protected float speed_of_sound = 343;   // 343m/s is the speed of sound in air at sea level.