Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VBO (Significant performance boost with Angelica) #6

Merged
merged 18 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependencies {
compileOnly("com.github.GTNewHorizons:Angelica:1.0.0-alpha16:dev")
implementation("com.github.GTNewHorizons:GTNHLib:0.1.0:dev")
}
1 change: 0 additions & 1 deletion repositories.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Add any additional repositories for your dependencies here

repositories {

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

@Mod(
acceptedMinecraftVersions = "[1.7.10]",
dependencies = "required-after:gtnhlib@[0.0.10)",
dependencies = "required-after:gtnhlib@[0.0.10,);after:angelica",
modid = AmazingTrophies.MODID,
name = AmazingTrophies.MODNAME,
version = AmazingTrophies.VERSION)
Expand All @@ -52,6 +52,7 @@ public class AmazingTrophies {
public static final String VERSION = Reference.VERSION;
public static final Logger LOGGER = LogManager.getLogger(MODNAME);
public static final Path CONFIG_DIR = getConfigDir();
public static boolean isAngelicaLoaded = false;

@EventHandler
public static void construct(FMLConstructionEvent event) {
Expand All @@ -63,6 +64,8 @@ public static void construct(FMLConstructionEvent event) {

@EventHandler
public static void preInit(FMLPreInitializationEvent event) {
isAngelicaLoaded = Loader.isModLoaded("angelica");

registerConditionHandlers();

Block blockTrophy = new BlockTrophy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.AdvancedModelLoader;
import net.minecraftforge.client.model.IModelCustom;

import org.lwjgl.opengl.GL11;

Expand All @@ -21,20 +19,20 @@ public class BasicTrophyModelHandler extends PedestalTrophyModelHandler {
public static final String PROPERTY_MODEL = "model";
public static final String PROPERTY_TEXTURE = "texture";

private IModelCustom model;
private ModelWrapper<?> model;
private ResourceLocation texture;

public BasicTrophyModelHandler() {}

public BasicTrophyModelHandler(IModelCustom model, ResourceLocation texture) {
public BasicTrophyModelHandler(ModelWrapper<?> model, ResourceLocation texture) {
this.model = model;
this.texture = texture;
}

@Override
public void parse(String id, JsonObject json) throws JsonSyntaxException {
this.model = AdvancedModelLoader.loadModel(
AssetHandler.getResourceLocation(ConfigHandler.getStringProperty(json, PROPERTY_MODEL), "models/"));
this.model = ModelWrapper
.get(AssetHandler.getResourceLocation(ConfigHandler.getStringProperty(json, PROPERTY_MODEL), "models/"));
this.texture = AssetHandler
.getResourceLocation(ConfigHandler.getStringProperty(json, PROPERTY_TEXTURE), "textures/blocks/");
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/glowredman/amazingtrophies/model/ModelWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package glowredman.amazingtrophies.model;

import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.AdvancedModelLoader;
import net.minecraftforge.client.model.IModelCustom;
import net.minecraftforge.client.model.ModelFormatException;

import com.gtnewhorizons.angelica.config.AngelicaConfig;
import com.gtnewhorizons.angelica.mixins.interfaces.IModelCustomExt;

import glowredman.amazingtrophies.AmazingTrophies;

public abstract class ModelWrapper<T extends IModelCustom> {

protected T model;

public abstract void renderAll();

public static ModelWrapper<? extends IModelCustom> get(ResourceLocation resource)
throws IllegalArgumentException, ModelFormatException {
if (AmazingTrophies.isAngelicaLoaded && AngelicaConfig.enableVBO) {
return new ModelCustomWrapperExt(resource);
}
return new ModelCustomWrapper(resource);
}

private static class ModelCustomWrapper extends ModelWrapper<IModelCustom> {

private ModelCustomWrapper(ResourceLocation resource) throws IllegalArgumentException, ModelFormatException {
this.model = AdvancedModelLoader.loadModel(resource);
}

@Override
public void renderAll() {
this.model.renderAll();
}
}

private static class ModelCustomWrapperExt extends ModelWrapper<IModelCustomExt> {

private ModelCustomWrapperExt(ResourceLocation resource) throws IllegalArgumentException, ModelFormatException {
this.model = (IModelCustomExt) AdvancedModelLoader.loadModel(resource);
}

@Override
public void renderAll() {
if (AngelicaConfig.enableVBO) {
this.model.renderAllVBO();
} else {
this.model.renderAll();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.AdvancedModelLoader;
import net.minecraftforge.client.model.IModelCustom;

import org.lwjgl.opengl.GL11;

Expand All @@ -17,8 +15,8 @@ public class PedestalTrophyModelHandler extends TrophyModelHandler {

public static final String ID = "pedestal";

private static final IModelCustom MODEL_BASE = AdvancedModelLoader
.loadModel(new ResourceLocation(AmazingTrophies.MODID, "models/trophy_pedestal.obj"));
private static final ModelWrapper<?> MODEL_BASE = ModelWrapper
.get(new ResourceLocation(AmazingTrophies.MODID, "models/trophy_pedestal.obj"));
private static final ResourceLocation TEXTURE_BASE = new ResourceLocation(
AmazingTrophies.MODID,
"textures/blocks/trophy_pedestal.png");
Expand All @@ -34,6 +32,7 @@ public void render(double x, double y, double z, int rotation, @Nullable String
GL11.glPushMatrix();
GL11.glTranslated(x, y, z);
GL11.glRotatef(22.5f * rotation, 0.0f, 1.0f, 0.0f);

MODEL_BASE.renderAll();

// text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

public class BaseModelStructure {

/**
* This is an instance of {@code com.gtnewhorizons.angelica.compat.mojang.VertexBuffer} when Angelica is installed.
*/
protected Object vertexBuffer;
protected RenderFacesInfo[][][] renderFacesArray;
protected Map<Character, Pair<Block, Integer>> charToBlock = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.gson.JsonSyntaxException;

import cpw.mods.fml.common.registry.GameData;
import glowredman.amazingtrophies.AmazingTrophies;
import glowredman.amazingtrophies.ConfigHandler;
import glowredman.amazingtrophies.model.PedestalTrophyModelHandler;

Expand Down Expand Up @@ -153,7 +154,13 @@ public void render(double x, double y, double z, int rotation, @Nullable String
GL11.glTranslated(x, y - 0.5 + TROPHY_PEDESTAL_HEIGHT, z);
GL11.glRotatef(-90, 0.0f, 1.0f, 0.0f);
GL11.glRotatef(22.5f * rotation, 0.0f, 1.0f, 0.0f);
RenderHelper.renderModel(model);

if (AmazingTrophies.isAngelicaLoaded) {
RenderHelperAngelica.renderModel(model);
} else {
RenderHelper.renderModel(model);
}

GL11.glPopAttrib();
GL11.glPopMatrix();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraft.world.IBlockAccess;

public class CustomRenderBlocks extends RenderBlocks {

Expand All @@ -12,7 +12,7 @@ public class CustomRenderBlocks extends RenderBlocks {

private RenderFacesInfo renderFacesInfo;

public CustomRenderBlocks(World world) {
public CustomRenderBlocks(IBlockAccess world) {
super(world);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package glowredman.amazingtrophies.model.complex;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureMap;

import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import com.gtnewhorizons.angelica.client.renderer.CapturingTessellator;
import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat;
import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormat;
import com.gtnewhorizons.angelica.config.AngelicaConfig;
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
import com.gtnewhorizons.angelica.glsm.VBOManager;

public class RenderHelperAngelica {

private static void centreModel(BaseModelStructure model) {

String[][] testShape = model.getStructureString();

int x = testShape.length / 2;
int z = testShape[0][0].length() / 2;

GL11.glTranslated(-x, -0.5, -1 - z);
}

private static VertexBuffer rebuildVBO(BaseModelStructure model) {
final CustomRenderBlocks renderBlocks = new CustomRenderBlocks(Minecraft.getMinecraft().theWorld);
renderBlocks.enableAO = false;

TessellatorManager.startCapturing();
CapturingTessellator tessellator = (CapturingTessellator) TessellatorManager.get();
for (int x = 0; x < model.getXLength(); x++) {
for (int y = 0; y < model.getYLength(); y++) {
for (int z = 0; z < model.getZLength(); z++) {
final Character blockChar = model.getStructureString()[x][z].charAt(y);

if (blockChar.equals(' ')) continue;
if (model.renderFacesArray[x][z][y].allHidden()) continue;

final Pair<Block, Integer> blockInfo = model.getAssociatedBlockInfo(blockChar);

renderBlocks.setRenderFacesInfo(model.renderFacesArray[x][z][y]);
tessellator.setTranslation(x, z + 1, y + 1);
renderBlock(blockInfo.getLeft(), blockInfo.getRight(), renderBlocks);
}
}
}
tessellator.setTranslation(0, 0, 0);
final int vboId = VBOManager.generateDisplayLists(1);
final VertexBuffer vertexBuffer = TessellatorManager.stopCapturingToVBO(format);
VBOManager.registerVBO(vboId, vertexBuffer);

model.vertexBuffer = vertexBuffer;
return vertexBuffer;
}

static final VertexFormat format = DefaultVertexFormat.POSITION_TEXTURE_NORMAL;

private static void renderModelInternal(BaseModelStructure model) {

Minecraft.getMinecraft()
.getTextureManager()
.bindTexture(TextureMap.locationBlocksTexture);

// Build the VBO if needed and store it on the model
VertexBuffer vertexBuffer = (VertexBuffer) model.vertexBuffer;
if (model.vertexBuffer == null) {
vertexBuffer = rebuildVBO(model);
}

// Now render the VBO
GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glEnable(GL11.GL_LIGHTING);
// Unclear if this is needed
GL11.glEnable(GL12.GL_RESCALE_NORMAL);

vertexBuffer.render();

}

private static final float TROPHY_BASE_RATIO = 12.0f / 16.0f; // 12x12 top in 16x16 texture.

private static void scaleModel(final BaseModelStructure model) {
final float maxScale = TROPHY_BASE_RATIO / model.maxAxisSize();
GL11.glScalef(maxScale, maxScale, maxScale);
}

public static void renderModel(final BaseModelStructure model) {

if (model == null) return;

if (!AngelicaConfig.enableVBO) {
RenderHelper.renderModel(model);
return;
}

GL11.glPushMatrix();

scaleModel(model);
centreModel(model);
renderModelInternal(model);

GL11.glPopMatrix();
}

public static void renderBlock(Block block, int metadata, CustomRenderBlocks renderBlocks) {
renderBlocks.renderBlockAsItem(block, metadata, 1.0f);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"requiredMods": [],
"dependencies": [],
"dependants": [],
"useDependencyInformation": true
"useDependencyInformation": false
}]
}