aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorFalsePattern <me@falsepattern.com>2023-11-28 09:41:56 +0100
committermakamys <makamys@outlook.com>2023-12-01 18:34:00 +0100
commitfbe408c777dcf6ea5d2d784b739287d6db2bf52c (patch)
tree77e4023ef11829d36adf1095a5f20bc8845b32f6 /src/main/java
parenta4ec946f5931f8fe6e6271b22c553f99b9d5afb8 (diff)
downloadNeodymium-fbe408c777dcf6ea5d2d784b739287d6db2bf52c.tar.gz
Neodymium-fbe408c777dcf6ea5d2d784b739287d6db2bf52c.tar.bz2
Neodymium-fbe408c777dcf6ea5d2d784b739287d6db2bf52c.zip
abstraction layer for attribute index/stride/offset
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/makamys/neodymium/renderer/NeoRenderer.java43
-rw-r--r--src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java49
-rw-r--r--src/main/java/makamys/neodymium/util/Preprocessor.java10
3 files changed, 81 insertions, 21 deletions
diff --git a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java
index 1aa83b6..61dac28 100644
--- a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java
+++ b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java
@@ -20,6 +20,8 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+
+import makamys.neodymium.renderer.attribs.AttributeSet;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
@@ -374,11 +376,23 @@ public class NeoRenderer {
fogColorBuf.position(0);
fogStartEnd.position(0);
}
+
+ private AttributeSet attributes;
public boolean init() {
// The average mesh is 60 KB. Let's be safe and assume 8 KB per mesh.
// This means 1 MB of index data per 512 MB of VRAM.
MAX_MESHES = Config.VRAMSize * 128;
+
+ attributes = new AttributeSet();
+ attributes.addAttribute("POS", 3, 4, GL_FLOAT);
+ if (Config.shortUV) {
+ attributes.addAttribute("TEXTURE", 2, 2, GL_UNSIGNED_SHORT);
+ } else {
+ attributes.addAttribute("TEXTURE", 2, 4, GL_FLOAT);
+ }
+ attributes.addAttribute("BRIGHTNESS", 2, 2, GL_SHORT);
+ attributes.addAttribute("COLOR", 4, 1, GL_UNSIGNED_BYTE);
reloadShader();
@@ -389,18 +403,7 @@ public class NeoRenderer {
glBindBuffer(GL_ARRAY_BUFFER, mem.VBO);
- int stride = MeshQuad.getStride();
-
- glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, 0);
- glVertexAttribPointer(1, 2, Config.shortUV ? GL_UNSIGNED_SHORT : GL_FLOAT, false, stride, 3 * 4);
- int uvEnd = Config.shortUV ? 4 * 4 : 5 * 4;
- glVertexAttribPointer(2, 2, GL_SHORT, false, stride, uvEnd);
- glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, false, stride, uvEnd + 1 * 4);
-
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- glEnableVertexAttribArray(3);
+ attributes.enable();
for(int i = 0; i < 2; i++) {
piFirst[i] = BufferUtils.createIntBuffer(MAX_MESHES);
@@ -415,18 +418,20 @@ public class NeoRenderer {
return true;
}
- public void reloadShader(int pass) {
+ public void reloadShader(int pass, AttributeSet attributeSet) {
for(int hasFog = 0; hasFog <= 1; hasFog++) {
- Set<String> defines = new HashSet<>();
+ Map<String, String> defines = new HashMap<>();
if(hasFog == 1) {
- defines.add("RENDER_FOG");
+ defines.put("RENDER_FOG", "");
}
if(Config.shortUV) {
- defines.add("SHORT_UV");
+ defines.put("SHORT_UV", "");
}
if(pass == 0) {
- defines.add("PASS_0");
+ defines.put("PASS_0", "");
}
+
+ attributeSet.addDefines(defines);
boolean errors = false;
@@ -472,8 +477,8 @@ public class NeoRenderer {
}
public void reloadShader() {
- reloadShader(0);
- reloadShader(1);
+ reloadShader(0, attributes);
+ reloadShader(1, attributes);
}
public void destroy() {
diff --git a/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java b/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java
new file mode 100644
index 0000000..ebf9c4c
--- /dev/null
+++ b/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java
@@ -0,0 +1,49 @@
+package makamys.neodymium.renderer.attribs;
+
+import org.lwjgl.opengl.GL20;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class AttributeSet {
+ private final List<VertexAttribute> attributes = new ArrayList<>();
+
+ public void addAttribute(String name, int size, int elementSize, int type) {
+ int index = attributes.size();
+ attributes.add(new VertexAttribute(name, index, size, elementSize, type));
+ }
+
+ public void enable() {
+ int stride = attributes.stream().mapToInt(attrib -> attrib.size * attrib.elementSize).sum();
+ int offset = 0;
+ for (int i = 0, size = attributes.size(); i < size; i++) {
+ VertexAttribute attribute = attributes.get(i);
+ GL20.glVertexAttribPointer(i, attribute.size, attribute.type, false, stride, offset);
+ offset += attribute.size * attribute.elementSize;
+ GL20.glEnableVertexAttribArray(i);
+ }
+ }
+
+ public void addDefines(Map<String, String> defines) {
+ for (VertexAttribute attribute: attributes) {
+ defines.put("ATTRIB_" + attribute.name, Integer.toString(attribute.index));
+ }
+ }
+
+ public static class VertexAttribute {
+ public final String name;
+ public final int index;
+ public final int size;
+ public final int elementSize;
+ public final int type;
+
+ private VertexAttribute(String name, int index, int size, int elementSize, int type) {
+ this.name = name;
+ this.index = index;
+ this.size = size;
+ this.elementSize = elementSize;
+ this.type = type;
+ }
+ }
+}
diff --git a/src/main/java/makamys/neodymium/util/Preprocessor.java b/src/main/java/makamys/neodymium/util/Preprocessor.java
index d2fb0ba..cd5d8df 100644
--- a/src/main/java/makamys/neodymium/util/Preprocessor.java
+++ b/src/main/java/makamys/neodymium/util/Preprocessor.java
@@ -1,10 +1,11 @@
package makamys.neodymium.util;
+import java.util.Map;
import java.util.Set;
public class Preprocessor {
- public static String preprocess(String text, Set<String> defines) {
+ public static String preprocess(String text, Map<String, String> defines) {
String[] lines = text.replaceAll("\\r\\n", "\n").split("\\n");
IfElseBlockStatus ifElseBlockStatus = IfElseBlockStatus.NONE;
@@ -17,7 +18,7 @@ public class Preprocessor {
if(line.startsWith("#ifdef ")) {
ifElseBlockStatus = IfElseBlockStatus.IF;
- ifElseConditionMet = defines.contains(line.split(" ")[1]);
+ ifElseConditionMet = defines.containsKey(line.split(" ")[1]);
commentLine = true;
} else if(line.startsWith("#else")) {
ifElseBlockStatus = IfElseBlockStatus.ELSE;
@@ -36,6 +37,11 @@ public class Preprocessor {
if(commentLine) {
lines[i] = "//" + line;
+ } else {
+ for (Map.Entry<String, String> define: defines.entrySet()) {
+ line = line.replace(define.getKey(), define.getValue());
+ }
+ lines[i] = line;
}
}