protected void convertMesh(ModelMesh modelMesh) { int numIndices = 0; for (ModelMeshPart part : modelMesh.parts) { numIndices += part.indices.length; } VertexAttributes attributes = new VertexAttributes(modelMesh.attributes); int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4); Mesh mesh = new Mesh(true, numVertices, numIndices, attributes); meshes.add(mesh); disposables.add(mesh); BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0); int offset = 0; mesh.getIndicesBuffer().clear(); for (ModelMeshPart part : modelMesh.parts) { MeshPart meshPart = new MeshPart(); meshPart.id = part.id; meshPart.primitiveType = part.primitiveType; meshPart.offset = offset; meshPart.size = part.indices.length; meshPart.mesh = mesh; mesh.getIndicesBuffer().put(part.indices); offset += meshPart.size; meshParts.add(meshPart); } mesh.getIndicesBuffer().position(0); for (MeshPart part : meshParts) part.update(); }
@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(); }
public AnimationPart(float x, float y, float z, float w, float h, float d) { box = new BoundingBox(); modelMatrix = new Matrix4(); FloatArray fa = new FloatArray(); this.x = x; this.y = y; this.z = z; this.w = w; this.h = h; this.d = d; addTopFace(fa, 0, 0, 0, w, h, d); addBotFace(fa, 0, 0, 0, w, h, d); addLeftFace(fa, 0, 0, 0, w, h, d); addRightFace(fa, 0, 0, 0, w, h, d); addFrontFace(fa, 0, 0, 0, w, h, d); addBackFace(fa, 0, 0, 0, w, h, d); if (fa.size > 0) { partMesh = new Mesh( true, fa.size, 0, VertexAttributes.position, VertexAttributes.normal, VertexAttributes.textureCoords); partMesh.setVertices(fa.items); } setTexture(new Texture(Gdx.files.internal("data/grassmap.png"))); partMesh.calculateBoundingBox(box); updateModelMatrix(); }
private void convertMesh(ModelMesh modelMesh) { int numIndices = 0; for (ModelMeshPart part : modelMesh.parts) { numIndices += part.indices.length; } VertexAttributes attributes = new VertexAttributes(modelMesh.attributes); int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4); Mesh mesh = new Mesh(true, numVertices, numIndices, attributes); meshes.add(mesh); disposables.add(mesh); BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0); int offset = 0; mesh.getIndicesBuffer().clear(); for (ModelMeshPart part : modelMesh.parts) { MeshPart meshPart = new MeshPart(); meshPart.id = part.id; // FIXME not storing the mesh name, part ids may collide! meshPart.primitiveType = part.primitiveType; meshPart.indexOffset = offset; meshPart.numVertices = part.indices.length; meshPart.mesh = mesh; mesh.getIndicesBuffer().put(part.indices); offset += meshPart.numVertices; meshParts.add(meshPart); } mesh.getIndicesBuffer().position(0); }
private void renderMesh() { if (idx == 0) return; renderCalls++; totalRenderCalls++; int spritesInBatch = idx / 20; if (spritesInBatch > maxSpritesInBatch) maxSpritesInBatch = spritesInBatch; lastTexture.bind(); mesh.setVertices(vertices, 0, idx); if (blendingDisabled) { Gdx.gl.glDisable(GL20.GL_BLEND); } else { Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(blendSrcFunc, blendDstFunc); } if (Gdx.graphics.isGL20Available()) { if (customShader != null) mesh.render(customShader, GL10.GL_TRIANGLES, 0, spritesInBatch * 6); else mesh.render(shader, GL10.GL_TRIANGLES, 0, spritesInBatch * 6); } else { mesh.render(GL10.GL_TRIANGLES, 0, spritesInBatch * 6); } idx = 0; currBufferIdx++; if (currBufferIdx == buffers.length) currBufferIdx = 0; mesh = buffers[currBufferIdx]; }
private void renderPlanet( GL10 gl, String texture, boolean uvtype, float radius, float x, float y, float z, Application app) { radius = radius * worldscale; if (texture.equals("earth")) { // render earth/jupiter texture planetTexture.bind(); } else if (texture.equals("sun")) { // render mars/sun texture planetTexture.bind(); } gl.glPushMatrix(); // move away from origin gl.glTranslatef(x, y, z); // scale to 10% size of earth gl.glScalef(radius, radius, radius); gl.glRotatef((Splash.planetmove / 10) - 180, 0, 1, 0); if (uvtype == true) { // render lower planet texture planetMesh.render(GL10.GL_TRIANGLES); } else { // render upper planet texture planetMesh02.render(GL10.GL_TRIANGLES); } gl.glPopMatrix(); }
public void clearManagedCaches() { Mesh.clearAllMeshes(app); Texture.clearAllTextures(app); ShaderProgram.clearAllShaderPrograms(app); FrameBuffer.clearAllFrameBuffers(app); Gdx.app.log("AndroidGraphics", Mesh.getManagedStatus()); Gdx.app.log("AndroidGraphics", Texture.getManagedStatus()); Gdx.app.log("AndroidGraphics", ShaderProgram.getManagedStatus()); Gdx.app.log("AndroidGraphics", FrameBuffer.getManagedStatus()); }
public void dispose() { sphere.dispose(); steel.dispose(); plane.dispose(); wood.dispose(); wall.dispose(); green.dispose(); maja.dispose(); pink.dispose(); black.dispose(); }
@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); }
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; }
public void rebuild() { FloatArray fa = new FloatArray(); addTopFace(fa, 0, 0, 0, w, h, d); addBotFace(fa, 0, 0, 0, w, h, d); addLeftFace(fa, 0, 0, 0, w, h, d); addRightFace(fa, 0, 0, 0, w, h, d); addFrontFace(fa, 0, 0, 0, w, h, d); addBackFace(fa, 0, 0, 0, w, h, d); if (fa.size > 0) { partMesh.setVertices(fa.items); } partMesh.calculateBoundingBox(box); updateModelMatrix(); }
public void clearManagedCaches() { Mesh.clearAllMeshes(app); Texture.clearAllTextures(app); ShaderProgram.clearAllShaderPrograms(app); FrameBuffer.clearAllFrameBuffers(app); if (AndroidLiveWallpaperService .DEBUG) { // to prevent creating too many string buffers in live wallpapers Gdx.app.debug("AndroidGraphics", Mesh.getManagedStatus()); Gdx.app.debug("AndroidGraphics", Texture.getManagedStatus()); Gdx.app.debug("AndroidGraphics", ShaderProgram.getManagedStatus()); Gdx.app.debug("AndroidGraphics", FrameBuffer.getManagedStatus()); } }
@Override public void render() { Gdx.gl10.glPushMatrix(); Gdx.gl10.glTranslatef(x, y, z); mesh.render(GL10.GL_TRIANGLES); Gdx.gl10.glPopMatrix(); }
@Override public void create() { mesh = new Mesh( true, 3, 0, new VertexAttribute(Usage.Position, 3, "a_Position"), new VertexAttribute(Usage.ColorPacked, 4, "a_Color"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")); float c1 = Color.toFloatBits(255, 0, 0, 255); float c2 = Color.toFloatBits(255, 0, 0, 255); ; float c3 = Color.toFloatBits(0, 0, 255, 255); ; mesh.setVertices( new float[] {-0.5f, -0.5f, 0, c1, 0, 0, 0.5f, -0.5f, 0, c2, 1, 0, 0, 0.5f, 0, c3, 0.5f, 1}); texture = new Texture(Gdx.files.internal("data/badlogic.jpg")); spriteBatch = new SpriteBatch(); frameBuffer = new FrameBuffer(Format.RGB565, 128, 128, true); createShader(Gdx.graphics); }
public void updateModelMatrix() { modelMatrix.setToTranslation(x, y, z); modelMatrix.rotate(1, 0, 0, rotationX); modelMatrix.rotate(0, 0, 1, rotationZ); modelMatrix.rotate(0, 1, 0, rotationY); partMesh.calculateBoundingBox(box); }
public void dispose() { try { mMesh.dispose(); } catch (Exception e) { System.out.println(mMesh + e.getMessage()); } }
/** Call this after scene. Renders the bloomed scene. */ public void render() { if (capturing) { capturing = false; frameBuffer.end(); } Gdx.gl.glDisable(GL20.GL_BLEND); Gdx.gl.glDisable(GL20.GL_DEPTH_TEST); Gdx.gl.glDepthMask(false); gaussianBlur(); if (blending) { Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); } pingPongTex1.bind(1); original.bind(0); bloomShader.begin(); { fullScreenQuad.render(bloomShader, GL20.GL_TRIANGLE_FAN); } bloomShader.end(); }
@Override public void render() { frameBuffer.begin(); Gdx.graphics.getGL20().glViewport(0, 0, frameBuffer.getWidth(), frameBuffer.getHeight()); Gdx.graphics.getGL20().glClearColor(0f, 1f, 0f, 1); Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.graphics.getGL20().glEnable(GL20.GL_TEXTURE_2D); texture.bind(); meshShader.begin(); meshShader.setUniformi("u_texture", 0); mesh.render(meshShader, GL20.GL_TRIANGLES); meshShader.end(); frameBuffer.end(); Gdx.graphics.getGL20().glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.graphics.getGL20().glClearColor(0.2f, 0.2f, 0.2f, 1); Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT); spriteBatch.begin(); spriteBatch.draw( frameBuffer.getColorBufferTexture(), 0, 0, 256, 256, 0, 0, frameBuffer.getColorBufferTexture().getWidth(), frameBuffer.getColorBufferTexture().getHeight(), false, true); spriteBatch.end(); }
private void gaussianBlur() { // cut bright areas of the picture and blit to smaller fbo original.bind(0); pingPongBuffer1.begin(); { tresholdShader.begin(); { // tresholdShader.setUniformi("u_texture0", 0); fullScreenQuad.render(tresholdShader, GL20.GL_TRIANGLE_FAN, 0, 4); } tresholdShader.end(); } pingPongBuffer1.end(); for (int i = 0; i < blurPasses; i++) { pingPongTex1.bind(0); // horizontal pingPongBuffer2.begin(); { blurShader.begin(); { blurShader.setUniformf("dir", 1f, 0f); fullScreenQuad.render(blurShader, GL20.GL_TRIANGLE_FAN, 0, 4); } blurShader.end(); } pingPongBuffer2.end(); pingPongTex2.bind(0); // vertical pingPongBuffer1.begin(); { blurShader.begin(); { blurShader.setUniformf("dir", 0f, 1f); fullScreenQuad.render(blurShader, GL20.GL_TRIANGLE_FAN, 0, 4); } blurShader.end(); } pingPongBuffer1.end(); } }
/** 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; }
public void render(Application app, boolean hit) { app.modelViewProjectionMatrix.set(app.cam.combined); app.modelViewProjectionMatrix.mul(modelMatrix); if (hit) app.shader.setUniformi("s_hit", 1); else app.shader.setUniformi("s_hit", 0); texture.bind(0); app.shader.setUniformi("s_texture", 0); app.shader.setUniformMatrix("u_mvpMatrix", app.modelViewProjectionMatrix); partMesh.render(app.shader, GL20.GL_TRIANGLES); }
@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 void render() { Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0); texture.bind(); shader.begin(); shader.setUniformf("s_texture", 0); Gdx.gl20.glTexParameteri(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_NEAREST); shader.setUniformf("u_offset", -0.6f); mesh.render(shader, GL20.GL_TRIANGLES); Gdx.gl20.glTexParameteri( GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_LINEAR_MIPMAP_LINEAR); shader.setUniformf("u_offset", 0.6f); mesh.render(shader, GL20.GL_TRIANGLES); shader.end(); }
/** Call this when application is exiting. */ public void dispose() { if (disposeFBO) frameBuffer.dispose(); fullScreenQuad.dispose(); pingPongBuffer1.dispose(); pingPongBuffer2.dispose(); blurShader.dispose(); bloomShader.dispose(); tresholdShader.dispose(); }
private void renderStaticShip(GL10 gl, Application app) { shipTexture.bind(); gl.glPushMatrix(); gl.glTranslatef(0, 0, 0); shipMesh.render(GL10.GL_TRIANGLES); gl.glPopMatrix(); float noise = (float) Math.random() / 2; renderJet(gl, 2.3f, 1f + noise, 1.1f, -.5f, 1.8f, app); noise = (float) Math.random() / 2; renderJet(gl, 2.3f, 1f + noise, -1.1f, -.5f, 1.8f, app); }
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; }
private Mesh createFullscreenQuad() { // vertex coord verts[X1] = -1; verts[Y1] = -1; verts[X2] = 1; verts[Y2] = -1; verts[X3] = 1; verts[Y3] = 1; verts[X4] = -1; verts[Y4] = 1; // tex coords verts[U1] = 0f; verts[V1] = 0f; verts[U2] = 1f; verts[V2] = 0f; verts[U3] = 1f; verts[V3] = 1f; verts[U4] = 0f; verts[V4] = 1f; Mesh tmpMesh = new Mesh( VertexDataType.VertexArray, true, 4, 0, new VertexAttribute(Usage.Position, 2, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0")); tmpMesh.setVertices(verts); return tmpMesh; }
private void renderSky(GL10 gl) { gl.glDisable(GL10.GL_LIGHTING); skyTexture.bind(); gl.glColor4f(1, 1, 1, 1); gl.glPushMatrix(); gl.glTranslatef(0, 0, 0); gl.glScalef(100f, 100f, 100f); skyMesh.render(GL10.GL_TRIANGLES); gl.glPopMatrix(); gl.glEnable(GL10.GL_LIGHTING); }
@Override public void onSurfaceCreated(javax.microedition.khronos.opengles.GL10 gl, EGLConfig config) { eglContext = ((EGL10) EGLContext.getEGL()).eglGetCurrentContext(); setupGL(gl); logConfig(config); updatePpi(); Mesh.invalidateAllMeshes(app); Texture.invalidateAllTextures(app); ShaderProgram.invalidateAllShaderPrograms(app); FrameBuffer.invalidateAllFrameBuffers(app); Gdx.app.log("AndroidGraphics", Mesh.getManagedStatus()); Gdx.app.log("AndroidGraphics", Texture.getManagedStatus()); Gdx.app.log("AndroidGraphics", ShaderProgram.getManagedStatus()); Gdx.app.log("AndroidGraphics", FrameBuffer.getManagedStatus()); Display display = app.getWindowManager().getDefaultDisplay(); this.width = display.getWidth(); this.height = display.getHeight(); mean = new WindowedMean(5); this.lastFrameTime = System.nanoTime(); gl.glViewport(0, 0, this.width, this.height); isSurfaceCreated = true; }