Skip to content

Commit c80ca28

Browse files
committed
Rewrote the base system, added entities, added matrices and textures.
1 parent 6e91408 commit c80ca28

32 files changed

+3463
-31
lines changed
819 Bytes
Binary file not shown.
533 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

bin/me/finnthehumanlol/lwjgl/engine/shaders/defaultVs.glsl

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ in vec3 position;
44

55
out vec3 color;
66

7+
uniform mat4 transformMatrix;
8+
uniform mat4 projectionMatrix;
9+
710
void main(void){
8-
gl_Position = vec4(position.xyz, 1.0f);
11+
gl_Position = projectionMatrix * transformMatrix * vec4(position.xyz, 1.0f);
912
color = vec3(1.0f, 0.0f, 1.0f);
1013
}
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

hs_err_pid10904.log

+344
Large diffs are not rendered by default.

hs_err_pid12216.log

+306
Large diffs are not rendered by default.

hs_err_pid15332.log

+306
Large diffs are not rendered by default.

hs_err_pid19876.log

+306
Large diffs are not rendered by default.

hs_err_pid21036.log

+344
Large diffs are not rendered by default.

hs_err_pid22784.log

+306
Large diffs are not rendered by default.

hs_err_pid6420.log

+307
Large diffs are not rendered by default.

hs_err_pid7552.log

+306
Large diffs are not rendered by default.

hs_err_pid8196.log

+306
Large diffs are not rendered by default.

hs_err_pid9980.log

+307
Large diffs are not rendered by default.

res/test/phantom.png

7.21 KB
Loading

src/me/finnthehumanlol/lwjgl/engine/Engine.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
package me.finnthehumanlol.lwjgl.engine;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import org.lwjgl.glfw.GLFW;
47

58
import me.finnthehumanlol.lwjgl.engine.core.MainGameLoop;
9+
import me.finnthehumanlol.lwjgl.engine.core.Updateable;
610
import me.finnthehumanlol.lwjgl.engine.io.Window;
11+
import me.finnthehumanlol.lwjgl.engine.render.Renderer;
12+
import me.finnthehumanlol.lwjgl.engine.render.Shader;
13+
import me.finnthehumanlol.lwjgl.engine.types.builtin.Entity;
14+
import me.finnthehumanlol.lwjgl.engine.util.MathUtil;
715

816
public final class Engine {
917

1018
public static MainGameLoop mainGameLoop;
1119
private static Window window;
20+
private static List<Entity> entities = new ArrayList<Entity>();
1221

1322
public static Thread mainThread = new Thread(new Runnable() {
1423

1524
@Override
1625
public void run() {
26+
Renderer.projectionMatrix = MathUtil.createProjectionMatrix(Renderer.FOV, Renderer.MIN_DIST, Renderer.MAX_DIST);
27+
Shader.projectionMatrix = Renderer.projectionMatrix;
1728
mainGameLoop.start();
1829
while(!GLFW.glfwWindowShouldClose(window.getWindow())) {
19-
mainGameLoop.update();
30+
mainGameLoop.onUpdate();
31+
for(Entity e : entities) {
32+
e.onUpdate();
33+
}
2034
window.onRender();
21-
mainGameLoop.render();
35+
mainGameLoop.onRender();
36+
for(Entity e : entities) {
37+
e.onRender();
38+
}
2239
GLFW.glfwSwapBuffers(window.getWindow());
2340
}
2441
mainGameLoop.stop();
@@ -46,4 +63,8 @@ public static Thread getMainThread() {
4663
return mainThread;
4764
}
4865

66+
public static void addEntity(Entity e) {
67+
Engine.entities.add(e);
68+
}
69+
4970
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package me.finnthehumanlol.lwjgl.engine.core;
22

3-
public interface MainGameLoop {
3+
public interface MainGameLoop extends Updateable{
44

55
public void start();
6-
public void update();
7-
public void render();
86
public void stop();
97

108
}

src/me/finnthehumanlol/lwjgl/engine/render/Renderer.java

+27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package me.finnthehumanlol.lwjgl.engine.render;
22

3+
import org.joml.Matrix4f;
4+
import org.joml.Vector3f;
35
import org.lwjgl.opengl.GL11;
46

57
import me.finnthehumanlol.lwjgl.engine.types.Mesh;
8+
import me.finnthehumanlol.lwjgl.engine.types.builtin.Entity;
9+
import me.finnthehumanlol.lwjgl.engine.util.MathUtil;
610

711
public class Renderer {
812

13+
public static float FOV = 70;
14+
public static float MIN_DIST = 0.1f;
15+
public static float MAX_DIST = 1000;
16+
17+
public static Matrix4f projectionMatrix;
18+
19+
public static void updateProjectionMatrix() {
20+
Renderer.projectionMatrix = MathUtil.createProjectionMatrix(FOV, MIN_DIST, MAX_DIST);
21+
}
22+
923
public static void render(Mesh mesh) {
1024
mesh.prepareRender();
1125
if(mesh.getIbo() == 0)
@@ -16,6 +30,19 @@ public static void render(Mesh mesh) {
1630
mesh.finishRender();
1731
}
1832

33+
public static void render(Entity entity) {
34+
entity.getMesh().prepareRender();
35+
Matrix4f mat4 = MathUtil.createTransformationMatrix(entity.getPosition(), entity.getRotation(), new Vector3f(entity.getScale(), entity.getScale(), entity.getScale()));
36+
entity.getMesh().getShader().setUniformMat4("transformMatrix", mat4);
37+
entity.getMesh().getShader().setUniformMat4("projectionMatrix", Renderer.projectionMatrix);
38+
if(entity.getMesh().getIbo() == 0)
39+
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, entity.getMesh().getVertexCount());
40+
else {
41+
GL11.glDrawElements(GL11.GL_TRIANGLES, entity.getMesh().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
42+
}
43+
entity.getMesh().finishRender();
44+
}
45+
1946
private Renderer() {}
2047

2148
}

src/me/finnthehumanlol/lwjgl/engine/render/Shader.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
import java.io.FileNotFoundException;
55
import java.io.FileReader;
66
import java.io.IOException;
7+
import java.nio.FloatBuffer;
78

9+
import org.joml.Matrix4f;
10+
import org.joml.Vector3f;
11+
import org.lwjgl.BufferUtils;
812
import org.lwjgl.opengl.GL20;
913

1014
public class Shader {
1115

1216
private String vertexPath, fragmentPath;
1317

1418
private int program, vertexShader, fragmentShader;
19+
20+
public FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
21+
22+
public static Matrix4f projectionMatrix;
1523

1624
public Shader(String vertexPath, String fragmentPath) {
1725
program = GL20.glCreateProgram();
@@ -25,9 +33,29 @@ public Shader(String vertexPath, String fragmentPath) {
2533
GL20.glAttachShader(program, fragmentShader);
2634
GL20.glLinkProgram(program);
2735
GL20.glValidateProgram(program);
28-
36+
37+
}
38+
39+
public int getUniformLocation(String variableName) {
40+
return GL20.glGetUniformLocation(program, variableName);
41+
}
42+
43+
public void setUniform1i(String variableName, int value) {
44+
GL20.glUniform1i(getUniformLocation(variableName), value);
45+
}
46+
47+
public void setUniform1f(String variableName, float value) {
48+
GL20.glUniform1f(getUniformLocation(variableName), value);
49+
}
50+
51+
public void setUniformVec3(String variableName, Vector3f value) {
52+
GL20.glUniform3f(getUniformLocation(variableName), value.x, value.y, value.z);
2953
}
3054

55+
public void setUniformMat4(String variableName, Matrix4f value) {
56+
GL20.glUniformMatrix4fv(getUniformLocation(variableName), false, value.get(matrixBuffer));
57+
}
58+
3159
public void bindAttribute(int index, String varName) {
3260
GL20.glBindAttribLocation(program, index, varName);
3361
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 400 core
2+
3+
in vec2 outTexCoord;
4+
5+
out vec4 color;
6+
7+
uniform sampler2D textureSampler;
8+
9+
void main(void){
10+
color = texture(textureSampler, outTexCoord);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 400 core
2+
3+
in vec3 position;
4+
in vec2 texCoord;
5+
6+
out vec2 outTexCoord;
7+
8+
uniform mat4 transformMatrix;
9+
uniform mat4 projectionMatrix;
10+
11+
void main(void){
12+
gl_Position = projectionMatrix * transformMatrix * vec4(position.xyz, 1.0f);
13+
outTexCoord = texCoord;
14+
}

src/me/finnthehumanlol/lwjgl/engine/shaders/defaultVs.glsl

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ in vec3 position;
44

55
out vec3 color;
66

7+
uniform mat4 transformMatrix;
8+
uniform mat4 projectionMatrix;
9+
710
void main(void){
8-
gl_Position = vec4(position.xyz, 1.0f);
11+
gl_Position = projectionMatrix * transformMatrix * vec4(position.xyz, 1.0f);
912
color = vec3(1.0f, 0.0f, 1.0f);
1013
}

src/me/finnthehumanlol/lwjgl/engine/types/Mesh.java

+53-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.lwjgl.opengl.GL11;
7+
import org.lwjgl.opengl.GL13;
68
import org.lwjgl.opengl.GL15;
79
import org.lwjgl.opengl.GL20;
810
import org.lwjgl.opengl.GL30;
@@ -14,8 +16,9 @@ public class Mesh {
1416
private int vao, vertexCount, attributeCount;
1517
private int ibo = 0;
1618
private Shader shader;
17-
private float[] positions;
19+
private float[] positions, texCoords;
1820
private int[] indices;
21+
private Texture texture;
1922

2023
private List<Integer> vbos = new ArrayList<>();
2124

@@ -25,6 +28,7 @@ public Mesh(float[] positions) {
2528
vertexCount = positions.length / 3;
2629
GL30.glBindVertexArray(vao);
2730
addAttribute(0, positions, 3);
31+
attachShader();
2832
}
2933

3034
public Mesh(float[] positions, int[] indices) {
@@ -34,39 +38,66 @@ public Mesh(float[] positions, int[] indices) {
3438
vertexCount = indices.length;
3539
GL30.glBindVertexArray(vao);
3640
addAttribute(0, positions, 3);
41+
addIndexBuffer(indices);
42+
attachShader();
3743
}
3844

45+
public void applyTexture(Texture texture, float[] texCoords) {
46+
this.texture = texture;
47+
this.shader = new Shader("src/me/finnthehumanlol/lwjgl/engine/shaders/defaultTexVs.glsl", "src/me/finnthehumanlol/lwjgl/engine/shaders/defaultTexFs.glsl");
48+
this.texCoords = texCoords;
49+
addAttribute(1, texCoords, 2);
50+
}
51+
52+
public void applyTexture(Texture texture, float[] texCoords, Shader texShader) {
53+
this.texture = texture;
54+
this.shader = texShader;
55+
this.texCoords = texCoords;
56+
}
57+
3958
public void attachShader(Shader shader) {
4059
this.shader = shader;
4160
}
4261

4362
public void attachShader() {
44-
this.shader = new Shader("src/me/finnthehumanlol/lwjgl/engine/shaders/defaultFs.glsl", "src/me/finnthehumanlol/lwjgl/engine/shaders/defaultVs.glsl");
63+
this.shader = new Shader("src/me/finnthehumanlol/lwjgl/engine/shaders/defaultVs.glsl", "src/me/finnthehumanlol/lwjgl/engine/shaders/defaultFs.glsl");
4564
}
4665

4766
public void detachShader() {
48-
this.shader = null;
67+
this.shader = new Shader("src/me/finnthehumanlol/lwjgl/engine/shaders/defaultVs.glsl", "src/me/finnthehumanlol/lwjgl/engine/shaders/defaultFs.glsl");
4968
}
5069

5170
public void prepareRender() {
5271
GL30.glBindVertexArray(vao);
5372

54-
for (int i = 0; i < attributeCount; i++)
55-
GL20.glEnableVertexAttribArray(i);
56-
if(shader != null) {
57-
GL20.glUseProgram(shader.getProgram());
58-
}
73+
GL20.glEnableVertexAttribArray(0);
74+
75+
GL20.glEnableVertexAttribArray(1);
76+
77+
GL20.glUseProgram(shader.getProgram());
5978

6079
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
80+
81+
if(texture != null) {
82+
GL13.glActiveTexture(GL13.GL_TEXTURE0);
83+
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
84+
}
6185
}
6286

6387
public void finishRender() {
6488
GL30.glBindVertexArray(0);
65-
for (int i = 0; i < attributeCount; i++)
66-
GL20.glDisableVertexAttribArray(i);
89+
90+
GL20.glDisableVertexAttribArray(0);
91+
GL20.glDisableVertexAttribArray(1);
92+
6793
GL20.glUseProgram(0);
6894

6995
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
96+
97+
if(texture != null) {
98+
GL13.glActiveTexture(0);
99+
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
100+
}
70101
}
71102

72103
private void addIndexBuffer(int[] data) {
@@ -116,4 +147,16 @@ public Shader getShader() {
116147
return shader;
117148
}
118149

150+
public float[] getPositions() {
151+
return positions;
152+
}
153+
154+
public int[] getIndices() {
155+
return indices;
156+
}
157+
158+
public Texture getTexture() {
159+
return texture;
160+
}
161+
119162
}

src/me/finnthehumanlol/lwjgl/engine/types/Texture.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@ public Texture(String texturePath) {
2929
IntBuffer w = stack.mallocInt(1);
3030
IntBuffer h = stack.mallocInt(1);
3131
IntBuffer comp = stack.mallocInt(1);
32-
32+
3333
image = STBImage.stbi_load(texturePath, w, h, comp, 4);
3434

3535
if(image == null) {
36-
System.err.println("Couldnt load texture!");
36+
System.err.println("Image could not be read:" + texturePath);
3737
}
3838

3939
width = w.get();
4040
height = h.get();
4141
this.texWidth = width;
4242
this.texHeight = height;
43-
4443
}
4544

4645
textureID = GL11.glGenTextures();
4746
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
4847
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
4948
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
49+
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
50+
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
5051
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, image);
5152
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
5253

0 commit comments

Comments
 (0)