Texture coords are now precomputed in VBOs

Also optimized GLSL code and started using LOC in RenderPrograms
This commit is contained in:
OLEGSHA 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.backend.shaders.uniforms.*;
import ru.windcorp.progressia.client.graphics.texture.Sprite; import ru.windcorp.progressia.client.graphics.texture.Sprite;
import ru.windcorp.progressia.client.graphics.texture.Texture; import ru.windcorp.progressia.client.graphics.texture.Texture;
import ru.windcorp.progressia.common.util.Vectors;
public class ShapeRenderProgram extends Program { public class ShapeRenderProgram extends Program {
@ -55,9 +56,7 @@ public class ShapeRenderProgram extends Program {
COLOR_MULTIPLER_ATTRIBUTE_NAME = "inputColorMultiplier", COLOR_MULTIPLER_ATTRIBUTE_NAME = "inputColorMultiplier",
TEXTURE_COORDS_ATTRIBUTE_NAME = "inputTextureCoords", TEXTURE_COORDS_ATTRIBUTE_NAME = "inputTextureCoords",
USE_TEXTURE_UNIFORM_NAME = "useTexture", USE_TEXTURE_UNIFORM_NAME = "useTexture",
TEXTURE_SLOT_UNIFORM_NAME = "textureSlot", TEXTURE_SLOT_UNIFORM_NAME = "textureSlot";
TEXTURE_START_UNIFORM_NAME = "textureStart",
TEXTURE_SIZE_UNIFORM_NAME = "textureSize";
private final Uniform4Matrix finalTransformUniform; private final Uniform4Matrix finalTransformUniform;
private final AttributeVertexArray positionsAttribute; private final AttributeVertexArray positionsAttribute;
@ -65,8 +64,6 @@ public class ShapeRenderProgram extends Program {
private final AttributeVertexArray textureCoordsAttribute; private final AttributeVertexArray textureCoordsAttribute;
private final Uniform1Int useTextureUniform; private final Uniform1Int useTextureUniform;
private final Uniform1Int textureSlotUniform; private final Uniform1Int textureSlotUniform;
private final Uniform2Float textureStartUniform;
private final Uniform2Float textureSizeUniform;
public ShapeRenderProgram( public ShapeRenderProgram(
String[] vertexShaderResources, String[] vertexShaderResources,
@ -98,12 +95,6 @@ public class ShapeRenderProgram extends Program {
this.textureSlotUniform = getUniform(TEXTURE_SLOT_UNIFORM_NAME) this.textureSlotUniform = getUniform(TEXTURE_SLOT_UNIFORM_NAME)
.as1Int(); .as1Int();
this.textureStartUniform = getUniform(TEXTURE_START_UNIFORM_NAME)
.as2Float();
this.textureSizeUniform = getUniform(TEXTURE_SIZE_UNIFORM_NAME)
.as2Float();
} }
private static String[] attachVertexShader(String[] others) { private static String[] attachVertexShader(String[] others) {
@ -188,9 +179,6 @@ public class ShapeRenderProgram extends Program {
sprite.getPrimitive().bind(0); sprite.getPrimitive().bind(0);
textureSlotUniform.set(0); textureSlotUniform.set(0);
useTextureUniform.set(1); useTextureUniform.set(1);
textureStartUniform.set(sprite.getStart());
textureSizeUniform.set(sprite.getSize());
} else { } else {
useTextureUniform.set(0); useTextureUniform.set(0);
} }
@ -208,7 +196,38 @@ public class ShapeRenderProgram extends Program {
} }
public void preprocess(Shape shape) { 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() { 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.Shape;
import ru.windcorp.progressia.client.graphics.model.ShapeRenderHelper; import ru.windcorp.progressia.client.graphics.model.ShapeRenderHelper;
import ru.windcorp.progressia.client.graphics.model.ShapeRenderProgram; import ru.windcorp.progressia.client.graphics.model.ShapeRenderProgram;
import ru.windcorp.progressia.common.util.Vectors;
public class WorldRenderProgram extends ShapeRenderProgram { public class WorldRenderProgram extends ShapeRenderProgram {
@ -141,10 +142,10 @@ public class WorldRenderProgram extends ShapeRenderProgram {
} }
private void computeNormals(Face face) { private void computeNormals(Face face) {
Vec3 a = new Vec3(); Vec3 a = Vectors.grab3();
Vec3 b = new Vec3(); Vec3 b = Vectors.grab3();
Vec3 c = new Vec3(); Vec3 c = Vectors.grab3();
Vec3 normal = new Vec3(); Vec3 normal = Vectors.grab3();
for (int i = 0; i < face.getIndexCount(); i += 3) { for (int i = 0; i < face.getIndexCount(); i += 3) {
int indexA = face.getIndex(i + 0); int indexA = face.getIndex(i + 0);
@ -161,6 +162,11 @@ public class WorldRenderProgram extends ShapeRenderProgram {
saveVertexNormal(face, indexB, normal); saveVertexNormal(face, indexB, normal);
saveVertexNormal(face, indexC, normal); saveVertexNormal(face, indexC, normal);
} }
Vectors.release(a);
Vectors.release(b);
Vectors.release(c);
Vectors.release(normal);
} }
private void computeOneNormal( private void computeOneNormal(

View File

@ -4,8 +4,6 @@ varying vec3 varyingColorMultiplier;
varying vec2 varyingTextureCoords; varying vec2 varyingTextureCoords;
uniform sampler2D textureSlot; uniform sampler2D textureSlot;
uniform vec2 textureStart;
uniform vec2 textureSize;
uniform bool useTexture; uniform bool useTexture;
@ -13,32 +11,12 @@ void applyTexture() {
if (!useTexture) { if (!useTexture) {
gl_FragColor = vec4(1, 1, 1, 1); gl_FragColor = vec4(1, 1, 1, 1);
} else { } else {
gl_FragColor = texture2D( gl_FragColor = texture2D(textureSlot, varyingTextureCoords);
textureSlot,
vec2(
varyingTextureCoords[0] * textureSize[0] + textureStart[0],
varyingTextureCoords[1] * textureSize[1] + textureStart[1]
)
);
} }
} }
void multiply(inout vec4 vector, float scalar) {
vector.x *= scalar;
vector.y *= scalar;
vector.z *= scalar;
vector.w *= scalar;
}
void linearMultiply(inout vec4 vector, vec4 scalars) {
vector.x *= scalars.x;
vector.y *= scalars.y;
vector.z *= scalars.z;
vector.w *= scalars.w;
}
void applyColorMultiplier() { void applyColorMultiplier() {
linearMultiply(gl_FragColor, vec4(varyingColorMultiplier, 1.0)); gl_FragColor *= vec4(varyingColorMultiplier, 1.0);
} }
void applyAlpha() { void applyAlpha() {

View File

@ -9,5 +9,5 @@ void applyShading() {
float angleCos = dot(normal, light); float angleCos = dot(normal, light);
float lightness = (angleCos + 1.5) / 2; float lightness = (angleCos + 1.5) / 2;
linearMultiply(gl_FragColor, vec4(lightness, lightness, lightness, 1.0)); gl_FragColor *= vec4(lightness, lightness, lightness, 1.0);
} }