Skip to content

Commit

Permalink
feat: add skinned mesh Material
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Nov 29, 2021
1 parent 63d817a commit 06f2189
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class StandardSkinnedMeshData extends SkinnedMeshData {
public static final int BONE_IDX1_INDEX = 5;
public static final int BONE_IDX2_INDEX = 6;
public static final int BONE_IDX3_INDEX = 7;
public static final int BONE_WEIGHT_INDEX = 7;
public static final int BONE_WEIGHT_INDEX = 8;

public final VertexResource positionBuffer;
public final VertexAttributeBinding<Vector3fc, Vector3f> position;
Expand Down Expand Up @@ -90,19 +90,19 @@ public StandardSkinnedMeshData(DrawingMode mode, AllocationType allocationType,
uv1Buffer = builder.build();

builder = new VertexResourceBuilder();
boneIndex0 = builder.add(BONE_IDX0_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
boneIndex0 = builder.add(BONE_IDX0_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE, VertexResource.FEATURE_INTEGER);
boneIndexBuffer0 = builder.build();

builder = new VertexResourceBuilder();
boneIndex1 = builder.add(BONE_IDX1_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
boneIndex1 = builder.add(BONE_IDX1_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE, VertexResource.FEATURE_INTEGER);
boneIndexBuffer1 = builder.build();

builder = new VertexResourceBuilder();
boneIndex2 = builder.add(BONE_IDX2_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
boneIndex2 = builder.add(BONE_IDX2_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE, VertexResource.FEATURE_INTEGER);
boneIndexBuffer2 = builder.build();

builder = new VertexResourceBuilder();
boneIndex3 = builder.add(BONE_IDX3_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
boneIndex3 = builder.add(BONE_IDX3_INDEX, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE, VertexResource.FEATURE_INTEGER);
boneIndexBuffer3 = builder.build();

builder = new VertexResourceBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* A resource that represents vertex data
*/
public class VertexResource extends BufferedResource {
public static final int FEATURE_NORMALIZED = 0x1;
public static final int FEATURE_INTEGER = 0x2;

private int inStride = 0;
private VertexDefinition[] attributes;

Expand Down Expand Up @@ -102,14 +105,17 @@ public boolean isEmpty() {
* describes the metadata and placement into the buffer based off the stride.
*/
public static class VertexDefinition {

public final int location;
public final BaseVertexAttribute attribute;
public final int offset;
public final int features;

public VertexDefinition(int location, int offset, BaseVertexAttribute attribute) {
public VertexDefinition(int location, int offset, BaseVertexAttribute attribute, int features) {
this.location = location;
this.attribute = attribute;
this.offset = offset;
this.features = features;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,61 @@ public VertexResourceBuilder() {
* @param <T> the target object type
* @param <I> a class implementing the target object type
*/
public <T, I extends T> VertexAttributeBinding<T, I> add(int location, VertexAttribute<T, I> attribute) {
public <T, I extends T> VertexAttributeBinding<T, I> add(int location, VertexAttribute<T, I> attribute, int feature) {
VertexAttributeBinding<T, I> result = new VertexAttributeBinding<T, I>(resource, inStride, attribute);
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute));
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute, feature));
inStride += attribute.mapping.size * attribute.count;
return result;
}

public VertexIntegerAttributeBinding add(int location, VertexIntegerAttribute attribute) {
public <T, I extends T> VertexAttributeBinding<T, I> add(int location, VertexAttribute<T, I> attribute) {
return add(location, attribute, 0);
}

public VertexIntegerAttributeBinding add(int location, VertexIntegerAttribute attribute, int feature) {
VertexIntegerAttributeBinding result = new VertexIntegerAttributeBinding(resource, inStride, attribute);
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute));
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute, feature));
inStride += attribute.mapping.size * attribute.count;
return result;
}

public VertexFloatAttributeBinding add(int location, VertexFloatAttribute attribute) {
public VertexIntegerAttributeBinding add(int location, VertexIntegerAttribute attribute) {
return add(location, attribute, 0);
}

public VertexFloatAttributeBinding add(int location, VertexFloatAttribute attribute, int features) {
VertexFloatAttributeBinding result = new VertexFloatAttributeBinding(resource, inStride, attribute);
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute));
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute, features));
inStride += attribute.mapping.size * attribute.count;
return result;
}

public VertexByteAttributeBinding add(int location, VertexByteAttribute attribute) {
public VertexFloatAttributeBinding add(int location, VertexFloatAttribute attribute) {
return add(location, attribute, 0);
}

public VertexByteAttributeBinding add(int location, VertexByteAttribute attribute, int features) {
VertexByteAttributeBinding result = new VertexByteAttributeBinding(resource, inStride, attribute);
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute));
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute, features));
inStride += attribute.mapping.size * attribute.count;
return result;
}

public VertexShortAttributeBinding add(int location, VertexShortAttribute attribute) {
public VertexByteAttributeBinding add(int location, VertexByteAttribute attribute) {
return add(location, attribute, 0);
}

public VertexShortAttributeBinding add(int location, VertexShortAttribute attribute, int features) {
VertexShortAttributeBinding result = new VertexShortAttributeBinding(resource, inStride, attribute);
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute));
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute, features));
inStride += attribute.mapping.size * attribute.count;
return result;
}

public VertexShortAttributeBinding add(int location, VertexShortAttribute attribute) {
return add(location, attribute, 0);
}

public VertexResource build() {
resource.setDefinitions(definitions.toArray(new VertexResource.VertexDefinition[]{}));
resource.allocate(0, inStride);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ public class SkeletonRenderer extends BaseComponentSystem implements RenderSyste
private Mesh mesh;
private Material material;

private FloatBuffer matrixBuffer = FloatBuffer.allocate(16 * 254);

private Random random = new Random();

@Override
Expand Down Expand Up @@ -306,19 +304,15 @@ public void renderOpaque() {
Matrix4f boneTransform = new Matrix4f();
boneLocation.getRelativeTransform(boneTransform, entity);
boneTransform.mul(bone.getInverseBindMatrix());
boneTransforms[bone.getIndex()] = boneTransform.transpose();
boneTransforms[bone.getIndex()] = boneTransform;
} else {
logger.warn("Unable to resolve bone \"{}\"", bone.getName());
boneTransforms[bone.getIndex()] = new Matrix4f();
}
}
matrixBuffer.rewind();
matrixBuffer.limit(16 * boneTransforms.length);
for (Matrix4f transform : boneTransforms) {
transform.get(matrixBuffer);
for (int i = 0; i < boneTransforms.length; i++) {
skeletalMesh.material.setMatrix4("boneTransforms[" + i + "]", boneTransforms[i], true);
}

material.setMatrix4("boneTransforms", matrixBuffer);
skeletalMesh.mesh.render();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ default VBOContext buildVBO(int vbo, AllocationType allocation, VertexResource[]
});
for (VertexResource.VertexDefinition attribute : resource.definitions()) {
GL30.glEnableVertexAttribArray(attribute.location);
GL30.glVertexAttribPointer(attribute.location, attribute.attribute.count,
attribute.attribute.mapping.glType, false, resource.inStride(), offset + attribute.offset);
boolean normalized = (attribute.features & VertexResource.FEATURE_NORMALIZED) > 0;
if ((attribute.features & VertexResource.FEATURE_INTEGER) > 0) {
GL30.glVertexAttribIPointer(attribute.location, attribute.attribute.count,
attribute.attribute.mapping.glType, resource.inStride(), offset + attribute.offset);
} else {
GL30.glVertexAttribPointer(attribute.location, attribute.attribute.count,
attribute.attribute.mapping.glType, normalized, resource.inStride(), offset + attribute.offset);
}


}
offset += resource.inSize();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"params": [
{
"name": "diffuse",
"type": "sampler2D"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ uniform bool textured;

in vec3 v_normal;
in vec2 v_uv0;
in vec4 v_color0;

layout(location = 0) out vec4 outColor;
layout(location = 1) out vec4 outNormal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ void main() {
skinMat += boneTransforms[int(in_bone[2])] * in_weight.z;
skinMat += boneTransforms[int(in_bone[3])] * in_weight.w;

gl_Position = (projectionMatrix * modelViewMatrix) * (skinMat * vec4(in_vert, 1.0));
gl_Position = (projectionMatrix * modelViewMatrix * skinMat) * vec4(in_vert, 1.0);
v_normal = normalMatrix * in_normal * mat3(skinMat);
v_uv0 = in_uv0;
v_color0 = in_color0;

}

0 comments on commit 06f2189

Please sign in to comment.