Skip to content

Commit

Permalink
RPLE compat
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsePattern authored and makamys committed Dec 2, 2023
1 parent 7534096 commit dd40c5c
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/main/java/makamys/neodymium/Compat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ public class Compat {
private static boolean wasAdvancedOpenGLEnabled;

private static int notEnoughVRAMAmountMB = -1;

private static boolean RPLE;

public static void init() {
isGL33Supported = GLContext.getCapabilities().OpenGL33;

if (Loader.isModLoaded("triangulator")) {
disableTriangulator();
}

if (Loader.isModLoaded("rple")) {
RPLE = true;
}
}

public static boolean RPLE() {
return RPLE;
}

private static void disableTriangulator() {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/makamys/neodymium/renderer/ChunkMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

import lombok.val;
import makamys.neodymium.Compat;
import makamys.neodymium.Neodymium;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
Expand Down Expand Up @@ -108,10 +109,12 @@ private void addTessellatorData(Tessellator t) {
FLAGS.hasColor = t.hasColor;

int verticesPerPrimitive = t.drawMode == GL11.GL_QUADS ? 4 : 3;

int tessellatorVertexSize = Compat.RPLE() ? 12 : 8;

for(int quadI = 0; quadI < t.vertexCount / verticesPerPrimitive; quadI++) {
MeshQuad quad = quadBuf.next();
quad.setState(t.rawBuffer, quadI * (verticesPerPrimitive * 8), FLAGS, t.drawMode, (float)-t.xOffset, (float)-t.yOffset, (float)-t.zOffset);
quad.setState(t.rawBuffer, tessellatorVertexSize, quadI * (verticesPerPrimitive * tessellatorVertexSize), FLAGS, t.drawMode, (float)-t.xOffset, (float)-t.yOffset, (float)-t.zOffset);
if(quad.deleted) {
quadBuf.remove();
}
Expand Down
41 changes: 29 additions & 12 deletions src/main/java/makamys/neodymium/renderer/MeshQuad.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Locale;

import makamys.neodymium.Neodymium;
import makamys.neodymium.Compat;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;

Expand All @@ -26,6 +27,10 @@ public class MeshQuad {
// TODO normals?
public int[] bs = new int[4];

//RPLE compat. bs reused as RED
public int[] bsG = new int[4];
public int[] bsB = new int[4];

public boolean deleted;

public QuadNormal normal;
Expand All @@ -34,10 +39,10 @@ public class MeshQuad {
private static Vector3f vectorB = new Vector3f();
private static Vector3f vectorC = new Vector3f();

private void read(int[] rawBuffer, int offset, float offsetX, float offsetY, float offsetZ, int drawMode, ChunkMesh.Flags flags) {
private void read(int[] rawBuffer, int tessellatorVertexSize, int offset, float offsetX, float offsetY, float offsetZ, int drawMode, ChunkMesh.Flags flags) {
int vertices = drawMode == GL11.GL_TRIANGLES ? 3 : 4;
for(int vi = 0; vi < vertices; vi++) {
int i = offset + vi * 8;
int i = offset + vi * tessellatorVertexSize;

xs[vi] = Float.intBitsToFloat(rawBuffer[i + 0]) + offsetX;
ys[vi] = Float.intBitsToFloat(rawBuffer[i + 1]) + offsetY;
Expand All @@ -52,7 +57,15 @@ private void read(int[] rawBuffer, int offset, float offsetX, float offsetY, flo

bs[vi] = flags.hasBrightness ? rawBuffer[i + 7] : DEFAULT_BRIGHTNESS;

i += 8;
if (Compat.RPLE()) {
if (flags.hasBrightness) {
bsG[vi] = rawBuffer[i + 8];
bsB[vi] = rawBuffer[i + 9];
} else {
bsG[vi] = DEFAULT_BRIGHTNESS;
bsB[vi] = DEFAULT_BRIGHTNESS;
}
}
}

if(vertices == 3) {
Expand All @@ -65,14 +78,18 @@ private void read(int[] rawBuffer, int offset, float offsetX, float offsetY, flo
vs[3] = vs[2];

bs[3] = bs[2];
if (Compat.RPLE()) {
bsG[3] = bsG[2];
bsB[3] = bsB[2];
}
cs[3] = cs[2];
}
}

public void setState(int[] rawBuffer, int offset, ChunkMesh.Flags flags, int drawMode, float offsetX, float offsetY, float offsetZ) {
public void setState(int[] rawBuffer, int tessellatorVertexSize, int offset, ChunkMesh.Flags flags, int drawMode, float offsetX, float offsetY, float offsetZ) {
deleted = false;

read(rawBuffer, offset, offsetX, offsetY, offsetZ, drawMode, flags);
read(rawBuffer, tessellatorVertexSize, offset, offsetX, offsetY, offsetZ, drawMode, flags);

if(xs[0] == xs[1] && xs[1] == xs[2] && xs[2] == xs[3] && ys[0] == ys[1] && ys[1] == ys[2] && ys[2] == ys[3]) {
// ignore empty quads (e.g. alpha pass of EnderIO item conduits)
Expand All @@ -91,9 +108,7 @@ public void setState(int[] rawBuffer, int offset, ChunkMesh.Flags flags, int dra
* @implSpec This needs to be kept in sync with the attributes in {@link NeoRenderer#init()}
*/
public void writeToBuffer(BufferWriter out, int expectedStride) throws IOException {
for(int vertexI = 0; vertexI < 4; vertexI++) {
int vi = vertexI;

for(int vi = 0; vi < 4; vi++) {
float x = xs[vi];
float y = ys[vi];
float z = zs[vi];
Expand All @@ -112,10 +127,12 @@ public void writeToBuffer(BufferWriter out, int expectedStride) throws IOExcepti
out.writeFloat(u);
out.writeFloat(v);
}

int b = bs[vi];

out.writeInt(b);

out.writeInt(bs[vi]);
if (Compat.RPLE()) {
out.writeInt(bsG[vi]);
out.writeInt(bsB[vi]);
}

int c = cs[vi];

Expand Down
33 changes: 29 additions & 4 deletions src/main/java/makamys/neodymium/renderer/NeoRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;
import java.util.Set;

import makamys.neodymium.Compat;
import makamys.neodymium.renderer.attribs.AttributeSet;
import makamys.neodymium.util.BufferWriter;
import org.lwjgl.BufferUtils;
Expand Down Expand Up @@ -349,7 +350,14 @@ private void updateUniforms(double alpha, int pass) {
int u_modelView = glGetUniformLocation(shaderProgram, "modelView");
int u_proj = glGetUniformLocation(shaderProgram, "proj");
int u_playerPos = glGetUniformLocation(shaderProgram, "playerPos");
int u_light = glGetUniformLocation(shaderProgram, "lightTex");
int u_light = 0, u_light_r = 0, u_light_g = 0, u_light_b = 0;
if (Compat.RPLE()) {
u_light_r = glGetUniformLocation(shaderProgram, "lightTexR");
u_light_g = glGetUniformLocation(shaderProgram, "lightTexG");
u_light_b = glGetUniformLocation(shaderProgram, "lightTexB");
} else {
u_light = glGetUniformLocation(shaderProgram, "lightTex");
}
int u_viewport = glGetUniformLocation(shaderProgram, "viewport");
int u_projInv = glGetUniformLocation(shaderProgram, "projInv");
int u_fogColor = glGetUniformLocation(shaderProgram, "fogColor");
Expand All @@ -367,8 +375,16 @@ private void updateUniforms(double alpha, int pass) {
glUniform1f(u_fogDensity, glGetFloat(GL_FOG_DENSITY));

glUniform3f(u_playerPos, (float)eyePosX, (float)eyePosY, (float)eyePosZ);

glUniform1i(u_light, 1);

if (Compat.RPLE()) {
//TODO connect to RPLE gl api (once that exists)
// For now we just use the RPLE default texture indices
glUniform1i(u_light_r, 1);
glUniform1i(u_light_g, 2);
glUniform1i(u_light_b, 3);
} else {
glUniform1i(u_light, 1);
}

modelView.position(0);
projBuf.position(0);
Expand All @@ -395,7 +411,13 @@ public boolean init() {
} else {
attributes.addAttribute("TEXTURE", 2, 4, GL_FLOAT);
}
attributes.addAttribute("BRIGHTNESS", 2, 2, GL_SHORT);
if (Compat.RPLE()) {
attributes.addAttribute("BRIGHTNESS_RED", 2, 2, GL_SHORT);
attributes.addAttribute("BRIGHTNESS_GREEN", 2, 2, GL_SHORT);
attributes.addAttribute("BRIGHTNESS_BLUE", 2, 2, GL_SHORT);
} else {
attributes.addAttribute("BRIGHTNESS", 2, 2, GL_SHORT);
}
attributes.addAttribute("COLOR", 4, 1, GL_UNSIGNED_BYTE);

reloadShader();
Expand Down Expand Up @@ -435,6 +457,9 @@ public void reloadShader(int pass, AttributeSet attributeSet) {
if(Config.shortUV) {
defines.put("SHORT_UV", "");
}
if (Compat.RPLE()) {
defines.put("RPLE", "");
}
if(pass == 0) {
defines.put("PASS_0", "");
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/resources/shaders/chunk.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
out vec4 FragColor;

in vec2 TexCoord;
#ifdef RPLE
in vec2 BTexCoordR;
in vec2 BTexCoordG;
in vec2 BTexCoordB;
#else
in vec2 BTexCoord;
#endif
in vec4 Color;
in vec4 Viewport;
in mat4 ProjInv;
Expand All @@ -11,7 +17,13 @@ in vec2 FogStartEnd;
in float FogFactor;

uniform sampler2D atlas;
#ifdef RPLE
uniform sampler2D lightTexR;
uniform sampler2D lightTexG;
uniform sampler2D lightTexB;
#else
uniform sampler2D lightTex;
#endif

void main()
{
Expand All @@ -23,7 +35,21 @@ void main()

vec4 colorMult = Color/256.0;

vec4 lightyColor = texture(lightTex, (BTexCoord + 8.0) / 256.0);
vec4 lightyColor =
#ifdef RPLE
// RPLE assumes that we're using the legacy opengl pipeline, so it creates 3 textures:
// color dark bright
// RED: Cyan -> White
// GREEN: Magenta -> White
// BLUE: Yellow -> White
// In each texture, only a single channel varies, while the other 2 are set to 1, so the result becomes:
// (r, 1, 1) * (1, g, 1) * (1, 1, b) = (r, g, b)
texture(lightTexR, (BTexCoordR + 32767) / 65535.0) *
texture(lightTexG, (BTexCoordG + 32767) / 65535.0) *
texture(lightTexB, (BTexCoordB + 32767) / 65535.0);
#else
texture(lightTex, (BTexCoord + 8.0) / 256.0);
#endif

vec4 rasterColor =
#ifdef PASS_0
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/shaders/chunk.vert
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#version 330 core
layout (location = ATTRIB_POS) in vec3 aPos;
layout (location = ATTRIB_TEXTURE) in vec2 aTexCoord;
#ifdef RPLE
layout (location = ATTRIB_BRIGHTNESS_RED) in vec2 aBTexCoordR;
layout (location = ATTRIB_BRIGHTNESS_GREEN) in vec2 aBTexCoordG;
layout (location = ATTRIB_BRIGHTNESS_BLUE) in vec2 aBTexCoordB;
#else
layout (location = ATTRIB_BRIGHTNESS) in vec2 aBTexCoord;
#endif
layout (location = ATTRIB_COLOR) in vec4 aColor;

uniform mat4 modelView;
Expand All @@ -16,7 +22,13 @@ uniform float fogDensity;
uniform vec3 playerPos;

out vec2 TexCoord;
#ifdef RPLE
out vec2 BTexCoordR;
out vec2 BTexCoordG;
out vec2 BTexCoordB;
#else
out vec2 BTexCoord;
#endif
out vec4 Color;
out vec4 Viewport;
out mat4 ProjInv;
Expand All @@ -28,7 +40,13 @@ void main()
{
gl_Position = proj * modelView * (vec4(aPos - playerPos, 1.0) + vec4(0, 0.12, 0, 0));
TexCoord = aTexCoord;
#ifdef RPLE
BTexCoordR = aBTexCoordR;
BTexCoordG = aBTexCoordG;
BTexCoordB = aBTexCoordB;
#else
BTexCoord = aBTexCoord;
#endif
Color = aColor;

Viewport = viewport;
Expand Down

0 comments on commit dd40c5c

Please sign in to comment.