Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 3324c96

Browse files
committed
Stencil test ready again for WorldRenderer
1 parent 6bb3358 commit 3324c96

File tree

7 files changed

+192
-67
lines changed

7 files changed

+192
-67
lines changed

src/main/java/org/overrun/swgl/core/gl/GLClear.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.lwjgl.opengl.GL11C;
2828

2929
import static org.lwjgl.opengl.GL11C.*;
30+
import static org.overrun.swgl.core.util.math.Numbers.isNonEqual;
3031

3132
/**
3233
* GL clear functions for users.
@@ -47,6 +48,9 @@ public class GLClear {
4748
* AttribMask
4849
*/
4950
public static final int STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT;
51+
private static final float[] clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
52+
private static double clearDepth = 1.0;
53+
private static int clearStencil = 1;
5054

5155
/**
5256
* Call {@link GL11C#glClear glClear}
@@ -66,7 +70,16 @@ public static void clear(int mask) {
6670
* @param a Alpha value
6771
*/
6872
public static void clearColor(float r, float g, float b, float a) {
69-
glClearColor(r, g, b, a);
73+
if (isNonEqual(clearColor[0], r)
74+
|| isNonEqual(clearColor[1], g)
75+
|| isNonEqual(clearColor[2], b)
76+
|| isNonEqual(clearColor[3], a)) {
77+
clearColor[0] = r;
78+
clearColor[1] = g;
79+
clearColor[2] = b;
80+
clearColor[3] = a;
81+
glClearColor(r, g, b, a);
82+
}
7083
}
7184

7285
/**
@@ -75,7 +88,10 @@ public static void clearColor(float r, float g, float b, float a) {
7588
* @param depth The depth
7689
*/
7790
public static void clearDepth(double depth) {
78-
glClearDepth(depth);
91+
if (isNonEqual(clearDepth, depth)) {
92+
clearDepth = depth;
93+
glClearDepth(depth);
94+
}
7995
}
8096

8197
/**
@@ -84,6 +100,9 @@ public static void clearDepth(double depth) {
84100
* @param s The stencil
85101
*/
86102
public static void clearStencil(int s) {
87-
glClearStencil(s);
103+
if (clearStencil != s) {
104+
clearStencil = s;
105+
glClearStencil(s);
106+
}
88107
}
89108
}

src/main/java/org/overrun/swgl/core/gl/ims/GLImmeMode.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,7 @@ public static Matrix4fStack lglGetMatrixMode() {
471471
}
472472

473473
public static void lglMatrixMode(MatrixMode mode) {
474-
switch (mode) {
475-
case MODELVIEW -> currentMat = modelviewMat;
476-
case PROJECTION -> currentMat = projectionMat;
477-
case TEXTURE -> currentMat = textureMat;
478-
case COLOR -> currentMat = colorMat;
479-
}
474+
currentMat = lglGetMatrix(mode);
480475
}
481476

482477
public static void lglPerspective(float fovy,
@@ -597,6 +592,10 @@ public static void lglRotateLocalDeg(float ang,
597592
currentMat.rotateLocal(Math.toRadians(ang), x, y, z);
598593
}
599594

595+
public static void lglScale(float xyz) {
596+
currentMat.scale(xyz);
597+
}
598+
600599
public static void lglScale(float x,
601600
float y,
602601
float z) {

src/main/java/org/overrun/swgl/core/util/math/Numbers.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public class Numbers {
4646
* </p>
4747
*/
4848
public static final float EPSILON_SINGLE = 1.0E-06f;
49+
/**
50+
* The epsilon of double float point.
51+
* <p>
52+
* Writing in plain format: {@code 0.000000000000001}
53+
* </p>
54+
*/
55+
public static final double EPSILON_DOUBLE = 1.0E-15;
4956

5057
/**
5158
* Check if two single float point numbers are equal.
@@ -75,6 +82,34 @@ public static boolean isNonEqual(float a, float b) {
7582
return Math.abs(a - b) >= EPSILON_SINGLE;
7683
}
7784

85+
/**
86+
* Check if two double float point numbers are equal.
87+
* <p>
88+
* Checking method: {@code |a-b| &lt; 10<sup>-15</sup>}
89+
* </p>
90+
*
91+
* @param a The first number.
92+
* @param b The second number.
93+
* @return The result.
94+
*/
95+
public static boolean isEqual(double a, double b) {
96+
return Math.abs(a - b) < EPSILON_DOUBLE;
97+
}
98+
99+
/**
100+
* Check if two single double point numbers are non-equal.
101+
* <p>
102+
* Checking method: {@code |a-b| &gt;= 10<sup>-15</sup>}
103+
* </p>
104+
*
105+
* @param a The first number.
106+
* @param b The second number.
107+
* @return The result.
108+
*/
109+
public static boolean isNonEqual(double a, double b) {
110+
return Math.abs(a - b) >= EPSILON_DOUBLE;
111+
}
112+
78113
/**
79114
* Check if the number is 0.
80115
*

src/test/java/org/overrun/swgl/game/HitResult.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@
2525
package org.overrun.swgl.game;
2626

2727
import org.overrun.swgl.core.util.math.Direction;
28+
import org.overrun.swgl.game.world.block.Block;
2829

2930
/**
3031
* The hit result.
3132
*
32-
* @param x The pos x.
33-
* @param y The pos y.
34-
* @param z The pos z.
35-
* @param face The hit face.
33+
* @param block The target block.
34+
* @param x The pos x.
35+
* @param y The pos y.
36+
* @param z The pos z.
37+
* @param face The hit face.
3638
* @author squid233
3739
* @since 0.1.0
3840
*/
39-
public record HitResult(int x, int y, int z, Direction face) {
41+
public record HitResult(Block block, int x, int y, int z, Direction face) {
4042
}

src/test/java/org/overrun/swgl/game/world/WorldRenderer.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.overrun.swgl.game.Frustum;
3434
import org.overrun.swgl.game.HitResult;
3535
import org.overrun.swgl.game.phys.AABB;
36-
import org.overrun.swgl.game.world.block.Blocks;
3736
import org.overrun.swgl.game.world.entity.Player;
3837

3938
import java.util.ArrayList;
@@ -111,15 +110,17 @@ public void updateDirtyChunks(Player player) {
111110
}
112111

113112
public void renderHit(HitResult hitResult) {
114-
enableBlend();
115-
glLineWidth(2.0f);
113+
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
114+
disableDepthTest();
116115
enableBlend();
117116
blendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
118-
lglBegin(GLDrawMode.LINES);
119-
Blocks.STONE.renderOutline(hitResult.x(), hitResult.y(), hitResult.z());
117+
lglBegin(GLDrawMode.TRIANGLES);
118+
lglColor(0, 0, 0, 0.5f);
119+
hitResult.block().renderOutline(world, hitResult.x(), hitResult.y(), hitResult.z());
120120
lglEnd();
121121
disableBlend();
122-
glLineWidth(1.0f);
122+
enableDepthTest();
123+
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
123124
}
124125

125126
public HitResult pick(Player player, Matrix4fc viewMatrix, FpsCamera camera) {
@@ -153,7 +154,7 @@ public HitResult pick(Player player, Matrix4fc viewMatrix, FpsCamera camera) {
153154
nearFar)
154155
&& nearFar.x < closestDistance) {
155156
closestDistance = nearFar.x;
156-
hitResult = new HitResult(x, y, z, rayCast.rayCastFacing(camera.getPosition(), dir));
157+
hitResult = new HitResult(block, x, y, z, rayCast.rayCastFacing(camera.getPosition(), dir));
157158
}
158159
}
159160
}

src/test/java/org/overrun/swgl/game/world/block/AirBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public AirBlock(byte id) {
3636
}
3737

3838
@Override
39-
public AABB getCollision(int x, int y, int z) {
39+
public AABB getOutline(int x, int y, int z) {
4040
return null;
4141
}
4242

src/test/java/org/overrun/swgl/game/world/block/Block.java

Lines changed: 114 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,16 @@ public boolean isAir() {
5656
public AABB getOutline(int x, int y, int z) {
5757
var aabb = new AABB();
5858
aabb.min.set(x, y, z);
59-
aabb.max.set(x + 1, y + 1, z + 1);
59+
aabb.max.set(x + 1.0f, y + 1.0f, z + 1.0f);
6060
return aabb;
6161
}
6262

6363
public AABB getRayCast(int x, int y, int z) {
64-
var aabb = new AABB();
65-
aabb.min.set(x, y, z);
66-
aabb.max.set(x + 1, y + 1, z + 1);
67-
return aabb;
64+
return getOutline(x, y, z);
6865
}
6966

7067
public AABB getCollision(int x, int y, int z) {
71-
var aabb = new AABB();
72-
aabb.min.set(x, y, z);
73-
aabb.max.set(x + 1, y + 1, z + 1);
74-
return aabb;
68+
return getOutline(x, y, z);
7569
}
7670

7771
public void tick(World world, int x, int y, int z, Random random) {
@@ -89,42 +83,111 @@ public boolean shouldRenderFace(World world, int x, int y, int z, int layer) {
8983
return !world.getBlock(x, y, z).isSolid() && (world.isLit(x, y, z) ^ (layer == 1));
9084
}
9185

92-
public void renderOutline(int x, int y, int z) {
93-
float x0 = (float) x - 0.0001f;
94-
float y0 = (float) y - 0.0001f;
95-
float z0 = (float) z - 0.0001f;
96-
float x1 = x + 1.0001f;
97-
float y1 = y + 1.0001f;
98-
float z1 = z + 1.0001f;
99-
lglIndices(
100-
// -x
101-
0, 1, 1, 2, 2, 3, 3, 0,
102-
// +x
103-
4, 5, 5, 6, 6, 7, 7, 4,
104-
// -z
105-
7, 0, 6, 1,
106-
// +z
107-
3, 4, 2, 5
108-
);
109-
lglColor(0, 0, 0, 0.5f);
110-
// -x
111-
lglVertex(x0, y1, z0);
112-
lglEmit();
113-
lglVertex(x0, y0, z0);
114-
lglEmit();
115-
lglVertex(x0, y0, z1);
116-
lglEmit();
117-
lglVertex(x0, y1, z1);
118-
lglEmit();
119-
// +x
120-
lglVertex(x1, y1, z1);
121-
lglEmit();
122-
lglVertex(x1, y0, z1);
123-
lglEmit();
124-
lglVertex(x1, y0, z0);
125-
lglEmit();
126-
lglVertex(x1, y1, z0);
127-
lglEmit();
86+
private void renderOutlineFace(Direction face, int x, int y, int z) {
87+
var outline = getOutline(x, y, z);
88+
float x0 = outline.min.x;
89+
float y0 = outline.min.y;
90+
float z0 = outline.min.z;
91+
float x1 = outline.max.x;
92+
float y1 = outline.max.y;
93+
float z1 = outline.max.z;
94+
switch (face) {
95+
case WEST -> {
96+
// -x
97+
lglIndices(0, 1, 2, 2, 3, 0);
98+
lglNormal(-1, 0, 0);
99+
lglVertex(x0, y1, z0);
100+
lglEmit();
101+
lglVertex(x0, y0, z0);
102+
lglEmit();
103+
lglVertex(x0, y0, z1);
104+
lglEmit();
105+
lglVertex(x0, y1, z1);
106+
lglEmit();
107+
}
108+
case EAST -> {
109+
// +x
110+
lglIndices(0, 1, 2, 2, 3, 0);
111+
lglNormal(1, 0, 0);
112+
lglVertex(x1, y1, z1);
113+
lglEmit();
114+
lglVertex(x1, y0, z1);
115+
lglEmit();
116+
lglVertex(x1, y0, z0);
117+
lglEmit();
118+
lglVertex(x1, y1, z0);
119+
lglEmit();
120+
}
121+
case DOWN -> {
122+
// -y
123+
lglIndices(0, 1, 2, 2, 3, 0);
124+
lglNormal(0, -1, 0);
125+
lglVertex(x0, y0, z1);
126+
lglEmit();
127+
lglVertex(x0, y0, z0);
128+
lglEmit();
129+
lglVertex(x1, y0, z0);
130+
lglEmit();
131+
lglVertex(x1, y0, z1);
132+
lglEmit();
133+
}
134+
case UP -> {
135+
// +y
136+
lglIndices(0, 1, 2, 2, 3, 0);
137+
lglNormal(0, 1, 0);
138+
lglVertex(x0, y1, z0);
139+
lglEmit();
140+
lglVertex(x0, y1, z1);
141+
lglEmit();
142+
lglVertex(x1, y1, z1);
143+
lglEmit();
144+
lglVertex(x1, y1, z0);
145+
lglEmit();
146+
}
147+
case NORTH -> {
148+
// -z
149+
lglIndices(0, 1, 2, 2, 3, 0);
150+
lglNormal(0, 0, -1);
151+
lglVertex(x1, y1, z0);
152+
lglEmit();
153+
lglVertex(x1, y0, z0);
154+
lglEmit();
155+
lglVertex(x0, y0, z0);
156+
lglEmit();
157+
lglVertex(x0, y1, z0);
158+
lglEmit();
159+
}
160+
case SOUTH -> {
161+
// +z
162+
lglIndices(0, 1, 2, 2, 3, 0);
163+
lglNormal(0, 0, 1);
164+
lglVertex(x0, y1, z1);
165+
lglEmit();
166+
lglVertex(x0, y0, z1);
167+
lglEmit();
168+
lglVertex(x1, y0, z1);
169+
lglEmit();
170+
lglVertex(x1, y1, z1);
171+
lglEmit();
172+
}
173+
}
174+
}
175+
176+
public void renderOutline(World world, int x, int y, int z) {
177+
for (var face : Direction.values()) {
178+
if (shouldRenderFace(world,
179+
x + face.getOffsetX(),
180+
y + face.getOffsetY(),
181+
z + face.getOffsetZ(),
182+
0)
183+
|| shouldRenderFace(world,
184+
x + face.getOffsetX(),
185+
y + face.getOffsetY(),
186+
z + face.getOffsetZ(),
187+
1)) {
188+
renderOutlineFace(face, x, y, z);
189+
}
190+
}
128191
}
129192

130193
public void renderFaceNoTex(Direction face, int x, int y, int z) {
@@ -359,6 +422,12 @@ public void renderFace(Direction face, int x, int y, int z) {
359422
}
360423
}
361424

425+
public void renderAllNoTex(int x, int y, int z) {
426+
for (var face : Direction.values()) {
427+
renderFaceNoTex(face, x, y, z);
428+
}
429+
}
430+
362431
public void renderAll(int x, int y, int z) {
363432
for (var face : Direction.values()) {
364433
renderFace(face, x, y, z);

0 commit comments

Comments
 (0)