Skip to content

Commit

Permalink
Merge pull request #103 from javagl/builder-extensions
Browse files Browse the repository at this point in the history
Extensions and bugfixes for the model builder package
  • Loading branch information
javagl authored Apr 11, 2024
2 parents 8dfc2a9 + 25e4b9b commit ebeb9b9
Show file tree
Hide file tree
Showing 34 changed files with 2,099 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import de.javagl.jgltf.model.AccessorData;
import de.javagl.jgltf.model.AccessorDatas;
import de.javagl.jgltf.model.AccessorModel;
import de.javagl.jgltf.model.Accessors;
import de.javagl.jgltf.model.BufferViewModel;
import de.javagl.jgltf.model.ElementType;
import de.javagl.jgltf.model.GltfConstants;
import de.javagl.jgltf.model.GltfModel;
import de.javagl.jgltf.model.MeshPrimitiveModel;
import de.javagl.jgltf.model.impl.DefaultAccessorModel;
import de.javagl.jgltf.model.io.Buffers;

Expand All @@ -47,10 +50,56 @@
* {@link AccessorModel#getAccessorData() accessor data} that
* simply represents the data that was given at construction time.
* The instances do <b>not</b> have an associated {@link BufferViewModel}
* instance.
* instance.<br>
* <br>
* The instances of accessor models that are created by this class
* are supposed to be used during the construction of a glTF model,
* using the {@link GltfModelBuilder}: They hold the data of the
* model elements (for example, attributes in a {@link MeshPrimitiveModel}).<br>
* <br>
* The {@link GltfModelBuilder} will use the accessor data internally,
* in order to build the associated buffers and buffer views.<br>
* <br>
* This class offers some convenience methods for commonly used accessor
* model types - for example, 3D floating point vectors (positions and
* normals), or unsigned int scalar values (for indices).<br>
* <br>
* Arbitrary (more specific) accessor model types can be constructed with
* the {@link #create(int, String, boolean, ByteBuffer)} method, which
* receives all information that is required for the accessor model.
* This method expects the data to be given as a byte buffer. When the
* input data is a <code>FloatBuffer</code>, then the byte buffer can be
* created with the utility methods in the {@link Buffers} class - for
* example, {@link Buffers#createByteBufferFrom(FloatBuffer)}.
*/
public class AccessorModels
{
/**
* Creates a copy of the given accessor model, only based on its
* {@link AccessorData}.<br>
* <br>
* This will return a model that has the same data type and data
* as the given model, but without an associated buffer view.<br>
* <br>
* This may be used to create a copy of an accessor model from
* one {@link GltfModel}, and pass it to a {@link GltfModelBuilder}
* for the construction of a new {@link GltfModel}.
*
* @param input The input
* @return The copy
*/
public static DefaultAccessorModel copy(AccessorModel input)
{
AccessorData inputAccessorData = input.getAccessorData();
ByteBuffer byteBuffer = inputAccessorData.createByteBuffer();
DefaultAccessorModel copy = AccessorModels.create(
input.getComponentType(),
input.getElementType().toString(),
input.isNormalized(),
byteBuffer);
return copy;
}

/**
* Creates a new {@link AccessorModel} with the component type
* <code>GL_UNSIGNED_INT</code> and the type <code>"SCALAR"</code>.
Expand Down Expand Up @@ -330,7 +379,7 @@ public static DefaultAccessorModel create(
throw new IllegalArgumentException(
"Invalid data for type " + type + " accessor with "
+ Accessors.getDataTypeForAccessorComponentType(componentType)
+ "components: The data length is " + byteBuffer.capacity()
+ " components: The data length is " + byteBuffer.capacity()
+ " which is not divisble by " + numBytesPerElement);
}
int count = byteBuffer.capacity() / numBytesPerElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ void processSkinModels(

/**
* Process all {@link AccessorModel} instances from the given collection
* by the given {@link SkinModel} instances
*
* @param accessorModels The {@link AccessorModel} instances
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ public boolean isPaddingByteIndex(BufferModel bufferModel, int index)
* contained in the given collection
*
* @param prefix The prefix for the ID
* @param existingIds The existing URI strings
* @return The new URI string
* @param existingIds The existing ID strings
* @return The new ID string
*/
private String createId(String prefix,
Collection<? extends String> existingIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import de.javagl.jgltf.model.BufferViewModel;
import de.javagl.jgltf.model.ElementType;
import de.javagl.jgltf.model.GltfConstants;
import de.javagl.jgltf.model.GltfException;
import de.javagl.jgltf.model.impl.DefaultAccessorModel;
import de.javagl.jgltf.model.impl.DefaultBufferModel;
import de.javagl.jgltf.model.impl.DefaultBufferViewModel;
Expand Down Expand Up @@ -147,6 +148,30 @@ public int getNumBufferModels()
{
return bufferStructure.getBufferModels().size();
}

/**
* Returns the number of {@link AccessorModel} instances that have been
* added, but for which no {@link BufferViewModel} has been created
* yet.
*
* @return The number of pending {@link AccessorModel} instances
*/
public int getNumCurrentAccessorModels()
{
return currentAccessorModels.size();
}

/**
* Returns the number of {@link BufferViewModel} instances that have been
* added, but for which no {@link BufferModel} has been created
* yet.
*
* @return The number of pending {@link BufferViewModel} instances
*/
public int getNumCurrentBufferViewModels()
{
return currentBufferViewModels.size();
}

/**
* Create an {@link AccessorModel} in the {@link BufferStructure} that
Expand Down Expand Up @@ -281,10 +306,18 @@ public AccessorModel createAccessorModel(String idPrefix,
*
* @param idPrefix The ID prefix of the {@link AccessorModel}
* @param accessorModel The {@link AccessorModel}
* @throws GltfException If the given accessor model already has an
* associated buffer view
*/
public void addAccessorModel(
String idPrefix, DefaultAccessorModel accessorModel)
{
BufferViewModel bufferViewModel = accessorModel.getBufferViewModel();
if (bufferViewModel != null)
{
throw new GltfException(
"The accessor already contains a buffer view");
}
bufferStructure.addAccessorModel(accessorModel, idPrefix);
currentAccessorModels.add(accessorModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@

/**
* Utility methods for creating the glTF 1.0 elements that correspond to
* a {@link BufferStructure}
* a {@link BufferStructure}.<br>
* <br>
* This class is only intended for internal use, and may be removed in
* the future.
*/
public class BufferStructureGltfV1
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@

/**
* Utility methods for creating the glTF 2.0 elements that correspond to
* a {@link BufferStructure}
* a {@link BufferStructure}.<br>
* <br>
* This class is only intended for internal use, and may be removed in
* the future.
*/
public class BufferStructureGltfV2
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
import de.javagl.jgltf.model.io.GltfReference;

/**
* Utility methods related to {@link BufferStructure} instances.
* Utility methods related to {@link BufferStructure} instances.<br>
* <br>
* Only intended for internal use.
*/
public class BufferStructures
Expand Down
Loading

0 comments on commit ebeb9b9

Please sign in to comment.