[1] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "OgreMaterialManager.h" |
---|
| 31 | #include "OgreHighLevelGpuProgramManager.h" |
---|
| 32 | #include "OgreGpuProgramManager.h" |
---|
| 33 | #include "MaterialScriptCompilerTests.h" |
---|
| 34 | #include "OgreStringConverter.h" |
---|
| 35 | |
---|
| 36 | #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) |
---|
| 37 | |
---|
| 38 | // Regsiter the suite |
---|
| 39 | CPPUNIT_TEST_SUITE_REGISTRATION( MaterialScriptCompilerTests ); |
---|
| 40 | |
---|
| 41 | void MaterialScriptCompilerTests::setUp() |
---|
| 42 | { |
---|
| 43 | } |
---|
| 44 | void MaterialScriptCompilerTests::tearDown() |
---|
| 45 | { |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void MaterialScriptCompilerTests::testPositionToNextSymbol() |
---|
| 49 | { |
---|
| 50 | struct test1result{ |
---|
| 51 | const char character; |
---|
| 52 | const int line; |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | const String TestStr1 = " \n\r //c \n\r// test\n\r \t c - \n\r , e"; |
---|
| 56 | const test1result test1results[] = { |
---|
| 57 | {'c', 4}, |
---|
| 58 | {'-', 4}, |
---|
| 59 | {',', 5}, |
---|
| 60 | {'e', 5} |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | // first test: see if positionToNextSymbol can find a valid Symbol |
---|
| 64 | mSource = &TestStr1; |
---|
| 65 | mCharPos = 0; |
---|
| 66 | size_t resultID = 0; |
---|
| 67 | mCurrentLine = 1; |
---|
| 68 | mEndOfSource = mSource->length(); |
---|
| 69 | while (positionToNextLexeme()) { |
---|
| 70 | CPPUNIT_ASSERT_MESSAGE( "test " + StringConverter::toString(resultID) + " character found: " + (*mSource)[mCharPos] + |
---|
| 71 | " Line:%d : " + StringConverter::toString(mCurrentLine) |
---|
| 72 | , ((*mSource)[mCharPos] == test1results[resultID].character) && (mCurrentLine==test1results[resultID].line) ); |
---|
| 73 | resultID++; |
---|
| 74 | mCharPos++; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void MaterialScriptCompilerTests::testIsFloatValue(void) |
---|
| 80 | { |
---|
| 81 | struct testfloatresult{ |
---|
| 82 | const String teststr; |
---|
| 83 | const float fvalue; |
---|
| 84 | const size_t charsize; |
---|
| 85 | testfloatresult(const String& _teststr, const float _fvalue, const size_t _charsize) |
---|
| 86 | : teststr(_teststr) |
---|
| 87 | , fvalue(_fvalue) |
---|
| 88 | , charsize(_charsize) |
---|
| 89 | {}; |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | testfloatresult testfloatresults[] = { |
---|
| 93 | testfloatresult("1 test", 1.0f, 1), |
---|
| 94 | testfloatresult("2.3f test", 2.3f, 3), |
---|
| 95 | testfloatresult("-0.5 test", -0.5f, 4), |
---|
| 96 | testfloatresult(" 23.6 test", 23.6f, 5), |
---|
| 97 | testfloatresult(" -0.021 test", -0.021f, 8), |
---|
| 98 | testfloatresult("12 test", 12.0f, 2), |
---|
| 99 | testfloatresult("3test", 3.0f, 1) |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | float fvalue = 0; |
---|
| 103 | size_t charsize = 0; |
---|
| 104 | mCharPos = 0; |
---|
| 105 | size_t testsize = ARRAYSIZE(testfloatresults); |
---|
| 106 | for(size_t resultID=0; resultID<testsize; resultID++) |
---|
| 107 | { |
---|
| 108 | mSource = &testfloatresults[resultID].teststr; |
---|
| 109 | isFloatValue(fvalue, charsize); |
---|
| 110 | CPPUNIT_ASSERT_MESSAGE( "test " + StringConverter::toString(resultID) + " value returned: " + StringConverter::toString(fvalue) |
---|
| 111 | , fvalue == testfloatresults[resultID].fvalue); |
---|
| 112 | CPPUNIT_ASSERT_MESSAGE( "test " + StringConverter::toString(resultID) + " char size returned: " + StringConverter::toString(charsize) |
---|
| 113 | , charsize == testfloatresults[resultID].charsize); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void MaterialScriptCompilerTests::testIsLexemeMatch(void) |
---|
| 119 | { |
---|
| 120 | const String TestStr = "material test"; |
---|
| 121 | const String TestSymbols = "material"; |
---|
| 122 | |
---|
| 123 | mSource = &TestStr; |
---|
| 124 | mCharPos = 0; |
---|
| 125 | CPPUNIT_ASSERT(isLexemeMatch(TestSymbols, false)); |
---|
| 126 | CPPUNIT_ASSERT(isLexemeMatch(TestSymbols, true)); |
---|
| 127 | mCharPos = 1; |
---|
| 128 | CPPUNIT_ASSERT(!isLexemeMatch(TestSymbols, false)); |
---|
| 129 | CPPUNIT_ASSERT(!isLexemeMatch(TestSymbols, true)); |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void MaterialScriptCompilerTests::testCompileMaterialScript() |
---|
| 134 | { |
---|
| 135 | Math* mth = new Math(); |
---|
| 136 | if (!ResourceGroupManager::getSingletonPtr()) |
---|
| 137 | new ResourceGroupManager(); |
---|
| 138 | |
---|
| 139 | if (!MaterialManager::getSingletonPtr()) |
---|
| 140 | { |
---|
| 141 | new MaterialManager(); |
---|
| 142 | MaterialManager::getSingleton().initialise(); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if (!HighLevelGpuProgramManager::getSingletonPtr()) |
---|
| 146 | new HighLevelGpuProgramManager(); |
---|
| 147 | |
---|
| 148 | //if (!GpuProgramManager::getSingletonPtr()) |
---|
| 149 | // new GpuProgramManager(); |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | const String simpleScript = |
---|
| 153 | "vertex_program boo cg { \n" |
---|
| 154 | " source program.cg \n" |
---|
| 155 | " entry_point main \n" |
---|
| 156 | " profiles vs_1_1 arbvp1 \n" |
---|
| 157 | " } \n" |
---|
| 158 | "material test2 { \n" |
---|
| 159 | " receive_shadows on lod_distances 100 203.6 700 \n" |
---|
| 160 | " set_texture_alias foo-bar MyTexture.png \n" |
---|
| 161 | " transparency_casts_shadows on \n" |
---|
| 162 | " technique { scheme hdr lod_index 1 \n" |
---|
| 163 | " pass pass-1 { \n" |
---|
| 164 | " ambient 0 1 0.5\n" |
---|
| 165 | " ambient 0 1 0.5 1 \n" |
---|
| 166 | " ambient vertexcolour \n" |
---|
| 167 | " diffuse 0.5 1.0 0 0.5 \n" |
---|
| 168 | " specular vertexcolour 40 \n" |
---|
| 169 | " specular 1 1 1 18 \n" |
---|
| 170 | " specular 0 1 0 1 128 \n" |
---|
| 171 | " scene_blend alpha_blend \n" |
---|
| 172 | " scene_blend one src_alpha \n" |
---|
| 173 | " depth_check on \n" |
---|
| 174 | " depth_write off \n" |
---|
| 175 | " depth_func less_equal \n" |
---|
| 176 | " depth_bias 5 \n" |
---|
| 177 | " alpha_rejection not_equal 127 \n" |
---|
| 178 | " cull_hardware clockwise \n" |
---|
| 179 | " cull_software front \n" |
---|
| 180 | " lighting on \n" |
---|
| 181 | " shading gouraud \n" |
---|
| 182 | " polygon_mode wireframe \n" |
---|
| 183 | " fog_override false \n" |
---|
| 184 | " fog_override true exp 1 1 1 0.002 100 10000 \n" |
---|
| 185 | " fog_override true \n" |
---|
| 186 | " colour_write off \n" |
---|
| 187 | " max_lights 7 \n" |
---|
| 188 | " iteration once \n" |
---|
| 189 | " iteration once_per_light point \n" |
---|
| 190 | " iteration 5 \n" |
---|
| 191 | " iteration 5 per_light point \n" |
---|
| 192 | " point_size 3 \n" |
---|
| 193 | " point_sprites on \n" |
---|
| 194 | " point_size_attenuation on \n" |
---|
| 195 | " point_size_min 1.00 \n" |
---|
| 196 | " point_size_max 10 \n" |
---|
| 197 | " texture_unit first {texture MyAlphaTexture.png alpha tex_coord_set 0 } \n" |
---|
| 198 | " texture_unit first { \n" |
---|
| 199 | " cubic_texture cubemap_fr.jpg \t cubemap_bk.jpg cubemap_lf.jpg cubemap_rt.jpg cubemap_up.jpg cubemap_dn.jpg separateUV \n" |
---|
| 200 | " } \n" |
---|
| 201 | " texture_unit \"second one\" { \n" |
---|
| 202 | " texture_alias foo-bar \n" |
---|
| 203 | " anim_texture MyAlphaTexture2.png 4 6.5 \n" |
---|
| 204 | " anim_texture MyAlphaTexture2.png MyAlphaTexture6.png MyAlphaTexture9.png 6.5 \n" |
---|
| 205 | " anim_texture MyAlphaTexture2.png MyAlphaTexture6.png \n" |
---|
| 206 | " MyAlphaTexture9.png MyAlphaTexture11.png MyAlphaTexture12.png 6.5 \n" |
---|
| 207 | " tex_address_mode wrap \n" |
---|
| 208 | " tex_address_mode wrap mirror \n" |
---|
| 209 | " tex_address_mode wrap mirror border \n" |
---|
| 210 | " tex_border_colour 1.0 0.5 0.7 1.0 \n" |
---|
| 211 | " filtering trilinear \n" |
---|
| 212 | " filtering none \n" |
---|
| 213 | " filtering linear point none \n" |
---|
| 214 | " max_anisotropy 5 \n" |
---|
| 215 | " colour_op replace \n" |
---|
| 216 | " colour_op add \n" |
---|
| 217 | " colour_op_ex add_signed src_manual src_texture 1 1 1 1\n" |
---|
| 218 | " colour_op_multipass_fallback one one_minus_dest_alpha \n" |
---|
| 219 | " alpha_op_ex add_signed src_manual src_current 0.5 \n" |
---|
| 220 | " env_map cubic_reflection \n" |
---|
| 221 | " scroll 5 2 \n" |
---|
| 222 | " scroll_anim 2 4 \n" |
---|
| 223 | " rotate 2.3 \n" |
---|
| 224 | " rotate_anim 0.5 \n" |
---|
| 225 | " scale 1 1.4 \n" |
---|
| 226 | " wave_xform scale_x sine 1.0 0.2 0.0 5.0 \n" |
---|
| 227 | " transform 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 \n" |
---|
| 228 | |
---|
| 229 | " } \n" |
---|
| 230 | " texture_unit \"second one\" { \n" |
---|
| 231 | " anim_texture MyAlphaTexture2.png MyAlphaTexture3.png 6.5} \n" |
---|
| 232 | " \n" |
---|
| 233 | " } } } \n" |
---|
| 234 | "material clone 1 : test2 {} \n" |
---|
| 235 | // GPU Programs |
---|
| 236 | "vertex_program miny cg { \n" |
---|
| 237 | " source program.cg \n" |
---|
| 238 | " entry_point main \n" |
---|
| 239 | " profiles vs_1_1 arbvp1 \n" |
---|
| 240 | " } \n" |
---|
| 241 | "vertex_program tiny hlsl { \n" |
---|
| 242 | " source program.hlsl \n" |
---|
| 243 | " entry_point main \n" |
---|
| 244 | " target vs_1_1 \n" |
---|
| 245 | " default_params { \n" |
---|
| 246 | " param_named google float4 0 1 2 3 \n" |
---|
| 247 | " param_indexed 1 int 0 \n" |
---|
| 248 | " param_indexed_auto 0 blah 0 \n" |
---|
| 249 | " param_named_auto chicken mustard \n" |
---|
| 250 | " } \n" |
---|
| 251 | " } \n" |
---|
| 252 | "fragment_program better glsl { \n" |
---|
| 253 | " source program.glsl \n" |
---|
| 254 | " attach specular.glsl \n" |
---|
| 255 | " } \n" |
---|
| 256 | ; |
---|
| 257 | CPPUNIT_ASSERT(compile(simpleScript, "MaterialScriptTest")); |
---|
| 258 | |
---|
| 259 | delete mth; |
---|
| 260 | } |
---|
| 261 | |
---|