This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
461 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/net/guerra24/voxel/client/graphics/ParticleRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.guerra24.voxel.client.graphics; | ||
|
||
import static org.lwjgl.opengl.GL11.GL_TRIANGLE_STRIP; | ||
import static org.lwjgl.opengl.GL11.glDepthMask; | ||
import static org.lwjgl.opengl.GL11.glDrawArrays; | ||
import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; | ||
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; | ||
import static org.lwjgl.opengl.GL30.glBindVertexArray; | ||
|
||
import java.util.Queue; | ||
|
||
import net.guerra24.voxel.client.graphics.shaders.ParticleShader; | ||
import net.guerra24.voxel.client.particle.Particle; | ||
import net.guerra24.voxel.client.resources.Loader; | ||
import net.guerra24.voxel.client.resources.models.RawModel; | ||
import net.guerra24.voxel.client.util.Maths; | ||
import net.guerra24.voxel.client.world.entities.Camera; | ||
import net.guerra24.voxel.universal.util.vector.Matrix4f; | ||
import net.guerra24.voxel.universal.util.vector.Vector3f; | ||
|
||
public class ParticleRenderer { | ||
|
||
private static final float[] VERTICES = { -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f }; | ||
|
||
private RawModel quad; | ||
private ParticleShader shader; | ||
|
||
public ParticleRenderer(Loader loader, Matrix4f projectionMatrix) { | ||
quad = loader.loadToVAO(VERTICES, 2); | ||
shader = new ParticleShader(); | ||
shader.start(); | ||
shader.loadProjectionMatrix(projectionMatrix); | ||
} | ||
|
||
public void render(Queue<Particle> particles, Camera camera) { | ||
Matrix4f viewMatrix = Maths.createViewMatrix(camera); | ||
prepare(); | ||
for (Particle particle : particles) { | ||
updateModelViewMatrix(particle.getPosition(), particle.getRotation(), particle.getScale(), viewMatrix); | ||
glDrawArrays(GL_TRIANGLE_STRIP, 0, quad.getVertexCount()); | ||
} | ||
finishRendering(); | ||
} | ||
|
||
private void updateModelViewMatrix(Vector3f position, float rotation, float scale, Matrix4f viewMatrix) { | ||
Matrix4f modelMatrix = new Matrix4f(); | ||
Matrix4f.translate(position, modelMatrix, modelMatrix); | ||
modelMatrix.m00 = viewMatrix.m00; | ||
modelMatrix.m01 = viewMatrix.m10; | ||
modelMatrix.m02 = viewMatrix.m20; | ||
modelMatrix.m10 = viewMatrix.m01; | ||
modelMatrix.m11 = viewMatrix.m11; | ||
modelMatrix.m12 = viewMatrix.m21; | ||
modelMatrix.m20 = viewMatrix.m02; | ||
modelMatrix.m21 = viewMatrix.m12; | ||
modelMatrix.m22 = viewMatrix.m22; | ||
Matrix4f.rotate((float) Math.toRadians(rotation), new Vector3f(0, 0, 1), modelMatrix, modelMatrix); | ||
Matrix4f.scale(new Vector3f(scale, scale, scale), modelMatrix, modelMatrix); | ||
Matrix4f modelViewMatrix = Matrix4f.mul(viewMatrix, modelMatrix, null); | ||
shader.loadModelViewMatrix(modelViewMatrix); | ||
} | ||
|
||
private void prepare() { | ||
shader.start(); | ||
glBindVertexArray(quad.getVaoID()); | ||
glEnableVertexAttribArray(0); | ||
glDepthMask(false); | ||
} | ||
|
||
private void finishRendering() { | ||
glDepthMask(true); | ||
glDisableVertexAttribArray(0); | ||
glBindVertexArray(0); | ||
shader.stop(); | ||
} | ||
|
||
public void cleanUp() { | ||
shader.cleanUp(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/net/guerra24/voxel/client/graphics/shaders/ParticleShader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.guerra24.voxel.client.graphics.shaders; | ||
|
||
import net.guerra24.voxel.client.core.VoxelVariables; | ||
import net.guerra24.voxel.universal.util.vector.Matrix4f; | ||
|
||
public class ParticleShader extends ShaderProgram { | ||
|
||
private int location_modelViewMatrix; | ||
private int location_projectionMatrix; | ||
|
||
public ParticleShader() { | ||
super(VoxelVariables.VERTEX_FILE_PARTICLE, VoxelVariables.FRAGMENT_FILE_PARTICLE); | ||
} | ||
|
||
@Override | ||
protected void getAllUniformLocations() { | ||
location_modelViewMatrix = super.getUniformLocation("modelViewMatrix"); | ||
location_projectionMatrix = super.getUniformLocation("projectionMatrix"); | ||
} | ||
|
||
@Override | ||
protected void bindAttributes() { | ||
super.bindAttribute(0, "position"); | ||
} | ||
|
||
public void loadModelViewMatrix(Matrix4f modelView) { | ||
super.loadMatrix(location_modelViewMatrix, modelView); | ||
} | ||
|
||
public void loadProjectionMatrix(Matrix4f projectionMatrix) { | ||
super.loadMatrix(location_projectionMatrix, projectionMatrix); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/net/guerra24/voxel/client/particle/Particle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.guerra24.voxel.client.particle; | ||
|
||
import net.guerra24.voxel.client.core.VoxelVariables; | ||
import net.guerra24.voxel.universal.util.vector.Vector3f; | ||
|
||
public class Particle { | ||
|
||
private Vector3f position; | ||
private Vector3f velocity; | ||
|
||
private float gravityEffect; | ||
private float lifeLeght; | ||
private float rotation; | ||
private float scale; | ||
private float elapsedTime = 0; | ||
|
||
public Particle(Vector3f position, Vector3f velocity, float gravityEffect, float lifeLeght, float rotation, | ||
float scale) { | ||
this.position = position; | ||
this.velocity = velocity; | ||
this.gravityEffect = gravityEffect; | ||
this.lifeLeght = lifeLeght; | ||
this.rotation = rotation; | ||
this.scale = scale; | ||
ParticleMaster.getInstance().addParticle(this); | ||
} | ||
|
||
public boolean update(float delta) { | ||
velocity.y += VoxelVariables.GRAVITY * gravityEffect * delta; | ||
Vector3f change = new Vector3f(velocity); | ||
change.scale(delta); | ||
Vector3f.add(change, position, position); | ||
elapsedTime += delta; | ||
return elapsedTime < lifeLeght; | ||
} | ||
|
||
public Vector3f getPosition() { | ||
return position; | ||
} | ||
|
||
public float getRotation() { | ||
return rotation; | ||
} | ||
|
||
public float getScale() { | ||
return scale; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/net/guerra24/voxel/client/particle/ParticleMaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.guerra24.voxel.client.particle; | ||
|
||
import java.util.Iterator; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
import net.guerra24.voxel.client.graphics.ParticleRenderer; | ||
import net.guerra24.voxel.client.resources.Loader; | ||
import net.guerra24.voxel.client.world.entities.Camera; | ||
import net.guerra24.voxel.universal.util.vector.Matrix4f; | ||
|
||
public class ParticleMaster { | ||
|
||
private static ParticleMaster instance = null; | ||
|
||
public static ParticleMaster getInstance() { | ||
if (instance == null) { | ||
instance = new ParticleMaster(); | ||
} | ||
return instance; | ||
} | ||
|
||
private Queue<Particle> particles; | ||
private ParticleRenderer renderer; | ||
|
||
private ParticleMaster() { | ||
} | ||
|
||
public void init(Loader loader, Matrix4f projectionMatrix) { | ||
particles = new ConcurrentLinkedQueue<Particle>(); | ||
renderer = new ParticleRenderer(loader, projectionMatrix); | ||
} | ||
|
||
public void update(float delta) { | ||
Iterator<Particle> iterator = particles.iterator(); | ||
while (iterator.hasNext()) { | ||
Particle p = iterator.next(); | ||
boolean stillAlive = p.update(delta); | ||
if (!stillAlive) | ||
iterator.remove(); | ||
} | ||
|
||
} | ||
|
||
public void render(Camera camera) { | ||
renderer.render(particles, camera); | ||
} | ||
|
||
public void cleanUp() { | ||
renderer.cleanUp(); | ||
} | ||
|
||
public void addParticle(Particle particle) { | ||
particles.add(particle); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/net/guerra24/voxel/client/particle/ParticlePoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.guerra24.voxel.client.particle; | ||
|
||
import net.guerra24.voxel.universal.util.vector.Vector3f; | ||
|
||
public class ParticlePoint { | ||
private Vector3f pos; | ||
|
||
public ParticlePoint(Vector3f pos) { | ||
this.pos = pos; | ||
} | ||
|
||
public Vector3f getPos() { | ||
return pos; | ||
} | ||
} |
Oops, something went wrong.