88 lines
2.8 KiB
Java
88 lines
2.8 KiB
Java
package io.github.hydos.ginger.engine.renderEngine.renderers;
|
|
|
|
import java.util.List;
|
|
|
|
import org.lwjgl.opengl.GL11;
|
|
import org.lwjgl.opengl.GL13;
|
|
import org.lwjgl.opengl.GL20;
|
|
import org.lwjgl.opengl.GL30;
|
|
|
|
import io.github.hydos.ginger.engine.mathEngine.Maths;
|
|
import io.github.hydos.ginger.engine.mathEngine.matrixes.Matrix4f;
|
|
import io.github.hydos.ginger.engine.mathEngine.vectors.Vector3f;
|
|
import io.github.hydos.ginger.engine.renderEngine.models.RawModel;
|
|
import io.github.hydos.ginger.engine.renderEngine.shaders.TerrainShader;
|
|
import io.github.hydos.ginger.engine.terrain.Terrain;
|
|
import io.github.hydos.ginger.engine.terrain.TerrainTexture;
|
|
import io.github.hydos.ginger.engine.terrain.TerrainTexturePack;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|