79 lines
2.4 KiB
Java
79 lines
2.4 KiB
Java
package io.github.hydos.ginger.engine.render.renderers;
|
|
|
|
import java.util.List;
|
|
|
|
import org.joml.*;
|
|
import org.lwjgl.opengl.*;
|
|
|
|
import io.github.hydos.ginger.engine.math.Maths;
|
|
import io.github.hydos.ginger.engine.render.models.RawModel;
|
|
import io.github.hydos.ginger.engine.render.shaders.TerrainShader;
|
|
import io.github.hydos.ginger.engine.terrain.*;
|
|
|
|
public class TerrainRenderer
|
|
{
|
|
private TerrainShader shader;
|
|
|
|
public TerrainRenderer(TerrainShader shader, Matrix4f projectionMatrix)
|
|
{
|
|
this.shader = shader;
|
|
shader.start();
|
|
shader.loadProjectionMatrix(projectionMatrix);
|
|
shader.connectTextureUnits();
|
|
shader.stop();
|
|
}
|
|
|
|
public void render(List<Terrain> terrains, Matrix4f toShadowSpace)
|
|
{
|
|
shader.loadToShadowMapSpace(toShadowSpace);
|
|
for (Terrain t : terrains)
|
|
{
|
|
prepareTerrain(t);
|
|
loadModelMatrix(t);
|
|
GL11.glDrawElements(GL11.GL_TRIANGLES, t.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
|
unbindTerrain();
|
|
}
|
|
}
|
|
|
|
private void prepareTerrain(Terrain terrain)
|
|
{
|
|
RawModel rawModel = terrain.getModel();
|
|
GL30.glBindVertexArray(rawModel.getVaoID());
|
|
GL20.glEnableVertexAttribArray(0);
|
|
GL20.glEnableVertexAttribArray(1);
|
|
GL20.glEnableVertexAttribArray(2);
|
|
bindTextures(terrain);
|
|
shader.loadShine(1, 0);
|
|
}
|
|
|
|
private void bindTextures(Terrain terrain)
|
|
{
|
|
TerrainTexturePack texturePack = terrain.getTexturePack();
|
|
TerrainTexture blendMap = terrain.getBlendMap();
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getBackgroundTexture().getTextureID());
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE1);
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getrTexture().getTextureID());
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE2);
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getgTexture().getTextureID());
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE3);
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getbTexture().getTextureID());
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE4);
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, blendMap.getTextureID());
|
|
}
|
|
|
|
private void unbindTerrain()
|
|
{
|
|
GL20.glDisableVertexAttribArray(0);
|
|
GL20.glDisableVertexAttribArray(1);
|
|
GL20.glDisableVertexAttribArray(2);
|
|
GL30.glBindVertexArray(0);
|
|
}
|
|
|
|
private void loadModelMatrix(Terrain terrain)
|
|
{
|
|
Matrix4f transformationMatrix = Maths.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0, 0, 0, new Vector3f(1, 1, 1));
|
|
shader.loadTransformationMatrix(transformationMatrix);
|
|
}
|
|
}
|