| 1 | #include "osg/Stencil" |
|---|
| 2 | |
|---|
| 3 | #include "osgDB/Registry" |
|---|
| 4 | #include "osgDB/Input" |
|---|
| 5 | #include "osgDB/Output" |
|---|
| 6 | |
|---|
| 7 | #include <string.h> |
|---|
| 8 | |
|---|
| 9 | using namespace osg; |
|---|
| 10 | using namespace osgDB; |
|---|
| 11 | using namespace std; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | bool Stencil_readLocalData(Object& obj, Input& fr); |
|---|
| 15 | bool Stencil_writeLocalData(const Object& obj, Output& fw); |
|---|
| 16 | |
|---|
| 17 | bool Stencil_matchFuncStr(const char* str,Stencil::Function& func); |
|---|
| 18 | const char* Stencil_getFuncStr(Stencil::Function func); |
|---|
| 19 | bool Stencil_matchOperationStr(const char* str,Stencil::Operation& op); |
|---|
| 20 | const char* Stencil_getOperationStr(Stencil::Operation op); |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | REGISTER_DOTOSGWRAPPER(Stencil) |
|---|
| 25 | ( |
|---|
| 26 | new osg::Stencil, |
|---|
| 27 | "Stencil", |
|---|
| 28 | "Object StateAttribute Stencil", |
|---|
| 29 | &Stencil_readLocalData, |
|---|
| 30 | &Stencil_writeLocalData |
|---|
| 31 | ); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | bool Stencil_readLocalData(Object& obj, Input& fr) |
|---|
| 35 | { |
|---|
| 36 | bool iteratorAdvanced = false; |
|---|
| 37 | |
|---|
| 38 | Stencil& stencil = static_cast<Stencil&>(obj); |
|---|
| 39 | |
|---|
| 40 | bool setFunction = false; |
|---|
| 41 | Stencil::Function func = stencil.getFunction(); |
|---|
| 42 | if (fr[0].matchWord("function") && Stencil_matchFuncStr(fr[1].getStr(),func)) |
|---|
| 43 | { |
|---|
| 44 | setFunction = true; |
|---|
| 45 | fr+=2; |
|---|
| 46 | iteratorAdvanced = true; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | int ref = stencil.getFunctionRef(); |
|---|
| 50 | if (fr[0].matchWord("functionRef") && fr[1].getInt(ref)) |
|---|
| 51 | { |
|---|
| 52 | setFunction = true; |
|---|
| 53 | fr+=2; |
|---|
| 54 | iteratorAdvanced = true; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | unsigned int mask = stencil.getFunctionMask(); |
|---|
| 58 | if (fr[0].matchWord("functionMask") && fr[1].getUInt(mask)) |
|---|
| 59 | { |
|---|
| 60 | setFunction = true; |
|---|
| 61 | fr+=2; |
|---|
| 62 | iteratorAdvanced = true; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | if (setFunction) stencil.setFunction(func,ref,mask); |
|---|
| 66 | |
|---|
| 67 | bool setOperation = false; |
|---|
| 68 | osg::Stencil::Operation sfail = stencil.getStencilFailOperation(); |
|---|
| 69 | if (fr[0].matchWord("stencilFailOperation") && Stencil_matchOperationStr(fr[1].getStr(),sfail)) |
|---|
| 70 | { |
|---|
| 71 | setOperation = true; |
|---|
| 72 | fr+=2; |
|---|
| 73 | iteratorAdvanced = true; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | osg::Stencil::Operation zfail = stencil.getStencilPassAndDepthFailOperation(); |
|---|
| 77 | if (fr[0].matchWord("stencilPassAndDepthFailOperation") && Stencil_matchOperationStr(fr[1].getStr(),zfail)) |
|---|
| 78 | { |
|---|
| 79 | setOperation = true; |
|---|
| 80 | fr+=2; |
|---|
| 81 | iteratorAdvanced = true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | osg::Stencil::Operation zpass = stencil.getStencilPassAndDepthPassOperation(); |
|---|
| 85 | if (fr[0].matchWord("stencilPassAndDepthPassOperation") && Stencil_matchOperationStr(fr[1].getStr(),zpass)) |
|---|
| 86 | { |
|---|
| 87 | setOperation = true; |
|---|
| 88 | fr+=2; |
|---|
| 89 | iteratorAdvanced = true; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if (setOperation) stencil.setOperation(sfail, zfail, zpass); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | if (fr[0].matchWord("writeMask") && fr[1].getUInt(mask)) |
|---|
| 96 | { |
|---|
| 97 | stencil.setWriteMask(mask); |
|---|
| 98 | fr+=2; |
|---|
| 99 | iteratorAdvanced = true; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | return iteratorAdvanced; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | bool Stencil_writeLocalData(const Object& obj,Output& fw) |
|---|
| 107 | { |
|---|
| 108 | const Stencil& stencil = static_cast<const Stencil&>(obj); |
|---|
| 109 | |
|---|
| 110 | fw.indent() << "function " << Stencil_getFuncStr(stencil.getFunction()) << std::endl; |
|---|
| 111 | fw.indent() << "functionRef " << stencil.getFunctionRef() << std::endl; |
|---|
| 112 | fw.indent() << "functionMask 0x" << hex << stencil.getFunctionMask() << dec << std::endl; |
|---|
| 113 | |
|---|
| 114 | fw.indent() << "stencilFailOperation " << Stencil_getOperationStr(stencil.getStencilFailOperation()) << std::endl; |
|---|
| 115 | fw.indent() << "stencilPassAndDepthFailOperation " << Stencil_getOperationStr(stencil.getStencilPassAndDepthFailOperation()) << std::endl; |
|---|
| 116 | fw.indent() << "stencilPassAndDepthPassOperation " << Stencil_getOperationStr(stencil.getStencilPassAndDepthPassOperation()) << std::endl; |
|---|
| 117 | |
|---|
| 118 | fw.indent() << "writeMask 0x" << hex << stencil.getWriteMask() << dec << std::endl; |
|---|
| 119 | |
|---|
| 120 | return true; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | bool Stencil_matchFuncStr(const char* str,Stencil::Function& func) |
|---|
| 125 | { |
|---|
| 126 | if (strcmp(str,"NEVER")==0) func = Stencil::NEVER; |
|---|
| 127 | else if (strcmp(str,"LESS")==0) func = Stencil::LESS; |
|---|
| 128 | else if (strcmp(str,"EQUAL")==0) func = Stencil::EQUAL; |
|---|
| 129 | else if (strcmp(str,"LEQUAL")==0) func = Stencil::LEQUAL; |
|---|
| 130 | else if (strcmp(str,"GREATER")==0) func = Stencil::GREATER; |
|---|
| 131 | else if (strcmp(str,"NOTEQUAL")==0) func = Stencil::NOTEQUAL; |
|---|
| 132 | else if (strcmp(str,"GEQUAL")==0) func = Stencil::GEQUAL; |
|---|
| 133 | else if (strcmp(str,"ALWAYS")==0) func = Stencil::ALWAYS; |
|---|
| 134 | else return false; |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | const char* Stencil_getFuncStr(Stencil::Function func) |
|---|
| 140 | { |
|---|
| 141 | switch(func) |
|---|
| 142 | { |
|---|
| 143 | case(Stencil::NEVER): return "NEVER"; |
|---|
| 144 | case(Stencil::LESS): return "LESS"; |
|---|
| 145 | case(Stencil::EQUAL): return "EQUAL"; |
|---|
| 146 | case(Stencil::LEQUAL): return "LEQUAL"; |
|---|
| 147 | case(Stencil::GREATER): return "GREATER"; |
|---|
| 148 | case(Stencil::NOTEQUAL): return "NOTEQUAL"; |
|---|
| 149 | case(Stencil::GEQUAL): return "GEQUAL"; |
|---|
| 150 | case(Stencil::ALWAYS): return "ALWAYS"; |
|---|
| 151 | } |
|---|
| 152 | return ""; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | bool Stencil_matchOperationStr(const char* str,Stencil::Operation& op) |
|---|
| 156 | { |
|---|
| 157 | if (strcmp(str,"KEEP")==0) op = Stencil::KEEP; |
|---|
| 158 | else if (strcmp(str,"ZERO")==0) op = Stencil::ZERO; |
|---|
| 159 | else if (strcmp(str,"REPLACE")==0) op = Stencil::REPLACE; |
|---|
| 160 | else if (strcmp(str,"INCR")==0) op = Stencil::INCR; |
|---|
| 161 | else if (strcmp(str,"DECR")==0) op = Stencil::DECR; |
|---|
| 162 | else if (strcmp(str,"INVERT")==0) op = Stencil::INVERT; |
|---|
| 163 | else if (strcmp(str,"INCR_WRAP")==0) op = Stencil::INCR_WRAP; |
|---|
| 164 | else if (strcmp(str,"DECR_WRAP")==0) op = Stencil::DECR_WRAP; |
|---|
| 165 | else return false; |
|---|
| 166 | return true; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | const char* Stencil_getOperationStr(Stencil::Operation op) |
|---|
| 170 | { |
|---|
| 171 | switch(op) |
|---|
| 172 | { |
|---|
| 173 | case(Stencil::KEEP): return "KEEP"; |
|---|
| 174 | case(Stencil::ZERO): return "ZERO"; |
|---|
| 175 | case(Stencil::REPLACE): return "REPLACE"; |
|---|
| 176 | case(Stencil::INCR): return "INCR"; |
|---|
| 177 | case(Stencil::DECR): return "DECR"; |
|---|
| 178 | case(Stencil::INVERT): return "INVERT"; |
|---|
| 179 | case(Stencil::INCR_WRAP): return "INCR_WRAP"; |
|---|
| 180 | case(Stencil::DECR_WRAP): return "DECR_WRAP"; |
|---|
| 181 | } |
|---|
| 182 | return ""; |
|---|
| 183 | } |
|---|