Compare commits

...

2 Commits

20 changed files with 288 additions and 72 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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -12,6 +12,7 @@ import org.lwjgl.openvr.Texture;
import entities.Camera;
import entities.Entity;
import entities.Light;
import entities.Player;
import models.RawModel;
import models.TexturedModel;
import renderEngine.DisplayManager;
@ -22,6 +23,8 @@ import renderEngine.OBJLoader;
import renderEngine.EntityRenderer;
import shaders.StaticShader;
import terrains.Terrain;
import terrains.TerrainTexture;
import terrains.TerrainTexturePack;
import textures.ModelTexture;
/**
@ -57,14 +60,28 @@ public class MainGameLoop {
new ModelTexture(loader.loadTexture("grassTexture")));
grassModel.getTexture().setHasTransparency(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),
new ModelTexture(loader.loadTexture("fern")));
fernModel.getTexture().setHasTransparency(true);
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 terrain2 = new Terrain(-1,-1,loader, new ModelTexture(loader.loadTexture("grass")));
Terrain terrain = new Terrain(0,-1,loader, texturePack, blendMap);
Terrain terrain2 = new Terrain(-1,-1,loader, texturePack, blendMap);
Camera camera = new Camera();
@ -81,8 +98,16 @@ public class MainGameLoop {
entities.add(new Entity(fernModel,
new Vector3f(random.nextFloat()*800 - 400, 0, random.nextFloat() * -600),
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));
}
TexturedModel stanfordBunnyModel = new TexturedModel(OBJLoader.loadObjModel("bunny", loader),
new ModelTexture(loader.loadTexture("white")));
Player player = new Player(stanfordBunnyModel, new Vector3f(0,0,-50), new Vector3f(0,0,0), 1);
MasterRenderer renderer = new MasterRenderer();
manager.setDrawer(new DisplayManagerDraw() {
@Override
@ -91,8 +116,10 @@ public class MainGameLoop {
//entity.increaseRotation(0, 0, 0.01f);
//entity.increaseRotation(0.01f, 0.02f, 0.0f);
camera.move();
player.move();
renderer.processTerrain(terrain);
renderer.processTerrain(terrain2);
renderer.processEntity(player);
for (Entity entity : entities) {
renderer.processEntity(entity);
}

View File

@ -14,17 +14,11 @@ public class Camera {
}
public void move() {
if (DisplayManager.isKeyDown('w')) {
position.z -= 0.8f;
if (DisplayManager.isKeyDown('q')) {
position.y += 0.8f;
}
if (DisplayManager.isKeyDown('s')) {
position.z += 0.8f;
}
if (DisplayManager.isKeyDown('d')) {
position.x += 0.8f;
}
if (DisplayManager.isKeyDown('a')) {
position.x -= 0.8f;
if (DisplayManager.isKeyDown('z')) {
position.y -= 0.8f;
}
}

94
src/entities/Player.java Normal file
View File

@ -0,0 +1,94 @@
package entities;
import javax.vecmath.Vector3f;
import models.TexturedModel;
import renderEngine.DisplayManager;
public class Player extends Entity {
private static final float RUN_SPEED = 20;
private static final float TRUN_SPEED = (float) Math.toRadians(160);
private static final float GRAVITY = -50;
private static final float JUMP_POWER = 30;
private static final float TERRAIN_HEIGHT = 0;
private float currentSpeed = 0;
private float currentTurnSpeed = 0;
private float upwardSpeed = 0;
private boolean isInAir = false;
public Player(TexturedModel model, Vector3f position, Vector3f rotation, float scale) {
super(model, position, rotation, scale);
}
public void move() {
checkInputs();
if (isInAir == false) {
super.increaseRotation(0, this.currentTurnSpeed * DisplayManager.getFrameTimeSecconds(), 0);
}
float distance = currentSpeed * DisplayManager.getFrameTimeSecconds();
float dx = (float) (distance * Math.sin(super.getRotation().y));
float dz = (float) (distance * Math.cos(super.getRotation().y));
super.increasePosition(dx, 0, dz);
upwardSpeed += GRAVITY * DisplayManager.getFrameTimeSecconds();
super.increasePosition(0, upwardSpeed * DisplayManager.getFrameTimeSecconds(), 0);
if (super.getPosition().y < TERRAIN_HEIGHT) {
upwardSpeed = 0;
super.getPosition().y = TERRAIN_HEIGHT;
isInAir = false;
}
}
private void jump() {
if (isInAir == true) {
return;
}
this.upwardSpeed = JUMP_POWER;
isInAir = true;
}
public void checkInputs() {
if (DisplayManager.isKeyDown('w') && DisplayManager.isKeyDown('s')) {
this.currentSpeed = 0;
} else if (DisplayManager.isKeyDown('w')) {
this.currentSpeed = RUN_SPEED;
} else if (DisplayManager.isKeyDown('s')) {
this.currentSpeed = -RUN_SPEED;
} else {
this.currentSpeed = 0;
}
if (DisplayManager.isKeyDown('d') && DisplayManager.isKeyDown('a')) {
this.currentTurnSpeed = 0;
} else if (DisplayManager.isKeyDown('a')) {
this.currentTurnSpeed = TRUN_SPEED;
} else if (DisplayManager.isKeyDown('d')) {
this.currentTurnSpeed = -TRUN_SPEED;
} else {
this.currentTurnSpeed = 0;
}
if (DisplayManager.isKeyDown(' ')) {
this.jump();
}
//System.out.println("position = " + super.getPosition());
/*
if (DisplayManager.isKeyDown('d')) {
position.x += 0.8f;
}
if (DisplayManager.isKeyDown('a')) {
position.x -= 0.8f;
}
if (DisplayManager.isKeyDown('q')) {
position.y += 0.8f;
}
if (DisplayManager.isKeyDown('z')) {
position.y -= 0.8f;
}
*/
}
}

View File

@ -26,48 +26,15 @@ public class DisplayManager {
private static final int WIDTH = 1280;
private static final int HEIGHT = 720;
private static final int FPS_CAP = 60;
private static final String TITLE = "Our First Display";
private static final String TITLE = "Open GL tutorial";
private static long lastFrameTime;
private static float delta;
private DisplayManagerDraw drawer = null;
public void setDrawer(DisplayManagerDraw drawer) {
this.drawer = drawer;
}
// /**
// * Creates a display window on which we can render our game. The dimensions
// * of the window are determined by setting the display mode. By using
// * "glViewport" we tell OpenGL which part of the window we want to render
// * our game onto. We indicated that we want to use the entire window.
// */
// public static void createDisplay() {
// ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
// try {
// Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
// Display.create(new PixelFormat(), attribs);
// Display.setTitle(TITLE);
// } catch (LWJGLException e) {
// e.printStackTrace();
// }
// GL11.glViewport(0, 0, WIDTH, HEIGHT);
// }
//
// /**
// * This method is used to update the display at the end of every frame. When
// * we have set up a rendering process this method will display whatever
// * we've been rendering onto the screen. The "sync" method is used here to
// * cap the frame rate. Without this the computer would just try to run the
// * game as fast as it possibly can, doing more work than it needs to.
// */
// public static void updateDisplay() {
// Display.sync(FPS_CAP);
// Display.update();
// }
//
// /**
// * This closes the window when the game is closed.
// */
// public static void closeDisplay() {
// Display.destroy();
// }
// The window handle
private static long window;
@ -106,6 +73,7 @@ public class DisplayManager {
private static boolean valueD = false;
private static boolean valueA = false;
private static boolean valueW = false;
private static boolean valueSPACE = false;
public static boolean isKeyDown(char value) {
if (value == 's') {
return valueS;
@ -125,6 +93,9 @@ public class DisplayManager {
if (value == 'a') {
return valueA;
}
if (value == ' ') {
return valueSPACE;
}
return false;
}
@ -206,6 +177,15 @@ public class DisplayManager {
valueW = false;
}
}
if ( key == GLFW_KEY_SPACE ) {
if (action == GLFW_PRESS ) {
System.out.println("Key SPACE is pressed");
valueSPACE = true;
} else if (action == GLFW_RELEASE ) {
System.out.println("Key SPACE is release");
valueSPACE = false;
}
}
});
// Get the thread stack and push a new frame
@ -234,6 +214,9 @@ public class DisplayManager {
// Make the window visible
glfwShowWindow(window);
lastFrameTime = getCurrentTime();
}
public void loop() {
@ -241,6 +224,9 @@ public class DisplayManager {
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while ( !glfwWindowShouldClose(window) ) {
long currentFrameTime = getCurrentTime();
delta = (currentFrameTime-lastFrameTime)/1000f;
lastFrameTime = currentFrameTime;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
if (this.drawer != null) {
this.drawer.draw();
@ -252,5 +238,14 @@ public class DisplayManager {
glfwPollEvents();
}
}
public static float getFrameTimeSecconds() {
return delta;
}
private static long getCurrentTime() {
return System.currentTimeMillis();
}
}

View File

@ -57,7 +57,6 @@ public class Loader {
}
private int loadPNGTexture(String filename, int textureUnit) {
ByteBuffer bufIn = null;
ByteBuffer buf = null;
int tWidth = 0;
int tHeight = 0;
@ -71,7 +70,8 @@ public class Loader {
tHeight = decoder.getHeight();
// Decode the PNG file in a ByteBuffer
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();
in.close();
} catch (IOException e) {
@ -96,10 +96,10 @@ public class Loader {
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
// 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_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_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_MAG_FILTER, GL11.GL_LINEAR);
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
this.exitOnGLError("loadPNGTexture");

View File

@ -6,7 +6,8 @@ import objConverter.OBJFileLoader;
public class OBJLoader {
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());
}
}

View File

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

View File

@ -60,10 +60,15 @@ public abstract class ShaderProgram {
protected void loadFloat(int location, float value) {
GL20.glUniform1f(location, value);
}
protected void loadInt(int location, int value) {
GL20.glUniform1i(location, value);
}
protected void loadVector(int location, Vector3f value) {
GL20.glUniform3f(location, value.x, value.y, value.z);
}
protected void loadBoolean(int location, boolean value) {
//System.out.println("set value " + value + " " + (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_shineDamper;
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() {
super(VERTEX_FILE, FRAGMENT_FILE);
@ -41,6 +46,19 @@ public class TerrainShader extends ShaderProgram {
location_reflectivity = super.getUniformLocation("reflectivity");
location_shineDamper = super.getUniformLocation("shineDamper");
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) {

View File

@ -9,7 +9,12 @@ in float visibility;
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 float reflectivity;
uniform float shineDamper;
@ -18,6 +23,17 @@ uniform vec3 skyColor;
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 unitLightVector = normalize(toLightVector);
@ -35,7 +51,7 @@ void main(void) {
float damperFactor = pow(specularFactor, shineDamper);
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);
}

View File

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