package electrosphere.renderer; import electrosphere.main.Globals; import electrosphere.engine.assetmanager.AssetDataStrings; import electrosphere.renderer.texture.Texture; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.joml.Vector3f; import org.lwjgl.BufferUtils; import static org.lwjgl.opengl.GL30.glBindVertexArray; import static org.lwjgl.opengl.GL30.glGenVertexArrays; /** * * @author satellite */ public class ModelUtils { // public static Model createTerrainModel(float[][] heightfield, 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.0 * width / stride); // int actualHeight = (int)Math.ceil(1.0 * height / stride); // //// System.out.println(actualWidth + " " + actualHeight); // //// System.out.println((actualWidth - 1) * (actualHeight - 1)); // // FloatBuffer vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3); // FloatBuffer normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3); // IntBuffer faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); // FloatBuffer texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 2); // for(int x = 0; x < width; x = x + stride){ // for(int y = 0; y < height; y = y + stride){ // //deal with vertex // vertices.put(x); // vertices.put(heightfield[x][y]); // vertices.put(y); // //deal with normal // 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]; // } // Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); // Normal.normalize(); // normals.put(Normal.x); // normals.put(Normal.y); // normals.put(Normal.z); // } 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]; // Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); // Normal.normalize(); // normals.put(Normal.x); // normals.put(Normal.y); // normals.put(Normal.z); // } // } 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]; // } // Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); // Normal.normalize(); // normals.put(Normal.x); // normals.put(Normal.y); // normals.put(Normal.z); // } else { // float hL = heightfield[x-1][y]; // float hR = heightfield[x][y]; // float hD = heightfield[x][y]; // float hU = heightfield[x][y-1]; // Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); // Normal.normalize(); // 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); // } 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); // } // } // //deal with faces // if(x / stride < actualWidth - 1 && y / stride < actualHeight - 1){ // faces.put((x / stride + 0) * actualHeight + (y / stride + 0)); // faces.put((x / stride + 0) * actualHeight + (y / stride + 1)); // faces.put((x / stride + 1) * actualHeight + (y / stride + 0)); // faces.put((x / stride + 1) * actualHeight + (y / stride + 0)); // faces.put((x / stride + 0) * actualHeight + (y / stride + 1)); // faces.put((x / stride + 1) * actualHeight + (y / stride + 1)); // } // } // } // 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.loadSpecificShader("/Shaders/terrain/terrain.vs", "/Shaders/terrain/terrain.fs"); // 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.set_material(groundMat); // // rVal.meshes.add(m); // return rVal; // } public static void printModelDimension(Model m){ float minX = 0; float maxX = 0; float minY = 0; float maxY = 0; float minZ = 0; float maxZ = 0; boolean initiated = false; for(Mesh currentMesh : m.meshes){ if(initiated){ if(currentMesh.vertexMinX < minX){ minX = currentMesh.vertexMinX; } if(currentMesh.vertexMaxX > maxX){ maxX = currentMesh.vertexMaxX; } if(currentMesh.vertexMinY < minY){ minY = currentMesh.vertexMinY; } if(currentMesh.vertexMaxY > maxY){ maxY = currentMesh.vertexMaxY; } if(currentMesh.vertexMinZ < minZ){ minZ = currentMesh.vertexMinZ; } if(currentMesh.vertexMaxZ > maxZ){ maxZ = currentMesh.vertexMaxZ; } } else { initiated = true; minX = currentMesh.vertexMinX; maxX = currentMesh.vertexMaxX; minY = currentMesh.vertexMinY; maxY = currentMesh.vertexMaxY; minZ = currentMesh.vertexMinZ; maxZ = currentMesh.vertexMaxZ; } } System.out.println("dimensions: " + (maxX - minX) + "," + (maxY - minY) + "," + (maxZ-minZ)); } }