modifying normal model loading
parent
e97b87d79a
commit
b6459bb737
|
@ -0,0 +1,94 @@
|
|||
package normalMappingRenderer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
import org.lwjgl.util.vector.Vector4f;
|
||||
|
||||
import entities.Camera;
|
||||
import entities.Entity;
|
||||
import entities.Light;
|
||||
import models.RawModel;
|
||||
import models.TexturedModel;
|
||||
import renderEngine.MasterRenderer;
|
||||
import textures.ModelTexture;
|
||||
import toolbox.Maths;
|
||||
|
||||
public class NormalMappingRenderer {
|
||||
|
||||
private NormalMappingShader shader;
|
||||
|
||||
public NormalMappingRenderer(Matrix4f projectionMatrix) {
|
||||
this.shader = new NormalMappingShader();
|
||||
shader.start();
|
||||
shader.loadProjectionMatrix(projectionMatrix);
|
||||
shader.connectTextureUnits();
|
||||
shader.stop();
|
||||
}
|
||||
|
||||
public void render(Map<TexturedModel, List<Entity>> entities, Vector4f clipPlane, List<Light> lights, Camera camera) {
|
||||
shader.start();
|
||||
prepare(clipPlane, lights, camera);
|
||||
for (TexturedModel model : entities.keySet()) {
|
||||
prepareTexturedModel(model);
|
||||
List<Entity> batch = entities.get(model);
|
||||
for (Entity entity : batch) {
|
||||
prepareInstance(entity);
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
unbindTexturedModel();
|
||||
}
|
||||
shader.stop();
|
||||
}
|
||||
|
||||
public void cleanUp(){
|
||||
shader.cleanUp();
|
||||
}
|
||||
|
||||
private void prepareTexturedModel(TexturedModel model) {
|
||||
RawModel rawModel = model.getRawModel();
|
||||
GL30.glBindVertexArray(rawModel.getVaoID());
|
||||
GL20.glEnableVertexAttribArray(0);
|
||||
GL20.glEnableVertexAttribArray(1);
|
||||
GL20.glEnableVertexAttribArray(2);
|
||||
ModelTexture texture = model.getTexture();
|
||||
shader.loadNumberOfRows(texture.getNumberOfRows());
|
||||
if (texture.isHasTransparency()) {
|
||||
MasterRenderer.disableCulling();
|
||||
}
|
||||
shader.loadShineVariables(texture.getShineDamper(), texture.getReflectivity());
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getID());
|
||||
}
|
||||
|
||||
private void unbindTexturedModel() {
|
||||
MasterRenderer.enableCulling();
|
||||
GL20.glDisableVertexAttribArray(0);
|
||||
GL20.glDisableVertexAttribArray(1);
|
||||
GL20.glDisableVertexAttribArray(2);
|
||||
GL30.glBindVertexArray(0);
|
||||
}
|
||||
|
||||
private void prepareInstance(Entity entity) {
|
||||
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
|
||||
entity.getRotY(), entity.getRotZ(), entity.getScale());
|
||||
shader.loadTransformationMatrix(transformationMatrix);
|
||||
shader.loadOffset(entity.getTextureXOffset(), entity.getTextureYOffset());
|
||||
}
|
||||
|
||||
private void prepare(Vector4f clipPlane, List<Light> lights, Camera camera) {
|
||||
shader.loadClipPlane(clipPlane);
|
||||
//need to be public variables in MasterRenderer
|
||||
shader.loadSkyColour(MasterRenderer.RED, MasterRenderer.GREEN, MasterRenderer.BLUE);
|
||||
Matrix4f viewMatrix = Maths.createViewMatrix(camera);
|
||||
|
||||
shader.loadLights(lights, viewMatrix);
|
||||
shader.loadViewMatrix(viewMatrix);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
package normalMappingRenderer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
import org.lwjgl.util.vector.Vector2f;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
import org.lwjgl.util.vector.Vector4f;
|
||||
|
||||
import entities.Light;
|
||||
import shaders.ShaderProgram;
|
||||
|
||||
public class NormalMappingShader extends ShaderProgram{
|
||||
|
||||
private static final int MAX_LIGHTS = 4;
|
||||
|
||||
private static final String VERTEX_FILE = "src/normalMappingRenderer/normalMapVShader.txt";
|
||||
private static final String FRAGMENT_FILE = "src/normalMappingRenderer/normalMapFShader.txt";
|
||||
|
||||
private int location_transformationMatrix;
|
||||
private int location_projectionMatrix;
|
||||
private int location_viewMatrix;
|
||||
private int location_lightPositionEyeSpace[];
|
||||
private int location_lightColour[];
|
||||
private int location_attenuation[];
|
||||
private int location_shineDamper;
|
||||
private int location_reflectivity;
|
||||
private int location_skyColour;
|
||||
private int location_numberOfRows;
|
||||
private int location_offset;
|
||||
private int location_plane;
|
||||
private int location_modelTexture;
|
||||
|
||||
public NormalMappingShader() {
|
||||
super(VERTEX_FILE, FRAGMENT_FILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void bindAttributes() {
|
||||
super.bindAttribute(0, "position");
|
||||
super.bindAttribute(1, "textureCoordinates");
|
||||
super.bindAttribute(2, "normal");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void getAllUniformLocations() {
|
||||
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
|
||||
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
|
||||
location_viewMatrix = super.getUniformLocation("viewMatrix");
|
||||
location_shineDamper = super.getUniformLocation("shineDamper");
|
||||
location_reflectivity = super.getUniformLocation("reflectivity");
|
||||
location_skyColour = super.getUniformLocation("skyColour");
|
||||
location_numberOfRows = super.getUniformLocation("numberOfRows");
|
||||
location_offset = super.getUniformLocation("offset");
|
||||
location_plane = super.getUniformLocation("plane");
|
||||
location_modelTexture = super.getUniformLocation("modelTexture");
|
||||
|
||||
location_lightPositionEyeSpace = new int[MAX_LIGHTS];
|
||||
location_lightColour = new int[MAX_LIGHTS];
|
||||
location_attenuation = new int[MAX_LIGHTS];
|
||||
for(int i=0;i<MAX_LIGHTS;i++){
|
||||
location_lightPositionEyeSpace[i] = super.getUniformLocation("lightPositionEyeSpace[" + i + "]");
|
||||
location_lightColour[i] = super.getUniformLocation("lightColour[" + i + "]");
|
||||
location_attenuation[i] = super.getUniformLocation("attenuation[" + i + "]");
|
||||
}
|
||||
}
|
||||
|
||||
protected void connectTextureUnits(){
|
||||
super.loadInt(location_modelTexture, 0);
|
||||
}
|
||||
|
||||
protected void loadClipPlane(Vector4f plane){
|
||||
super.loadVector(location_plane, plane);
|
||||
}
|
||||
|
||||
protected void loadNumberOfRows(int numberOfRows){
|
||||
super.loadFloat(location_numberOfRows, numberOfRows);
|
||||
}
|
||||
|
||||
protected void loadOffset(float x, float y){
|
||||
super.load2DVector(location_offset, new Vector2f(x,y));
|
||||
}
|
||||
|
||||
protected void loadSkyColour(float r, float g, float b){
|
||||
super.loadVector(location_skyColour, new Vector3f(r,g,b));
|
||||
}
|
||||
|
||||
protected void loadShineVariables(float damper,float reflectivity){
|
||||
super.loadFloat(location_shineDamper, damper);
|
||||
super.loadFloat(location_reflectivity, reflectivity);
|
||||
}
|
||||
|
||||
protected void loadTransformationMatrix(Matrix4f matrix){
|
||||
super.loadMatrix(location_transformationMatrix, matrix);
|
||||
}
|
||||
|
||||
protected void loadLights(List<Light> lights, Matrix4f viewMatrix){
|
||||
for(int i=0;i<MAX_LIGHTS;i++){
|
||||
if(i<lights.size()){
|
||||
super.loadVector(location_lightPositionEyeSpace[i], getEyeSpacePosition(lights.get(i), viewMatrix));
|
||||
super.loadVector(location_lightColour[i], lights.get(i).getColour());
|
||||
super.loadVector(location_attenuation[i], lights.get(i).getAttenuation());
|
||||
}else{
|
||||
super.loadVector(location_lightPositionEyeSpace[i], new Vector3f(0, 0, 0));
|
||||
super.loadVector(location_lightColour[i], new Vector3f(0, 0, 0));
|
||||
super.loadVector(location_attenuation[i], new Vector3f(1, 0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void loadViewMatrix(Matrix4f viewMatrix){
|
||||
super.loadMatrix(location_viewMatrix, viewMatrix);
|
||||
}
|
||||
|
||||
protected void loadProjectionMatrix(Matrix4f projection){
|
||||
super.loadMatrix(location_projectionMatrix, projection);
|
||||
}
|
||||
|
||||
private Vector3f getEyeSpacePosition(Light light, Matrix4f viewMatrix){
|
||||
Vector3f position = light.getPosition();
|
||||
Vector4f eyeSpacePos = new Vector4f(position.x,position.y, position.z, 1f);
|
||||
Matrix4f.transform(viewMatrix, eyeSpacePos, eyeSpacePos);
|
||||
return new Vector3f(eyeSpacePos);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#version 400 core
|
||||
|
||||
in vec2 pass_textureCoordinates;
|
||||
in vec3 surfaceNormal;
|
||||
in vec3 toLightVector[4];
|
||||
in vec3 toCameraVector;
|
||||
in float visibility;
|
||||
|
||||
out vec4 out_Color;
|
||||
|
||||
uniform sampler2D modelTexture;
|
||||
uniform vec3 lightColour[4];
|
||||
uniform vec3 attenuation[4];
|
||||
uniform float shineDamper;
|
||||
uniform float reflectivity;
|
||||
uniform vec3 skyColour;
|
||||
|
||||
void main(void){
|
||||
|
||||
vec3 unitNormal = normalize(surfaceNormal);
|
||||
vec3 unitVectorToCamera = normalize(toCameraVector);
|
||||
|
||||
vec3 totalDiffuse = vec3(0.0);
|
||||
vec3 totalSpecular = vec3(0.0);
|
||||
|
||||
for(int i=0;i<4;i++){
|
||||
float distance = length(toLightVector[i]);
|
||||
float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
|
||||
vec3 unitLightVector = normalize(toLightVector[i]);
|
||||
float nDotl = dot(unitNormal,unitLightVector);
|
||||
float brightness = max(nDotl,0.0);
|
||||
vec3 lightDirection = -unitLightVector;
|
||||
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);
|
||||
float specularFactor = dot(reflectedLightDirection , unitVectorToCamera);
|
||||
specularFactor = max(specularFactor,0.0);
|
||||
float dampedFactor = pow(specularFactor,shineDamper);
|
||||
totalDiffuse = totalDiffuse + (brightness * lightColour[i])/attFactor;
|
||||
totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColour[i])/attFactor;
|
||||
}
|
||||
totalDiffuse = max(totalDiffuse, 0.2);
|
||||
|
||||
vec4 textureColour = texture(modelTexture,pass_textureCoordinates);
|
||||
if(textureColour.a<0.5){
|
||||
discard;
|
||||
}
|
||||
|
||||
out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular,1.0);
|
||||
out_Color = mix(vec4(skyColour,1.0),out_Color, visibility);
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
#version 400 core
|
||||
|
||||
in vec3 position;
|
||||
in vec2 textureCoordinates;
|
||||
in vec3 normal;
|
||||
|
||||
out vec2 pass_textureCoordinates;
|
||||
out vec3 surfaceNormal;
|
||||
out vec3 toLightVector[4];
|
||||
out vec3 toCameraVector;
|
||||
out float visibility;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform vec3 lightPositionEyeSpace[4];
|
||||
|
||||
uniform float numberOfRows;
|
||||
uniform vec2 offset;
|
||||
|
||||
const float density = 0;
|
||||
const float gradient = 5.0;
|
||||
|
||||
uniform vec4 plane;
|
||||
|
||||
void main(void){
|
||||
|
||||
vec4 worldPosition = transformationMatrix * vec4(position,1.0);
|
||||
gl_ClipDistance[0] = dot(worldPosition, plane);
|
||||
mat4 modelViewMatrix = viewMatrix * transformationMatrix;
|
||||
vec4 positionRelativeToCam = modelViewMatrix * vec4(position,1.0);
|
||||
gl_Position = projectionMatrix * positionRelativeToCam;
|
||||
|
||||
pass_textureCoordinates = (textureCoordinates/numberOfRows) + offset;
|
||||
|
||||
surfaceNormal = (modelViewMatrix * vec4(normal,0.0)).xyz;
|
||||
for(int i=0;i<4;i++){
|
||||
toLightVector[i] = lightPositionEyeSpace[i] - positionRelativeToCam.xyz;
|
||||
}
|
||||
toCameraVector = -positionRelativeToCam.xyz;
|
||||
|
||||
float distance = length(positionRelativeToCam.xyz);
|
||||
visibility = exp(-pow((distance*density),gradient));
|
||||
visibility = clamp(visibility,0.0,1.0);
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,50 @@
|
|||
#version 400 core
|
||||
|
||||
in vec2 pass_textureCoordinates;
|
||||
in vec3 surfaceNormal;
|
||||
in vec3 toLightVector[4];
|
||||
in vec3 toCameraVector;
|
||||
in float visibility;
|
||||
|
||||
out vec4 out_Color;
|
||||
|
||||
uniform sampler2D modelTexture;
|
||||
uniform vec3 lightColour[4];
|
||||
uniform vec3 attenuation[4];
|
||||
uniform float shineDamper;
|
||||
uniform float reflectivity;
|
||||
uniform vec3 skyColour;
|
||||
|
||||
void main(void){
|
||||
|
||||
vec3 unitNormal = normalize(surfaceNormal);
|
||||
vec3 unitVectorToCamera = normalize(toCameraVector);
|
||||
|
||||
vec3 totalDiffuse = vec3(0.0);
|
||||
vec3 totalSpecular = vec3(0.0);
|
||||
|
||||
for(int i=0;i<4;i++){
|
||||
float distance = length(toLightVector[i]);
|
||||
float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
|
||||
vec3 unitLightVector = normalize(toLightVector[i]);
|
||||
float nDotl = dot(unitNormal,unitLightVector);
|
||||
float brightness = max(nDotl,0.0);
|
||||
vec3 lightDirection = -unitLightVector;
|
||||
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);
|
||||
float specularFactor = dot(reflectedLightDirection , unitVectorToCamera);
|
||||
specularFactor = max(specularFactor,0.0);
|
||||
float dampedFactor = pow(specularFactor,shineDamper);
|
||||
totalDiffuse = totalDiffuse + (brightness * lightColour[i])/attFactor;
|
||||
totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColour[i])/attFactor;
|
||||
}
|
||||
totalDiffuse = max(totalDiffuse, 0.2);
|
||||
|
||||
vec4 textureColour = texture(modelTexture,pass_textureCoordinates);
|
||||
if(textureColour.a<0.5){
|
||||
discard;
|
||||
}
|
||||
|
||||
out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular,1.0);
|
||||
out_Color = mix(vec4(skyColour,1.0),out_Color, visibility);
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
#version 400 core
|
||||
|
||||
in vec3 position;
|
||||
in vec2 textureCoordinates;
|
||||
in vec3 normal;
|
||||
|
||||
out vec2 pass_textureCoordinates;
|
||||
out vec3 surfaceNormal;
|
||||
out vec3 toLightVector[4];
|
||||
out vec3 toCameraVector;
|
||||
out float visibility;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform vec3 lightPositionEyeSpace[4];
|
||||
|
||||
uniform float numberOfRows;
|
||||
uniform vec2 offset;
|
||||
|
||||
const float density = 0;
|
||||
const float gradient = 5.0;
|
||||
|
||||
uniform vec4 plane;
|
||||
|
||||
void main(void){
|
||||
|
||||
vec4 worldPosition = transformationMatrix * vec4(position,1.0);
|
||||
gl_ClipDistance[0] = dot(worldPosition, plane);
|
||||
mat4 modelViewMatrix = viewMatrix * transformationMatrix;
|
||||
vec4 positionRelativeToCam = modelViewMatrix * vec4(position,1.0);
|
||||
gl_Position = projectionMatrix * positionRelativeToCam;
|
||||
|
||||
pass_textureCoordinates = (textureCoordinates/numberOfRows) + offset;
|
||||
|
||||
surfaceNormal = (modelViewMatrix * vec4(normal,0.0)).xyz;
|
||||
for(int i=0;i<4;i++){
|
||||
toLightVector[i] = lightPositionEyeSpace[i] - positionRelativeToCam.xyz;
|
||||
}
|
||||
toCameraVector = -positionRelativeToCam.xyz;
|
||||
|
||||
float distance = length(positionRelativeToCam.xyz);
|
||||
visibility = exp(-pow((distance*density),gradient));
|
||||
visibility = clamp(visibility,0.0,1.0);
|
||||
|
||||
}
|
Loading…
Reference in New Issue