Skip to content

Commit

Permalink
Removed Graphics data and singleton TextAtlas, now controlled by each…
Browse files Browse the repository at this point in the history
… task
  • Loading branch information
Hilligans committed Dec 26, 2023
1 parent bc78ea6 commit f27ddec
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 136 deletions.
8 changes: 4 additions & 4 deletions src/main/java/dev/hilligans/ourcraft/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public void generateTextures(TextAtlas textAtlas) {
blockProperties.blockTextureManager.generate(textAtlas);
}

public void addVertices(PrimitiveBuilder primitiveBuilder, int side, float size, BlockState blockState, BlockPos blockPos, int x, int z) {
blockProperties.blockShape. addVertices(primitiveBuilder,side,size,blockState,blockProperties.blockTextureManager, new BlockPos(x,blockPos.y,z).get3f());
public void addVertices(TextAtlas textAtlas, PrimitiveBuilder primitiveBuilder, int side, float size, BlockState blockState, BlockPos blockPos, int x, int z) {
blockProperties.blockShape. addVertices(textAtlas, primitiveBuilder,side,size,blockState,blockProperties.blockTextureManager, new BlockPos(x,blockPos.y,z).get3f());
}

public int getSide(BlockState blockState, int side) {
Expand Down Expand Up @@ -260,8 +260,8 @@ public IBlockState getDefaultState1() {
return table.getBlockState(0);
}

public void addVertices(PrimitiveBuilder primitiveBuilder, int side, float size, IBlockState blockState, BlockPos blockPos, int x, int z) {
blockProperties.blockShape.addVertices(primitiveBuilder,side,size,blockState,blockProperties.blockTextureManager,new Vector3f(), x,blockPos.y,z);
public void addVertices(TextAtlas textAtlas, PrimitiveBuilder primitiveBuilder, int side, float size, IBlockState blockState, BlockPos blockPos, int x, int z) {
blockProperties.blockShape.addVertices(textAtlas, primitiveBuilder,side,size,blockState,blockProperties.blockTextureManager,new Vector3f(), x,blockPos.y,z);
}

public void addVertices(IPrimitiveBuilder primitiveBuilder, int side, float size, IBlockState blockState, BlockPos blockPos, int offsetX, int offsetY, int offsetZ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hilligans.ourcraft.block.blocktypes;

import dev.hilligans.ourcraft.client.rendering.newrenderer.PrimitiveBuilder;
import dev.hilligans.ourcraft.client.rendering.newrenderer.TextAtlas;
import dev.hilligans.ourcraft.data.other.BlockPos;
import dev.hilligans.ourcraft.data.other.BlockProperties;
import dev.hilligans.ourcraft.data.other.blockstates.BlockState;
Expand All @@ -13,11 +14,11 @@ public WeepingVineBlock(String name, BlockProperties blockProperties) {
}

@Override
public void addVertices(PrimitiveBuilder primitiveBuilder, int side, float size, BlockState blockState, BlockPos blockPos, int x, int z) {
public void addVertices(TextAtlas textAtlas, PrimitiveBuilder primitiveBuilder, int side, float size, BlockState blockState, BlockPos blockPos, int x, int z) {
long seed = ((long)blockPos.x) | ((long)blockPos.z << 32);
Random random = new Random(seed);
for(int a = 0; a < 3; a++) {
blockProperties.blockShape.addVertices(primitiveBuilder,side,size,blockState,blockProperties.blockTextureManager, new BlockPos(x,blockPos.y,z).get3f(),random.nextFloat() - 0.5f, 0,random.nextFloat() - 0.5f);
blockProperties.blockShape.addVertices(textAtlas, primitiveBuilder,side,size,blockState,blockProperties.blockTextureManager, new BlockPos(x,blockPos.y,z).get3f(),random.nextFloat() - 0.5f, 0,random.nextFloat() - 0.5f);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package dev.hilligans.ourcraft.client.rendering.graphics;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsElement;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.mod.handler.content.UnknownResourceException;
import dev.hilligans.ourcraft.util.registry.IRegistryElement;

import java.util.HashMap;

public abstract class RenderTaskSource implements IRegistryElement {
public abstract class RenderTaskSource implements IRegistryElement, IGraphicsElement {

public HashMap<String, RenderTask> tasks = new HashMap<>();
public RenderTask defaultTask;
Expand Down Expand Up @@ -56,4 +59,12 @@ public String getResourceOwner() {
public String getResourceType() {
return "render_task";
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.hilligans.ourcraft.client.rendering.graphics.api;

import dev.hilligans.ourcraft.client.rendering.graphics.GraphicsData;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.world.StringRenderer;
import dev.hilligans.ourcraft.GameInstance;
Expand All @@ -11,13 +10,11 @@ public abstract class GraphicsEngineBase<Q extends RenderWindow,V extends IDefau

public StringRenderer stringRenderer;
public ArrayList<Q> windows = new ArrayList<>();
public GraphicsData graphicsData;

public GameInstance gameInstance;


public GraphicsEngineBase() {
graphicsData = new GraphicsData(this);
}

@Override
Expand All @@ -36,11 +33,6 @@ public ArrayList<Q> getWindows() {
return windows;
}

@Override
public GraphicsData getGraphicsData() {
return graphicsData;
}

@Override
public void load(GameInstance gameInstance) {
this.gameInstance = gameInstance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.hilligans.ourcraft.client.rendering.graphics.api;

import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.GraphicsData;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.world.StringRenderer;
import dev.hilligans.ourcraft.GameInstance;
Expand Down Expand Up @@ -79,8 +78,6 @@ default void setWindowsFrameRate(int frameRate) {

int getProgram(String name);

GraphicsData getGraphicsData();

default RenderWindow startEngine() {
RenderWindow window = setup();
//getGameInstance().build(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ public void invoke(int source, int type, int id, int severity, int length, long
glEnable(GL_PROGRAM_POINT_SIZE);
// glDisable();

graphicsData.build();
//TextAtlas.instance.upload(this);

return renderWindow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.client.rendering.MeshHolder;
import dev.hilligans.ourcraft.client.rendering.newrenderer.PrimitiveBuilder;
import dev.hilligans.ourcraft.client.rendering.newrenderer.TextAtlas;
import dev.hilligans.ourcraft.data.other.BlockPos;
import dev.hilligans.ourcraft.data.primitives.Tuple;
import dev.hilligans.ourcraft.GameInstance;
Expand Down Expand Up @@ -39,12 +40,14 @@ public WorldRenderTask() {
public CullingEngine cullingEngine;

public ExecutorService chunkBuilder = Executors.newFixedThreadPool(2);
TextAtlas textAtlas = new TextAtlas();

@Override
public RenderTask getDefaultTask() {
IThreeDContainer<MeshHolder> meshes = new EmptyContainer<>();

return new RenderTask() {

@Override
public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphicsEngine<?, ?, ?> engine, Client client, MatrixStack worldStack, MatrixStack screenStack, float delta) {
int a = 0;
Expand All @@ -58,7 +61,7 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
Vector3i playerChunkPos = new Vector3i((int) pos.x / chunkWidth, (int) pos.y / chunkHeight, (int) pos.z / chunkWidth);
if (client.renderWorld) {
engine.getDefaultImpl().bindPipeline(graphicsContext, shaderSource.program);
engine.getDefaultImpl().bindTexture(graphicsContext, engine.getGraphicsData().getWorldTexture());
engine.getDefaultImpl().bindTexture(graphicsContext, textAtlas.texture);
for (int x = 0; x < client.renderDistance; x++) {
for (int y = 0; y < renderYDist; y++) {
for (int z = 0; z < client.renderDistance; z++) {
Expand Down Expand Up @@ -237,7 +240,7 @@ public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk, PrimitiveBuilder primi
newState = chunk.getBlockState1(p);
}
if (newState.getBlock().blockProperties.transparent && (Settings.renderSameTransparent || block.getBlock() != newState.getBlock()) || block.getBlock().blockProperties.alwaysRender || newState.getBlock().blockProperties.translucent) {
block.getBlock().addVertices(primitiveBuilder, a, 1f, block, p.set(x, y, z), x, z);
block.getBlock().addVertices(textAtlas, primitiveBuilder, a, 1f, block, p.set(x, y, z), x, z);
}
}
}
Expand Down Expand Up @@ -296,6 +299,21 @@ public PipelineState getPipelineState() {
};
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(Block block : gameInstance.getBlocks()) {
if(!block.blockProperties.translucent) {
block.generateTextures(textAtlas);
}
}
textAtlas.upload(graphicsEngine);
}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, textAtlas.texture);
}

@Override
public void load(GameInstance gameInstance) {
super.load(gameInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.client.rendering.MeshHolder;
import dev.hilligans.ourcraft.client.rendering.newrenderer.PrimitiveBuilder;
import dev.hilligans.ourcraft.client.rendering.newrenderer.TextAtlas;
import dev.hilligans.ourcraft.data.other.BlockPos;
import dev.hilligans.ourcraft.data.primitives.Tuple;
import dev.hilligans.ourcraft.GameInstance;
Expand Down Expand Up @@ -39,10 +40,12 @@ public WorldTransparentRenderTask() {
public ExecutorService chunkBuilder = Executors.newFixedThreadPool(2);

public IThreeDContainer<MeshHolder> meshes = new EmptyContainer<>();
TextAtlas textAtlas = new TextAtlas();

@Override
public RenderTask getDefaultTask() {
return new RenderTask() {

@Override
public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphicsEngine<?, ?, ?> engine, Client client, MatrixStack worldStack, MatrixStack screenStack, float delta) {
IWorld world = client.newClientWorld;
Expand All @@ -55,7 +58,7 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
Vector3i playerChunkPos = new Vector3i((int) pos.x / chunkWidth, (int) pos.y / chunkHeight, (int) pos.z / chunkWidth);
if (client.renderWorld) {
engine.getDefaultImpl().bindPipeline(graphicsContext, shaderSource.program);
engine.getDefaultImpl().bindTexture(graphicsContext, engine.getGraphicsData().getWorldTexture());
engine.getDefaultImpl().bindTexture(graphicsContext, textAtlas.texture);
for (int x = client.renderDistance - 1; x >= 0; x--) {
for (int y = renderYDist - 1; y >= 0; y--) {
for (int z = client.renderDistance - 1; z >= 0; z--) {
Expand Down Expand Up @@ -93,11 +96,14 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
}
}
}

@Override
public PipelineState getPipelineState() {
return new PipelineState().setDepth(true);
}
};


}


Expand Down Expand Up @@ -179,7 +185,7 @@ public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk) {
newState = chunk.getBlockState1(p);
}
if (newState.getBlock() != block.getBlock()) {
block.getBlock().addVertices(primitiveBuilder, a, 1f, block, p.set(x, y, z), x, z);
block.getBlock().addVertices(textAtlas, primitiveBuilder, a, 1f, block, p.set(x, y, z), x, z);
}
}
}
Expand All @@ -201,6 +207,21 @@ public void load(GameInstance gameInstance) {
super.load(gameInstance);
shaderSource = gameInstance.SHADERS.get("ourcraft:world_shader");
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(Block block : gameInstance.getBlocks()) {
if(block.blockProperties.translucent) {
block.generateTextures(textAtlas);
}
}
textAtlas.upload(graphicsEngine);
}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, textAtlas.texture);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void modifyData(float[] data, int swapMode, boolean negateX, boolean neg
}

@Override
public void addData(PrimitiveBuilder primitiveBuilder, BlockTextureManager textureManager, int side, float size, Vector3f offset, int rotX, int rotY) {
public void addData(TextAtlas textAtlas, PrimitiveBuilder primitiveBuilder, BlockTextureManager textureManager, int side, float size, Vector3f offset, int rotX, int rotY) {
rotX = 0;
rotY = 0;
float[] vertices = getVertices(side,rotX,rotY);
Expand All @@ -128,10 +128,10 @@ public void addData(PrimitiveBuilder primitiveBuilder, BlockTextureManager textu
//float[] vals = new float[vertices.length];
int id = textureManager.textures[side];

float startX = TextAtlas.getMinX(id);
float startY = TextAtlas.getMinY(id);
float offsetX = TextAtlas.getMaxX(id) - startX;
float offsetY = TextAtlas.getMaxY(id) - startY;
float startX = textAtlas.getMinX(id);
float startY = textAtlas.getMinY(id);
float offsetX = textAtlas.getMaxX(id) - startX;
float offsetY = textAtlas.getMaxY(id) - startY;

primitiveBuilder.ensureCapacity(vertices.length, indices.length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default float[] getVertices(int side, int rotX, int rotY) {
default int[] getIndices(int side, int rotX, int rotY) {
return getIndices(side);
}
void addData(PrimitiveBuilder primitiveBuilder, BlockTextureManager textureManager, int side, float size, Vector3f offset, int rotX, int rotY);
void addData(TextAtlas textAtlas, PrimitiveBuilder primitiveBuilder, BlockTextureManager textureManager, int side, float size, Vector3f offset, int rotX, int rotY);

String getModel();
String getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int[] getIndices(int side) {
}

@Override
public void addData(PrimitiveBuilder primitiveBuilder, BlockTextureManager textureManager, int side, float size, Vector3f offset, int rotX, int rotY) {
public void addData(TextAtlas textAtlas, PrimitiveBuilder primitiveBuilder, BlockTextureManager textureManager, int side, float size, Vector3f offset, int rotX, int rotY) {
//int id = textureManager.getTextureId();
/* BufferedImage bufferedImage = TextAtlas.instance.register(new ImageLocation("Items/" + path,"purcraft"));
float minX = TextAtlas.getMinX(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,20 @@ public int loadTextureId(String path, String textureName, String source) {
return register(new ImageLocation("Images/" + path,source));
}

public static TextAtlas instance = new TextAtlas();

public static float getMinX(int id) {
return (get(id,16)) / (float)instance.size;
public float getMinX(int id) {
return (get(id,16)) / (float)size;
}
public static float getMaxX(int id) {
return (get(id,16) + get(id,32)) / (float)instance.size;
public float getMaxX(int id) {
return (get(id,16) + get(id,32)) / (float)size;
}
public static float getMinY(int id) {
return (get(id,0)) / (float)instance.size;
public float getMinY(int id) {
return (get(id,0)) / (float)size;
}
public static float getMaxY(int id) {
return (get(id,0) + get(id,32)) / (float)instance.size;
public float getMaxY(int id) {
return (get(id,0) + get(id,32)) / (float)size;
}

public static int get(int id, int shift) {
return (short)(instance.ids.get(id) >> shift);
public int get(int id, int shift) {
return (short)(ids.get(id) >> shift);
}
}
Loading

0 comments on commit f27ddec

Please sign in to comment.