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

Implement BW Rendering #252

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package nova.core.wrapper.mc.forge.v17.wrapper.block.backward;

import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -32,6 +34,7 @@
import nova.core.block.component.BlockProperty;
import nova.core.block.component.LightEmitter;
import nova.core.component.misc.Collider;
import nova.core.component.renderer.DynamicRenderer;
import nova.core.component.renderer.StaticRenderer;
import nova.core.component.transform.BlockTransform;
import nova.core.item.ItemFactory;
Expand All @@ -46,8 +49,12 @@
import nova.core.wrapper.mc.forge.v17.wrapper.block.world.BWWorld;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -97,11 +104,37 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
.map(cuboid -> cuboid.subtract(pos))
.collect(Collectors.toSet());
});
//TODO: Set selection bounds
components.add(new StaticRenderer())
.onRender(model -> model.addChild(new CustomModel(self -> RenderBlocks.getInstance().renderStandardBlock(mcBlock, x(), y(), z()))));
.onRender(model -> {
model.addChild(new CustomModel(self -> {
GL11.glPushMatrix();
DoubleBuffer buffer = BufferUtils.createDoubleBuffer(4 * 4);
double[] flatArray = Arrays.stream(self.matrix.getMatrix().getData())
.flatMapToDouble(Arrays::stream)
.toArray();
buffer.put(flatArray);
buffer.position(0);
GL11.glMultMatrix(buffer);
IBlockAccess backup = RenderBlocks.getInstance().blockAccess;
RenderBlocks.getInstance().blockAccess = getMcBlockAccess();
RenderBlocks.getInstance()
.renderBlockByRenderType(mcBlock, x(), y(), z());
RenderBlocks.getInstance().blockAccess = backup;
GL11.glPopMatrix();
}));
});
getTileEntity().ifPresent(tileEntity -> {
components.add(new DynamicRenderer())
.onRender(model -> {
model.addChild(new CustomModel(self -> {
TileEntityRendererDispatcher.instance.renderTileEntityAt(tileEntity, position().getX(), position().getY(), position().getZ(), 0);
}));
});
});

WrapperEvent.BWBlockCreate event = new WrapperEvent.BWBlockCreate(world, pos, this, mcBlock);
Game.events().publish(event);
//TODO: Set selection bounds
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import net.minecraft.util.DamageSource;
import nova.core.component.inventory.InventoryPlayer;
import nova.core.component.misc.Damageable;
import nova.core.component.renderer.DynamicRenderer;
import nova.core.entity.Entity;
import nova.core.entity.component.Living;
import nova.core.entity.component.Player;
import nova.core.render.model.CustomModel;
import nova.core.wrapper.mc.forge.v17.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.MCEntityTransform;
import nova.core.wrapper.mc.forge.v17.wrapper.inventory.BWInventory;
Expand Down Expand Up @@ -61,6 +63,12 @@ public void damage(double amount, DamageType type) {
}
});

components.add(new DynamicRenderer()).onRender(model -> {
model.addChild(new CustomModel(self -> {
net.minecraft.client.renderer.entity.RenderManager.instance.renderEntityWithPosYaw(entity, entity.posX, entity.posY, entity.posZ, entity.rotationYaw, 1);
}));
});

if (entity instanceof EntityLivingBase) {
if (entity instanceof EntityPlayer) {
MCPlayer player = components.add(new MCPlayer(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,21 @@ public BWItem(net.minecraft.item.Item item, int meta, NBTTagCompound tag) {

components.add(new StaticRenderer())
.onRender(model -> {
model.addChild(new CustomModel(self -> {
Tessellator.instance.draw();
GL11.glPushMatrix();
DoubleBuffer buffer = BufferUtils.createDoubleBuffer(4 * 4);
double[] flatArray = Arrays.stream(self.matrix.getMatrix().getData())
.flatMapToDouble(Arrays::stream)
.toArray();
buffer.put(flatArray);
buffer.position(0);
GL11.glMultMatrix(buffer);
RenderItem.getInstance().doRender(fakeEntity, 0, 0, 0, 0, 0);
GL11.glPopMatrix();
Tessellator.instance.startDrawingQuads();
}));
}
);
model.addChild(new CustomModel(self -> {
// Tessellator.instance.draw();
GL11.glPushMatrix();
DoubleBuffer buffer = BufferUtils.createDoubleBuffer(4 * 4);
double[] flatArray = Arrays.stream(self.matrix.getMatrix().getData())
.flatMapToDouble(Arrays::stream)
.toArray();
buffer.put(flatArray);
buffer.position(0);
GL11.glMultMatrix(buffer);
RenderItem.getInstance().doRender(fakeEntity, 0, 0, 0, 0, 0);
GL11.glPopMatrix();
// Tessellator.instance.startDrawingQuads();
}));
});
}

public net.minecraft.item.Item getItem() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package nova.core.wrapper.mc.forge.v17.wrapper.item;

import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
Expand All @@ -36,10 +37,14 @@
import nova.core.wrapper.mc.forge.v17.wrapper.render.BWModel;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import java.util.List;
import java.util.Optional;

import static org.lwjgl.opengl.GL11.GL_TEXTURE_BIT;

/**
* An interface implemented by ItemBlockWrapper and ItemWrapper classes to override Minecraft's item events.
* @author Calclavia
Expand Down Expand Up @@ -89,19 +94,29 @@ default boolean shouldUseRenderHelper(IItemRenderer.ItemRenderType type, ItemSta
@Override
default void renderItem(IItemRenderer.ItemRenderType type, ItemStack itemStack, Object... data) {
Item item = Game.natives().toNova(itemStack);
BWModel model = new BWModel();

if (item.components.has(StaticRenderer.class)) {
StaticRenderer staticRenderer = item.components.get(StaticRenderer.class);
staticRenderer.onRender.accept(model);
} else if (item.components.has(DynamicRenderer.class)) {
DynamicRenderer dynamicRenderer = item.components.get(DynamicRenderer.class);
dynamicRenderer.onRender.accept(model);
if (item.components.has(Renderer.class)) {
GL11.glPushAttrib(GL_TEXTURE_BIT);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glPushMatrix();
Tessellator.instance.startDrawingQuads();
BWModel model = new BWModel();
model.matrix.rotate(Direction.UP.toVector(), 1 / 4 * Math.PI);
model.matrix.rotate(Direction.EAST.toVector(), 1 / 6 * Math.PI);
model.matrix.scale(1.6, 1.6, 1.6);

if (item.components.has(StaticRenderer.class)) {
StaticRenderer staticRenderer = item.components.get(StaticRenderer.class);
staticRenderer.onRender.accept(model);
} else if (item.components.has(DynamicRenderer.class)) {
DynamicRenderer dynamicRenderer = item.components.get(DynamicRenderer.class);
dynamicRenderer.onRender.accept(model);
}

model.render();
Tessellator.instance.draw();
GL11.glPopMatrix();
GL11.glPopAttrib();
}

// For some reason calling model.faces.size() returns 0.
// Yet staticRenderer.onRender.accept(model) still gets executed.
model.render();
}

default int getColorFromItemStack(ItemStack itemStack, int p_82790_2_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public void render(Optional<IBlockAccess> access, Optional<RenderManager> entity
model -> {
if (model instanceof MeshModel) {
MeshModel meshModel = (MeshModel) model;
meshModel.faces.forEach(face ->
{
meshModel.faces.forEach(face -> {
// Brightness is defined as: skyLight << 20 | blockLight << 4
if (face.getBrightness() >= 0) {
tessellator.setBrightness((int) (face.getBrightness() * (15 << 20 | 11 << 4)));
Expand All @@ -80,15 +79,15 @@ public void render(Optional<IBlockAccess> access, Optional<RenderManager> entity

// TODO: Add Ambient Occlusion
/*
int aoBrightnessXYNN = block.getMixedBrightnessForBlock(blockAccess, translation.getX() - 1, translation.getY(), translation.zi());
int aoBrightnessYZNN = block.getMixedBrightnessForBlock(blockAccess, translation.getX(), translation.getY(), translation.zi() - 1);
int aoBrightnessYZNP = block.getMixedBrightnessForBlock(blockAccess, translation.getX(), translation.getY(), translation.zi() + 1);
int aoBrightnessXYPN = block.getMixedBrightnessForBlock(blockAccess, translation.getX() + 1, translation.getY(), translation.zi());

int brightnessTopLeft = getAoBrightness(aoBrightnessXYZNNP, this.aoBrightnessXYNN, this.aoBrightnessYZNP, i1);
int brightnessTopRight = getAoBrightness(aoBrightnessYZNP, this.aoBrightnessXYZPNP, this.aoBrightnessXYPN, i1);
int brightnessBottomRight = getAoBrightness(this.aoBrightnessYZNN, this.aoBrightnessXYPN, this.aoBrightnessXYZPNN, i1);
int brightnessBottomLeft = getAoBrightness(this.aoBrightnessXYNN, this.aoBrightnessXYZNNN, this.aoBrightnessYZNN, i1);
int aoBrightnessXYNN = block.getMixedBrightnessForBlock(access.get(), (int) nearestPos.getX() - 1, (int) nearestPos.getY(), (int) nearestPos.getZ());
int aoBrightnessYZNN = block.getMixedBrightnessForBlock(access.get(), (int) nearestPos.getX(), (int) nearestPos.getY(), (int) nearestPos.getZ() - 1);
int aoBrightnessYZNP = block.getMixedBrightnessForBlock(access.get(), (int) nearestPos.getX(), (int) nearestPos.getY(), (int) nearestPos.getZ() + 1);
int aoBrightnessXYPN = block.getMixedBrightnessForBlock(access.get(), (int) nearestPos.getX() + 1, (int) nearestPos.getY(), (int) nearestPos.getZ());

int brightnessTopLeft = getAoBrightness(aoBrightnessXYZNNP, aoBrightnessXYNN, aoBrightnessYZNP, i1);
int brightnessTopRight = getAoBrightness(aoBrightnessYZNP, aoBrightnessXYZPNP, aoBrightnessXYPN, i1);
int brightnessBottomRight = getAoBrightness(aoBrightnessYZNN, aoBrightnessXYPN, aoBrightnessXYZPNN, i1);
int brightnessBottomLeft = getAoBrightness(aoBrightnessXYNN, aoBrightnessXYZNNN, aoBrightnessYZNN, i1);
*/

tessellator.setBrightness(brightness);
Expand All @@ -98,7 +97,7 @@ public void render(Optional<IBlockAccess> access, Optional<RenderManager> entity
tessellator.setBrightness(15 << 20 | 11 << 4);
}

tessellator.setNormal((int) face.normal.getX(), (int) face.normal.getY(), (int) face.normal.getZ());
tessellator.setNormal((float) face.normal.getX(), (float) face.normal.getY(), (float) face.normal.getZ());

if (face.texture.isPresent()) {
if (entityRenderManager.isPresent() && face.texture.get() instanceof EntityTexture) {
Expand All @@ -107,6 +106,11 @@ public void render(Optional<IBlockAccess> access, Optional<RenderManager> entity
entityRenderManager.get().renderEngine.bindTexture(AssetConverter.instance().toNative(t));
face.vertices.forEach(
v -> {
if (v.normal.isPresent())
tessellator.setNormal((float) v.normal.get().getX(), (float) v.normal.get().getY(), (float) v.normal.get().getZ());
else
tessellator.setNormal((float) face.normal.getX(), (float) face.normal.getY(), (float) face.normal.getZ());

tessellator.setColorRGBA(v.color.red(), v.color.green(), v.color.blue(), v.color.alpha());
tessellator.addVertexWithUV(v.vec.getX(), v.vec.getY(), v.vec.getZ(), v.uv.getX(), v.uv.getY());
}
Expand All @@ -116,6 +120,11 @@ public void render(Optional<IBlockAccess> access, Optional<RenderManager> entity
IIcon icon = RenderUtility.instance.getIcon(texture);
face.vertices.forEach(
v -> {
if (v.normal.isPresent())
tessellator.setNormal((float) v.normal.get().getX(), (float) v.normal.get().getY(), (float) v.normal.get().getZ());
else
tessellator.setNormal((float) face.normal.getX(), (float) face.normal.getY(), (float) face.normal.getZ());

tessellator.setColorRGBA(v.color.red(), v.color.green(), v.color.blue(), v.color.alpha());
if (icon != null) {
tessellator.addVertexWithUV(v.vec.getX(), v.vec.getY(), v.vec.getZ(), icon.getInterpolatedU(16 * v.uv.getX()), icon.getInterpolatedV(16 * v.uv.getY()));
Expand All @@ -128,6 +137,11 @@ public void render(Optional<IBlockAccess> access, Optional<RenderManager> entity
} else {
face.vertices.forEach(
v -> {
if (v.normal.isPresent())
tessellator.setNormal((float) v.normal.get().getX(), (float) v.normal.get().getY(), (float) v.normal.get().getZ());
else
tessellator.setNormal((float) face.normal.getX(), (float) face.normal.getY(), (float) face.normal.getZ());

tessellator.setColorRGBA(v.color.red(), v.color.green(), v.color.blue(), v.color.alpha());
tessellator.addVertex(v.vec.getX(), v.vec.getY(), v.vec.getZ());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

import net.minecraft.block.BlockSnow;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -34,8 +38,11 @@
import nova.core.block.component.BlockProperty;
import nova.core.block.component.LightEmitter;
import nova.core.component.misc.Collider;
import nova.core.component.renderer.DynamicRenderer;
import nova.core.component.renderer.StaticRenderer;
import nova.core.component.transform.BlockTransform;
import nova.core.item.ItemFactory;
import nova.core.render.model.CustomModel;
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.retention.Store;
Expand All @@ -44,10 +51,15 @@
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v18.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v18.wrapper.block.world.BWWorld;
import nova.core.wrapper.mc.forge.v18.wrapper.render.backward.BWBakedModel;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -97,9 +109,50 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
.map(cuboid -> cuboid.subtract(pos))
.collect(Collectors.toSet());
});
//TODO: Set selection bounds
components.add(new StaticRenderer())
.onRender(model -> {
switch (block.getRenderType()) {
default:
// rendering of other type
case 1:
// fluid rendering
model.addChild(new CustomModel(self -> {
GL11.glPushMatrix();
DoubleBuffer buffer = BufferUtils.createDoubleBuffer(4 * 4);
double[] flatArray = Arrays.stream(self.matrix.getMatrix().getData())
.flatMapToDouble(Arrays::stream)
.toArray();
buffer.put(flatArray);
buffer.position(0);
GL11.glMultMatrix(buffer);
Minecraft.getMinecraft().getBlockRendererDispatcher()
.renderBlock(blockState(), new BlockPos(x(), y(), z()), getMcBlockAccess(), Tessellator.getInstance().getWorldRenderer());
GL11.glPopMatrix();
}));
break;
case 2:
// chest rendering
// Handled by DynamicRenderer
break;
case 3:
// model rendering
model.addChild(new BWBakedModel(Minecraft.getMinecraft().getBlockRendererDispatcher()
.getModelFromBlockState(blockState(), getMcBlockAccess(), new BlockPos(x(), y(), z())), DefaultVertexFormats.BLOCK));
break;
}
});
getTileEntity().ifPresent(tileEntity -> {
components.add(new DynamicRenderer())
.onRender(model -> {
model.addChild(new CustomModel(self -> {
TileEntityRendererDispatcher.instance.renderTileEntityAt(tileEntity, position().getX(), position().getY(), position().getZ(), 0);
}));
});
});

WrapperEvent.BWBlockCreate event = new WrapperEvent.BWBlockCreate(world, pos, this, mcBlock);
Game.events().publish(event);
//TODO: Set selection bounds
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

package nova.core.wrapper.mc.forge.v18.wrapper.entity.backward;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.DamageSource;
import nova.core.component.inventory.InventoryPlayer;
import nova.core.component.misc.Damageable;
import nova.core.component.renderer.DynamicRenderer;
import nova.core.entity.Entity;
import nova.core.entity.component.Living;
import nova.core.entity.component.Player;
import nova.core.render.model.CustomModel;
import nova.core.wrapper.mc.forge.v18.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v18.wrapper.entity.forward.MCEntityTransform;
import nova.core.wrapper.mc.forge.v18.wrapper.inventory.BWInventory;
Expand Down Expand Up @@ -61,6 +64,12 @@ public void damage(double amount, DamageType type) {
}
});

components.add(new DynamicRenderer()).onRender(model -> {
model.addChild(new CustomModel(self -> {
Minecraft.getMinecraft().getRenderManager().renderEntityWithPosYaw(entity, entity.posX, entity.posY, entity.posZ, entity.rotationYaw, 1);
}));
});

if (entity instanceof EntityLivingBase) {
if (entity instanceof EntityPlayer) {
MCPlayer player = components.add(new MCPlayer(this));
Expand Down
Loading