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

Commit 9e730ed

Browse files
committed
Human
1 parent bf85a55 commit 9e730ed

File tree

12 files changed

+444
-80
lines changed

12 files changed

+444
-80
lines changed

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

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class GLImmeMode {
5454
private static GLDrawMode drawMode;
5555
static ByteBuffer buffer; /* Package private, for GLLists */
5656
static IntBuffer indicesBuffer; /* Package private, for GLLists */
57+
private static boolean indexBufferExtended = true;
5758
private static float
5859
x = 0.0f, y = 0.0f, z = 0.0f, w = 1.0f,
5960
s = 0.0f, t = 0.0f, p = 0.0f, q = 1.0f;
@@ -477,8 +478,10 @@ public static void lglNormal(float nx, float ny, float nz) {
477478
}
478479

479480
private static void growIB() {
480-
if (indicesBuffer.capacity() - indicesBuffer.position() < 128)
481+
if (indicesBuffer.capacity() - indicesBuffer.position() < 128) {
481482
indicesBuffer = memRealloc(indicesBuffer, indicesBuffer.capacity() + 256);
483+
indexBufferExtended = true;
484+
}
482485
}
483486

484487
public static void lglIndices(int... indices) {
@@ -504,7 +507,7 @@ public static void lglEnd() {
504507
lglEnd0();
505508
}
506509

507-
private static void lglEnd0() {
510+
private static void prepareDraw() {
508511
pipeline.bind();
509512
pipeline.getUniformSafe("projectionMat", M4F).set(projectionMat);
510513
pipeline.getUniformSafe("modelviewMat", M4F).set(modelviewMat);
@@ -520,16 +523,16 @@ private static void lglEnd0() {
520523
vao = glGenVertexArrays();
521524
glBindVertexArray(vao);
522525
}
526+
}
523527

524-
final boolean notVbo = vbo == 0;
525-
if (notVbo)
526-
vbo = glGenBuffers();
527-
glBindBuffer(GL_ARRAY_BUFFER, vbo);
528-
if (notVbo)
529-
nglBufferData(GL_ARRAY_BUFFER, Integer.toUnsignedLong(buffer.capacity()), memAddress(buffer), GL_DYNAMIC_DRAW);
530-
else
531-
glBufferSubData(GL_ARRAY_BUFFER, 0L, buffer);
528+
private static void postDraw() {
529+
if (ENABLE_CORE_PROFILE)
530+
glBindVertexArray(0);
532531

532+
pipeline.unbind();
533+
}
534+
535+
private static void prepareVA() {
533536
if (vertexArrayState) glEnableVertexAttribArray(0);
534537
else glDisableVertexAttribArray(0);
535538
if (colorArrayState) glEnableVertexAttribArray(1);
@@ -568,26 +571,63 @@ private static void lglEnd0() {
568571
true,
569572
stride,
570573
36L);
574+
}
575+
576+
private static void lglEnd0() {
577+
prepareDraw();
578+
579+
final boolean notVbo = vbo == 0;
580+
if (notVbo)
581+
vbo = glGenBuffers();
582+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
583+
if (notVbo)
584+
nglBufferData(GL_ARRAY_BUFFER, Integer.toUnsignedLong(buffer.capacity()), memAddress(buffer), GL_DYNAMIC_DRAW);
585+
else
586+
glBufferSubData(GL_ARRAY_BUFFER, 0L, buffer);
587+
588+
prepareVA();
571589

572590
if (lglGetIndexCount() > 0) {
573591
if (ebo == 0)
574592
ebo = glGenBuffers();
575593
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
576-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_DYNAMIC_DRAW);
594+
if (indexBufferExtended) {
595+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_DYNAMIC_DRAW);
596+
} else {
597+
nglBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0L, Integer.toUnsignedLong(indicesBuffer.limit()) << 2L, memAddress(indicesBuffer));
598+
}
599+
indexBufferExtended = false;
577600
glDrawElements(drawMode.getGlType(), lglGetIndexCount(), GL_UNSIGNED_INT, 0L);
578601
} else {
579602
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
580-
glDrawArrays(drawMode.getGlType(), 0, vertexCount);
603+
glDrawArrays(drawMode.getGlType(), 0, lglGetVertexCount());
581604
}
582605

583-
if (ENABLE_CORE_PROFILE)
584-
glBindVertexArray(0);
585-
586-
pipeline.unbind();
606+
postDraw();
587607

588608
drawMode = null;
589609
}
590610

611+
public static void lglDrawBuffers(GLDrawMode mode,
612+
int vertexCount, int indexCount,
613+
int vbo, int ebo) {
614+
prepareDraw();
615+
616+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
617+
618+
prepareVA();
619+
620+
if (indexCount > 0) {
621+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
622+
glDrawElements(mode.getGlType(), indexCount, GL_UNSIGNED_INT, 0L);
623+
} else {
624+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
625+
glDrawArrays(mode.getGlType(), 0, vertexCount);
626+
}
627+
628+
postDraw();
629+
}
630+
591631
///////////////////////////////////////////////////////////////////////////
592632
// Matrix state
593633
///////////////////////////////////////////////////////////////////////////
@@ -620,7 +660,7 @@ public static void lglPerspectiveDeg(float fovy,
620660
float aspect,
621661
float zNear,
622662
float zFar) {
623-
currentMat.perspective(Math.toRadians(fovy), aspect, zNear, zFar);
663+
lglPerspective(Math.toRadians(fovy), aspect, zNear, zFar);
624664
}
625665

626666
public static void lglFrustum(float left,
@@ -710,7 +750,7 @@ public static void lglRotateDeg(float ang,
710750
float x,
711751
float y,
712752
float z) {
713-
currentMat.rotate(Math.toRadians(ang), x, y, z);
753+
lglRotate(Math.toRadians(ang), x, y, z);
714754
}
715755

716756
public static void lglRotateLocal(float ang,
@@ -724,7 +764,19 @@ public static void lglRotateLocalDeg(float ang,
724764
float x,
725765
float y,
726766
float z) {
727-
currentMat.rotateLocal(Math.toRadians(ang), x, y, z);
767+
lglRotateLocal(Math.toRadians(ang), x, y, z);
768+
}
769+
770+
public static void lglRotateXYZ(float angleX,
771+
float angleY,
772+
float angleZ) {
773+
currentMat.rotateXYZ(angleX, angleY, angleZ);
774+
}
775+
776+
public static void lglRotateXYZDeg(float angleX,
777+
float angleY,
778+
float angleZ) {
779+
lglRotateXYZ(Math.toRadians(angleX), Math.toRadians(angleY), Math.toRadians(angleZ));
728780
}
729781

730782
public static void lglScale(float xyz) {

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424

2525
package org.overrun.swgl.core.gl.ims;
2626

27-
import org.lwjgl.system.MemoryUtil;
2827
import org.overrun.swgl.core.gl.GLDrawMode;
2928

30-
import java.nio.ByteBuffer;
31-
import java.nio.IntBuffer;
29+
import static org.lwjgl.opengl.GL15C.glDeleteBuffers;
30+
import static org.lwjgl.opengl.GL15C.glIsBuffer;
3231

3332
/**
3433
* The IMS OpenGL list includes the buffers.
@@ -40,8 +39,8 @@ public class GLList implements AutoCloseable {
4039
private final int id;
4140
GLDrawMode drawMode;
4241
int vertexCount;
43-
ByteBuffer buffer;
44-
IntBuffer indexBuffer;
42+
int indexCount;
43+
int vbo, ebo;
4544

4645
public GLList(int id) {
4746
this.id = id;
@@ -51,23 +50,35 @@ public int getId() {
5150
return id;
5251
}
5352

54-
public ByteBuffer getBuffer() {
55-
return buffer;
53+
public GLDrawMode getDrawMode() {
54+
return drawMode;
5655
}
5756

58-
public IntBuffer getIndexBuffer() {
59-
return indexBuffer;
57+
public int getVertexCount() {
58+
return vertexCount;
59+
}
60+
61+
public int getIndexCount() {
62+
return indexCount;
63+
}
64+
65+
public int getVbo() {
66+
return vbo;
67+
}
68+
69+
public int getEbo() {
70+
return ebo;
6071
}
6172

6273
@Override
6374
public void close() {
64-
if (buffer != null) {
65-
MemoryUtil.memFree(buffer);
66-
buffer = null;
75+
if (glIsBuffer(vbo)) {
76+
glDeleteBuffers(vbo);
77+
vbo = 0;
6778
}
68-
if (indexBuffer != null) {
69-
MemoryUtil.memFree(indexBuffer);
70-
indexBuffer = null;
79+
if (glIsBuffer(ebo)) {
80+
glDeleteBuffers(ebo);
81+
ebo = 0;
7182
}
7283
}
7384
}

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424

2525
package org.overrun.swgl.core.gl.ims;
2626

27-
import org.lwjgl.system.MemoryUtil;
2827
import org.overrun.swgl.core.cfg.GlobalConfig;
2928

3029
import java.util.LinkedHashMap;
3130
import java.util.Map;
3231
import java.util.OptionalInt;
3332

33+
import static org.lwjgl.opengl.GL15C.*;
34+
import static org.lwjgl.system.MemoryUtil.*;
3435
import static org.overrun.swgl.core.gl.ims.GLImmeMode.*;
3536

3637
/**
@@ -75,19 +76,30 @@ public static void lglNewList(int n) {
7576
public static void lglEndList() {
7677
currentList.drawMode = lglGetDrawMode();
7778
currentList.vertexCount = lglGetVertexCount();
79+
currentList.indexCount = lglGetIndexCount();
7880

7981
currentList.close();
80-
currentList.buffer = MemoryUtil.memCalloc(buffer.limit());
81-
for (int i = 0; currentList.buffer.hasRemaining(); i++) {
82-
currentList.buffer.put(buffer.get(i));
82+
if (currentList.vbo <= 0)
83+
currentList.vbo = glGenBuffers();
84+
glBindBuffer(GL_ARRAY_BUFFER, currentList.vbo);
85+
var bb = memCalloc(buffer.limit());
86+
for (int i = 0; bb.hasRemaining(); i++) {
87+
bb.put(buffer.get(i));
8388
}
84-
currentList.buffer.flip();
89+
glBufferData(GL_ARRAY_BUFFER, bb.flip(), GL_STATIC_DRAW);
90+
memFree(bb);
91+
glBindBuffer(GL_ARRAY_BUFFER, 0);
8592

86-
currentList.indexBuffer = MemoryUtil.memCallocInt(indicesBuffer.limit());
87-
for (int i = 0; currentList.indexBuffer.hasRemaining(); i++) {
88-
currentList.indexBuffer.put(indicesBuffer.get(i));
93+
if (currentList.ebo <= 0)
94+
currentList.ebo = glGenBuffers();
95+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, currentList.ebo);
96+
var ib = memCallocInt(indicesBuffer.limit());
97+
for (int i = 0; ib.hasRemaining(); i++) {
98+
ib.put(indicesBuffer.get(i));
8999
}
90-
currentList.indexBuffer.flip();
100+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib.flip(), GL_STATIC_DRAW);
101+
memFree(ib);
102+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
91103

92104
currentList = null;
93105
lglSetRendering(true);
@@ -97,16 +109,9 @@ public static void lglCallList(int list) {
97109
if (checkNotPresent(list))
98110
return;
99111
var lst = LIST_MAP.get(list);
100-
if (lst.getBuffer() == null)
112+
if (lst.vbo <= 0)
101113
return;
102-
lglBegin(lst.drawMode);
103-
lglBuffer(lst.getBuffer());
104-
lst.getBuffer().position(0);
105-
if (lst.getIndexBuffer() != null) {
106-
lglIndexBuffer(lst.getIndexBuffer());
107-
lst.getIndexBuffer().position(0);
108-
}
109-
lglEnd();
114+
lglDrawBuffers(lst.drawMode, lst.vertexCount, lst.indexCount, lst.vbo, lst.ebo);
110115
}
111116

112117
public static void lglDeleteLists(int list, int range) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public String getName() {
159159
}
160160

161161
/**
162-
* Get the direction id in enum order.
162+
* Get the direction id in enum {@link #ordinal() order}.
163163
*
164164
* @return The direction id.
165165
*/

src/test/java/org/overrun/swgl/game/phys/AABB.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public AABB(Vector3fc min,
5151
Vector3fc max) {
5252
this.min.set(min);
5353
this.max.set(max);
54+
fix();
5455
}
5556

5657
public AABB(float minX,
@@ -61,6 +62,7 @@ public AABB(float minX,
6162
float maxZ) {
6263
min.set(minX, minY, minZ);
6364
max.set(maxX, maxY, maxZ);
65+
fix();
6466
}
6567

6668
/**

src/test/java/org/overrun/swgl/game/world/entity/Entity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class Entity {
5151
protected float eyeHeight = 0.0f;
5252
protected float bbWidth = 0.6f;
5353
protected float bbHeight = 1.8f;
54+
public boolean sneaking = false;
55+
public boolean flying = false;
5456

5557
public Entity(World world) {
5658
this(world, UUID.randomUUID());
@@ -64,7 +66,7 @@ public Entity(World world, UUID uuid) {
6466

6567
public void setPos(float x, float y, float z) {
6668
aabb = new AABB();
67-
float hw = bbWidth / 2.0f;
69+
float hw = bbWidth * 0.5f;
6870
aabb.min.set(x - hw, y, z - hw);
6971
aabb.max.set(x + hw, y + bbHeight, z + hw);
7072
}

0 commit comments

Comments
 (0)