Add entity handling.
This commit is contained in:
parent
45e8b4ee49
commit
32ed0a2f6c
6 changed files with 103 additions and 9 deletions
|
@ -1,6 +1,8 @@
|
|||
package com.okseby.core;
|
||||
|
||||
import com.okseby.core.entity.Entity;
|
||||
import com.okseby.core.entity.Model;
|
||||
import com.okseby.core.utils.Transformation;
|
||||
import com.okseby.core.utils.Utils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
|
@ -22,21 +24,25 @@ public class RenderManager {
|
|||
shader.createFragmentShader(Utils.loadResource("/shaders/fragment.fs"));
|
||||
|
||||
shader.link();
|
||||
|
||||
shader.createUniform("textureSampler");
|
||||
shader.createUniform("transformationMatrix");
|
||||
}
|
||||
|
||||
public void render(Model model) {
|
||||
public void render(Entity entity) {
|
||||
clear();
|
||||
|
||||
shader.bind();
|
||||
shader.setUniform("textureSampler", 0);
|
||||
|
||||
GL30.glBindVertexArray(model.getId());
|
||||
shader.setUniform("textureSampler", 0);
|
||||
shader.setUniform("transformationMatrix", Transformation.createTransformationMatrix(entity));
|
||||
|
||||
GL30.glBindVertexArray(entity.getModel().getId());
|
||||
GL20.glEnableVertexAttribArray(0);
|
||||
GL20.glEnableVertexAttribArray(1);
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getId());
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, entity.getModel().getTexture().getId());
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, entity.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
|
||||
GL20.glDisableVertexAttribArray(0);
|
||||
GL20.glDisableVertexAttribArray(1);
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.okseby.core;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
|
@ -35,10 +37,27 @@ public class ShaderManager {
|
|||
}
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, Vector4f value) {
|
||||
GL20.glUniform4f(uniforms.get(uniformName), value.x, value.y, value.z, value.w);
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, Vector3f value) {
|
||||
GL20.glUniform3f(uniforms.get(uniformName), value.x, value.y, value.z);
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, boolean value) {
|
||||
float res = value ? 1 : 0;
|
||||
GL20.glUniform1f(uniforms.get(uniformName), res);
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, int value) {
|
||||
GL20.glUniform1i(uniforms.get(uniformName), value);
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, float value) {
|
||||
GL20.glUniform1f(uniforms.get(uniformName), value);
|
||||
}
|
||||
|
||||
public void createVertexShader(String shaderCode) throws Exception {
|
||||
vertexShaderID = createShader(shaderCode, GL20.GL_VERTEX_SHADER);
|
||||
}
|
||||
|
|
41
src/main/java/com/okseby/core/entity/Entity.java
Normal file
41
src/main/java/com/okseby/core/entity/Entity.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package com.okseby.core.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class Entity {
|
||||
@Getter private Model model;
|
||||
@Getter private Vector3f position, rotation;
|
||||
@Getter private float scale;
|
||||
|
||||
public Entity(Model model, Vector3f position, Vector3f rotation, float scale) {
|
||||
this.model = model;
|
||||
this.position = position;
|
||||
this.rotation = rotation;
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
public void incrementPosition(float x, float y, float z) {
|
||||
this.position.x += x;
|
||||
this.position.y += y;
|
||||
this.position.z += z;
|
||||
}
|
||||
|
||||
public void setPosition(float x, float y, float z) {
|
||||
this.position.x = x;
|
||||
this.position.y = y;
|
||||
this.position.z = z;
|
||||
}
|
||||
|
||||
public void incrementRotation(float x, float y, float z) {
|
||||
this.rotation.x += x;
|
||||
this.rotation.y += y;
|
||||
this.rotation.z += z;
|
||||
}
|
||||
|
||||
public void setRotation(float x, float y, float z) {
|
||||
this.rotation.x = x;
|
||||
this.rotation.y = y;
|
||||
this.rotation.z = z;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
package com.okseby.core.test;
|
||||
|
||||
import com.okseby.core.*;
|
||||
import com.okseby.core.entity.Entity;
|
||||
import com.okseby.core.entity.Model;
|
||||
import com.okseby.core.entity.Texture;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
|
@ -14,7 +16,7 @@ public class TestGame implements ILogic {
|
|||
private final ObjectLoader loader;
|
||||
private final WindowManager window;
|
||||
|
||||
private Model model;
|
||||
private Entity entity;
|
||||
|
||||
public TestGame() {
|
||||
renderer = new RenderManager();
|
||||
|
@ -45,8 +47,10 @@ public class TestGame implements ILogic {
|
|||
1, 0
|
||||
};
|
||||
|
||||
model = loader.loadModel(vertices, textureCoordinates, indices);
|
||||
Model model = loader.loadModel(vertices, textureCoordinates, indices);
|
||||
model.setTexture(new Texture(loader.loadTexture("res/textures/grassblock.png")));
|
||||
|
||||
entity = new Entity(model, new Vector3f(1, 0, 0), new Vector3f(0, 0, 0), 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -66,6 +70,10 @@ public class TestGame implements ILogic {
|
|||
color = 1.0f;
|
||||
else if (color <= 0)
|
||||
color = 0.0f;
|
||||
|
||||
if (entity.getPosition().x < -1.5f)
|
||||
entity.getPosition().x = 1.5f;
|
||||
entity.getPosition().x -= 0.01f;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,7 +84,7 @@ public class TestGame implements ILogic {
|
|||
}
|
||||
|
||||
window.setClearColor(color, color, color, 0.0f);
|
||||
renderer.render(model);
|
||||
renderer.render(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
18
src/main/java/com/okseby/core/utils/Transformation.java
Normal file
18
src/main/java/com/okseby/core/utils/Transformation.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
package com.okseby.core.utils;
|
||||
|
||||
import com.okseby.core.entity.Entity;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class Transformation {
|
||||
public static Matrix4f createTransformationMatrix(Entity entity) {
|
||||
Matrix4f matrix = new Matrix4f();
|
||||
|
||||
matrix.identity().translate(entity.getPosition()).
|
||||
rotateX((float) Math.toRadians(entity.getRotation().x)).
|
||||
rotateY((float) Math.toRadians(entity.getRotation().y)).
|
||||
rotateZ((float) Math.toRadians(entity.getRotation().z)).
|
||||
scale(entity.getScale());
|
||||
|
||||
return matrix;
|
||||
}
|
||||
}
|
|
@ -5,7 +5,9 @@ in vec2 textureCoordinates;
|
|||
|
||||
out vec2 fragmentTextureCoordinates;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(position, 1.0);
|
||||
gl_Position = transformationMatrix * vec4(position, 1.0);
|
||||
fragmentTextureCoordinates = textureCoordinates;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue