MCPcopy Index your code
hub / github.com/codenameone/CodenameOne / build

Method build

CodenameOne/src/com/codename1/gpu/GltfLoader.java:185–254  ·  view source on GitHub ↗
(GraphicsDevice device, Map root, byte[] binChunk)

Source from the content-addressed store, hash-verified

183 }
184
185 private static Mesh build(GraphicsDevice device, Map root, byte[] binChunk) {
186 List meshes = (List) root.get("meshes");
187 if (meshes == null || meshes.isEmpty()) {
188 throw new IllegalArgumentException("glTF has no meshes");
189 }
190 Map mesh = (Map) meshes.get(0);
191 List primitives = (List) mesh.get("primitives");
192 if (primitives == null || primitives.isEmpty()) {
193 throw new IllegalArgumentException("glTF mesh has no primitives");
194 }
195 Map primitive = (Map) primitives.get(0);
196 // Mode 4 (TRIANGLES) is the default and the only mode this loader builds.
197 int mode = primitive.containsKey("mode") ? asInt(primitive.get("mode")) : 4;
198 if (mode != 4) {
199 throw new IllegalArgumentException("glTF primitive mode " + mode + " is not supported (TRIANGLES only)");
200 }
201 Map attributes = (Map) primitive.get("attributes");
202 if (attributes == null || !attributes.containsKey("POSITION")) {
203 throw new IllegalArgumentException("glTF primitive has no POSITION attribute");
204 }
205
206 List accessors = (List) root.get("accessors");
207 List bufferViews = (List) root.get("bufferViews");
208 List buffers = (List) root.get("buffers");
209
210 float[] positions = readFloatAccessor(asInt(attributes.get("POSITION")), 3,
211 accessors, bufferViews, buffers, binChunk);
212 int vertexCount = positions.length / 3;
213 float[] normals = attributes.containsKey("NORMAL")
214 ? readFloatAccessor(asInt(attributes.get("NORMAL")), 3, accessors, bufferViews, buffers, binChunk)
215 : null;
216 float[] texcoords = attributes.containsKey("TEXCOORD_0")
217 ? readFloatAccessor(asInt(attributes.get("TEXCOORD_0")), 2, accessors, bufferViews, buffers, binChunk)
218 : null;
219
220 int[] indices;
221 if (primitive.containsKey("indices")) {
222 indices = readIntAccessor(asInt(primitive.get("indices")), accessors, bufferViews, buffers, binChunk);
223 } else {
224 indices = new int[vertexCount];
225 for (int i = 0; i < vertexCount; i++) {
226 indices[i] = i;
227 }
228 }
229
230 if (normals == null) {
231 normals = computeFlatNormals(positions, indices);
232 }
233
234 float[] interleaved = new float[vertexCount * FLOATS_PER_VERTEX];
235 for (int i = 0; i < vertexCount; i++) {
236 int o = i * FLOATS_PER_VERTEX;
237 interleaved[o] = positions[i * 3];
238 interleaved[o + 1] = positions[i * 3 + 1];
239 interleaved[o + 2] = positions[i * 3 + 2];
240 interleaved[o + 3] = normals[i * 3];
241 interleaved[o + 4] = normals[i * 3 + 1];
242 interleaved[o + 5] = normals[i * 3 + 2];

Callers 2

loadMethod · 0.95
loadModelMethod · 0.95

Calls 13

isEmptyMethod · 0.95
getMethod · 0.95
getMethod · 0.95
containsKeyMethod · 0.95
asIntMethod · 0.95
readFloatAccessorMethod · 0.95
readIntAccessorMethod · 0.95
computeFlatNormalsMethod · 0.95
setDataMethod · 0.95
setDataMethod · 0.95
createVertexBufferMethod · 0.80
createIndexBufferMethod · 0.80

Tested by

no test coverage detected