|
| 1 | +require "XmlWriter" |
| 2 | +require "vmath" |
| 3 | + |
| 4 | +local function GenStringFromArray(theArray, bAsInt) |
| 5 | + local array = {" "} |
| 6 | + for i, vector in ipairs(theArray) do |
| 7 | + local elements = vector; |
| 8 | + if(bAsInt) then |
| 9 | + elements = {}; |
| 10 | + for i, value in ipairs(vector) do |
| 11 | + elements[#elements + 1] = string.format("%i", value); |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + array[#array + 1] = " " .. table.concat(vector, " "); |
| 16 | + end |
| 17 | + |
| 18 | + return table.concat(array, "\n"); |
| 19 | +end |
| 20 | + |
| 21 | +local positions = |
| 22 | +{ |
| 23 | + --Front |
| 24 | + vmath.vec3( 0.5, 0.5, 0.5), |
| 25 | + vmath.vec3( 0.5, -0.5, 0.5), |
| 26 | + vmath.vec3(-0.5, -0.5, 0.5), |
| 27 | + vmath.vec3(-0.5, 0.5, 0.5), |
| 28 | + |
| 29 | + --Top |
| 30 | + vmath.vec3( 0.5, 0.5, 0.5), |
| 31 | + vmath.vec3(-0.5, 0.5, 0.5), |
| 32 | + vmath.vec3(-0.5, 0.5, -0.5), |
| 33 | + vmath.vec3( 0.5, 0.5, -0.5), |
| 34 | + |
| 35 | + --Left |
| 36 | + vmath.vec3( 0.5, 0.5, 0.5), |
| 37 | + vmath.vec3( 0.5, 0.5, -0.5), |
| 38 | + vmath.vec3( 0.5, -0.5, -0.5), |
| 39 | + vmath.vec3( 0.5, -0.5, 0.5), |
| 40 | + |
| 41 | + --Back |
| 42 | + vmath.vec3( 0.5, 0.5, -0.5), |
| 43 | + vmath.vec3(-0.5, 0.5, -0.5), |
| 44 | + vmath.vec3(-0.5, -0.5, -0.5), |
| 45 | + vmath.vec3( 0.5, -0.5, -0.5), |
| 46 | + |
| 47 | + --Bottom |
| 48 | + vmath.vec3( 0.5, -0.5, 0.5), |
| 49 | + vmath.vec3( 0.5, -0.5, -0.5), |
| 50 | + vmath.vec3(-0.5, -0.5, -0.5), |
| 51 | + vmath.vec3(-0.5, -0.5, 0.5), |
| 52 | + |
| 53 | + --Right |
| 54 | + vmath.vec3(-0.5, 0.5, 0.5), |
| 55 | + vmath.vec3(-0.5, -0.5, 0.5), |
| 56 | + vmath.vec3(-0.5, -0.5, -0.5), |
| 57 | + vmath.vec3(-0.5, 0.5, -0.5), |
| 58 | +}; |
| 59 | + |
| 60 | +local normals = |
| 61 | +{ |
| 62 | + --Front |
| 63 | + vmath.vec3(0.0, 0.0, 1.0), |
| 64 | + vmath.vec3(0.0, 0.0, 1.0), |
| 65 | + vmath.vec3(0.0, 0.0, 1.0), |
| 66 | + vmath.vec3(0.0, 0.0, 1.0), |
| 67 | + |
| 68 | + --Top |
| 69 | + vmath.vec3(0.0, 1.0, 0.0), |
| 70 | + vmath.vec3(0.0, 1.0, 0.0), |
| 71 | + vmath.vec3(0.0, 1.0, 0.0), |
| 72 | + vmath.vec3(0.0, 1.0, 0.0), |
| 73 | + |
| 74 | + --Left |
| 75 | + vmath.vec3(1.0, 0.0, 0.0), |
| 76 | + vmath.vec3(1.0, 0.0, 0.0), |
| 77 | + vmath.vec3(1.0, 0.0, 0.0), |
| 78 | + vmath.vec3(1.0, 0.0, 0.0), |
| 79 | + |
| 80 | + --Back |
| 81 | + vmath.vec3(0.0, 0.0, -1.0), |
| 82 | + vmath.vec3(0.0, 0.0, -1.0), |
| 83 | + vmath.vec3(0.0, 0.0, -1.0), |
| 84 | + vmath.vec3(0.0, 0.0, -1.0), |
| 85 | + |
| 86 | + --Bottom |
| 87 | + vmath.vec3(0.0, -1.0, 0.0), |
| 88 | + vmath.vec3(0.0, -1.0, 0.0), |
| 89 | + vmath.vec3(0.0, -1.0, 0.0), |
| 90 | + vmath.vec3(0.0, -1.0, 0.0), |
| 91 | + |
| 92 | + --Right |
| 93 | + vmath.vec3(-1.0, 0.0, 0.0), |
| 94 | + vmath.vec3(-1.0, 0.0, 0.0), |
| 95 | + vmath.vec3(-1.0, 0.0, 0.0), |
| 96 | + vmath.vec3(-1.0, 0.0, 0.0), |
| 97 | +} |
| 98 | + |
| 99 | +local colors = |
| 100 | +{ |
| 101 | + vmath.vec4(0.25, 1.0, 0.25, 1.0), |
| 102 | + vmath.vec4(0.25, 1.0, 0.25, 1.0), |
| 103 | + vmath.vec4(0.25, 1.0, 0.25, 1.0), |
| 104 | + vmath.vec4(0.25, 1.0, 0.25, 1.0), |
| 105 | + |
| 106 | + vmath.vec4(0.5, 0.5, 1.0, 1.0), |
| 107 | + vmath.vec4(0.5, 0.5, 1.0, 1.0), |
| 108 | + vmath.vec4(0.5, 0.5, 1.0, 1.0), |
| 109 | + vmath.vec4(0.5, 0.5, 1.0, 1.0), |
| 110 | + |
| 111 | + vmath.vec4(1.0, 0.5, 0.5, 1.0), |
| 112 | + vmath.vec4(1.0, 0.5, 0.5, 1.0), |
| 113 | + vmath.vec4(1.0, 0.5, 0.5, 1.0), |
| 114 | + vmath.vec4(1.0, 0.5, 0.5, 1.0), |
| 115 | + |
| 116 | + vmath.vec4(1.0, 1.0, 0.5, 1.0), |
| 117 | + vmath.vec4(1.0, 1.0, 0.5, 1.0), |
| 118 | + vmath.vec4(1.0, 1.0, 0.5, 1.0), |
| 119 | + vmath.vec4(1.0, 1.0, 0.5, 1.0), |
| 120 | + |
| 121 | + vmath.vec4(0.5, 1.0, 1.0, 1.0), |
| 122 | + vmath.vec4(0.5, 1.0, 1.0, 1.0), |
| 123 | + vmath.vec4(0.5, 1.0, 1.0, 1.0), |
| 124 | + vmath.vec4(0.5, 1.0, 1.0, 1.0), |
| 125 | + |
| 126 | + vmath.vec4(1.0, 0.5, 1.0, 1.0), |
| 127 | + vmath.vec4(1.0, 0.5, 1.0, 1.0), |
| 128 | + vmath.vec4(1.0, 0.5, 1.0, 1.0), |
| 129 | + vmath.vec4(1.0, 0.5, 1.0, 1.0), |
| 130 | +} |
| 131 | + |
| 132 | +local indices = |
| 133 | +{ |
| 134 | + vmath.vec3(0, 1, 2), |
| 135 | + vmath.vec3(2, 3, 0), |
| 136 | + |
| 137 | + vmath.vec3(4, 5, 6), |
| 138 | + vmath.vec3(6, 7, 4), |
| 139 | + |
| 140 | + vmath.vec3(8, 9, 10), |
| 141 | + vmath.vec3(10, 11, 8), |
| 142 | + |
| 143 | + vmath.vec3(12, 13, 14), |
| 144 | + vmath.vec3(14, 15, 12), |
| 145 | + |
| 146 | + vmath.vec3(16, 17, 18), |
| 147 | + vmath.vec3(18, 19, 16), |
| 148 | + |
| 149 | + vmath.vec3(20, 21, 22), |
| 150 | + vmath.vec3(22, 23, 20), |
| 151 | +}; |
| 152 | + |
| 153 | +do |
| 154 | + local writer = XmlWriter.XmlWriter("UnitCube.xml"); |
| 155 | + writer:AddPI("oxygen", [[RNGSchema="../../Documents/meshFormat.rnc" type="compact"]]); |
| 156 | + writer:PushElement("mesh", "http://www.arcsynthesis.com/gltut/mesh"); |
| 157 | + writer:PushElement("attribute"); |
| 158 | + writer:AddAttribute("index", "0"); |
| 159 | + writer:AddAttribute("type", "float"); |
| 160 | + writer:AddAttribute("size", "3"); |
| 161 | + writer:AddText(GenStringFromArray(positions)); |
| 162 | + writer:PopElement(); |
| 163 | + writer:PushElement("attribute"); |
| 164 | + writer:AddAttribute("index", "1"); |
| 165 | + writer:AddAttribute("type", "float"); |
| 166 | + writer:AddAttribute("size", "4"); |
| 167 | + writer:AddText(GenStringFromArray(colors)); |
| 168 | + writer:PopElement(); |
| 169 | + writer:PushElement("attribute"); |
| 170 | + writer:AddAttribute("index", "2"); |
| 171 | + writer:AddAttribute("type", "float"); |
| 172 | + writer:AddAttribute("size", "3"); |
| 173 | + writer:AddText(GenStringFromArray(normals)); |
| 174 | + writer:PopElement(); |
| 175 | + writer:PushElement("vao"); |
| 176 | + writer:AddAttribute("name", "lit"); |
| 177 | + writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement(); |
| 178 | + writer:PushElement("source"); writer:AddAttribute("attrib", "2"); writer:PopElement(); |
| 179 | + writer:PopElement(); |
| 180 | + writer:PushElement("vao"); |
| 181 | + writer:AddAttribute("name", "lit-color"); |
| 182 | + writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement(); |
| 183 | + writer:PushElement("source"); writer:AddAttribute("attrib", "1"); writer:PopElement(); |
| 184 | + writer:PushElement("source"); writer:AddAttribute("attrib", "2"); writer:PopElement(); |
| 185 | + writer:PopElement(); |
| 186 | + writer:PushElement("vao"); |
| 187 | + writer:AddAttribute("name", "color"); |
| 188 | + writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement(); |
| 189 | + writer:PushElement("source"); writer:AddAttribute("attrib", "1"); writer:PopElement(); |
| 190 | + writer:PopElement(); |
| 191 | + writer:PushElement("vao"); |
| 192 | + writer:AddAttribute("name", "flat"); |
| 193 | + writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement(); |
| 194 | + writer:PopElement(); |
| 195 | + writer:PushElement("indices"); |
| 196 | + writer:AddAttribute("cmd", "triangles"); |
| 197 | + writer:AddAttribute("type", "ushort"); |
| 198 | + writer:AddText(GenStringFromArray(indices, true)); |
| 199 | + writer:PopElement(); |
| 200 | + writer:PopElement(); |
| 201 | + writer:Close(); |
| 202 | +end |
0 commit comments