Skip to content

Commit

Permalink
feat: change COLOR_RGB and COLOR_RGBA type from FLOAT to UNSIGNED_BYTE
Browse files Browse the repository at this point in the history
  • Loading branch information
mouse0w0 committed Dec 8, 2023
1 parent 5c65a70 commit 570fe46
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void putVertexes(VertexDataBuffer buffer, int coveredFace) {
}

for (var vertex : vertexes.getValue()) {
buffer.pos(vertex, 0).rgba(1, 1, 1, 1).tex(vertex, 3).normal(vertex, 5).endVertex();
buffer.pos(vertex, 0).rgba(0xFFFFFFFF).tex(vertex, 3).normal(vertex, 5).endVertex();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private BakedModel(List<float[]> vertexes, Transform[] transforms) {
@Override
public void putVertexes(VertexDataBuffer buffer, int coveredFace) {
for (var vertex : this.vertexes) {
buffer.pos(vertex, 0).rgba(1, 1, 1, 1).tex(vertex, 3).normal(vertex, 5).endVertex();
buffer.pos(vertex, 0).rgba(0xFFFFFFFF).tex(vertex, 3).normal(vertex, 5).endVertex();
}
}

Expand Down
6 changes: 3 additions & 3 deletions client/src/main/java/engine/graphics/sky/SkyBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public SkyBox() {
-256, -256, -256,
-256, -256, 256 //Down
});
float[] floats = new float[24 * 4 * 4]; // 24 vertexes, 4 components, 4 bytes per component(float)
Arrays.fill(floats, 1f); // fill color white(1f, 1f, 1f, 1f)
ByteBuffer colors = BufferUtils.wrapAsByteBuffer(floats);
int[] ints = new int[24];
Arrays.fill(ints, 0xFFFFFFFF); // fill color white
ByteBuffer colors = BufferUtils.wrapAsByteBuffer(ints);
ByteBuffer texCoords = BufferUtils.wrapAsByteBuffer(0.6666667f, 0.0f,
1.0f, 0.0f,
0.6666667f, 0.5f,
Expand Down
37 changes: 23 additions & 14 deletions graphics/src/main/java/engine/graphics/vertex/VertexDataBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,41 +261,50 @@ public VertexDataBuffer color(Color color) {
return rgba(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
}

public VertexDataBuffer color(int argb) {
public VertexDataBuffer rgba(int rgba) {
if (vertexFormat.isUsingColor()) {
float a = ((argb >> 24) & 255) / 255f;
float r = ((argb >> 16) & 255) / 255f;
float g = ((argb >> 8) & 255) / 255f;
float b = (argb & 255) / 255f;
if (!vertexFormat.isUsingAlpha()) {
return rgb(r, g, b);
if (vertexFormat.isUsingAlpha()) {
byteBuffer.putInt(rgba);
} else {
return rgba(r, g, b, a);
byte r = (byte) ((rgba >> 24) & 0xFF);
byte g = (byte) ((rgba >> 16) & 0xFF);
byte b = (byte) ((rgba >> 8) & 0xFF);
byteBuffer.put(r).put(g).put(b);
}
}
return this;
}

public VertexDataBuffer rgb(float r, float g, float b) {
public VertexDataBuffer rgb(int r, int g, int b) {
if (vertexFormat.isUsingColor()) {
byteBuffer.putFloat(r).putFloat(g).putFloat(b);
if (vertexFormat.isUsingAlpha()) {
byteBuffer.putFloat(1f);
byteBuffer.put((byte) r).put((byte) g).put((byte) b).put((byte) 0xFF);
} else {
byteBuffer.put((byte) r).put((byte) g).put((byte) b);
}
}
return this;
}

public VertexDataBuffer rgba(float r, float g, float b, float a) {
public VertexDataBuffer rgba(int r, int g, int b, int a) {
if (vertexFormat.isUsingColor()) {
byteBuffer.putFloat(r).putFloat(g).putFloat(b);
if (vertexFormat.isUsingAlpha()) {
byteBuffer.putFloat(a);
byteBuffer.put((byte) r).put((byte) g).put((byte) b).put((byte) a);
} else {
byteBuffer.put((byte) r).put((byte) g).put((byte) b);
}
}
return this;
}

public VertexDataBuffer rgb(float r, float g, float b) {
return rgb((int) (r * 255), (int) (g * 255), (int) (b * 255));
}

public VertexDataBuffer rgba(float r, float g, float b, float a) {
return rgba((int) (r * 255), (int) (g * 255), (int) (b * 255), (int) (a * 255));
}

public VertexDataBuffer rgb(float[] array, int start) {
return rgb(array[start], array[start + 1], array[start + 2]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public final class VertexElement {
public static final String NAME_UNKNOWN = "Unknown";

public static final VertexElement POSITION = new VertexElement(DataType.FLOAT, NAME_POSITION, 3);
public static final VertexElement COLOR_RGB = new VertexElement(DataType.FLOAT, NAME_COLOR, 3);
public static final VertexElement COLOR_RGBA = new VertexElement(DataType.FLOAT, NAME_COLOR, 4);
public static final VertexElement COLOR_RGB = new VertexElement(DataType.UNSIGNED_BYTE, NAME_COLOR, 3, true);
public static final VertexElement COLOR_RGBA = new VertexElement(DataType.UNSIGNED_BYTE, NAME_COLOR, 4, true);
public static final VertexElement TEX_COORD = new VertexElement(DataType.FLOAT, NAME_TEX_COORD, 2);
public static final VertexElement NORMAL = new VertexElement(DataType.FLOAT, NAME_NORMAL, 3, true);
public static final VertexElement TANGENT = new VertexElement(DataType.FLOAT, NAME_TANGENT, 3, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ public void drawTexture(Texture2D texture, float x, float y, float width, float
public void drawTexture(Texture2D texture, float x, float y, float width, float height, float minU, float minV, float maxU, float maxV) {
buffer.begin(VertexFormat.POSITION_COLOR_ALPHA_TEX_COORD);
float x2 = x + width, y2 = y + height;
buffer.pos(x, y, 0).rgba(1, 1, 1, 1).tex(minU, minV).endVertex();
buffer.pos(x, y2, 0).rgba(1, 1, 1, 1).tex(minU, maxV).endVertex();
buffer.pos(x2, y, 0).rgba(1, 1, 1, 1).tex(maxU, minV).endVertex();
buffer.pos(x2, y2, 0).rgba(1, 1, 1, 1).tex(maxU, maxV).endVertex();
buffer.pos(x, y, 0).rgba(0xFFFFFFFF).tex(minU, minV).endVertex();
buffer.pos(x, y2, 0).rgba(0xFFFFFFFF).tex(minU, maxV).endVertex();
buffer.pos(x2, y, 0).rgba(0xFFFFFFFF).tex(maxU, minV).endVertex();
buffer.pos(x2, y2, 0).rgba(0xFFFFFFFF).tex(maxU, maxV).endVertex();
uniformTexture.setTexture(texture);
buffer.finish();
renderer.drawStreamed(DrawMode.TRIANGLE_STRIP, buffer);
Expand Down

0 comments on commit 570fe46

Please sign in to comment.