Basic model abstraction

- Stop providing a buffered model supplier
 - Instead, provide an IModel supplier
 - IModel exposes basic properties of models
 - IModel exposes a method to copy the model to a VecBuffer
This commit is contained in:
Jozufozu 2021-07-23 12:26:33 -07:00
parent b188f97ae0
commit f06983c339

View file

@ -1,5 +1,6 @@
package com.simibubi.create.content.contraptions.components.structureMovement.glue; package com.simibubi.create.content.contraptions.components.structureMovement.glue;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.instancing.ITickableInstance; import com.jozufozu.flywheel.backend.instancing.ITickableInstance;
import com.jozufozu.flywheel.backend.instancing.Instancer; import com.jozufozu.flywheel.backend.instancing.Instancer;
@ -7,12 +8,15 @@ import com.jozufozu.flywheel.backend.material.MaterialManager;
import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance;
import com.jozufozu.flywheel.backend.material.MaterialGroup; import com.jozufozu.flywheel.backend.material.MaterialGroup;
import com.jozufozu.flywheel.backend.model.BufferedModel; import com.jozufozu.flywheel.backend.model.BufferedModel;
import com.jozufozu.flywheel.backend.model.ElementBuffer;
import com.jozufozu.flywheel.backend.model.IndexedModel; import com.jozufozu.flywheel.backend.model.IndexedModel;
import com.jozufozu.flywheel.backend.state.TextureRenderState; import com.jozufozu.flywheel.backend.state.TextureRenderState;
import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.Materials;
import com.jozufozu.flywheel.core.QuadConverter;
import com.jozufozu.flywheel.core.instancing.ConditionalInstance; import com.jozufozu.flywheel.core.instancing.ConditionalInstance;
import com.jozufozu.flywheel.core.materials.OrientedData; import com.jozufozu.flywheel.core.materials.OrientedData;
import com.jozufozu.flywheel.core.model.IModel;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.AllStitchedTextures; import com.simibubi.create.AllStitchedTextures;
import com.simibubi.create.Create; import com.simibubi.create.Create;
@ -54,7 +58,7 @@ public class GlueInstance extends EntityInstance<SuperGlueEntity> implements ITi
private Instancer<OrientedData> getInstancer(MaterialManager<?> materialManager, SuperGlueEntity entity) { private Instancer<OrientedData> getInstancer(MaterialManager<?> materialManager, SuperGlueEntity entity) {
MaterialGroup<?> group = USE_ATLAS ? materialManager.defaultSolid() : materialManager.solid(TextureRenderState.get(TEXTURE)); MaterialGroup<?> group = USE_ATLAS ? materialManager.defaultSolid() : materialManager.solid(TextureRenderState.get(TEXTURE));
return group.material(Materials.ORIENTED).model(entity.getType(), GlueInstance::supplyModel); return group.material(Materials.ORIENTED).model(entity.getType(), GlueModel::new);
} }
@Override @Override
@ -95,64 +99,76 @@ public class GlueInstance extends EntityInstance<SuperGlueEntity> implements ITi
|| AllItems.SUPER_GLUE.isIn(player.getOffhandItem()); || AllItems.SUPER_GLUE.isIn(player.getOffhandItem());
} }
public static BufferedModel supplyModel() { public static class GlueModel implements IModel {
Vector3d diff = Vector3d.atLowerCornerOf(Direction.SOUTH.getNormal()); @Override
Vector3d extension = diff.normalize() public void buffer(VecBuffer buffer) {
.scale(1 / 32f - 1 / 128f); Vector3d diff = Vector3d.atLowerCornerOf(Direction.SOUTH.getNormal());
Vector3d extension = diff.normalize()
.scale(1 / 32f - 1 / 128f);
Vector3d plane = VecHelper.axisAlingedPlaneOf(diff); Vector3d plane = VecHelper.axisAlingedPlaneOf(diff);
Direction.Axis axis = Direction.getNearest(diff.x, diff.y, diff.z) Direction.Axis axis = Direction.getNearest(diff.x, diff.y, diff.z)
.getAxis(); .getAxis();
Vector3d start = Vector3d.ZERO.subtract(extension); Vector3d start = Vector3d.ZERO.subtract(extension);
Vector3d end = Vector3d.ZERO.add(extension); Vector3d end = Vector3d.ZERO.add(extension);
plane = plane.scale(1 / 2f); plane = plane.scale(1 / 2f);
Vector3d a1 = plane.add(start); Vector3d a1 = plane.add(start);
Vector3d b1 = plane.add(end); Vector3d b1 = plane.add(end);
plane = VecHelper.rotate(plane, -90, axis); plane = VecHelper.rotate(plane, -90, axis);
Vector3d a2 = plane.add(start); Vector3d a2 = plane.add(start);
Vector3d b2 = plane.add(end); Vector3d b2 = plane.add(end);
plane = VecHelper.rotate(plane, -90, axis); plane = VecHelper.rotate(plane, -90, axis);
Vector3d a3 = plane.add(start); Vector3d a3 = plane.add(start);
Vector3d b3 = plane.add(end); Vector3d b3 = plane.add(end);
plane = VecHelper.rotate(plane, -90, axis); plane = VecHelper.rotate(plane, -90, axis);
Vector3d a4 = plane.add(start); Vector3d a4 = plane.add(start);
Vector3d b4 = plane.add(end); Vector3d b4 = plane.add(end);
VecBuffer buffer = VecBuffer.allocate(Formats.UNLIT_MODEL.getStride() * 8); float minU;
float maxU;
float minV;
float maxV;
float minU; if (USE_ATLAS) {
float maxU; TextureAtlasSprite sprite = AllStitchedTextures.SUPER_GLUE.getSprite();
float minV; minU = sprite.getU0();
float maxV; maxU = sprite.getU1();
minV = sprite.getV0();
maxV = sprite.getV1();
} else {
minU = minV = 0;
maxU = maxV = 1;
}
if (USE_ATLAS) { // pos normal uv
TextureAtlasSprite sprite = AllStitchedTextures.SUPER_GLUE.getSprite(); // inside quad
minU = sprite.getU0(); buffer.putVec3((float) a1.x, (float) a1.y, (float) a1.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(maxU, minV);
maxU = sprite.getU1(); buffer.putVec3((float) a2.x, (float) a2.y, (float) a2.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(maxU, maxV);
minV = sprite.getV0(); buffer.putVec3((float) a3.x, (float) a3.y, (float) a3.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(minU, maxV);
maxV = sprite.getV1(); buffer.putVec3((float) a4.x, (float) a4.y, (float) a4.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(minU, minV);
} else { // outside quad
minU = minV = 0; buffer.putVec3((float) b4.x, (float) b4.y, (float) b4.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(minU, minV);
maxU = maxV = 1; buffer.putVec3((float) b3.x, (float) b3.y, (float) b3.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(minU, maxV);
buffer.putVec3((float) b2.x, (float) b2.y, (float) b2.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(maxU, maxV);
buffer.putVec3((float) b1.x, (float) b1.y, (float) b1.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(maxU, minV);
} }
// pos normal uv @Override
// inside quad public int vertexCount() {
buffer.putVec3((float) a1.x, (float) a1.y, (float) a1.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(maxU, minV); return 8;
buffer.putVec3((float) a2.x, (float) a2.y, (float) a2.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(maxU, maxV); }
buffer.putVec3((float) a3.x, (float) a3.y, (float) a3.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(minU, maxV);
buffer.putVec3((float) a4.x, (float) a4.y, (float) a4.z).putVec3((byte) 0, (byte) 0, (byte) -127).putVec2(minU, minV);
// outside quad
buffer.putVec3((float) b4.x, (float) b4.y, (float) b4.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(minU, minV);
buffer.putVec3((float) b3.x, (float) b3.y, (float) b3.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(minU, maxV);
buffer.putVec3((float) b2.x, (float) b2.y, (float) b2.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(maxU, maxV);
buffer.putVec3((float) b1.x, (float) b1.y, (float) b1.z).putVec3((byte) 0, (byte) 0, (byte) 127).putVec2(maxU, minV);
buffer.rewind(); @Override
public VertexFormat format() {
return Formats.UNLIT_MODEL;
}
@Override
return IndexedModel.fromSequentialQuads(Formats.UNLIT_MODEL, buffer.unwrap(), 8); public ElementBuffer createEBO() {
return QuadConverter.getInstance()
.quads2Tris(2);
}
} }
} }