private void setIndices () { final int w = width - 1; final int h = height - 1; short indices[] = new short[w * h * 6]; int i = -1; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { final int c00 = smooth ? (y * width + x) : (y * 2 * w + x * 2); final int c10 = c00 + 1; final int c01 = c00 + (smooth ? width : w * 2); final int c11 = c10 + (smooth ? width : w * 2); indices[++i] = (short)c11; indices[++i] = (short)c10; indices[++i] = (short)c00; indices[++i] = (short)c00; indices[++i] = (short)c01; indices[++i] = (short)c11; } } mesh.setIndices(indices); }
protected Renderable allocRenderable () { Renderable renderable = new Renderable(); renderable.meshPart.primitiveType = GL20.GL_TRIANGLES; renderable.meshPart.offset = 0; renderable.material = new Material(this.blendingAttribute, this.depthTestAttribute, TextureAttribute.createDiffuse(texture)); renderable.meshPart.mesh = new Mesh(false, MAX_VERTICES_PER_MESH, MAX_PARTICLES_PER_MESH * 6, currentAttributes); renderable.meshPart.mesh.setIndices(indices); renderable.shader = shader; return renderable; }
protected Renderable allocRenderable () { Renderable renderable = new Renderable(); renderable.meshPart.primitiveType = GL20.GL_TRIANGLES; renderable.meshPart.offset = 0; renderable.material = new Material(this.blendingAttribute, this.depthTestAttribute, TextureAttribute.createDiffuse(texture)); renderable.meshPart.mesh = new Mesh(false, MAX_VERTICES_PER_MESH, MAX_PARTICLES_PER_MESH * 6, currentAttributes); renderable.meshPart.mesh.setIndices(indices); renderable.shader = shader; return renderable; }
@Override public void flush () { if (vertexIndex == 0) return; renderCalls++; totalRenderCalls++; int trianglesInBatch = triangleIndex; if (trianglesInBatch > maxTrianglesInBatch) maxTrianglesInBatch = trianglesInBatch; lastTexture.bind(); Mesh mesh = this.mesh; mesh.setVertices(vertices, 0, vertexIndex); mesh.setIndices(triangles, 0, triangleIndex); if (blendingDisabled) { Gdx.gl.glDisable(GL20.GL_BLEND); } else { Gdx.gl.glEnable(GL20.GL_BLEND); if (blendSrcFunc != -1) Gdx.gl.glBlendFuncSeparate(blendSrcFunc, blendDstFunc, blendSrcFuncAlpha, blendDstFuncAlpha); } mesh.render(customShader != null ? customShader : shader, GL20.GL_TRIANGLES, 0, trianglesInBatch); vertexIndex = 0; triangleIndex = 0; }
@Override public void flush () { if (vertexIndex == 0) return; renderCalls++; totalRenderCalls++; int trianglesInBatch = triangleIndex; if (trianglesInBatch > maxTrianglesInBatch) maxTrianglesInBatch = trianglesInBatch; lastTexture.bind(); Mesh mesh = this.mesh; mesh.setVertices(vertices, 0, vertexIndex); mesh.setIndices(triangles, 0, triangleIndex); if (blendingDisabled) { Gdx.gl.glDisable(GL20.GL_BLEND); } else { Gdx.gl.glEnable(GL20.GL_BLEND); if (blendSrcFunc != -1) Gdx.gl.glBlendFuncSeparate(blendSrcFunc, blendDstFunc, blendSrcFuncAlpha, blendDstFuncAlpha); } mesh.render(customShader != null ? customShader : shader, GL20.GL_TRIANGLES, 0, trianglesInBatch); vertexIndex = 0; triangleIndex = 0; }
private void createQuad () { if (quad != null) return; quad = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.ColorUnpacked, 4, "a_color"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")); quad.setVertices(new float[] {-1, -1, 0, 1, 1, 1, 1, 0, 1, 1, -1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 0}); quad.setIndices(new short[] {0, 1, 2, 2, 3, 0}); }
/** Initializes the batch with the given amount of decal objects the buffer is able to hold when full. * * @param size Maximum size of decal objects to hold in memory */ public void initialize (int size) { vertices = new float[size * Decal.SIZE]; Mesh.VertexDataType vertexDataType = Mesh.VertexDataType.VertexArray; if(Gdx.gl30 != null) { vertexDataType = Mesh.VertexDataType.VertexBufferObjectWithVAO; } mesh = new Mesh(vertexDataType, false, size * 4, size * 6, new VertexAttribute( VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute( VertexAttributes.Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute( VertexAttributes.Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); short[] indices = new short[size * 6]; int v = 0; for (int i = 0; i < indices.length; i += 6, v += 4) { indices[i] = (short)(v); indices[i + 1] = (short)(v + 2); indices[i + 2] = (short)(v + 1); indices[i + 3] = (short)(v + 1); indices[i + 4] = (short)(v + 2); indices[i + 5] = (short)(v + 3); } mesh.setIndices(indices); }
/** Initializes the batch with the given amount of decal objects the buffer is able to hold when full. * * @param size Maximum size of decal objects to hold in memory */ public void initialize (int size) { vertices = new float[size * Decal.SIZE]; Mesh.VertexDataType vertexDataType = Mesh.VertexDataType.VertexArray; if(Gdx.gl30 != null) { vertexDataType = Mesh.VertexDataType.VertexBufferObjectWithVAO; } mesh = new Mesh(vertexDataType, false, size * 4, size * 6, new VertexAttribute( VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute( VertexAttributes.Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute( VertexAttributes.Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); short[] indices = new short[size * 6]; int v = 0; for (int i = 0; i < indices.length; i += 6, v += 4) { indices[i] = (short)(v); indices[i + 1] = (short)(v + 2); indices[i + 2] = (short)(v + 1); indices[i + 3] = (short)(v + 1); indices[i + 4] = (short)(v + 2); indices[i + 5] = (short)(v + 3); } mesh.setIndices(indices); }
/** Creates a cache with the specified size and OpenGL ES 2.0 shader. * @param size The maximum number of images this cache can hold. The memory required to hold the images is allocated up front. * Max of 8191 if indices are used. * @param useIndices If true, indexed geometry will be used. */ public SpriteCache (int size, ShaderProgram shader, boolean useIndices) { this.shader = shader; if (useIndices && size > 8191) throw new IllegalArgumentException("Can't have more than 8191 sprites per batch: " + size); mesh = new Mesh(true, size * (useIndices ? 4 : 6), useIndices ? size * 6 : 0, new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); mesh.setAutoBind(false); if (useIndices) { int length = size * 6; short[] indices = new short[length]; short j = 0; for (int i = 0; i < length; i += 6, j += 4) { indices[i + 0] = (short)j; indices[i + 1] = (short)(j + 1); indices[i + 2] = (short)(j + 2); indices[i + 3] = (short)(j + 2); indices[i + 4] = (short)(j + 3); indices[i + 5] = (short)j; } mesh.setIndices(indices); } projectionMatrix.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); }
@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(); }
/** Creates a cache with the specified size and OpenGL ES 2.0 shader. * @param size The maximum number of images this cache can hold. The memory required to hold the images is allocated up front. * Max of 8191 if indices are used. * @param useIndices If true, indexed geometry will be used. */ public SpriteCache (int size, ShaderProgram shader, boolean useIndices) { this.shader = shader; if (useIndices && size > 8191) throw new IllegalArgumentException("Can't have more than 8191 sprites per batch: " + size); mesh = new Mesh(true, size * (useIndices ? 4 : 6), useIndices ? size * 6 : 0, new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); mesh.setAutoBind(false); if (useIndices) { int length = size * 6; short[] indices = new short[length]; short j = 0; for (int i = 0; i < length; i += 6, j += 4) { indices[i + 0] = (short)j; indices[i + 1] = (short)(j + 1); indices[i + 2] = (short)(j + 2); indices[i + 3] = (short)(j + 2); indices[i + 4] = (short)(j + 3); indices[i + 5] = (short)j; } mesh.setIndices(indices); } projectionMatrix.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); }
@Override public void create () { String vertexShader = "attribute vec4 a_position; \n" + "attribute vec2 a_texCoord; \n" + "varying vec2 v_texCoord; \n" + "void main() \n" + "{ \n" + " gl_Position = a_position; \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" + "uniform sampler2D s_texture2; \n" + "void main() \n" + "{ \n" + " gl_FragColor = texture2D( s_texture, v_texCoord ) * texture2D( s_texture2, v_texCoord);\n" + "} \n"; shader = new ShaderProgram(vertexShader, fragmentShader); mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 2, "a_position"), new VertexAttribute( Usage.TextureCoordinates, 2, "a_texCoord")); float[] vertices = {-0.5f, 0.5f, // Position 0 0.0f, 0.0f, // TexCoord 0 -0.5f, -0.5f, // Position 1 0.0f, 1.0f, // TexCoord 1 0.5f, -0.5f, // Position 2 1.0f, 1.0f, // TexCoord 2 0.5f, 0.5f, // Position 3 1.0f, 0.0f // TexCoord 3 }; short[] indices = {0, 1, 2, 0, 2, 3}; mesh.setVertices(vertices); mesh.setIndices(indices); createTexture(); }
indices[i + 5] = j; mesh.setIndices(indices);
mesh.setIndices(indices);
private void createScreenQuad () { if (screenQuad != null) return; screenQuad = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.ColorUnpacked, 4, "a_color"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")); Vector3 vec0 = new Vector3(0, 0, 0); screenCamera.unproject(vec0); Vector3 vec1 = new Vector3(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0); screenCamera.unproject(vec1); screenQuad.setVertices(new float[] {vec0.x, vec0.y, 0, 1, 1, 1, 1, 0, 1, vec1.x, vec0.y, 0, 1, 1, 1, 1, 1, 1, vec1.x, vec1.y, 0, 1, 1, 1, 1, 1, 0, vec0.x, vec1.y, 0, 1, 1, 1, 1, 0, 0}); screenQuad.setIndices(new short[] {0, 1, 2, 2, 3, 0}); } }
mesh.setVertices(new float[] {-0.5f, -0.5f, 0, 1, 1, 1, 1, 0, 1, 0.5f, -0.5f, 0, 1, 1, 1, 1, 1, 1, 0.5f, 0.5f, 0, 1, 1, 1, 1, 1, 0, -0.5f, 0.5f, 0, 1, 1, 1, 1, 0, 0}); mesh.setIndices(new short[] {0, 1, 2, 2, 3, 0}); -0.5f, 0.5f, 0, Color.WHITE_FLOAT_BITS, toSingleFloat(0, 0) }); meshCustomVA.setIndices(new short[] {0, 1, 2, 2, 3, 0});
/** End building the mesh and returns the mesh * @param mesh The mesh to receive the built vertices and indices, must have the same attributes and must be big enough to hold * the data, any existing data will be overwritten. */ public Mesh end (Mesh mesh) { endpart(); if (attributes == null) throw new GdxRuntimeException("Call begin() first"); if (!attributes.equals(mesh.getVertexAttributes())) throw new GdxRuntimeException("Mesh attributes don't match"); if ((mesh.getMaxVertices() * stride) < vertices.size) throw new GdxRuntimeException("Mesh can't hold enough vertices: " + mesh.getMaxVertices() + " * " + stride + " < " + vertices.size); if (mesh.getMaxIndices() < indices.size) throw new GdxRuntimeException("Mesh can't hold enough indices: " + mesh.getMaxIndices() + " < " + indices.size); 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; }
/** End building the mesh and returns the mesh * @param mesh The mesh to receive the built vertices and indices, must have the same attributes and must be big enough to hold * the data, any existing data will be overwritten. */ public Mesh end (Mesh mesh) { endpart(); if (attributes == null) throw new GdxRuntimeException("Call begin() first"); if (!attributes.equals(mesh.getVertexAttributes())) throw new GdxRuntimeException("Mesh attributes don't match"); if ((mesh.getMaxVertices() * stride) < vertices.size) throw new GdxRuntimeException("Mesh can't hold enough vertices: " + mesh.getMaxVertices() + " * " + stride + " < " + vertices.size); if (mesh.getMaxIndices() < indices.size) throw new GdxRuntimeException("Mesh can't hold enough indices: " + mesh.getMaxIndices() + " < " + indices.size); 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; }
public void setupScene () { plane = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute( Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE)); plane.setVertices(new float[] {-10, -1, 10, 0, 1, 0, 10, -1, 10, 0, 1, 0, 10, -1, -10, 0, 1, 0, -10, -1, -10, 0, 1, 0}); plane.setIndices(new short[] {3, 2, 1, 1, 0, 3}); texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true); texture.setFilter(TextureFilter.MipMap, TextureFilter.Nearest); cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam.position.set(0, 5, 10); cam.lookAt(0, 0, 0); cam.update(); controller = new PerspectiveCamController(cam); projector = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); projector.position.set(2, 3, 2); projector.lookAt(0, 0, 0); projector.normalizeUp(); projector.update(); }
@Override public void create () { camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.position.set(0, 1.5f, 1.5f); camera.lookAt(0, 0, 0); camera.update(); controller = new PerspectiveCamController(camera); mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute( Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE)); mesh.setVertices(new float[] {-1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, -1, 1, 0, -1, 0, -1, 0, 0,}); mesh.setIndices(new short[] {0, 1, 2, 3}); shader = new ShaderProgram(Gdx.files.internal("data/shaders/flattex-vert.glsl").readString(), Gdx.files.internal( "data/shaders/flattex-frag.glsl").readString()); if (!shader.isCompiled()) throw new GdxRuntimeException("shader error: " + shader.getLog()); textureHW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true); MipMapGenerator.setUseHardwareMipMap(false); textureSW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true); currTexture = textureHW; createUI(); multiplexer = new InputMultiplexer(); Gdx.input.setInputProcessor(multiplexer); multiplexer.addProcessor(ui); multiplexer.addProcessor(controller); }