[DEV] tutorial 17 'multiple texturing' implementation

This commit is contained in:
Edouard DUPIN 2020-04-20 22:41:20 +02:00
parent 2779326069
commit 6cd99d61cb
17 changed files with 155 additions and 24 deletions

BIN
res/blendMap.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 KiB

BIN
res/dirt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

BIN
res/grassFlowers.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

BIN
res/grassy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

BIN
res/mud.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

BIN
res/path.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 KiB

View File

@ -22,6 +22,8 @@ import renderEngine.OBJLoader;
import renderEngine.EntityRenderer; import renderEngine.EntityRenderer;
import shaders.StaticShader; import shaders.StaticShader;
import terrains.Terrain; import terrains.Terrain;
import terrains.TerrainTexture;
import terrains.TerrainTexturePack;
import textures.ModelTexture; import textures.ModelTexture;
/** /**
@ -57,14 +59,28 @@ public class MainGameLoop {
new ModelTexture(loader.loadTexture("grassTexture"))); new ModelTexture(loader.loadTexture("grassTexture")));
grassModel.getTexture().setHasTransparency(true); grassModel.getTexture().setHasTransparency(true);
grassModel.getTexture().setUseFakeLighting(true); grassModel.getTexture().setUseFakeLighting(true);
TexturedModel flowerModel = new TexturedModel(OBJLoader.loadObjModel("grassModel", loader),
new ModelTexture(loader.loadTexture("flower")));
flowerModel.getTexture().setHasTransparency(true);
flowerModel.getTexture().setUseFakeLighting(true);
TexturedModel fernModel = new TexturedModel(OBJLoader.loadObjModel("fern", loader), TexturedModel fernModel = new TexturedModel(OBJLoader.loadObjModel("fern", loader),
new ModelTexture(loader.loadTexture("fern"))); new ModelTexture(loader.loadTexture("fern")));
fernModel.getTexture().setHasTransparency(true); fernModel.getTexture().setHasTransparency(true);
Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1)); Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1));
TerrainTexture backgroundTexture = new TerrainTexture(loader.loadTexture("grass"));
TerrainTexture rTexture = new TerrainTexture(loader.loadTexture("dirt"));
TerrainTexture gTexture = new TerrainTexture(loader.loadTexture("grassFlowers"));
TerrainTexture bTexture = new TerrainTexture(loader.loadTexture("path"));
TerrainTexturePack texturePack = new TerrainTexturePack(backgroundTexture, rTexture, gTexture, bTexture);
TerrainTexture blendMap = new TerrainTexture(loader.loadTexture("blendMap"));
Terrain terrain = new Terrain(0,-1,loader, new ModelTexture(loader.loadTexture("grass"))); Terrain terrain = new Terrain(0,-1,loader, texturePack, blendMap);
Terrain terrain2 = new Terrain(-1,-1,loader, new ModelTexture(loader.loadTexture("grass"))); Terrain terrain2 = new Terrain(-1,-1,loader, texturePack, blendMap);
Camera camera = new Camera(); Camera camera = new Camera();
@ -81,6 +97,9 @@ public class MainGameLoop {
entities.add(new Entity(fernModel, entities.add(new Entity(fernModel,
new Vector3f(random.nextFloat()*800 - 400, 0, random.nextFloat() * -600), new Vector3f(random.nextFloat()*800 - 400, 0, random.nextFloat() * -600),
new Vector3f(0,0,0),0.6f)); new Vector3f(0,0,0),0.6f));
entities.add(new Entity(flowerModel,
new Vector3f(random.nextFloat()*800 - 400, 0, random.nextFloat() * -600),
new Vector3f(0,0,0),1.5f));
} }
MasterRenderer renderer = new MasterRenderer(); MasterRenderer renderer = new MasterRenderer();

View File

@ -26,6 +26,12 @@ public class Camera {
if (DisplayManager.isKeyDown('a')) { if (DisplayManager.isKeyDown('a')) {
position.x -= 0.8f; position.x -= 0.8f;
} }
if (DisplayManager.isKeyDown('q')) {
position.y += 0.8f;
}
if (DisplayManager.isKeyDown('z')) {
position.y -= 0.8f;
}
} }
public Vector3f getPosition() { public Vector3f getPosition() {

View File

@ -57,7 +57,6 @@ public class Loader {
} }
private int loadPNGTexture(String filename, int textureUnit) { private int loadPNGTexture(String filename, int textureUnit) {
ByteBuffer bufIn = null;
ByteBuffer buf = null; ByteBuffer buf = null;
int tWidth = 0; int tWidth = 0;
int tHeight = 0; int tHeight = 0;
@ -71,7 +70,8 @@ public class Loader {
tHeight = decoder.getHeight(); tHeight = decoder.getHeight();
// Decode the PNG file in a ByteBuffer // Decode the PNG file in a ByteBuffer
buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight()); buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
decoder.decodeFlipped(buf, decoder.getWidth() * 4, Format.RGBA); //decoder.decodeFlipped(buf, decoder.getWidth() * 4, Format.RGBA);
decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
buf.flip(); buf.flip();
in.close(); in.close();
} catch (IOException e) { } catch (IOException e) {
@ -96,10 +96,10 @@ public class Loader {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
// Setup what to do when the texture has to be scaled // Setup what to do when the texture has to be scaled
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); // GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); // GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
this.exitOnGLError("loadPNGTexture"); this.exitOnGLError("loadPNGTexture");

View File

@ -6,7 +6,8 @@ import objConverter.OBJFileLoader;
public class OBJLoader { public class OBJLoader {
public static RawModel loadObjModel(String fileName, Loader loader) { public static RawModel loadObjModel(String fileName, Loader loader) {
ModelData data = OBJFileLoader.loadOBJ("tree"); System.out.println("Load file " + fileName);
ModelData data = OBJFileLoader.loadOBJ(fileName);
return loader.loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getIndices()); return loader.loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getIndices());
} }
} }

View File

@ -13,6 +13,7 @@ import org.lwjgl.opengl.GL30;
import models.RawModel; import models.RawModel;
import shaders.TerrainShader; import shaders.TerrainShader;
import terrains.Terrain; import terrains.Terrain;
import terrains.TerrainTexturePack;
import textures.ModelTexture; import textures.ModelTexture;
import toolbox.Maths; import toolbox.Maths;
@ -24,6 +25,7 @@ public class TerrainRenderer {
this.shader = shader; this.shader = shader;
shader.start(); shader.start();
shader.loadProjectionMatrix(projectionMatrix); shader.loadProjectionMatrix(projectionMatrix);
shader.connectTextureUnits();
shader.stop(); shader.stop();
} }
@ -43,12 +45,24 @@ public class TerrainRenderer {
GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2); GL20.glEnableVertexAttribArray(2);
ModelTexture texture = terrain.getTexture(); bindTextures(terrain);
shader.loadShineVariable(texture.getShineDamper(), texture.getReflectivity()); shader.loadShineVariable(1, 0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTexturedID());
} }
private void bindTextures(Terrain terrain) {
TerrainTexturePack texturePack = terrain.getTexturePack();
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, terrain.getBlendMap().getTextureId());
}
private void unbindTexturedModel() { private void unbindTexturedModel() {
GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1); GL20.glDisableVertexAttribArray(1);

View File

@ -60,10 +60,15 @@ public abstract class ShaderProgram {
protected void loadFloat(int location, float value) { protected void loadFloat(int location, float value) {
GL20.glUniform1f(location, value); GL20.glUniform1f(location, value);
} }
protected void loadInt(int location, int value) {
GL20.glUniform1i(location, value);
}
protected void loadVector(int location, Vector3f value) { protected void loadVector(int location, Vector3f value) {
GL20.glUniform3f(location, value.x, value.y, value.z); GL20.glUniform3f(location, value.x, value.y, value.z);
} }
protected void loadBoolean(int location, boolean value) { protected void loadBoolean(int location, boolean value) {
//System.out.println("set value " + value + " " + (value==true?1.0f:0.0f)); //System.out.println("set value " + value + " " + (value==true?1.0f:0.0f));
GL20.glUniform1f(location, value==true?1.0f:0.0f); GL20.glUniform1f(location, value==true?1.0f:0.0f);

View File

@ -19,6 +19,11 @@ public class TerrainShader extends ShaderProgram {
private int location_reflectivity; private int location_reflectivity;
private int location_shineDamper; private int location_shineDamper;
private int location_skyColor; private int location_skyColor;
private int location_backgroundTexture;
private int location_rTexture;
private int location_gTexture;
private int location_bTexture;
private int location_blendMap;
public TerrainShader() { public TerrainShader() {
super(VERTEX_FILE, FRAGMENT_FILE); super(VERTEX_FILE, FRAGMENT_FILE);
@ -41,6 +46,19 @@ public class TerrainShader extends ShaderProgram {
location_reflectivity = super.getUniformLocation("reflectivity"); location_reflectivity = super.getUniformLocation("reflectivity");
location_shineDamper = super.getUniformLocation("shineDamper"); location_shineDamper = super.getUniformLocation("shineDamper");
location_skyColor = super.getUniformLocation("skyColor"); location_skyColor = super.getUniformLocation("skyColor");
location_backgroundTexture = super.getUniformLocation("backgroundTexture");
location_rTexture = super.getUniformLocation("rTexture");
location_gTexture = super.getUniformLocation("gTexture");
location_bTexture = super.getUniformLocation("bTexture");
location_blendMap = super.getUniformLocation("blendMap");
}
public void connectTextureUnits() {
super.loadInt(location_backgroundTexture, 0);
super.loadInt(location_rTexture, 1);
super.loadInt(location_gTexture, 2);
super.loadInt(location_bTexture, 3);
super.loadInt(location_blendMap, 4);
} }
public void loadSkyColour(Vector3f color) { public void loadSkyColour(Vector3f color) {

View File

@ -9,7 +9,12 @@ in float visibility;
out vec4 out_Color; out vec4 out_Color;
uniform sampler2D textureSampler; uniform sampler2D backgroundTexture;
uniform sampler2D rTexture;
uniform sampler2D gTexture;
uniform sampler2D bTexture;
uniform sampler2D blendMap;
uniform vec3 lightColour; uniform vec3 lightColour;
uniform float reflectivity; uniform float reflectivity;
uniform float shineDamper; uniform float shineDamper;
@ -18,6 +23,17 @@ uniform vec3 skyColor;
void main(void) { void main(void) {
vec4 blendMapColour = texture(blendMap, pass_textureCoordinates);
float backTextureAmount = 1 - (blendMapColour.r + blendMapColour.g + blendMapColour.b);
vec2 tiledCoords = pass_textureCoordinates * 40.0;
vec4 backgroundTextureColour = texture(backgroundTexture, tiledCoords) * backTextureAmount;
vec4 rTextureColour = texture(rTexture, tiledCoords) * blendMapColour.r;
vec4 gTextureColour = texture(gTexture, tiledCoords) * blendMapColour.g;
vec4 bTextureColour = texture(bTexture, tiledCoords) * blendMapColour.b;
vec4 totalColour = backgroundTextureColour + rTextureColour + gTextureColour + bTextureColour;
vec3 unitNormal = normalize(surfaceNormal); vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector); vec3 unitLightVector = normalize(toLightVector);
@ -35,7 +51,7 @@ void main(void) {
float damperFactor = pow(specularFactor, shineDamper); float damperFactor = pow(specularFactor, shineDamper);
vec3 finalSpecular = damperFactor * reflectivity * lightColour; vec3 finalSpecular = damperFactor * reflectivity * lightColour;
out_Color = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoordinates) + vec4(finalSpecular, 1.0); out_Color = vec4(diffuse,1.0) * totalColour + vec4(finalSpecular, 1.0);
out_Color = mix(vec4(skyColor,1.0), out_Color, visibility); out_Color = mix(vec4(skyColor,1.0), out_Color, visibility);
} }

View File

@ -11,10 +11,12 @@ public class Terrain {
private float x; private float x;
private float z; private float z;
private RawModel model; private RawModel model;
private ModelTexture texture; private TerrainTexturePack texturePack;
private TerrainTexture blendMap;
public Terrain(int girdX, int girdZ, Loader loader, ModelTexture texture) { public Terrain(int girdX, int girdZ, Loader loader, TerrainTexturePack texturePack, TerrainTexture blendMap) {
this.texture = texture; this.texturePack = texturePack;
this.blendMap = blendMap;
this.x = girdX * SIZE; this.x = girdX * SIZE;
this.z = girdZ * SIZE; this.z = girdZ * SIZE;
this.model = generateTerrain(loader); this.model = generateTerrain(loader);
@ -35,8 +37,8 @@ public class Terrain {
normals[vertexPointer*3] = 0; normals[vertexPointer*3] = 0;
normals[vertexPointer*3+1] = 1; normals[vertexPointer*3+1] = 1;
normals[vertexPointer*3+2] = 0; normals[vertexPointer*3+2] = 0;
textureCoords[vertexPointer*2] = (float)j/((float)VERTEX_COUNT - 1)*100; textureCoords[vertexPointer*2] = (float)j/((float)VERTEX_COUNT - 1);
textureCoords[vertexPointer*2+1] = (float)i/((float)VERTEX_COUNT - 1)*100; textureCoords[vertexPointer*2+1] = (float)i/((float)VERTEX_COUNT - 1);
vertexPointer++; vertexPointer++;
} }
} }
@ -82,11 +84,11 @@ public class Terrain {
this.model = model; this.model = model;
} }
public ModelTexture getTexture() { public TerrainTexture getBlendMap() {
return texture; return blendMap;
} }
public void setTexture(ModelTexture texture) { public TerrainTexturePack getTexturePack() {
this.texture = texture; return texturePack;
} }
} }

View File

@ -0,0 +1,18 @@
package terrains;
public class TerrainTexture {
private int textureId;
public TerrainTexture(int textureId) {
this.textureId = textureId;
}
public int getTextureId() {
return textureId;
}
public void setTextureId(int textureId) {
this.textureId = textureId;
}
}

View File

@ -0,0 +1,32 @@
package terrains;
public class TerrainTexturePack {
private TerrainTexture backgroundTexture;
private TerrainTexture rTexture;
private TerrainTexture gTexture;
private TerrainTexture bTexture;
public TerrainTexturePack(TerrainTexture backgroundTexture, TerrainTexture rTexture, TerrainTexture gTexture,
TerrainTexture bTexture) {
this.backgroundTexture = backgroundTexture;
this.rTexture = rTexture;
this.gTexture = gTexture;
this.bTexture = bTexture;
}
public TerrainTexture getBackgroundTexture() {
return backgroundTexture;
}
public TerrainTexture getrTexture() {
return rTexture;
}
public TerrainTexture getgTexture() {
return gTexture;
}
public TerrainTexture getbTexture() {
return bTexture;
}
}