Texture coords are now precomputed in VBOs

Also optimized GLSL code and started using LOC in RenderPrograms
This commit is contained in:
2020-12-18 00:30:22 +03:00
parent 7cc4fcbffc
commit cd6e6d7d9c
4 changed files with 47 additions and 44 deletions

View File

@@ -36,6 +36,7 @@ import ru.windcorp.progressia.client.graphics.backend.shaders.attributes.*;
import ru.windcorp.progressia.client.graphics.backend.shaders.uniforms.*;
import ru.windcorp.progressia.client.graphics.texture.Sprite;
import ru.windcorp.progressia.client.graphics.texture.Texture;
import ru.windcorp.progressia.common.util.Vectors;
public class ShapeRenderProgram extends Program {
@@ -55,9 +56,7 @@ public class ShapeRenderProgram extends Program {
COLOR_MULTIPLER_ATTRIBUTE_NAME = "inputColorMultiplier",
TEXTURE_COORDS_ATTRIBUTE_NAME = "inputTextureCoords",
USE_TEXTURE_UNIFORM_NAME = "useTexture",
TEXTURE_SLOT_UNIFORM_NAME = "textureSlot",
TEXTURE_START_UNIFORM_NAME = "textureStart",
TEXTURE_SIZE_UNIFORM_NAME = "textureSize";
TEXTURE_SLOT_UNIFORM_NAME = "textureSlot";
private final Uniform4Matrix finalTransformUniform;
private final AttributeVertexArray positionsAttribute;
@@ -65,8 +64,6 @@ public class ShapeRenderProgram extends Program {
private final AttributeVertexArray textureCoordsAttribute;
private final Uniform1Int useTextureUniform;
private final Uniform1Int textureSlotUniform;
private final Uniform2Float textureStartUniform;
private final Uniform2Float textureSizeUniform;
public ShapeRenderProgram(
String[] vertexShaderResources,
@@ -98,12 +95,6 @@ public class ShapeRenderProgram extends Program {
this.textureSlotUniform = getUniform(TEXTURE_SLOT_UNIFORM_NAME)
.as1Int();
this.textureStartUniform = getUniform(TEXTURE_START_UNIFORM_NAME)
.as2Float();
this.textureSizeUniform = getUniform(TEXTURE_SIZE_UNIFORM_NAME)
.as2Float();
}
private static String[] attachVertexShader(String[] others) {
@@ -188,9 +179,6 @@ public class ShapeRenderProgram extends Program {
sprite.getPrimitive().bind(0);
textureSlotUniform.set(0);
useTextureUniform.set(1);
textureStartUniform.set(sprite.getStart());
textureSizeUniform.set(sprite.getSize());
} else {
useTextureUniform.set(0);
}
@@ -208,7 +196,38 @@ public class ShapeRenderProgram extends Program {
}
public void preprocess(Shape shape) {
// To be overridden
for (Face face : shape.getFaces()) {
applySprites(face);
}
}
private void applySprites(Face face) {
if (face.getTexture() == null) return;
Vec2 v = Vectors.grab2();
ByteBuffer vertices = face.getVertices();
Sprite sprite = face.getTexture().getSprite();
for (int i = 0; i < face.getVertexCount(); i++) {
int offset = vertices.position() + i * getBytesPerVertex() + (
3 * Float.BYTES +
3 * Float.BYTES
);
v.set(
vertices.getFloat(offset + 0 * Float.BYTES),
vertices.getFloat(offset + 1 * Float.BYTES)
);
v.mul(sprite.getSize()).add(sprite.getStart());
vertices.putFloat(offset + 0 * Float.BYTES, v.x);
vertices.putFloat(offset + 1 * Float.BYTES, v.y);
}
face.markForVertexUpdate();
Vectors.release(v);
}
public VertexBuilder getVertexBuilder() {

View File

@@ -35,6 +35,7 @@ import ru.windcorp.progressia.client.graphics.model.Face;
import ru.windcorp.progressia.client.graphics.model.Shape;
import ru.windcorp.progressia.client.graphics.model.ShapeRenderHelper;
import ru.windcorp.progressia.client.graphics.model.ShapeRenderProgram;
import ru.windcorp.progressia.common.util.Vectors;
public class WorldRenderProgram extends ShapeRenderProgram {
@@ -141,10 +142,10 @@ public class WorldRenderProgram extends ShapeRenderProgram {
}
private void computeNormals(Face face) {
Vec3 a = new Vec3();
Vec3 b = new Vec3();
Vec3 c = new Vec3();
Vec3 normal = new Vec3();
Vec3 a = Vectors.grab3();
Vec3 b = Vectors.grab3();
Vec3 c = Vectors.grab3();
Vec3 normal = Vectors.grab3();
for (int i = 0; i < face.getIndexCount(); i += 3) {
int indexA = face.getIndex(i + 0);
@@ -161,6 +162,11 @@ public class WorldRenderProgram extends ShapeRenderProgram {
saveVertexNormal(face, indexB, normal);
saveVertexNormal(face, indexC, normal);
}
Vectors.release(a);
Vectors.release(b);
Vectors.release(c);
Vectors.release(normal);
}
private void computeOneNormal(