@Override public void create() { super.create(); shaderProgram = loadShader("defaultVS.glsl", "spot-phong-FS.glsl"); spotlight = new Spotlight( new Vector3(0, 3, 0), new Vector3(0, (float) Math.PI / 4 + 0.3f, 0), new Vector3(0.3f, 1.0f, 0.3f), 1, (float) Math.PI / 4); ModelLoader<?> cubeLoader = new ObjLoader(); ModelData floorData = cubeLoader.loadModelData(Gdx.files.internal("box.obj")); floorTxt = new Texture("texture.png"); floorMesh = new Mesh( true, floorData.meshes.get(0).vertices.length, floorData.meshes.get(0).parts[0].indices.length, VertexAttribute.Position(), VertexAttribute.Normal(), VertexAttribute.TexCoords(0)); floorMesh.setVertices(floorData.meshes.get(0).vertices); floorMesh.setIndices(floorData.meshes.get(0).parts[0].indices); floor = new ModelObject( floorMesh, 0.2f, new Vector3(-150, -2, -150), new Vector3(0, 0, 0), new Vector3(3000f, 0.1f, 3000f)); ((MoveablePCamera) camera).addModel(model); }
@Override public void create() { ShaderProgram.pedantic = false; String vs = Gdx.files.internal("signed/vertex.glsl").readString(); String fs = Gdx.files.internal("signed/fragment.glsl").readString(); shader = new ShaderProgram(vs, fs); if (!shader.isCompiled()) { throw new IllegalArgumentException( "Error compiling distance field shader: " + shader.getLog()); } mesh = new Mesh(true, 4, 6, new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position")); float[] vertices = {1, -1, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0}; short[] indices = { 0, 1, 2, 2, 3, 0 }; mesh.setVertices(vertices); mesh.setIndices(indices); startTime = TimeUtils.millis(); }
private Mesh generateBlock(Mesh mesh, float width, float length, float height) { mesh.setVertices( new float[] { width / 2, length / 2, height / 2, -width / 2, length / 2, height / 2, -width / 2, -length / 2, height / 2, width / 2, -length / 2, height / 2, -width / 2, -length / 2, -height / 2, -width / 2, length / 2, -height / 2, width / 2, length / 2, -height / 2, width / 2, -length / 2, -height / 2 }); mesh.setIndices(new short[] {0, 1, 5, 2, 4, 7, 5, 6, 0, 7, 3, 2, 0, 1}); return mesh; }
@Deprecated public static Model createFromMesh( final float[] vertices, final VertexAttribute[] attributes, final short[] indices, int primitiveType, final Material material) { final Mesh mesh = new Mesh(false, vertices.length, indices.length, attributes); mesh.setVertices(vertices); mesh.setIndices(indices); return createFromMesh(mesh, 0, indices.length, primitiveType, material); }
/** End building the mesh and returns the mesh */ public Mesh end() { if (this.attributes == null) throw new RuntimeException("Call begin() first"); endpart(); final Mesh mesh = new Mesh(true, vertices.size, indices.size, attributes); mesh.setVertices(vertices.items, 0, vertices.size); mesh.setIndices(indices.items, 0, indices.size); for (MeshPart p : parts) p.mesh = mesh; parts.clear(); attributes = null; vertices.clear(); indices.clear(); return mesh; }
@Override public void create() { texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg")); texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); mesh = new Mesh( true, 4, 6, new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_pos"), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoords")); float[] vertices = new float[4 * 4]; int idx = 0; vertices[idx++] = -1; vertices[idx++] = -1; vertices[idx++] = 0; vertices[idx++] = 0; vertices[idx++] = -1; vertices[idx++] = 1; vertices[idx++] = 0; vertices[idx++] = 1; vertices[idx++] = 1; vertices[idx++] = 1; vertices[idx++] = 1; vertices[idx++] = 1; vertices[idx++] = 1; vertices[idx++] = -1; vertices[idx++] = 1; vertices[idx++] = 0; short[] indices = {0, 1, 2, 2, 3, 0}; mesh.setVertices(vertices); mesh.setIndices(indices); Gdx.input.setInputProcessor(this); }
public Mesh getMesh() { float[] verts = new float[getVertexSize() * numVertices]; short[] indices = new short[numIndices]; VertexAttribute[] attributes = getVertexAttributes(); for (int i = 0; i < numIndices; i++) { VertexIndices vertex = triangles.get(i); if (vertex.index > Short.MAX_VALUE || vertex.index < Short.MIN_VALUE) throw new GdxRuntimeException("index to big for short: " + vertex.index); indices[i] = (short) vertex.index; } int idx = 0; int destOffset = 0; for (int i = 0; i < vertices.size; i++) { VertexIndices vertex = vertices.get(i); for (int j = 0; j < sources.length; j++) { Source source = sources[j]; float[] data = source.data; int index = vertex.indices[j]; int components = source.components; int sourceOffset = index * components; for (int k = 0; k < components; k++) { if ((attributes[j].usage == Usage.TextureCoordinates) && k == 1) { verts[destOffset++] = 1 - data[sourceOffset++]; } else { verts[destOffset++] = data[sourceOffset++]; } } } } Mesh mesh = new Mesh(true, vertices.size, indices.length, attributes); mesh.setVertices(verts); mesh.setIndices(indices); return mesh; }
@Override public void create() { String vertexShader = "uniform float u_offset; \n" + "attribute vec4 a_position; \n" + "attribute vec2 a_texCoord; \n" + "varying vec2 v_texCoord; \n" + "void main() \n" + "{ \n" + " gl_Position = a_position; \n" + " gl_Position.x += u_offset;\n" + " v_texCoord = a_texCoord; \n" + "} \n"; String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec2 v_texCoord; \n" + "uniform sampler2D s_texture; \n" + "void main() \n" + "{ \n" + " gl_FragColor = texture2D( s_texture, v_texCoord );\n" + "} \n"; shader = new ShaderProgram(vertexShader, fragmentShader); mesh = new Mesh( true, 4, 6, new VertexAttribute(Usage.Position, 4, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord")); float[] vertices = { -0.5f, 0.5f, 0.0f, 1.5f, // Position 0 0.0f, 0.0f, // TexCoord 0 -0.5f, -0.5f, 0.0f, 0.75f, // Position 1 0.0f, 1.0f, // TexCoord 1 0.5f, -0.5f, 0.0f, 0.75f, // Position 2 1.0f, 1.0f, // TexCoord 2 0.5f, 0.5f, 0.0f, 1.5f, // Position 3 1.0f, 0.0f // TexCoord 3 }; short[] indices = {0, 1, 2, 0, 2, 3}; mesh.setVertices(vertices); mesh.setIndices(indices); createTexture(); }
public GameMapMesh generateMapMesh(GameMap map) { Mesh mesh = new Mesh( true, 25 * map.getSize(), 36 * map.getSize(), new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position"), new VertexAttribute(VertexAttributes.Usage.ColorUnpacked, 4, "a_color"), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 3, "a_texcoords")); TextureArray textureMap = new TextureArray( "mp09901.bmp", "mp09902.bmp", "mp09903.bmp", "mp09904.bmp", "mp09905.bmp", "mp09906.bmp", "mp09907.bmp", "mp09908.bmp", "mp09991.bmp"); /** Cube vertices */ short[] indices = { 0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 15, 14, 12, 14, 13, 16, 17, 18, 16, 18, 19, 20, 23, 22, 20, 22, 21 }; // F ----- G // /| /| // E ------H | ^ Y Z // | | | | | / // | |B ---|-|C | / // |/ |/ | / // A ------D |------>X TFloatList vertList = new TFloatArrayList(9 * 24 * map.getSize()); TShortList indList = new TShortArrayList(36 * map.getSize()); int i = 0; for (int x = 0; x < map.getWidth(); x++) { for (int y = 0; y < map.getLength(); y++) { Tile t = map.getTile(x, y); if (t == null) continue; float size = 0.5f; Vector3 pta = new Vector3(x - size, 0, y - size); Vector3 ptb = new Vector3(x - size, 0, y + size); Vector3 ptc = new Vector3(x + size, 0, y + size); Vector3 ptd = new Vector3(x + size, 0, y - size); Vector3 pte = new Vector3(x - size, 0.1f * t.heightData.get(Tile.Corner.UPPER_SOUTH_WEST), y - size); Vector3 ptf = new Vector3(x - size, 0.1f * t.heightData.get(Tile.Corner.UPPER_SOUTH_EAST), y + size); Vector3 ptg = new Vector3(x + size, 0.1f * t.heightData.get(Tile.Corner.UPPER_NORTH_EAST), y + size); Vector3 pth = new Vector3(x + size, 0.1f * t.heightData.get(Tile.Corner.UPPER_NORTH_WEST), y - size); Rect2f[] texReg = new Rect2f[5]; texReg[0] = getTextureBounds(t.screenData.get(Tile.Screen.UPPER), textureMap); texReg[1] = getTextureBounds(t.screenData.get(Tile.Screen.SOUTH), textureMap); texReg[2] = getTextureBounds(t.screenData.get(Tile.Screen.NORTH), textureMap); texReg[3] = getTextureBounds(t.screenData.get(Tile.Screen.WEST), textureMap); texReg[4] = getTextureBounds(t.screenData.get(Tile.Screen.EAST), textureMap); int[] texUnits = new int[5]; texUnits[0] = t.screenData.get(Tile.Screen.UPPER).texUnit; texUnits[1] = t.screenData.get(Tile.Screen.SOUTH).texUnit; texUnits[2] = t.screenData.get(Tile.Screen.NORTH).texUnit; texUnits[3] = t.screenData.get(Tile.Screen.WEST).texUnit; texUnits[4] = t.screenData.get(Tile.Screen.EAST).texUnit; float[] verts = { // bottom (-y) pta.x, pta.y, pta.z, 1, 1, 1, 1, 0, 0, 0, ptb.x, ptb.y, ptb.z, 1, 1, 1, 1, 0, 0, 0, ptc.x, ptc.y, ptc.z, 1, 1, 1, 1, 0, 0, 0, ptd.x, ptd.y, ptd.z, 1, 1, 1, 1, 0, 0, 0, // top (+y) pte.x, pte.y, pte.z, 1, 1, 1, 1, texReg[0].minX(), texReg[0].maxY(), texUnits[0], ptf.x, ptf.y, ptf.z, 1, 1, 1, 1, texReg[0].maxX(), texReg[0].maxY(), texUnits[0], ptg.x, ptg.y, ptg.z, 1, 1, 1, 1, texReg[0].maxX(), texReg[0].minY(), texUnits[0], pth.x, pth.y, pth.z, 1, 1, 1, 1, texReg[0].minX(), texReg[0].minY(), texUnits[0], // back (-z) (south) pta.x, pta.y, pta.z, 1, 1, 1, 1, texReg[1].maxX(), texReg[1].maxY(), texUnits[1], pte.x, pte.y, pte.z, 1, 1, 1, 1, texReg[1].maxX(), texReg[1].minY(), texUnits[1], pth.x, pth.y, pth.z, 1, 1, 1, 1, texReg[1].minX(), texReg[1].minY(), texUnits[1], ptd.x, ptd.y, ptd.z, 1, 1, 1, 1, texReg[1].minX(), texReg[1].maxY(), texUnits[1], // front (+z) (north) ptb.x, ptb.y, ptb.z, 1, 1, 1, 1, texReg[2].minX(), texReg[2].maxY(), texUnits[2], ptf.x, ptf.y, ptf.z, 1, 1, 1, 1, texReg[2].minX(), texReg[2].minY(), texUnits[2], ptg.x, ptg.y, ptg.z, 1, 1, 1, 1, texReg[2].maxX(), texReg[2].minY(), texUnits[2], ptc.x, ptc.y, ptc.z, 1, 1, 1, 1, texReg[2].maxX(), texReg[2].maxY(), texUnits[2], // left (-x) (west) pta.x, pta.y, pta.z, 1, 1, 1, 1, texReg[3].minX(), texReg[3].maxY(), texUnits[3], ptb.x, ptb.y, ptb.z, 1, 1, 1, 1, texReg[3].maxX(), texReg[3].maxY(), texUnits[3], ptf.x, ptf.y, ptf.z, 1, 1, 1, 1, texReg[3].maxX(), texReg[3].minY(), texUnits[3], pte.x, pte.y, pte.z, 1, 1, 1, 1, texReg[3].minX(), texReg[3].minY(), texUnits[3], // right (+x) (east) ptd.x, ptd.y, ptd.z, 1, 1, 1, 1, texReg[4].maxX(), texReg[4].maxY(), texUnits[4], ptc.x, ptc.y, ptc.z, 1, 1, 1, 1, texReg[4].minX(), texReg[4].maxY(), texUnits[4], ptg.x, ptg.y, ptg.z, 1, 1, 1, 1, texReg[4].minX(), texReg[4].minY(), texUnits[4], pth.x, pth.y, pth.z, 1, 1, 1, 1, texReg[4].maxX(), texReg[4].minY(), texUnits[4], }; vertList.add(verts); short[] curIndices = new short[indices.length]; for (int j = 0; j < indices.length; j++) { curIndices[j] = (short) (indices[j] + (24 * i)); } indList.add(curIndices); i++; } } mesh.setVertices(vertList.toArray()); mesh.setIndices(indList.toArray()); // textureMap.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); mesh.getVertexAttribute(VertexAttributes.Usage.Position).alias = "a_position"; return new GameMapMesh(mesh, textureMap); }
private SubMesh[] generateSubMeshes(Mesh ogreMesh) { List<Submesh> ogreSubmeshes = ogreMesh.getSubmeshes().getSubmesh(); SubMesh[] submeshes = new SubMesh[ogreSubmeshes.size()]; for (int i = 0; i < ogreSubmeshes.size(); i++) { Submesh ogreSubmesh = ogreSubmeshes.get(i); boolean usesTriangleList = false; if (ogreSubmesh.use32Bitindexes) throw new GdxRuntimeException("submesh '" + i + "' uses 32-bit indices"); if (ogreSubmesh.getOperationtype().equals("triangle_list")) { usesTriangleList = true; } short[] indices = new short[ogreSubmesh.getFaces().count * (usesTriangleList ? 3 : 1)]; for (int j = 0, idx = 0; j < ogreSubmesh.getFaces().count; j++) { Face face = ogreSubmesh.getFaces().getFace().get(j); indices[idx++] = face.v1; if (usesTriangleList || j == 0) { indices[idx++] = face.v2; indices[idx++] = face.v3; } } List<VertexAttribute> attributes = new ArrayList<VertexAttribute>(); IntArray offsets = new IntArray(); int offset = 0; BaseGeometry geom; if (ogreSubmesh.useSharedVertices) { geom = ogreMesh.getSharedgeometry(); } else { geom = ogreSubmesh.getGeometry(); } for (int j = 0; j < geom.getVertexbuffer().size(); j++) { Vertexbuffer buffer = geom.getVertexbuffer().get(j); offsets.add(offset); if (buffer.positions) { attributes.add(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE)); offset += 3; } if (buffer.normals) { attributes.add(new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE)); offset += 3; } if (buffer.tangents) { attributes.add( new VertexAttribute( Usage.Generic, buffer.tangentDimensions, ShaderProgram.TANGENT_ATTRIBUTE)); offset += buffer.tangentDimensions; } if (buffer.binormals) { attributes.add(new VertexAttribute(Usage.Generic, 3, ShaderProgram.BINORMAL_ATTRIBUTE)); offset += 3; } if (buffer.coloursDiffuse) { attributes.add(new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE)); offset += 4; } for (int k = 0; k < buffer.textureCoords; k++) { try { int numTexCoords = 0; switch (k) { case 0: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions0()); break; case 1: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions1()); break; case 2: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions2()); break; case 3: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions3()); break; case 4: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions4()); break; case 5: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions5()); break; case 6: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions6()); break; case 7: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions7()); break; } attributes.add( new VertexAttribute( Usage.TextureCoordinates, numTexCoords, ShaderProgram.TEXCOORD_ATTRIBUTE + k)); offset += numTexCoords; } catch (NumberFormatException e) { throw new GdxRuntimeException( "Can't process texture coords with dimensions != 1, 2, 3, 4 (e.g. float1)"); } } } VertexAttributes attribs = new VertexAttributes(attributes.toArray(new VertexAttribute[0])); int vertexSize = offset; float[] vertices = new float[geom.getVertexCount() * offset]; for (int j = 0; j < geom.getVertexbuffer().size(); j++) { Vertexbuffer buffer = geom.getVertexbuffer().get(j); offset = offsets.get(j); int idx = offset; for (int k = 0; k < buffer.getVertex().size(); k++) { Vertex v = buffer.getVertex().get(k); if (v.getPosition() != null) { vertices[idx++] = v.getPosition().x; vertices[idx++] = v.getPosition().y; vertices[idx++] = v.getPosition().z; } if (v.getNormal() != null) { vertices[idx++] = v.getNormal().x; vertices[idx++] = v.getNormal().y; vertices[idx++] = v.getNormal().z; } if (v.getTangent() != null) { vertices[idx++] = v.getTangent().x; vertices[idx++] = v.getTangent().y; vertices[idx++] = v.getTangent().z; if (buffer.tangentDimensions == 4) vertices[idx++] = v.getTangent().w; } if (v.getBinormal() != null) { vertices[idx++] = v.getBinormal().x; vertices[idx++] = v.getBinormal().y; vertices[idx++] = v.getBinormal().z; } if (v.getColourDiffuse() != null) { float color = getColor(v.getColourDiffuse()); vertices[idx++] = color; } if (v.getTexcoord() != null) { for (int l = 0; l < v.getTexcoord().size(); l++) { Texcoord texCoord = v.getTexcoord().get(l); int numTexCoords = 0; switch (l) { case 0: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions0()); break; case 1: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions1()); break; case 2: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions2()); break; case 3: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions3()); break; case 4: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions4()); break; case 5: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions5()); break; case 6: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions6()); break; case 7: numTexCoords = Integer.valueOf(buffer.getTextureCoordDimensions7()); break; } if (numTexCoords == 1) { vertices[idx++] = texCoord.u; } if (numTexCoords == 2) { vertices[idx++] = texCoord.u; vertices[idx++] = texCoord.v; } if (numTexCoords == 3) { vertices[idx++] = texCoord.u; vertices[idx++] = texCoord.v; vertices[idx++] = texCoord.w; } if (numTexCoords == 4) { vertices[idx++] = texCoord.u; vertices[idx++] = texCoord.v; vertices[idx++] = texCoord.w; vertices[idx++] = texCoord.x; } } } offset += vertexSize; idx = offset; } } com.badlogic.gdx.graphics.Mesh mesh = new com.badlogic.gdx.graphics.Mesh( false, vertices.length / vertexSize, indices.length, attribs); mesh.setIndices(indices); mesh.setVertices(vertices); String meshName = ""; if (ogreMesh.getSubmeshnames() != null) { List<Submeshname> names = ogreMesh.getSubmeshnames().getSubmeshname(); for (int n = 0; n < names.size(); ++n) { if (Integer.parseInt(names.get(n).getIndex()) == i) meshName = names.get(n).getName(); } } SubMesh subMesh; Boneassignments boneAssigments = (ogreSubmesh.getBoneassignments() != null) ? ogreSubmesh.getBoneassignments() : ogreMesh.getBoneassignments(); if (boneAssigments != null) { subMesh = new SkeletonSubMesh(meshName, mesh, GL10.GL_TRIANGLES); } else { subMesh = new StillSubMesh(meshName, mesh, GL10.GL_TRIANGLES); } // FIXME ? subMesh.materialName = ogreSubmesh.material; if (boneAssigments != null) { SkeletonSubMesh subSkelMesh = (SkeletonSubMesh) subMesh; subSkelMesh.setVertices(vertices); subSkelMesh.setIndices(indices); subSkelMesh.skinnedVertices = new float[vertices.length]; System.arraycopy( subSkelMesh.vertices, 0, subSkelMesh.skinnedVertices, 0, subSkelMesh.vertices.length); loadBones(boneAssigments, subSkelMesh); } if (ogreSubmesh.getOperationtype().equals("triangle_list")) subMesh.primitiveType = GL10.GL_TRIANGLES; if (ogreSubmesh.getOperationtype().equals("triangle_fan")) subMesh.primitiveType = GL10.GL_TRIANGLE_FAN; if (ogreSubmesh.getOperationtype().equals("triangle_strip")) subMesh.primitiveType = GL10.GL_TRIANGLE_STRIP; submeshes[i] = subMesh; } return submeshes; }
@Override public void create() { world = new World(); final float width = Gdx.graphics.getWidth(); final float height = Gdx.graphics.getHeight(); if (width > height) camera = new PerspectiveCamera(67f, 3f * width / height, 3f); else camera = new PerspectiveCamera(67f, 3f, 3f * height / width); camera.position.set(10f, 10f, 10f); camera.lookAt(0, 0, 0); camera.update(); // Create some simple meshes final Mesh groundMesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_position")); groundMesh.setVertices( new float[] {20f, 0f, 20f, 20f, 0f, -20f, -20f, 0f, 20f, -20f, 0f, -20f}); groundMesh.setIndices(new short[] {0, 1, 2, 1, 2, 3}); final Mesh boxMesh = new Mesh(true, 8, 36, new VertexAttribute(Usage.Position, 3, "a_position")); boxMesh.setVertices( new float[] { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f }); boxMesh.setIndices( new short[] { 0, 1, 2, 1, 2, 3, // top 4, 5, 6, 5, 6, 7, // bottom 0, 2, 4, 4, 6, 2, // front 1, 3, 5, 5, 7, 3, // back 2, 3, 6, 6, 7, 3, // left 0, 1, 4, 4, 5, 1 // right }); // Add the constructers world.constructors.put( "ground", new Entity.ConstructInfo(groundMesh, 0f)); // mass = 0: static body world.constructors.put( "box", new Entity.ConstructInfo(boxMesh, 1f)); // mass = 1kg: dynamic body }