diff --git a/assets/Data/creatures/human.json b/assets/Data/creatures/human.json index 7f810977..c4a438d1 100644 --- a/assets/Data/creatures/human.json +++ b/assets/Data/creatures/human.json @@ -57,20 +57,7 @@ "TARGETABLE", "CAN_EQUIP" ], - "visualAttributes" : [ - { - "attributeId" : "hair", - "variants" : [ - { - "id" : "hairshort1", - "model" : "Models/hairshort1.fbx", - "meshes" : [ - "Hair" - ] - } - ] - } - ], + "visualAttributes" : [], "movementSystems" : [ { "type" : "GROUND", diff --git a/assets/Data/creatures/human2.json b/assets/Data/creatures/human2.json new file mode 100644 index 00000000..7f810977 --- /dev/null +++ b/assets/Data/creatures/human2.json @@ -0,0 +1,172 @@ +{ + "creatures" : [ + { + "creatureId" : "human", + "hitboxes" : [ + { + "type": "hurt", + "bone": "Bone.031", + "radius": 0.04 + }, + { + "type": "hurt", + "bone": "Bone.012", + "radius": 0.04 + }, + { + "type": "hurt", + "bone": "Bone.003", + "radius": 0.04 + }, + { + "type": "hurt", + "bone": "Bone.010", + "radius": 0.06 + }, + { + "type": "hurt", + "bone": "Bone.001", + "radius": 0.06 + }, + { + "type": "hurt", + "bone": "Bone.014", + "radius": 0.06 + }, + { + "type": "hurt", + "bone": "Bone", + "radius": 0.08 + }, + { + "type": "hurt", + "bone": "Bone.014", + "radius": 0.06 + }, + { + "type": "hurt", + "bone": "Bone.019", + "radius": 0.04 + } + ], + "tokens" : [ + "BLENDER_TRANSFORM", + "SENTIENT", + "ATTACKER", + "GRAVITY", + "TARGETABLE", + "CAN_EQUIP" + ], + "visualAttributes" : [ + { + "attributeId" : "hair", + "variants" : [ + { + "id" : "hairshort1", + "model" : "Models/hairshort1.fbx", + "meshes" : [ + "Hair" + ] + } + ] + } + ], + "movementSystems" : [ + { + "type" : "GROUND", + "acceleration" : 1000.0, + "maxVelocity" : 1.0, + "animationStartup" : { + "name" : "Armature|Walk", + "length" : 1, + "loops" : false + }, + "animationLoop" : { + "name" : "Armature|Walk", + "length" : 1, + "loops" : false + }, + "animationWindDown" : { + "name" : "Armature|Walk", + "length" : 1, + "loops" : false + }, + "sprintSystem" : { + "maxVelocity" : 5.8, + "staminaMax" : 500, + "animationStartUp" : { + "name" : "SprintStart", + "length" : 1, + "loops" : false + }, + "animationMain" : { + "name" : "Sprint", + "length" : 1, + "loops" : false + } + } + } + ], + "rotatorSystem" : { + "rotatorItems" : [ + { + "boneName" : "myTorsoBone", + "constraints" : [ + { + "followsView" : true, + "followsBone" : false, + "parentBone" : "", + "allowedMargin" : 0.2 + } + ] + }, + { + "boneName" : "myLeftLegBone", + "constraints" : [ + { + "followsView" : false, + "followsBone" : true, + "parentBone" : "myTorsoBone", + "allowedMargin" : 0.2 + } + ] + }, + { + "boneName" : "myRightLegBone", + "constraints" : [ + { + "followsView" : false, + "followsBone" : true, + "parentBone" : "myTorsoBone", + "allowedMargin" : 0.2 + } + ] + } + ] + }, + "collidable" : { + "type" : "CYLINDER", + "dimension1" : 0.1, + "dimension2" : 0.45, + "dimension3" : 0.1, + "offsetX" : 0, + "offsetY" : 0.45, + "offsetZ" : 0 + }, + "attackMoves" : [ + { + "type" : "MELEE_WEAPON_SWING_ONE_HAND", + "animationName" : "Armature|SwingWeapon", + "damageStartFrame" : 30, + "damageEndFrame" : 60 + } + ], + "healthSystem" : { + "maxHealth" : 100, + "onDamageIFrames" : 30 + }, + "modelPath" : "Models/baseman.fbx" + } + ], + "files" : [] +} \ No newline at end of file diff --git a/assets/Textures/Ground/tileableSlabstone.png b/assets/Textures/Ground/tileableSlabstone.png new file mode 100644 index 00000000..fe4e068b Binary files /dev/null and b/assets/Textures/Ground/tileableSlabstone.png differ diff --git a/src/main/java/electrosphere/engine/LoadingThread.java b/src/main/java/electrosphere/engine/LoadingThread.java index 30cecbef..74146a91 100644 --- a/src/main/java/electrosphere/engine/LoadingThread.java +++ b/src/main/java/electrosphere/engine/LoadingThread.java @@ -171,6 +171,8 @@ public class LoadingThread extends Thread { //initialize the basic graphical entities of the world (skybox, camera) initWorldBaseGraphicalEntities(); + //init game specific stuff (ie different skybox colors) + initGameGraphicalEntities(); while(Globals.clientConnection.getClientProtocol().isLoading()){ @@ -258,6 +260,8 @@ public class LoadingThread extends Thread { //initialize the basic graphical entities of the world (skybox, camera) initWorldBaseGraphicalEntities(); + //init arena specific stuff (ie different skybox colors) + initArenaGraphicalEntities(); creatingRandomEntities(); @@ -475,6 +479,33 @@ public class LoadingThread extends Thread { System.out.println("Draw Cell Manager ready"); } + + + static void initArenaGraphicalEntities(){ + + Globals.skyboxColors.add(new Vector3f(150,200,250)); + Globals.skyboxColors.add(new Vector3f(150,200,250)); + Globals.skyboxColors.add(new Vector3f(20,20,20)); + Globals.skyboxColors.add(new Vector3f(20,20,20)); + Globals.skyboxColors.add(new Vector3f(150,200,250)); + Globals.skyboxColors.add(new Vector3f(150,200,250)); + Globals.skyboxColors.add(new Vector3f(20,20,20)); + Globals.skyboxColors.add(new Vector3f(20,20,20)); + + } + + static void initGameGraphicalEntities(){ + + Globals.skyboxColors.add(new Vector3f(100,150,200)); + Globals.skyboxColors.add(new Vector3f(100,150,200)); + Globals.skyboxColors.add(new Vector3f(50,100,150)); + Globals.skyboxColors.add(new Vector3f(50,100,150)); + Globals.skyboxColors.add(new Vector3f(100,150,200)); + Globals.skyboxColors.add(new Vector3f(100,150,200)); + Globals.skyboxColors.add(new Vector3f(50,100,150)); + Globals.skyboxColors.add(new Vector3f(50,100,150)); + + } static void initWorldBaseGraphicalEntities(){ @@ -487,15 +518,6 @@ public class LoadingThread extends Thread { Globals.skybox = EntityUtils.spawnDrawableEntity(AssetDataStrings.ASSET_STRING_SKYBOX_BASIC); - Globals.skyboxColors.add(new Vector3f(100,150,200)); - Globals.skyboxColors.add(new Vector3f(100,150,200)); - Globals.skyboxColors.add(new Vector3f(50,100,150)); - Globals.skyboxColors.add(new Vector3f(50,100,150)); - Globals.skyboxColors.add(new Vector3f(100,150,200)); - Globals.skyboxColors.add(new Vector3f(100,150,200)); - Globals.skyboxColors.add(new Vector3f(50,100,150)); - Globals.skyboxColors.add(new Vector3f(50,100,150)); - /* Player Camera diff --git a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java index 837fdcd2..3e7f1bc5 100644 --- a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java @@ -448,12 +448,12 @@ public class GroundMovementTree { break; case IDLE: // body.clearForces(); -// if(entityActor != null){ -// if(!entityActor.isPlayingAnimation() || !entityActor.getCurrentAnimation().equals(Animation.ANIMATION_IDLE_1)){ -// entityActor.playAnimation(Animation.ANIMATION_IDLE_1); -// entityActor.incrementAnimationTime(0.01); -// } -// } + if(entityActor != null){ + String animationToPlay = determineCorrectAnimation(); + if(entityActor.isPlayingAnimation() && entityActor.isPlayingAnimation(animationToPlay)){ + entityActor.stopAnimation(animationToPlay); + } + } // if(Globals.collisionEngine.gravityCheck(Globals.commonWorldData, parent)){ // position.set(Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData,parent,new Vector3f(position.x,position.y - 9.8f,position.z))); // } diff --git a/src/main/java/electrosphere/game/client/cells/DrawCell.java b/src/main/java/electrosphere/game/client/cells/DrawCell.java index 4880d906..490dff9b 100644 --- a/src/main/java/electrosphere/game/client/cells/DrawCell.java +++ b/src/main/java/electrosphere/game/client/cells/DrawCell.java @@ -13,6 +13,7 @@ import electrosphere.main.Globals; import electrosphere.renderer.Mesh; import electrosphere.renderer.Model; import electrosphere.renderer.ModelUtils; +import electrosphere.renderer.RenderUtils; import electrosphere.renderer.ShaderProgram; import electrosphere.renderer.texture.Texture; import electrosphere.util.Utilities; @@ -38,8 +39,8 @@ public class DrawCell { CollisionObject physicsObject; - static Texture groundTextureOne = new Texture("/Textures/Ground/GrassTileable.png"); - static Texture groundTextureTwo = new Texture("/Textures/Ground/Dirt1.png"); + static Texture groundTextureOne = new Texture("/Textures/Ground/tileableSlabstone.png"); + static Texture groundTextureTwo = new Texture("/Textures/Ground/tileableSlabstone.png"); static Texture groundTextureThree = new Texture("/Textures/Ground/Dirt1.png"); static Texture groundTextureFour = new Texture("/Textures/Ground/Dirt1.png"); @@ -93,7 +94,7 @@ public class DrawCell { if(modelEntity != null){ Globals.entityManager.deregisterEntity(modelEntity); } - Model terrainModel = ModelUtils.createMinimizedTerrainModelPrecomputedShader(heightmap, texturemap, program, stride); + Model terrainModel = RenderUtils.createMinimizedTerrainModelPrecomputedShader(heightmap, texturemap, program, stride); Mesh terrainMesh = terrainModel.meshes.get(0); terrainMesh.useTextureList = true; terrainMesh.textureList.add(groundTextureOne); diff --git a/src/main/java/electrosphere/main/Globals.java b/src/main/java/electrosphere/main/Globals.java index c46f0841..05e55186 100644 --- a/src/main/java/electrosphere/main/Globals.java +++ b/src/main/java/electrosphere/main/Globals.java @@ -364,8 +364,8 @@ public class Globals { materialDefault.set_diffuse("Textures/default_diffuse.png"); materialDefault.set_specular("Textures/default_specular.png"); //create default lights - assetManager.registerModelToSpecificString(ModelUtils.createBitmapDisplay(), AssetDataStrings.ASSET_STRING_BITMAP_FONT); - assetManager.registerModelToSpecificString(ModelUtils.createBitmapCharacter(), AssetDataStrings.BITMAP_CHARACTER_MODEL); + assetManager.registerModelToSpecificString(RenderUtils.createBitmapDisplay(), AssetDataStrings.ASSET_STRING_BITMAP_FONT); + assetManager.registerModelToSpecificString(RenderUtils.createBitmapCharacter(), AssetDataStrings.BITMAP_CHARACTER_MODEL); RawFontMap fontMap = FileUtils.loadObjectFromAssetPath("Textures/Fonts/myFontMap.json", RawFontMap.class); FontUtils.setFontDataMap(fontMap); //particle billboard model diff --git a/src/main/java/electrosphere/renderer/ModelUtils.java b/src/main/java/electrosphere/renderer/ModelUtils.java index fdb45ea2..eabcac0d 100644 --- a/src/main/java/electrosphere/renderer/ModelUtils.java +++ b/src/main/java/electrosphere/renderer/ModelUtils.java @@ -166,970 +166,6 @@ public class ModelUtils { // return rVal; // } - public static Model createTerrainModelPrecomputedShader(float[][] heightfield, float[][] texturemap, ShaderProgram program, int stride){ - Model rVal = new Model(); - rVal.meshes = new ArrayList(); - Mesh m = new Mesh(); - int width = heightfield.length; - int height = heightfield[0].length; - - int actualWidth = (int)Math.ceil(1.0f * width / (1.0f * stride)); - int actualHeight = (int)Math.ceil(1.0f * height / (1.0f * stride)); - - -// System.out.println(actualWidth + " " + actualHeight); - -// System.out.println((actualWidth - 1) * (actualHeight - 1)); - - FloatBuffer vertices; - FloatBuffer normals; - IntBuffer faces; - FloatBuffer texture_coords; - FloatBuffer textureIndices; - if(stride * actualWidth > width){ - int drawWidth = actualWidth + 1; - int drawHeight = actualHeight + 1; - vertices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); - normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); - faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3); - texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8); - textureIndices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 16); - } else { - vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); - normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); - faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); - texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8); - textureIndices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 16); - } - - int incrementer = 0; - int numFaces = (actualWidth - 1) * (actualHeight - 1) * 2 * 3; - for(int x = 0; x < width - 1; x = x + stride){ - for(int y = 0; y < height - 1; y = y + stride){ - //deal with vertex - //0,0 - vertices.put(x); - vertices.put(heightfield[x][y]); - vertices.put(y); - //1,0 - vertices.put(x + stride); - vertices.put(heightfield[x+stride][y]); - vertices.put(y); - //0,1 - vertices.put(x); - vertices.put(heightfield[x][y+stride]); - vertices.put(y + stride); - //1,1 - vertices.put(x + stride); - vertices.put(heightfield[x+stride][y+stride]); - vertices.put(y + stride); - //deal with normal - Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y + stride); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y + stride); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - //deal with texture coordinates -// if(x / stride % 2 == 0){ -// if(y / stride % 2 == 0){ -// texture_coords.put(0); -// texture_coords.put(0); -// texture_coords.put(1); -// texture_coords.put(0); -// texture_coords.put(0); -// texture_coords.put(1); -// texture_coords.put(1); -// texture_coords.put(1); -// } else { -// texture_coords.put(0); -// texture_coords.put(1); -// } -// } else { -// if(y / stride % 2 == 0){ -// texture_coords.put(1); -// texture_coords.put(0); -// } else { -// texture_coords.put(1); -// texture_coords.put(1); -// } -// } - texture_coords.put(0); - texture_coords.put(0); - texture_coords.put(1); - texture_coords.put(0); - texture_coords.put(0); - texture_coords.put(1); - texture_coords.put(1); - texture_coords.put(1); - - if(x + stride < width - 1 && y + stride < height - 1){ -// texturemap[x+stride][y+stride]; - for(int i = 0; i < 4 ; i++){ -// textureIndices.put(1); -// textureIndices.put(0); -// textureIndices.put(0); -// textureIndices.put(0); - textureIndices.put(texturemap[x][y]); - textureIndices.put(texturemap[x+stride][y]); - textureIndices.put(texturemap[x][y+stride]); - textureIndices.put(texturemap[x+stride][y+stride]); - } - } else { - for(int i = 0; i < 4 ; i++){ - textureIndices.put(0); - textureIndices.put(0); - textureIndices.put(0); - textureIndices.put(0); - } - } - - - //deal with faces - if(1.0f * x / stride < actualWidth - 1 && 1.0f * y / stride < actualHeight - 1){ - faces.put(incrementer * 4 + 0); - faces.put(incrementer * 4 + 1); - faces.put(incrementer * 4 + 2); - faces.put(incrementer * 4 + 1); - faces.put(incrementer * 4 + 2); - faces.put(incrementer * 4 + 3); - } - incrementer++; - } - } - - vertices.flip(); - normals.flip(); - faces.flip(); - texture_coords.flip(); - textureIndices.flip(); - - m.vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(m.vertexArrayObject); - //buffer vertices - m.buffer_vertices(vertices, 3); - //buffer normals - m.buffer_normals(normals, 3); - //buffer faces - m.buffer_faces(faces); - //buffer texture coords - m.buffer_texture_coords(texture_coords, 2); - //texture indices - m.bufferCustomFloatAttribArray(textureIndices, 4, 5); - m.shader = program; - glBindVertexArray(0); - m.parent = rVal; - - m.hasBones = false; - - Material groundMat = new Material(); - Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); - groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); - groundMat.set_specular("/Textures/Ground/Dirt1.png"); - m.setMaterial(groundMat); - - rVal.meshes.add(m); - return rVal; - } - - - static float MINIMIZATION_DIFF_MAX = 0.1f; - - public static Model createMinimizedTerrainModelPrecomputedShader(float[][] heightfield, float[][] texturemap, ShaderProgram program, int stride){ - - class QuadToGenerate { - //coords are inclusive - int startX; - int endX; - int startY; - int endY; - float diff; - float min; - float max; - float texture; - boolean homogeneousTexture; - - QuadToGenerate(int startX, int startY, int endX, int endY, float diff, float min, float max, boolean homogeneousTexture, float texture){ - this.startX = startX; - this.startY = startY; - this.endX = endX; - this.endY = endY; - this.diff = diff; - this.min = min; - this.max = max; - this.texture = texture; - this.homogeneousTexture = homogeneousTexture; - } - - } - - - Model rVal = new Model(); - rVal.meshes = new ArrayList(); - Mesh m = new Mesh(); - int width = heightfield.length; - int height = heightfield[0].length; - - int actualWidth = (int)Math.ceil(1.0f * width / (1.0f * stride)); - int actualHeight = (int)Math.ceil(1.0f * height / (1.0f * stride)); - - -// System.out.println(actualWidth + " " + actualHeight); - -// System.out.println((actualWidth - 1) * (actualHeight - 1)); - - FloatBuffer vertices; - FloatBuffer normals; - IntBuffer faces; - FloatBuffer texture_coords; - FloatBuffer textureIndices; - if(stride * actualWidth > width){ - int drawWidth = actualWidth + 1; - int drawHeight = actualHeight + 1; - vertices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); - normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); - faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3); - texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8); - textureIndices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 16); - } else { - vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); - normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); - faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); - texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8); - textureIndices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 16); - } - - - //merge along y - - List firstPhaseQuads = new LinkedList(); - QuadToGenerate quadCurrent = null; - float minVal = 0; - float maxVal = 0; - int textureVal = -1; - for(int x = 0; x < width - 1; x = x + stride){ - quadCurrent = null; - for(int y = 0; y < height - 1; y = y + stride){ - if((x == 5 && y == 2)){ - // System.out.println(quadCurrent); -// continue; - } - if(quadCurrent == null){ - minVal = 100000000; - maxVal = 0; - textureVal = -1; - //minval - if(heightfield[x][y] < minVal){ - minVal = heightfield[x][y]; - } - if(heightfield[x+stride][y] < minVal){ - minVal = heightfield[x+stride][y]; - } - if(heightfield[x][y+stride] < minVal){ - minVal = heightfield[x][y+stride]; - } - if(heightfield[x+stride][y+stride] < minVal){ - minVal = heightfield[x+stride][y+stride]; - } - //maxval - if(heightfield[x][y] > maxVal){ - maxVal = heightfield[x][y]; - } - if(heightfield[x+stride][y] > maxVal){ - maxVal = heightfield[x+stride][y]; - } - if(heightfield[x][y+stride] > maxVal){ - maxVal = heightfield[x][y+stride]; - } - if(heightfield[x+stride][y+stride] > maxVal){ - maxVal = heightfield[x+stride][y+stride]; - } - boolean textureMatch = false; - float texture = -1; - if(x+stride < width - 1 && y+stride < height -1 && - texturemap[x][y] == texturemap[x+stride][y] && - texturemap[x][y] == texturemap[x][y+stride] && - texturemap[x][y] == texturemap[x+stride][y+stride]){ - - textureMatch = true; - texture = texturemap[x][y]; - } else { -// if(x > 8 && (x+stride < width - 1) && (y+stride < height -1)){ -// System.out.println( -// (x+stride < width - 1) + " " + -// (y+stride < height -1) + " " + -// (texturemap[x][y] == texturemap[x+stride][y]) + " " + -// (texturemap[x][y] == texturemap[x][y+stride]) + " " + -// (texturemap[x][y] == texturemap[x+stride][y+stride]) -// ); -// } - } - if(textureMatch){ - quadCurrent = new QuadToGenerate(x,y,x+stride,y+stride,maxVal - minVal,minVal,maxVal,textureMatch,texture); - } else { - firstPhaseQuads.add(new QuadToGenerate(x,y,x+stride,y+stride,maxVal - minVal,minVal,maxVal,textureMatch,texture)); - // quadCurrent = null; - } - } else { - float newMin = minVal; - float newMax = maxVal; - //min - if(heightfield[x][y+stride] < newMin){ - newMin = heightfield[x][y+stride]; - } - if(heightfield[x+stride][y+stride] < newMin){ - newMin = heightfield[x+stride][y+stride]; - } - //max - if(heightfield[x][y+stride] > newMax){ - newMax = heightfield[x][y+stride]; - } - if(heightfield[x+stride][y+stride] > newMax){ - newMax = heightfield[x+stride][y+stride]; - } - if(y+stride < height - 1 && x+stride < width - 1){ - if(newMax - newMin < MINIMIZATION_DIFF_MAX && - quadCurrent.texture == texturemap[x+stride][y] && - quadCurrent.texture == texturemap[x ][y+stride] && - quadCurrent.texture == texturemap[x+stride][y+stride] && - quadCurrent.homogeneousTexture - ){ - //add to quad - quadCurrent.endY = y + stride; - quadCurrent.diff = newMax - newMin; - quadCurrent.min = newMax; - quadCurrent.max = newMax; - } else { - //push quad - firstPhaseQuads.add(quadCurrent); - firstPhaseQuads.add(new QuadToGenerate(x,y,x+stride,y+stride,maxVal - minVal,minVal,maxVal,false,0)); - quadCurrent = null; -// System.out.println("Push"); - } - } else { - if(newMax - newMin < MINIMIZATION_DIFF_MAX){ - //add to quad - quadCurrent.endY = y + stride; - quadCurrent.diff = newMax - newMin; - quadCurrent.min = newMax; - quadCurrent.max = newMax; - } else { - //push quad - firstPhaseQuads.add(quadCurrent); - quadCurrent = null; -// System.out.println("Push"); - } - } - } - } - if(quadCurrent != null){ - firstPhaseQuads.add(quadCurrent); - } - } - - List finalQuads = new LinkedList(); - // for(QuadToGenerate current : firstPhaseQuads){ - // finalQuads.add(current); - // } - -// System.out.println(finalQuads.size()); - - //merge along x - - // QuadToGenerate currentQuad = null; - List toSkip = new LinkedList(); - for(QuadToGenerate currentQuad : firstPhaseQuads){ - // toRemove.clear(); - if(toSkip.contains(currentQuad)){ - continue; - } - for(QuadToGenerate currentPotentialMatch : firstPhaseQuads){ - if(currentPotentialMatch.startX <= currentQuad.startX){ - continue; - } - if(currentPotentialMatch.startX > currentQuad.endX){ - break; - } - if(currentPotentialMatch.startY != currentQuad.startY){ - continue; - } - if(currentPotentialMatch.endY != currentQuad.endY){ - continue; - } - if( - !(currentQuad.homogeneousTexture && - currentPotentialMatch.homogeneousTexture && - currentQuad.texture == currentPotentialMatch.texture) - ){ - continue; - } - if(currentQuad.min < currentPotentialMatch.min && currentQuad.max < currentPotentialMatch.max){ - float min = currentQuad.min; - float max = currentPotentialMatch.max; - if(max - min < MINIMIZATION_DIFF_MAX){ - currentQuad.endX = currentPotentialMatch.endX; - currentQuad.max = currentPotentialMatch.max; - toSkip.add(currentPotentialMatch); - } - } else if(currentQuad.min > currentPotentialMatch.min && currentQuad.max > currentPotentialMatch.max){ - float min = currentPotentialMatch.min; - float max = currentQuad.max; - if(max - min < MINIMIZATION_DIFF_MAX){ - currentQuad.endX = currentPotentialMatch.endX; - currentQuad.min = currentPotentialMatch.min; - toSkip.add(currentPotentialMatch); - } - } else { - if(currentQuad.min < currentPotentialMatch.min){ - currentQuad.endX = currentPotentialMatch.endX; - } else { - currentQuad.endX = currentPotentialMatch.endX; - currentQuad.min = currentPotentialMatch.min; - currentQuad.max = currentPotentialMatch.max; - } - toSkip.add(currentPotentialMatch); - } - } - finalQuads.add(currentQuad); - } - // for(QuadToGenerate currentIteration : firstPhaseQuads){ - // if(currentQuad == null){ - // currentQuad = currentIteration; - // } else { - // //if should merge: - // if( - // currentQuad.homogeneousTexture && - // currentIteration.homogeneousTexture && - // currentQuad.texture == currentIteration.texture - // ){ - // if(currentQuad.min < currentIteration.min && currentQuad.max < currentIteration.max){ - // float min = currentQuad.min; - // float max = currentIteration.max; - // if(max - min < MINIMIZATION_DIFF_MAX){ - // currentQuad.endX = currentIteration.endX; - // currentQuad.max = currentIteration.max; - // } else { - // finalQuads.add(currentQuad); - // currentQuad = currentIteration; - // } - // } else if(currentQuad.min > currentIteration.min && currentQuad.max > currentIteration.max){ - // float min = currentIteration.min; - // float max = currentQuad.max; - // if(max - min < MINIMIZATION_DIFF_MAX){ - // currentQuad.endX = currentIteration.endX; - // currentQuad.min = currentIteration.min; - // } else { - // finalQuads.add(currentQuad); - // currentQuad = currentIteration; - // } - // } else { - // if(currentQuad.min < currentIteration.min){ - // currentQuad.endX = currentIteration.endX; - // } else { - // currentQuad.endX = currentIteration.endX; - // currentQuad.min = currentIteration.min; - // currentQuad.max = currentIteration.max; - // } - // } - // } else { - // finalQuads.add(currentQuad); - // currentQuad = currentIteration; - // } - // } - // } - // finalQuads.add(currentQuad); - - // for(QuadToGenerate current : finalQuads){ - // if(current.startX > 0 && current.startY > 0 && current.endX < 99 && current.endY < 99){ - // System.out.println(current.startX + " " + current.startY + " " + current.endX + " " + current.endY); - // } - // } - - // System.out.println("AAAAAAAAAAAAAAAAAA"); -// System.out.println(finalQuads.size()); -// System.exit(0); - - int incrementer = 0; - - for(QuadToGenerate current : finalQuads){ - //deal with vertex - //0,0 - vertices.put(current.startX); - vertices.put(heightfield[current.startX][current.startY]); - vertices.put(current.startY); - //1,0 - vertices.put(current.endX); - vertices.put(heightfield[current.endX][current.startY]); - vertices.put(current.startY); - //0,1 - vertices.put(current.startX); - vertices.put(heightfield[current.startX][current.endY]); - vertices.put(current.endY); - //1,1 - vertices.put(current.endX); - vertices.put(heightfield[current.endX][current.endY]); - vertices.put(current.endY); - //deal with normal - Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.startX, current.startY); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.endX, current.startY); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.startX, current.endY); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.endX, current.endY); - normals.put(normal.x); - normals.put(normal.y); - normals.put(normal.z); - //deal with texture coordinates -// if(x / stride % 2 == 0){ -// if(y / stride % 2 == 0){ -// texture_coords.put(0); -// texture_coords.put(0); -// texture_coords.put(1); -// texture_coords.put(0); -// texture_coords.put(0); -// texture_coords.put(1); -// texture_coords.put(1); -// texture_coords.put(1); -// } else { -// texture_coords.put(0); -// texture_coords.put(1); -// } -// } else { -// if(y / stride % 2 == 0){ -// texture_coords.put(1); -// texture_coords.put(0); -// } else { -// texture_coords.put(1); -// texture_coords.put(1); -// } -// } - texture_coords.put(0); - texture_coords.put(0); - texture_coords.put(current.endX - current.startX); - texture_coords.put(0); - texture_coords.put(0); - texture_coords.put(current.endY - current.startY); - texture_coords.put(current.endX - current.startX); - texture_coords.put(current.endY - current.startY); - - if(current.endX < width - 1 && current.endY < height - 1){ -// texturemap[x+stride][y+stride]; - for(int i = 0; i < 4 ; i++){ -// textureIndices.put(1); -// textureIndices.put(0); -// textureIndices.put(0); -// textureIndices.put(0); - textureIndices.put(texturemap[current.startX][current.startY]); - textureIndices.put(texturemap[current.endX][current.startY]); - textureIndices.put(texturemap[current.startX][current.endY]); - textureIndices.put(texturemap[current.endX][current.endY]); - } - } else { - for(int i = 0; i < 4 ; i++){ - textureIndices.put(0); - textureIndices.put(0); - textureIndices.put(0); - textureIndices.put(0); - } - } - - - //deal with faces - faces.put(incrementer * 4 + 0); - faces.put(incrementer * 4 + 1); - faces.put(incrementer * 4 + 2); - faces.put(incrementer * 4 + 1); - faces.put(incrementer * 4 + 2); - faces.put(incrementer * 4 + 3); - - incrementer++; - } - - - -// int numFaces = (actualWidth - 1) * (actualHeight - 1) * 2 * 3; -// for(int x = 0; x < width - 1; x = x + stride){ -// for(int y = 0; y < height - 1; y = y + stride){ -// //deal with vertex -// //0,0 -// vertices.put(x); -// vertices.put(heightfield[x][y]); -// vertices.put(y); -// //1,0 -// vertices.put(x + stride); -// vertices.put(heightfield[x+stride][y]); -// vertices.put(y); -// //0,1 -// vertices.put(x); -// vertices.put(heightfield[x][y+stride]); -// vertices.put(y + stride); -// //1,1 -// vertices.put(x + stride); -// vertices.put(heightfield[x+stride][y+stride]); -// vertices.put(y + stride); -// //deal with normal -// Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y); -// normals.put(normal.x); -// normals.put(normal.y); -// normals.put(normal.z); -// normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y); -// normals.put(normal.x); -// normals.put(normal.y); -// normals.put(normal.z); -// normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y + stride); -// normals.put(normal.x); -// normals.put(normal.y); -// normals.put(normal.z); -// normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y + stride); -// normals.put(normal.x); -// normals.put(normal.y); -// normals.put(normal.z); -// //deal with texture coordinates -//// if(x / stride % 2 == 0){ -//// if(y / stride % 2 == 0){ -//// texture_coords.put(0); -//// texture_coords.put(0); -//// texture_coords.put(1); -//// texture_coords.put(0); -//// texture_coords.put(0); -//// texture_coords.put(1); -//// texture_coords.put(1); -//// texture_coords.put(1); -//// } else { -//// texture_coords.put(0); -//// texture_coords.put(1); -//// } -//// } else { -//// if(y / stride % 2 == 0){ -//// texture_coords.put(1); -//// texture_coords.put(0); -//// } else { -//// texture_coords.put(1); -//// texture_coords.put(1); -//// } -//// } -// texture_coords.put(0); -// texture_coords.put(0); -// texture_coords.put(1); -// texture_coords.put(0); -// texture_coords.put(0); -// texture_coords.put(1); -// texture_coords.put(1); -// texture_coords.put(1); -// -// if(x + stride < width - 1 && y + stride < height - 1){ -//// texturemap[x+stride][y+stride]; -// for(int i = 0; i < 4 ; i++){ -//// textureIndices.put(1); -//// textureIndices.put(0); -//// textureIndices.put(0); -//// textureIndices.put(0); -// textureIndices.put(texturemap[x][y]); -// textureIndices.put(texturemap[x+stride][y]); -// textureIndices.put(texturemap[x][y+stride]); -// textureIndices.put(texturemap[x+stride][y+stride]); -// } -// } else { -// for(int i = 0; i < 4 ; i++){ -// textureIndices.put(0); -// textureIndices.put(0); -// textureIndices.put(0); -// textureIndices.put(0); -// } -// } -// -// -// //deal with faces -// if(1.0f * x / stride < actualWidth - 1 && 1.0f * y / stride < actualHeight - 1){ -// faces.put(incrementer * 4 + 0); -// faces.put(incrementer * 4 + 1); -// faces.put(incrementer * 4 + 2); -// faces.put(incrementer * 4 + 1); -// faces.put(incrementer * 4 + 2); -// faces.put(incrementer * 4 + 3); -// } -// incrementer++; -// } -// } - - System.out.println(incrementer + " quads"); - - vertices.flip(); - normals.flip(); - faces.flip(); - texture_coords.flip(); - textureIndices.flip(); - - m.vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(m.vertexArrayObject); - //buffer vertices - m.buffer_vertices(vertices, 3); - //buffer normals - m.buffer_normals(normals, 3); - //buffer faces - m.buffer_faces(faces); - //buffer texture coords - m.buffer_texture_coords(texture_coords, 2); - //texture indices - m.bufferCustomFloatAttribArray(textureIndices, 4, 5); - m.shader = program; - glBindVertexArray(0); - m.parent = rVal; - - m.hasBones = false; - - Material groundMat = new Material(); - Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); - groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); - groundMat.set_specular("/Textures/Ground/Dirt1.png"); - m.setMaterial(groundMat); - - rVal.meshes.add(m); - return rVal; - } - - static Vector3f calculateTerrainNormal(float[][] heightfield, int actualWidth, int actualHeight, int stride, int x, int y){ - Vector3f rVal = new Vector3f(); - if(x / stride < actualWidth - 1){ - if(y / stride < actualHeight - 1){ - float hL; - if(x > 0){ - hL = heightfield[x-1][y]; - } else { - hL = heightfield[x][y]; - } - float hR = heightfield[x+1][y]; - float hD = heightfield[x][y+1]; - float hU; - if(y > 0){ - hU = heightfield[x][y-1]; - } else { - hU = heightfield[x][y]; - } - rVal = new Vector3f(hL - hR, 2.0f, hD - hU); - rVal.normalize(); - } else { - float hL; - if(x > 0){ - hL = heightfield[x-1][y]; - } else { - hL = heightfield[x][y]; - } - float hR = heightfield[x+1][y]; - float hD = heightfield[x][y]; - float hU = heightfield[x][y-1]; - rVal = new Vector3f(hL - hR, 2.0f, hD - hU); - rVal.normalize(); - } - } else { - if(y / stride < actualHeight - 1){ - float hL = heightfield[x-1][y]; - float hR = heightfield[x][y]; - float hD = heightfield[x][y+1]; - float hU; - if(y > 0){ - hU = heightfield[x][y-1]; - } else { - hU = heightfield[x][y]; - } - rVal = new Vector3f(hL - hR, 2.0f, hD - hU); - rVal.normalize(); - } else { - float hL = heightfield[x-1][y]; - float hR = heightfield[x][y]; - float hD = heightfield[x][y]; - float hU = heightfield[x][y-1]; - rVal = new Vector3f(hL - hR, 2.0f, hD - hU); - rVal.normalize(); - } - } - return rVal; - } - - - public static Model createUnitCube(){ - Model rVal = new Model(); - rVal.meshes = new ArrayList(); - Mesh m = new Mesh(); - -// System.out.println(actualWidth + " " + actualHeight); - -// System.out.println((actualWidth - 1) * (actualHeight - 1)); - - FloatBuffer vertices = BufferUtils.createFloatBuffer(8 * 3); - FloatBuffer normals = BufferUtils.createFloatBuffer(8 * 3); - IntBuffer faces = BufferUtils.createIntBuffer(6 * 2 * 3); - FloatBuffer texture_coords = BufferUtils.createFloatBuffer(8 * 2); - - //vertices - //0,0,0 - vertices.put(0); - vertices.put(0); - vertices.put(0); - //1,0,0 - vertices.put(1); - vertices.put(0); - vertices.put(0); - //0,1,0 - vertices.put(0); - vertices.put(1); - vertices.put(0); - //1,1,0 - vertices.put(1); - vertices.put(1); - vertices.put(0); - //0,0,1 - vertices.put(0); - vertices.put(0); - vertices.put(1); - //1,0,1 - vertices.put(1); - vertices.put(0); - vertices.put(1); - //0,1,1 - vertices.put(0); - vertices.put(1); - vertices.put(1); - //1,1,1 - vertices.put(1); - vertices.put(1); - vertices.put(1); - - //normals - //-1,-1,-1 - normals.put(-1); - normals.put(-1); - normals.put(-1); - // 1,-1,-1 - normals.put( 1); - normals.put(-1); - normals.put(-1); - //-1, 1,-1 - normals.put(-1); - normals.put( 1); - normals.put(-1); - // 1, 1,-1 - normals.put( 1); - normals.put( 1); - normals.put(-1); - //-1,-1, 1 - normals.put(-1); - normals.put(-1); - normals.put( 1); - // 1,-1, 1 - normals.put( 1); - normals.put(-1); - normals.put( 1); - //-1, 1, 1 - normals.put(-1); - normals.put( 1); - normals.put( 1); - // 1, 1, 1 - normals.put( 1); - normals.put( 1); - normals.put( 1); - - //faces - //0,1,2 - faces.put(0); - faces.put(1); - faces.put(2); - //1,2,3 - faces.put(1); - faces.put(2); - faces.put(3); - //1,3,5 - faces.put(1); - faces.put(3); - faces.put(5); - //3,5,7 - faces.put(3); - faces.put(5); - faces.put(7); - //0,1,4 - faces.put(0); - faces.put(1); - faces.put(4); - //1,4,5 - faces.put(1); - faces.put(4); - faces.put(5); - //0,2,4 - faces.put(0); - faces.put(2); - faces.put(4); - //2,4,6 - faces.put(2); - faces.put(4); - faces.put(6); - //2,3,6 - faces.put(2); - faces.put(3); - faces.put(6); - //3,6,7 - faces.put(3); - faces.put(6); - faces.put(7); - //4,5,6 - faces.put(4); - faces.put(5); - faces.put(6); - //5,6,7 - faces.put(5); - faces.put(6); - faces.put(7); - - //texture - for(int i = 0; i < 8 * 2; i++){ - texture_coords.put(0); - } - - - vertices.flip(); - normals.flip(); - faces.flip(); - texture_coords.flip(); - - m.vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(m.vertexArrayObject); - //buffer vertices - m.buffer_vertices(vertices, 3); - //buffer normals - m.buffer_normals(normals, 3); - //buffer faces - m.buffer_faces(faces); - //buffer texture coords - m.buffer_texture_coords(texture_coords, 2); - m.shader = ShaderProgram.smart_assemble_shader(false,true); - glBindVertexArray(0); - m.parent = rVal; - - Material groundMat = new Material(); - Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); - groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); - groundMat.set_specular("/Textures/Ground/Dirt1.png"); - m.setMaterial(groundMat); - - rVal.meshes.add(m); - return rVal; - } public static void printModelDimension(Model m){ float minX = 0; @@ -1161,201 +197,12 @@ public class ModelUtils { System.out.println("dimensions: " + (maxX - minX) + "," + (maxY - minY) + "," + (maxZ-minZ)); } - public static Model createBitmapDisplay(){ - - Model rVal = new Model(); - rVal.meshes = new ArrayList(); - Mesh m = new Mesh(); - m.vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(m.vertexArrayObject); - //vertices - FloatBuffer VertexArrayBufferData = BufferUtils.createFloatBuffer(12); - VertexArrayBufferData.put( 0); - VertexArrayBufferData.put( 1); - - VertexArrayBufferData.put( 0); - VertexArrayBufferData.put( 0); - - VertexArrayBufferData.put( 1); - VertexArrayBufferData.put( 0); - - VertexArrayBufferData.put( 0); - VertexArrayBufferData.put( 1); - - VertexArrayBufferData.put( 1); - VertexArrayBufferData.put( 0); - - VertexArrayBufferData.put( 1); - VertexArrayBufferData.put( 1); - VertexArrayBufferData.flip(); - - - IntBuffer faceArrayBufferData = BufferUtils.createIntBuffer(6); - faceArrayBufferData.put(0); - faceArrayBufferData.put(1); - faceArrayBufferData.put(2); - - faceArrayBufferData.put(3); - faceArrayBufferData.put(4); - faceArrayBufferData.put(5); - faceArrayBufferData.flip(); - - - - //texture coords - FloatBuffer TextureArrayBufferData = BufferUtils.createFloatBuffer(12); - TextureArrayBufferData.put(0); - TextureArrayBufferData.put(1); - - TextureArrayBufferData.put(0); - TextureArrayBufferData.put(0); - - TextureArrayBufferData.put(1); - TextureArrayBufferData.put(0); - - TextureArrayBufferData.put(0); - TextureArrayBufferData.put(1); - - TextureArrayBufferData.put(1); - TextureArrayBufferData.put(0); - - TextureArrayBufferData.put(1); - TextureArrayBufferData.put(1); - TextureArrayBufferData.flip(); - - - //buffer vertices - m.buffer_vertices(VertexArrayBufferData, 2); - //buffer normals - m.buffer_normals(VertexArrayBufferData, 2); - //buffer faces - m.buffer_faces(faceArrayBufferData); - //buffer texture coords - m.buffer_texture_coords(TextureArrayBufferData, 2); - - - m.shader = ShaderProgram.loadSpecificShader("/Shaders/font/basicbitmap/basicbitmap.vs", "/Shaders/font/basicbitmap/basicbitmap.fs"); - - - glBindVertexArray(0); - m.parent = rVal; - m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME; - - Material uiMat = new Material(); - Globals.assetManager.addTexturePathtoQueue("/Textures/Fonts/myfont1-harsher.png"); - uiMat.set_diffuse("/Textures/Fonts/myfont1-harsher.png"); - uiMat.set_specular("/Textures/Fonts/myfont1-harsher.png"); - m.setMaterial(uiMat); - rVal.materials = new ArrayList(); - rVal.materials.add(uiMat); - - rVal.meshes.add(m); - - return rVal; - } - public static Model createBitmapCharacter(){ - - Model rVal = new Model(); - rVal.meshes = new ArrayList(); - Mesh m = new Mesh(); - m.vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(m.vertexArrayObject); - //vertices - FloatBuffer VertexArrayBufferData = BufferUtils.createFloatBuffer(12); - VertexArrayBufferData.put(-1); - VertexArrayBufferData.put( 1); - - VertexArrayBufferData.put(-1); - VertexArrayBufferData.put(-1); - - VertexArrayBufferData.put( 1); - VertexArrayBufferData.put(-1); - - VertexArrayBufferData.put(-1); - VertexArrayBufferData.put( 1); - - VertexArrayBufferData.put( 1); - VertexArrayBufferData.put(-1); - - VertexArrayBufferData.put( 1); - VertexArrayBufferData.put( 1); - VertexArrayBufferData.flip(); - - - IntBuffer faceArrayBufferData = BufferUtils.createIntBuffer(6); - faceArrayBufferData.put(0); - faceArrayBufferData.put(1); - faceArrayBufferData.put(2); - - faceArrayBufferData.put(3); - faceArrayBufferData.put(4); - faceArrayBufferData.put(5); - faceArrayBufferData.flip(); - - - - //texture coords - FloatBuffer TextureArrayBufferData = BufferUtils.createFloatBuffer(12); - TextureArrayBufferData.put(0); - TextureArrayBufferData.put(1); - - TextureArrayBufferData.put(0); - TextureArrayBufferData.put(0); - - TextureArrayBufferData.put(1); - TextureArrayBufferData.put(0); - - TextureArrayBufferData.put(0); - TextureArrayBufferData.put(1); - - TextureArrayBufferData.put(1); - TextureArrayBufferData.put(0); - - TextureArrayBufferData.put(1); - TextureArrayBufferData.put(1); - TextureArrayBufferData.flip(); - - - //buffer vertices - m.buffer_vertices(VertexArrayBufferData, 2); - //buffer normals - m.buffer_normals(VertexArrayBufferData, 2); - //buffer faces - m.buffer_faces(faceArrayBufferData); - //buffer texture coords - m.buffer_texture_coords(TextureArrayBufferData, 2); - - - m.shader = ShaderProgram.loadSpecificShader("/Shaders/font/bitmapchar/bitmapchar.vs", "/Shaders/font/bitmapchar/bitmapchar.fs"); - - - glBindVertexArray(0); - m.parent = rVal; - m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME; - - Material uiMat = new Material(); - Globals.assetManager.addTexturePathtoQueue("/Textures/Fonts/myfont1-harsher.png"); - uiMat.set_diffuse("/Textures/Fonts/myfont1-harsher.png"); - uiMat.set_specular("/Textures/Fonts/myfont1-harsher.png"); - m.setMaterial(uiMat); - rVal.materials = new ArrayList(); - rVal.materials.add(uiMat); - - rVal.meshes.add(m); - - return rVal; - } - - - - - diff --git a/src/main/java/electrosphere/renderer/RenderUtils.java b/src/main/java/electrosphere/renderer/RenderUtils.java index 4e95414d..e96881d1 100644 --- a/src/main/java/electrosphere/renderer/RenderUtils.java +++ b/src/main/java/electrosphere/renderer/RenderUtils.java @@ -1,113 +1,39 @@ package electrosphere.renderer; +import electrosphere.engine.assetmanager.AssetDataStrings; import electrosphere.entity.CameraEntityUtils; -import electrosphere.entity.Entity; -import electrosphere.entity.EntityDataStrings; -import electrosphere.entity.EntityUtils; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + import electrosphere.main.Globals; -import electrosphere.main.Main; -import static electrosphere.main.Main.deltaTime; -import static electrosphere.main.Main.view_Range; -import electrosphere.renderer.framebuffer.Framebuffer; -import electrosphere.renderer.framebuffer.FramebufferUtils; -import electrosphere.renderer.framebuffer.Renderbuffer; -import electrosphere.renderer.ui.Widget; -import electrosphere.util.Utilities; import org.joml.Matrix4f; -import org.joml.Quaternionf; import org.joml.Vector3f; import org.lwjgl.BufferUtils; -import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MAJOR; -import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MINOR; -import static org.lwjgl.glfw.GLFW.GLFW_CURSOR; -import static org.lwjgl.glfw.GLFW.GLFW_CURSOR_DISABLED; -import static org.lwjgl.glfw.GLFW.GLFW_FALSE; -import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_CORE_PROFILE; -import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE; -import static org.lwjgl.glfw.GLFW.GLFW_TRANSPARENT_FRAMEBUFFER; -import static org.lwjgl.glfw.GLFW.GLFW_TRUE; -import static org.lwjgl.glfw.GLFW.glfwCreateWindow; -import static org.lwjgl.glfw.GLFW.glfwInit; import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent; import static org.lwjgl.glfw.GLFW.glfwMaximizeWindow; -import static org.lwjgl.glfw.GLFW.glfwPollEvents; -import static org.lwjgl.glfw.GLFW.glfwSetInputMode; -import static org.lwjgl.glfw.GLFW.glfwSwapBuffers; -import static org.lwjgl.glfw.GLFW.glfwTerminate; -import static org.lwjgl.glfw.GLFW.glfwWindowHint; -import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL11; -import static org.lwjgl.opengl.GL11.GL_BACK; -import static org.lwjgl.opengl.GL11.GL_BLEND; -import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; -import static org.lwjgl.opengl.GL11.GL_CULL_FACE; -import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; -import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; import static org.lwjgl.opengl.GL11.GL_FLOAT; -import static org.lwjgl.opengl.GL11.GL_FRONT; -import static org.lwjgl.opengl.GL11.GL_INT; import static org.lwjgl.opengl.GL11.GL_LEQUAL; import static org.lwjgl.opengl.GL11.GL_LESS; -import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA; -import static org.lwjgl.opengl.GL11.GL_SHORT; -import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA; -import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; import static org.lwjgl.opengl.GL11.GL_TRIANGLES; -import static org.lwjgl.opengl.GL11.GL_TRUE; import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; -import static org.lwjgl.opengl.GL11.glBindTexture; -import static org.lwjgl.opengl.GL11.glBlendFunc; -import static org.lwjgl.opengl.GL11.glClear; -import static org.lwjgl.opengl.GL11.glClearColor; -import static org.lwjgl.opengl.GL11.glCullFace; -import static org.lwjgl.opengl.GL11.glDisable; -import static org.lwjgl.opengl.GL11.glDrawArrays; -import static org.lwjgl.opengl.GL11.glEnable; -import static org.lwjgl.opengl.GL11.glViewport; -import static org.lwjgl.opengl.GL13.GL_TEXTURE0; -import static org.lwjgl.opengl.GL13.GL_TEXTURE1; -import static org.lwjgl.opengl.GL13.GL_TEXTURE2; -import static org.lwjgl.opengl.GL13.GL_TEXTURE3; -import static org.lwjgl.opengl.GL13.glActiveTexture; import org.lwjgl.opengl.GL15; import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; import static org.lwjgl.opengl.GL15.glBindBuffer; import static org.lwjgl.opengl.GL15.glGenBuffers; -import org.lwjgl.opengl.GL20; -import static org.lwjgl.opengl.GL20.GL_COMPILE_STATUS; -import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER; -import static org.lwjgl.opengl.GL20.GL_LINK_STATUS; -import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER; -import static org.lwjgl.opengl.GL20.glAttachShader; -import static org.lwjgl.opengl.GL20.glCompileShader; -import static org.lwjgl.opengl.GL20.glCreateProgram; -import static org.lwjgl.opengl.GL20.glCreateShader; -import static org.lwjgl.opengl.GL20.glDeleteShader; import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; -import static org.lwjgl.opengl.GL20.glGetProgramInfoLog; -import static org.lwjgl.opengl.GL20.glGetProgrami; -import static org.lwjgl.opengl.GL20.glGetShaderi; import static org.lwjgl.opengl.GL20.glGetUniformLocation; -import static org.lwjgl.opengl.GL20.glLinkProgram; -import static org.lwjgl.opengl.GL20.glShaderSource; -import static org.lwjgl.opengl.GL20.glUniform1fv; -import static org.lwjgl.opengl.GL20.glUniform1i; import static org.lwjgl.opengl.GL20.glUniform3fv; import static org.lwjgl.opengl.GL20.glUniformMatrix4fv; import static org.lwjgl.opengl.GL20.glUseProgram; import static org.lwjgl.opengl.GL20.glVertexAttribPointer; -import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER; -import static org.lwjgl.opengl.GL30.GL_RENDERBUFFER; -import static org.lwjgl.opengl.GL30.glBindFramebuffer; -import static org.lwjgl.opengl.GL30.glBindRenderbuffer; import static org.lwjgl.opengl.GL30.glBindVertexArray; import static org.lwjgl.opengl.GL30.glGenVertexArrays; -import static org.lwjgl.system.MemoryUtil.NULL; /** * @@ -203,7 +129,7 @@ public class RenderUtils { public static Model createSkyboxModel(Material optionalMaterial){ Model skyboxModel = new Model(); - skyboxModel.meshes = new ArrayList(); + skyboxModel.meshes = new ArrayList(); skyboxModel.modelMatrix = new Matrix4f(); @@ -445,7 +371,7 @@ public class RenderUtils { public static Model createParticleModel(){ Model particleModel = new Model(); - particleModel.meshes = new ArrayList(); + particleModel.meshes = new ArrayList(); particleModel.modelMatrix = new Matrix4f(); @@ -566,7 +492,7 @@ public class RenderUtils { public static Model createPlaneModel(){ Model rVal = new Model(); - rVal.meshes = new ArrayList(); + rVal.meshes = new ArrayList(); rVal.modelMatrix = new Matrix4f(); @@ -682,5 +608,1167 @@ public class RenderUtils { return rVal; } + + + public static Model createBitmapDisplay(){ + + Model rVal = new Model(); + rVal.meshes = new ArrayList(); + Mesh m = new Mesh(); + m.vertexArrayObject = glGenVertexArrays(); + glBindVertexArray(m.vertexArrayObject); + //vertices + FloatBuffer VertexArrayBufferData = BufferUtils.createFloatBuffer(12); + VertexArrayBufferData.put( 0); + VertexArrayBufferData.put( 1); + + VertexArrayBufferData.put( 0); + VertexArrayBufferData.put( 0); + + VertexArrayBufferData.put( 1); + VertexArrayBufferData.put( 0); + + VertexArrayBufferData.put( 0); + VertexArrayBufferData.put( 1); + + VertexArrayBufferData.put( 1); + VertexArrayBufferData.put( 0); + + VertexArrayBufferData.put( 1); + VertexArrayBufferData.put( 1); + VertexArrayBufferData.flip(); + + + IntBuffer faceArrayBufferData = BufferUtils.createIntBuffer(6); + faceArrayBufferData.put(0); + faceArrayBufferData.put(1); + faceArrayBufferData.put(2); + + faceArrayBufferData.put(3); + faceArrayBufferData.put(4); + faceArrayBufferData.put(5); + faceArrayBufferData.flip(); + + + + //texture coords + FloatBuffer TextureArrayBufferData = BufferUtils.createFloatBuffer(12); + TextureArrayBufferData.put(0); + TextureArrayBufferData.put(1); + + TextureArrayBufferData.put(0); + TextureArrayBufferData.put(0); + + TextureArrayBufferData.put(1); + TextureArrayBufferData.put(0); + + TextureArrayBufferData.put(0); + TextureArrayBufferData.put(1); + + TextureArrayBufferData.put(1); + TextureArrayBufferData.put(0); + + TextureArrayBufferData.put(1); + TextureArrayBufferData.put(1); + TextureArrayBufferData.flip(); + + + //buffer vertices + m.buffer_vertices(VertexArrayBufferData, 2); + //buffer normals + m.buffer_normals(VertexArrayBufferData, 2); + //buffer faces + m.buffer_faces(faceArrayBufferData); + //buffer texture coords + m.buffer_texture_coords(TextureArrayBufferData, 2); + + + m.shader = ShaderProgram.loadSpecificShader("/Shaders/font/basicbitmap/basicbitmap.vs", "/Shaders/font/basicbitmap/basicbitmap.fs"); + + + glBindVertexArray(0); + m.parent = rVal; + m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME; + + Material uiMat = new Material(); + Globals.assetManager.addTexturePathtoQueue("/Textures/Fonts/myfont1-harsher.png"); + uiMat.set_diffuse("/Textures/Fonts/myfont1-harsher.png"); + uiMat.set_specular("/Textures/Fonts/myfont1-harsher.png"); + m.setMaterial(uiMat); + rVal.materials = new ArrayList(); + rVal.materials.add(uiMat); + + rVal.meshes.add(m); + + return rVal; + } + + + + + + + public static Model createBitmapCharacter(){ + + Model rVal = new Model(); + rVal.meshes = new ArrayList(); + Mesh m = new Mesh(); + m.vertexArrayObject = glGenVertexArrays(); + glBindVertexArray(m.vertexArrayObject); + //vertices + FloatBuffer VertexArrayBufferData = BufferUtils.createFloatBuffer(12); + VertexArrayBufferData.put(-1); + VertexArrayBufferData.put( 1); + + VertexArrayBufferData.put(-1); + VertexArrayBufferData.put(-1); + + VertexArrayBufferData.put( 1); + VertexArrayBufferData.put(-1); + + VertexArrayBufferData.put(-1); + VertexArrayBufferData.put( 1); + + VertexArrayBufferData.put( 1); + VertexArrayBufferData.put(-1); + + VertexArrayBufferData.put( 1); + VertexArrayBufferData.put( 1); + VertexArrayBufferData.flip(); + + + IntBuffer faceArrayBufferData = BufferUtils.createIntBuffer(6); + faceArrayBufferData.put(0); + faceArrayBufferData.put(1); + faceArrayBufferData.put(2); + + faceArrayBufferData.put(3); + faceArrayBufferData.put(4); + faceArrayBufferData.put(5); + faceArrayBufferData.flip(); + + + + //texture coords + FloatBuffer TextureArrayBufferData = BufferUtils.createFloatBuffer(12); + TextureArrayBufferData.put(0); + TextureArrayBufferData.put(1); + + TextureArrayBufferData.put(0); + TextureArrayBufferData.put(0); + + TextureArrayBufferData.put(1); + TextureArrayBufferData.put(0); + + TextureArrayBufferData.put(0); + TextureArrayBufferData.put(1); + + TextureArrayBufferData.put(1); + TextureArrayBufferData.put(0); + + TextureArrayBufferData.put(1); + TextureArrayBufferData.put(1); + TextureArrayBufferData.flip(); + + + //buffer vertices + m.buffer_vertices(VertexArrayBufferData, 2); + //buffer normals + m.buffer_normals(VertexArrayBufferData, 2); + //buffer faces + m.buffer_faces(faceArrayBufferData); + //buffer texture coords + m.buffer_texture_coords(TextureArrayBufferData, 2); + + + m.shader = ShaderProgram.loadSpecificShader("/Shaders/font/bitmapchar/bitmapchar.vs", "/Shaders/font/bitmapchar/bitmapchar.fs"); + + + glBindVertexArray(0); + m.parent = rVal; + m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME; + + Material uiMat = new Material(); + Globals.assetManager.addTexturePathtoQueue("/Textures/Fonts/myfont1-harsher.png"); + uiMat.set_diffuse("/Textures/Fonts/myfont1-harsher.png"); + uiMat.set_specular("/Textures/Fonts/myfont1-harsher.png"); + m.setMaterial(uiMat); + rVal.materials = new ArrayList(); + rVal.materials.add(uiMat); + + rVal.meshes.add(m); + + return rVal; + } + + + + + public static Model createTerrainModelPrecomputedShader(float[][] heightfield, float[][] texturemap, ShaderProgram program, int stride){ + Model rVal = new Model(); + rVal.meshes = new ArrayList(); + Mesh m = new Mesh(); + int width = heightfield.length; + int height = heightfield[0].length; + + int actualWidth = (int)Math.ceil(1.0f * width / (1.0f * stride)); + int actualHeight = (int)Math.ceil(1.0f * height / (1.0f * stride)); + + +// System.out.println(actualWidth + " " + actualHeight); + +// System.out.println((actualWidth - 1) * (actualHeight - 1)); + + FloatBuffer vertices; + FloatBuffer normals; + IntBuffer faces; + FloatBuffer texture_coords; + FloatBuffer textureIndices; + if(stride * actualWidth > width){ + int drawWidth = actualWidth + 1; + int drawHeight = actualHeight + 1; + vertices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); + normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); + faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3); + texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8); + textureIndices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 16); + } else { + vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); + normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); + faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); + texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8); + textureIndices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 16); + } + + int incrementer = 0; + int numFaces = (actualWidth - 1) * (actualHeight - 1) * 2 * 3; + for(int x = 0; x < width - 1; x = x + stride){ + for(int y = 0; y < height - 1; y = y + stride){ + //deal with vertex + //0,0 + vertices.put(x); + vertices.put(heightfield[x][y]); + vertices.put(y); + //1,0 + vertices.put(x + stride); + vertices.put(heightfield[x+stride][y]); + vertices.put(y); + //0,1 + vertices.put(x); + vertices.put(heightfield[x][y+stride]); + vertices.put(y + stride); + //1,1 + vertices.put(x + stride); + vertices.put(heightfield[x+stride][y+stride]); + vertices.put(y + stride); + //deal with normal + Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y + stride); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y + stride); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + //deal with texture coordinates +// if(x / stride % 2 == 0){ +// if(y / stride % 2 == 0){ +// texture_coords.put(0); +// texture_coords.put(0); +// texture_coords.put(1); +// texture_coords.put(0); +// texture_coords.put(0); +// texture_coords.put(1); +// texture_coords.put(1); +// texture_coords.put(1); +// } else { +// texture_coords.put(0); +// texture_coords.put(1); +// } +// } else { +// if(y / stride % 2 == 0){ +// texture_coords.put(1); +// texture_coords.put(0); +// } else { +// texture_coords.put(1); +// texture_coords.put(1); +// } +// } + texture_coords.put(0); + texture_coords.put(0); + texture_coords.put(1); + texture_coords.put(0); + texture_coords.put(0); + texture_coords.put(1); + texture_coords.put(1); + texture_coords.put(1); + + if(x + stride < width - 1 && y + stride < height - 1){ +// texturemap[x+stride][y+stride]; + for(int i = 0; i < 4 ; i++){ +// textureIndices.put(1); +// textureIndices.put(0); +// textureIndices.put(0); +// textureIndices.put(0); + textureIndices.put(texturemap[x][y]); + textureIndices.put(texturemap[x+stride][y]); + textureIndices.put(texturemap[x][y+stride]); + textureIndices.put(texturemap[x+stride][y+stride]); + } + } else { + for(int i = 0; i < 4 ; i++){ + textureIndices.put(0); + textureIndices.put(0); + textureIndices.put(0); + textureIndices.put(0); + } + } + + + //deal with faces + if(1.0f * x / stride < actualWidth - 1 && 1.0f * y / stride < actualHeight - 1){ + faces.put(incrementer * 4 + 0); + faces.put(incrementer * 4 + 1); + faces.put(incrementer * 4 + 2); + faces.put(incrementer * 4 + 1); + faces.put(incrementer * 4 + 2); + faces.put(incrementer * 4 + 3); + } + incrementer++; + } + } + + vertices.flip(); + normals.flip(); + faces.flip(); + texture_coords.flip(); + textureIndices.flip(); + + m.vertexArrayObject = glGenVertexArrays(); + glBindVertexArray(m.vertexArrayObject); + //buffer vertices + m.buffer_vertices(vertices, 3); + //buffer normals + m.buffer_normals(normals, 3); + //buffer faces + m.buffer_faces(faces); + //buffer texture coords + m.buffer_texture_coords(texture_coords, 2); + //texture indices + m.bufferCustomFloatAttribArray(textureIndices, 4, 5); + m.shader = program; + glBindVertexArray(0); + m.parent = rVal; + + m.hasBones = false; + + Material groundMat = new Material(); + Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); + groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); + groundMat.set_specular("/Textures/Ground/Dirt1.png"); + m.setMaterial(groundMat); + + rVal.meshes.add(m); + return rVal; + } + + + static float MINIMIZATION_DIFF_MAX = 0.1f; + + public static Model createMinimizedTerrainModelPrecomputedShader(float[][] heightfield, float[][] texturemap, ShaderProgram program, int stride){ + + class QuadToGenerate { + //coords are inclusive + int startX; + int endX; + int startY; + int endY; + float diff; + float min; + float max; + float texture; + boolean homogeneousTexture; + + QuadToGenerate(int startX, int startY, int endX, int endY, float diff, float min, float max, boolean homogeneousTexture, float texture){ + this.startX = startX; + this.startY = startY; + this.endX = endX; + this.endY = endY; + this.diff = diff; + this.min = min; + this.max = max; + this.texture = texture; + this.homogeneousTexture = homogeneousTexture; + } + + } + + + Model rVal = new Model(); + rVal.meshes = new ArrayList(); + Mesh m = new Mesh(); + int width = heightfield.length; + int height = heightfield[0].length; + + int actualWidth = (int)Math.ceil(1.0f * width / (1.0f * stride)); + int actualHeight = (int)Math.ceil(1.0f * height / (1.0f * stride)); + + +// System.out.println(actualWidth + " " + actualHeight); + +// System.out.println((actualWidth - 1) * (actualHeight - 1)); + + FloatBuffer vertices; + FloatBuffer normals; + IntBuffer faces; + FloatBuffer texture_coords; + FloatBuffer textureIndices; + if(stride * actualWidth > width){ + int drawWidth = actualWidth + 1; + int drawHeight = actualHeight + 1; + vertices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); + normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); + faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3); + texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8); + textureIndices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 16); + } else { + vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); + normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); + faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); + texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8); + textureIndices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 16); + } + + + //merge along y + + List firstPhaseQuads = new LinkedList(); + QuadToGenerate quadCurrent = null; + float minVal = 0; + float maxVal = 0; + int textureVal = -1; + for(int x = 0; x < width - 1; x = x + stride){ + quadCurrent = null; + for(int y = 0; y < height - 1; y = y + stride){ + if((x == 5 && y == 2)){ + // System.out.println(quadCurrent); +// continue; + } + if(quadCurrent == null){ + minVal = 100000000; + maxVal = 0; + textureVal = -1; + //minval + if(heightfield[x][y] < minVal){ + minVal = heightfield[x][y]; + } + if(heightfield[x+stride][y] < minVal){ + minVal = heightfield[x+stride][y]; + } + if(heightfield[x][y+stride] < minVal){ + minVal = heightfield[x][y+stride]; + } + if(heightfield[x+stride][y+stride] < minVal){ + minVal = heightfield[x+stride][y+stride]; + } + //maxval + if(heightfield[x][y] > maxVal){ + maxVal = heightfield[x][y]; + } + if(heightfield[x+stride][y] > maxVal){ + maxVal = heightfield[x+stride][y]; + } + if(heightfield[x][y+stride] > maxVal){ + maxVal = heightfield[x][y+stride]; + } + if(heightfield[x+stride][y+stride] > maxVal){ + maxVal = heightfield[x+stride][y+stride]; + } + boolean textureMatch = false; + float texture = -1; + if(x+stride < width - 1 && y+stride < height -1 && + texturemap[x][y] == texturemap[x+stride][y] && + texturemap[x][y] == texturemap[x][y+stride] && + texturemap[x][y] == texturemap[x+stride][y+stride]){ + + textureMatch = true; + texture = texturemap[x][y]; + } else { +// if(x > 8 && (x+stride < width - 1) && (y+stride < height -1)){ +// System.out.println( +// (x+stride < width - 1) + " " + +// (y+stride < height -1) + " " + +// (texturemap[x][y] == texturemap[x+stride][y]) + " " + +// (texturemap[x][y] == texturemap[x][y+stride]) + " " + +// (texturemap[x][y] == texturemap[x+stride][y+stride]) +// ); +// } + } + if(textureMatch){ + quadCurrent = new QuadToGenerate(x,y,x+stride,y+stride,maxVal - minVal,minVal,maxVal,textureMatch,texture); + } else { + firstPhaseQuads.add(new QuadToGenerate(x,y,x+stride,y+stride,maxVal - minVal,minVal,maxVal,textureMatch,texture)); + // quadCurrent = null; + } + } else { + float newMin = minVal; + float newMax = maxVal; + //min + if(heightfield[x][y+stride] < newMin){ + newMin = heightfield[x][y+stride]; + } + if(heightfield[x+stride][y+stride] < newMin){ + newMin = heightfield[x+stride][y+stride]; + } + //max + if(heightfield[x][y+stride] > newMax){ + newMax = heightfield[x][y+stride]; + } + if(heightfield[x+stride][y+stride] > newMax){ + newMax = heightfield[x+stride][y+stride]; + } + if(y+stride < height - 1 && x+stride < width - 1){ + if(newMax - newMin < MINIMIZATION_DIFF_MAX && + quadCurrent.texture == texturemap[x+stride][y] && + quadCurrent.texture == texturemap[x ][y+stride] && + quadCurrent.texture == texturemap[x+stride][y+stride] && + quadCurrent.homogeneousTexture + ){ + //add to quad + quadCurrent.endY = y + stride; + quadCurrent.diff = newMax - newMin; + quadCurrent.min = newMax; + quadCurrent.max = newMax; + } else { + //push quad + firstPhaseQuads.add(quadCurrent); + firstPhaseQuads.add(new QuadToGenerate(x,y,x+stride,y+stride,maxVal - minVal,minVal,maxVal,false,0)); + quadCurrent = null; +// System.out.println("Push"); + } + } else { + if(newMax - newMin < MINIMIZATION_DIFF_MAX){ + //add to quad + quadCurrent.endY = y + stride; + quadCurrent.diff = newMax - newMin; + quadCurrent.min = newMax; + quadCurrent.max = newMax; + } else { + //push quad + firstPhaseQuads.add(quadCurrent); + quadCurrent = null; +// System.out.println("Push"); + } + } + } + } + if(quadCurrent != null){ + firstPhaseQuads.add(quadCurrent); + } + } + + List finalQuads = new LinkedList(); + // for(QuadToGenerate current : firstPhaseQuads){ + // finalQuads.add(current); + // } + +// System.out.println(finalQuads.size()); + + //merge along x + + // QuadToGenerate currentQuad = null; + List toSkip = new LinkedList(); + for(QuadToGenerate currentQuad : firstPhaseQuads){ + // toRemove.clear(); + if(toSkip.contains(currentQuad)){ + continue; + } + for(QuadToGenerate currentPotentialMatch : firstPhaseQuads){ + if(currentPotentialMatch.startX <= currentQuad.startX){ + continue; + } + if(currentPotentialMatch.startX > currentQuad.endX){ + break; + } + if(currentPotentialMatch.startY != currentQuad.startY){ + continue; + } + if(currentPotentialMatch.endY != currentQuad.endY){ + continue; + } + if( + !(currentQuad.homogeneousTexture && + currentPotentialMatch.homogeneousTexture && + currentQuad.texture == currentPotentialMatch.texture) + ){ + continue; + } + if(currentQuad.min < currentPotentialMatch.min && currentQuad.max < currentPotentialMatch.max){ + float min = currentQuad.min; + float max = currentPotentialMatch.max; + if(max - min < MINIMIZATION_DIFF_MAX){ + currentQuad.endX = currentPotentialMatch.endX; + currentQuad.max = currentPotentialMatch.max; + toSkip.add(currentPotentialMatch); + } + } else if(currentQuad.min > currentPotentialMatch.min && currentQuad.max > currentPotentialMatch.max){ + float min = currentPotentialMatch.min; + float max = currentQuad.max; + if(max - min < MINIMIZATION_DIFF_MAX){ + currentQuad.endX = currentPotentialMatch.endX; + currentQuad.min = currentPotentialMatch.min; + toSkip.add(currentPotentialMatch); + } + } else { + if(currentQuad.min < currentPotentialMatch.min){ + currentQuad.endX = currentPotentialMatch.endX; + } else { + currentQuad.endX = currentPotentialMatch.endX; + currentQuad.min = currentPotentialMatch.min; + currentQuad.max = currentPotentialMatch.max; + } + toSkip.add(currentPotentialMatch); + } + } + finalQuads.add(currentQuad); + } + // for(QuadToGenerate currentIteration : firstPhaseQuads){ + // if(currentQuad == null){ + // currentQuad = currentIteration; + // } else { + // //if should merge: + // if( + // currentQuad.homogeneousTexture && + // currentIteration.homogeneousTexture && + // currentQuad.texture == currentIteration.texture + // ){ + // if(currentQuad.min < currentIteration.min && currentQuad.max < currentIteration.max){ + // float min = currentQuad.min; + // float max = currentIteration.max; + // if(max - min < MINIMIZATION_DIFF_MAX){ + // currentQuad.endX = currentIteration.endX; + // currentQuad.max = currentIteration.max; + // } else { + // finalQuads.add(currentQuad); + // currentQuad = currentIteration; + // } + // } else if(currentQuad.min > currentIteration.min && currentQuad.max > currentIteration.max){ + // float min = currentIteration.min; + // float max = currentQuad.max; + // if(max - min < MINIMIZATION_DIFF_MAX){ + // currentQuad.endX = currentIteration.endX; + // currentQuad.min = currentIteration.min; + // } else { + // finalQuads.add(currentQuad); + // currentQuad = currentIteration; + // } + // } else { + // if(currentQuad.min < currentIteration.min){ + // currentQuad.endX = currentIteration.endX; + // } else { + // currentQuad.endX = currentIteration.endX; + // currentQuad.min = currentIteration.min; + // currentQuad.max = currentIteration.max; + // } + // } + // } else { + // finalQuads.add(currentQuad); + // currentQuad = currentIteration; + // } + // } + // } + // finalQuads.add(currentQuad); + + // for(QuadToGenerate current : finalQuads){ + // if(current.startX > 0 && current.startY > 0 && current.endX < 99 && current.endY < 99){ + // System.out.println(current.startX + " " + current.startY + " " + current.endX + " " + current.endY); + // } + // } + + // System.out.println("AAAAAAAAAAAAAAAAAA"); +// System.out.println(finalQuads.size()); +// System.exit(0); + + int incrementer = 0; + + for(QuadToGenerate current : finalQuads){ + //deal with vertex + //0,0 + vertices.put(current.startX); + vertices.put(heightfield[current.startX][current.startY]); + vertices.put(current.startY); + //1,0 + vertices.put(current.endX); + vertices.put(heightfield[current.endX][current.startY]); + vertices.put(current.startY); + //0,1 + vertices.put(current.startX); + vertices.put(heightfield[current.startX][current.endY]); + vertices.put(current.endY); + //1,1 + vertices.put(current.endX); + vertices.put(heightfield[current.endX][current.endY]); + vertices.put(current.endY); + //deal with normal + Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.startX, current.startY); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.endX, current.startY); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.startX, current.endY); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, current.endX, current.endY); + normals.put(normal.x); + normals.put(normal.y); + normals.put(normal.z); + //deal with texture coordinates +// if(x / stride % 2 == 0){ +// if(y / stride % 2 == 0){ +// texture_coords.put(0); +// texture_coords.put(0); +// texture_coords.put(1); +// texture_coords.put(0); +// texture_coords.put(0); +// texture_coords.put(1); +// texture_coords.put(1); +// texture_coords.put(1); +// } else { +// texture_coords.put(0); +// texture_coords.put(1); +// } +// } else { +// if(y / stride % 2 == 0){ +// texture_coords.put(1); +// texture_coords.put(0); +// } else { +// texture_coords.put(1); +// texture_coords.put(1); +// } +// } + texture_coords.put(0); + texture_coords.put(0); + texture_coords.put(current.endX - current.startX); + texture_coords.put(0); + texture_coords.put(0); + texture_coords.put(current.endY - current.startY); + texture_coords.put(current.endX - current.startX); + texture_coords.put(current.endY - current.startY); + + if(current.endX < width - 1 && current.endY < height - 1){ +// texturemap[x+stride][y+stride]; + for(int i = 0; i < 4 ; i++){ +// textureIndices.put(1); +// textureIndices.put(0); +// textureIndices.put(0); +// textureIndices.put(0); + textureIndices.put(texturemap[current.startX][current.startY]); + textureIndices.put(texturemap[current.endX][current.startY]); + textureIndices.put(texturemap[current.startX][current.endY]); + textureIndices.put(texturemap[current.endX][current.endY]); + } + } else { + for(int i = 0; i < 4 ; i++){ + textureIndices.put(0); + textureIndices.put(0); + textureIndices.put(0); + textureIndices.put(0); + } + } + + + //deal with faces + faces.put(incrementer * 4 + 0); + faces.put(incrementer * 4 + 1); + faces.put(incrementer * 4 + 2); + faces.put(incrementer * 4 + 1); + faces.put(incrementer * 4 + 2); + faces.put(incrementer * 4 + 3); + + incrementer++; + } + + + +// int numFaces = (actualWidth - 1) * (actualHeight - 1) * 2 * 3; +// for(int x = 0; x < width - 1; x = x + stride){ +// for(int y = 0; y < height - 1; y = y + stride){ +// //deal with vertex +// //0,0 +// vertices.put(x); +// vertices.put(heightfield[x][y]); +// vertices.put(y); +// //1,0 +// vertices.put(x + stride); +// vertices.put(heightfield[x+stride][y]); +// vertices.put(y); +// //0,1 +// vertices.put(x); +// vertices.put(heightfield[x][y+stride]); +// vertices.put(y + stride); +// //1,1 +// vertices.put(x + stride); +// vertices.put(heightfield[x+stride][y+stride]); +// vertices.put(y + stride); +// //deal with normal +// Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y); +// normals.put(normal.x); +// normals.put(normal.y); +// normals.put(normal.z); +// normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y); +// normals.put(normal.x); +// normals.put(normal.y); +// normals.put(normal.z); +// normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y + stride); +// normals.put(normal.x); +// normals.put(normal.y); +// normals.put(normal.z); +// normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + stride, y + stride); +// normals.put(normal.x); +// normals.put(normal.y); +// normals.put(normal.z); +// //deal with texture coordinates +//// if(x / stride % 2 == 0){ +//// if(y / stride % 2 == 0){ +//// texture_coords.put(0); +//// texture_coords.put(0); +//// texture_coords.put(1); +//// texture_coords.put(0); +//// texture_coords.put(0); +//// texture_coords.put(1); +//// texture_coords.put(1); +//// texture_coords.put(1); +//// } else { +//// texture_coords.put(0); +//// texture_coords.put(1); +//// } +//// } else { +//// if(y / stride % 2 == 0){ +//// texture_coords.put(1); +//// texture_coords.put(0); +//// } else { +//// texture_coords.put(1); +//// texture_coords.put(1); +//// } +//// } +// texture_coords.put(0); +// texture_coords.put(0); +// texture_coords.put(1); +// texture_coords.put(0); +// texture_coords.put(0); +// texture_coords.put(1); +// texture_coords.put(1); +// texture_coords.put(1); +// +// if(x + stride < width - 1 && y + stride < height - 1){ +//// texturemap[x+stride][y+stride]; +// for(int i = 0; i < 4 ; i++){ +//// textureIndices.put(1); +//// textureIndices.put(0); +//// textureIndices.put(0); +//// textureIndices.put(0); +// textureIndices.put(texturemap[x][y]); +// textureIndices.put(texturemap[x+stride][y]); +// textureIndices.put(texturemap[x][y+stride]); +// textureIndices.put(texturemap[x+stride][y+stride]); +// } +// } else { +// for(int i = 0; i < 4 ; i++){ +// textureIndices.put(0); +// textureIndices.put(0); +// textureIndices.put(0); +// textureIndices.put(0); +// } +// } +// +// +// //deal with faces +// if(1.0f * x / stride < actualWidth - 1 && 1.0f * y / stride < actualHeight - 1){ +// faces.put(incrementer * 4 + 0); +// faces.put(incrementer * 4 + 1); +// faces.put(incrementer * 4 + 2); +// faces.put(incrementer * 4 + 1); +// faces.put(incrementer * 4 + 2); +// faces.put(incrementer * 4 + 3); +// } +// incrementer++; +// } +// } + + System.out.println(incrementer + " quads"); + + vertices.flip(); + normals.flip(); + faces.flip(); + texture_coords.flip(); + textureIndices.flip(); + + m.vertexArrayObject = glGenVertexArrays(); + glBindVertexArray(m.vertexArrayObject); + //buffer vertices + m.buffer_vertices(vertices, 3); + //buffer normals + m.buffer_normals(normals, 3); + //buffer faces + m.buffer_faces(faces); + //buffer texture coords + m.buffer_texture_coords(texture_coords, 2); + //texture indices + m.bufferCustomFloatAttribArray(textureIndices, 4, 5); + m.shader = program; + glBindVertexArray(0); + m.parent = rVal; + + m.hasBones = false; + + Material groundMat = new Material(); + Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); + groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); + groundMat.set_specular("/Textures/Ground/Dirt1.png"); + m.setMaterial(groundMat); + + rVal.meshes.add(m); + return rVal; + } + + static Vector3f calculateTerrainNormal(float[][] heightfield, int actualWidth, int actualHeight, int stride, int x, int y){ + Vector3f rVal = new Vector3f(); + if(x / stride < actualWidth - 1){ + if(y / stride < actualHeight - 1){ + float hL; + if(x > 0){ + hL = heightfield[x-1][y]; + } else { + hL = heightfield[x][y]; + } + float hR = heightfield[x+1][y]; + float hD = heightfield[x][y+1]; + float hU; + if(y > 0){ + hU = heightfield[x][y-1]; + } else { + hU = heightfield[x][y]; + } + rVal = new Vector3f(hL - hR, 2.0f, hD - hU); + rVal.normalize(); + } else { + float hL; + if(x > 0){ + hL = heightfield[x-1][y]; + } else { + hL = heightfield[x][y]; + } + float hR = heightfield[x+1][y]; + float hD = heightfield[x][y]; + float hU = heightfield[x][y-1]; + rVal = new Vector3f(hL - hR, 2.0f, hD - hU); + rVal.normalize(); + } + } else { + if(y / stride < actualHeight - 1){ + float hL = heightfield[x-1][y]; + float hR = heightfield[x][y]; + float hD = heightfield[x][y+1]; + float hU; + if(y > 0){ + hU = heightfield[x][y-1]; + } else { + hU = heightfield[x][y]; + } + rVal = new Vector3f(hL - hR, 2.0f, hD - hU); + rVal.normalize(); + } else { + float hL = heightfield[x-1][y]; + float hR = heightfield[x][y]; + float hD = heightfield[x][y]; + float hU = heightfield[x][y-1]; + rVal = new Vector3f(hL - hR, 2.0f, hD - hU); + rVal.normalize(); + } + } + return rVal; + } + + + public static Model createUnitCube(){ + Model rVal = new Model(); + rVal.meshes = new ArrayList(); + Mesh m = new Mesh(); + +// System.out.println(actualWidth + " " + actualHeight); + +// System.out.println((actualWidth - 1) * (actualHeight - 1)); + + FloatBuffer vertices = BufferUtils.createFloatBuffer(8 * 3); + FloatBuffer normals = BufferUtils.createFloatBuffer(8 * 3); + IntBuffer faces = BufferUtils.createIntBuffer(6 * 2 * 3); + FloatBuffer texture_coords = BufferUtils.createFloatBuffer(8 * 2); + + //vertices + //0,0,0 + vertices.put(0); + vertices.put(0); + vertices.put(0); + //1,0,0 + vertices.put(1); + vertices.put(0); + vertices.put(0); + //0,1,0 + vertices.put(0); + vertices.put(1); + vertices.put(0); + //1,1,0 + vertices.put(1); + vertices.put(1); + vertices.put(0); + //0,0,1 + vertices.put(0); + vertices.put(0); + vertices.put(1); + //1,0,1 + vertices.put(1); + vertices.put(0); + vertices.put(1); + //0,1,1 + vertices.put(0); + vertices.put(1); + vertices.put(1); + //1,1,1 + vertices.put(1); + vertices.put(1); + vertices.put(1); + + //normals + //-1,-1,-1 + normals.put(-1); + normals.put(-1); + normals.put(-1); + // 1,-1,-1 + normals.put( 1); + normals.put(-1); + normals.put(-1); + //-1, 1,-1 + normals.put(-1); + normals.put( 1); + normals.put(-1); + // 1, 1,-1 + normals.put( 1); + normals.put( 1); + normals.put(-1); + //-1,-1, 1 + normals.put(-1); + normals.put(-1); + normals.put( 1); + // 1,-1, 1 + normals.put( 1); + normals.put(-1); + normals.put( 1); + //-1, 1, 1 + normals.put(-1); + normals.put( 1); + normals.put( 1); + // 1, 1, 1 + normals.put( 1); + normals.put( 1); + normals.put( 1); + + //faces + //0,1,2 + faces.put(0); + faces.put(1); + faces.put(2); + //1,2,3 + faces.put(1); + faces.put(2); + faces.put(3); + //1,3,5 + faces.put(1); + faces.put(3); + faces.put(5); + //3,5,7 + faces.put(3); + faces.put(5); + faces.put(7); + //0,1,4 + faces.put(0); + faces.put(1); + faces.put(4); + //1,4,5 + faces.put(1); + faces.put(4); + faces.put(5); + //0,2,4 + faces.put(0); + faces.put(2); + faces.put(4); + //2,4,6 + faces.put(2); + faces.put(4); + faces.put(6); + //2,3,6 + faces.put(2); + faces.put(3); + faces.put(6); + //3,6,7 + faces.put(3); + faces.put(6); + faces.put(7); + //4,5,6 + faces.put(4); + faces.put(5); + faces.put(6); + //5,6,7 + faces.put(5); + faces.put(6); + faces.put(7); + + //texture + for(int i = 0; i < 8 * 2; i++){ + texture_coords.put(0); + } + + + vertices.flip(); + normals.flip(); + faces.flip(); + texture_coords.flip(); + + m.vertexArrayObject = glGenVertexArrays(); + glBindVertexArray(m.vertexArrayObject); + //buffer vertices + m.buffer_vertices(vertices, 3); + //buffer normals + m.buffer_normals(normals, 3); + //buffer faces + m.buffer_faces(faces); + //buffer texture coords + m.buffer_texture_coords(texture_coords, 2); + m.shader = ShaderProgram.smart_assemble_shader(false,true); + glBindVertexArray(0); + m.parent = rVal; + + Material groundMat = new Material(); + Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); + groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); + groundMat.set_specular("/Textures/Ground/Dirt1.png"); + m.setMaterial(groundMat); + + rVal.meshes.add(m); + return rVal; + } + + } diff --git a/src/main/java/electrosphere/renderer/actor/Actor.java b/src/main/java/electrosphere/renderer/actor/Actor.java index 8a3b7e6a..ffda9ff5 100644 --- a/src/main/java/electrosphere/renderer/actor/Actor.java +++ b/src/main/java/electrosphere/renderer/actor/Actor.java @@ -77,6 +77,18 @@ public class Actor { public boolean isPlayingAnimation(){ return animationQueue.size() > 0; } + + public void stopAnimation(String animationName){ + List toRemove = new LinkedList(); + for(ActorAnimationMask mask : animationQueue){ + if(mask.getAnimationName().contains(animationName)){ + toRemove.add(mask); + } + } + for(ActorAnimationMask mask : toRemove){ + animationQueue.remove(mask); + } + } // public String getCurrentAnimation(){ // return animation;