public void draw() { GL11 gl = getGLManager().getGL(); // bind( ); gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indices); gl.glDrawElements(GL10.GL_TRIANGLES, indices_length, GL10.GL_UNSIGNED_SHORT, 0); gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); }
public void drawMesh( BasicTexture tex, int x, int y, int xyBuffer, int uvBuffer, int indexBuffer, int indexCount) { float alpha = mAlpha; if (!bindTexture(tex)) return; mGLState.setBlendEnabled(mBlendEnabled && (!tex.isOpaque() || alpha < OPAQUE_ALPHA)); mGLState.setTextureAlpha(alpha); // Reset the texture matrix. We will set our own texture coordinates // below. setTextureCoords(0, 0, 1, 1); saveTransform(); translate(x, y); mGL.glLoadMatrixf(mMatrixValues, 0); mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, xyBuffer); mGL.glVertexPointer(2, GL11.GL_FLOAT, 0, 0); mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, uvBuffer); mGL.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); mGL.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBuffer); mGL.glDrawElements(GL11.GL_TRIANGLE_STRIP, indexCount, GL11.GL_UNSIGNED_BYTE, 0); mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBoxCoords); mGL.glVertexPointer(2, GL11.GL_FLOAT, 0, 0); mGL.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); restoreTransform(); mCountDrawMesh++; }
public void render(GL10 gl, Texture texture) { gl.glEnable(GL10.GL_TEXTURE_2D); texture.bind(gl); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); GL11 gl11 = (GL11) gl; // draw using hardware buffers gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexbufferId); gl11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, texturebufferId); gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexbufferId); gl11.glDrawElements(GL11.GL_TRIANGLES, indices.length, GL11.GL_UNSIGNED_SHORT, 0); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // if (vertexbufferId == 0 || texturebufferId == 0 || indexbufferId == 0 || gl11.glGetError() // != 0) { // Log.e("Grid", "VBO Not available: " + gl11.glGetError() + ", " + // gl11.glGetString(gl11.glGetError())); // } }
public void createBufferObjects(GL gl) { checkGLError(gl); // Generate a the vertex and element buffer IDs int[] vboIds = new int[2]; GL11 gl11 = (GL11) gl; gl11.glGenBuffers(2, vboIds, 0); mVertexBufferObjectId = vboIds[0]; mElementBufferObjectId = vboIds[1]; // Upload the vertex data gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertexBufferObjectId); mVertexByteBuffer.position(0); gl11.glBufferData( GL11.GL_ARRAY_BUFFER, mVertexByteBuffer.capacity(), mVertexByteBuffer, GL11.GL_STATIC_DRAW); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mElementBufferObjectId); mIndexBuffer.position(0); gl11.glBufferData( GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer.capacity() * CHAR_SIZE, mIndexBuffer, GL11.GL_STATIC_DRAW); // We don't need the in-memory data any more mVertexBuffer = null; mVertexByteBuffer = null; mIndexBuffer = null; checkGLError(gl); }
private void prepareBuffers(GLCanvas canvas) { mBufferNames = new int[3]; GL11 gl = canvas.getGLInstance(); GLId.glGenBuffers(3, mBufferNames, 0); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBufferNames[0]); gl.glBufferData( GL11.GL_ARRAY_BUFFER, mXyBuffer.capacity() * (Float.SIZE / Byte.SIZE), mXyBuffer, GL11.GL_STATIC_DRAW); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBufferNames[1]); gl.glBufferData( GL11.GL_ARRAY_BUFFER, mUvBuffer.capacity() * (Float.SIZE / Byte.SIZE), mUvBuffer, GL11.GL_STATIC_DRAW); gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mBufferNames[2]); gl.glBufferData( GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer.capacity(), mIndexBuffer, GL11.GL_STATIC_DRAW); // These buffers are never used again. mXyBuffer = null; mUvBuffer = null; mIndexBuffer = null; }
public void setVertices(float[] vertices, int offset, int length) { super.setVertices(vertices, offset, length); GL11 gl = (GL11) glGraphics.getGL(); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, vboHandle); gl.glBufferData( GL11.GL_ARRAY_BUFFER, this.vertices.limit() * 4, this.vertices, GL11.GL_STATIC_DRAW); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); }
public void onDrawFrame(GL10 gl10, boolean fpsTimeElapsed) { GL11 gl = (GL11) gl10; updateWithDelta(1.0f / MAXIMUM_UPDATE_RATE); synchronized (transforms) { if (fpsTimeElapsed) { onTransform(); } } sourcePosition.x = x; sourcePosition.y = y; gl.glMatrixMode(GL11.GL_MODELVIEW); gl.glLoadIdentity(); gl.glEnableClientState(GL11.GL_COLOR_ARRAY); // unbind all buffers gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); gl.glBindTexture(GL11.GL_TEXTURE_2D, 0); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, verticesID[0]); // Using glBufferSubData means that a copy is done from the quads array to the buffer rather // than recreating the buffer which // would be an allocation and copy. The copy also only takes over the number of live particles. // This provides a nice performance // boost. quadsBuffer.put(quads); quadsBuffer.position(0); gl.glBufferSubData(GL11.GL_ARRAY_BUFFER, 0, 128 * particleIndex, quadsBuffer); // Configure the vertex pointer which will use the currently bound VBO for its data gl.glVertexPointer(2, GL11.GL_FLOAT, 32, 0); gl.glColorPointer(4, GL11.GL_FLOAT, 32, (4 * 4)); gl.glTexCoordPointer(2, GL11.GL_FLOAT, 32, (4 * 2)); if (hasTexture) { gl.glEnable(GL11.GL_TEXTURE_2D); gl.glBindTexture(GL11.GL_TEXTURE_2D, getTexture().getTextureId()); } gl.glBlendFunc(srcBlendFactor, dstBlendFactor); gl.glDrawElements(GL11.GL_TRIANGLES, particleIndex * 6, GL11.GL_UNSIGNED_SHORT, indicesBuffer); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA); gl.glDisableClientState(GL11.GL_COLOR_ARRAY); }
private void initialize() { GL11 gl = mGL; // First create an nio buffer, then create a VBO from it. int size = BOX_COORDINATES.length * Float.SIZE / Byte.SIZE; FloatBuffer xyBuffer = allocateDirectNativeOrderBuffer(size).asFloatBuffer(); xyBuffer.put(BOX_COORDINATES, 0, BOX_COORDINATES.length).position(0); int[] name = new int[1]; GLId.glGenBuffers(1, name, 0); mBoxCoords = name[0]; gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBoxCoords); gl.glBufferData( GL11.GL_ARRAY_BUFFER, xyBuffer.capacity() * (Float.SIZE / Byte.SIZE), xyBuffer, GL11.GL_STATIC_DRAW); gl.glVertexPointer(2, GL11.GL_FLOAT, 0, 0); gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); // Enable the texture coordinate array for Texture 1 gl.glClientActiveTexture(GL11.GL_TEXTURE1); gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); gl.glClientActiveTexture(GL11.GL_TEXTURE0); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // mMatrixValues and mAlpha will be initialized in setSize() }
public void bind() { GL11 gl = (GL11) glGraphics.getGL(); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, vboHandle); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, vertexSize, 0); if (hasColor) { gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glColorPointer(4, GL10.GL_FLOAT, vertexSize, 3 * 4); } if (hasTexCoords) { gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glTexCoordPointer(2, GL10.GL_FLOAT, vertexSize, (hasColor ? 7 : 3) * 4); } if (hasNormals) { gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); int offset = 3; if (hasColor) offset += 4; if (hasTexCoords) offset += 2; gl.glNormalPointer(GL10.GL_FLOAT, vertexSize, offset * 4); } }
public static void bindBuffer(final GL11 pGL11, final int pHardwareBufferID) { /* Reduce unnecessary buffer switching calls. */ if (GLHelper.sCurrentHardwareBufferID != pHardwareBufferID) { GLHelper.sCurrentHardwareBufferID = pHardwareBufferID; pGL11.glBindBuffer(GL11.GL_ARRAY_BUFFER, pHardwareBufferID); } }
/** * Allocates hardware buffers on the graphics card and fills them with data if a buffer has not * already been previously allocated. Note that this function uses the GL_OES_vertex_buffer_object * extension, which is not guaranteed to be supported on every device. * * @param gl A pointer to the OpenGL ES context. */ public void generateHardwareBuffers(GL10 gl) { if (!mUseHardwareBuffers) { Log.i("Grid", "Using Hardware Buffers"); if (gl instanceof GL11) { GL11 gl11 = (GL11) gl; int[] buffer = new int[1]; // Allocate and fill the vertex buffer. gl11.glGenBuffers(1, buffer, 0); mVertBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex); final int vertexSize = mVertexBuffer.capacity() * mCoordinateSize; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertexSize, mVertexBuffer, GL11.GL_STATIC_DRAW); // Allocate and fill the texture coordinate buffer. gl11.glGenBuffers(1, buffer, 0); mTextureCoordBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex); final int texCoordSize = mTexCoordBuffer.capacity() * mCoordinateSize; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoordSize, mTexCoordBuffer, GL11.GL_STATIC_DRAW); // Unbind the array buffer. gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); // Allocate and fill the index buffer. gl11.glGenBuffers(1, buffer, 0); mIndexBufferIndex = buffer[0]; gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex); // A char is 2 bytes. final int indexSize = mIndexBuffer.capacity() * 2; gl11.glBufferData( GL11.GL_ELEMENT_ARRAY_BUFFER, indexSize, mIndexBuffer, GL11.GL_STATIC_DRAW); // Unbind the element array buffer. gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); mUseHardwareBuffers = true; assert mVertBufferIndex != 0; assert mTextureCoordBufferIndex != 0; assert mIndexBufferIndex != 0; assert gl11.glGetError() == 0; } } }
public void unbind() { GL11 gl = (GL11) glGraphics.getGL(); if (hasTexCoords) gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); if (hasColor) gl.glDisableClientState(GL10.GL_COLOR_ARRAY); if (hasNormals) gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); }
public void beginDrawingStrips(GL10 gl, boolean useTexture) { beginDrawing(gl, useTexture); if (!mUseHardwareBuffers) { gl.glVertexPointer(3, mCoordinateType, 0, mVertexBuffer); if (useTexture) { gl.glTexCoordPointer(2, mCoordinateType, 0, mTexCoordBuffer); } } else { GL11 gl11 = (GL11) gl; // draw using hardware buffers gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex); gl11.glVertexPointer(3, mCoordinateType, 0, 0); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex); gl11.glTexCoordPointer(2, mCoordinateType, 0, 0); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex); } }
public void draw(GL10 gl) { checkGLError(gl); GL11 gl11 = (GL11) gl; gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertexBufferObjectId); gl11.glVertexPointer(3, GL10.GL_FLOAT, VERTEX_SIZE, 0); gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); gl11.glNormalPointer( GL10.GL_FLOAT, VERTEX_SIZE, VERTEX_NORMAL_BUFFER_INDEX_OFFSET * FLOAT_SIZE); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mElementBufferObjectId); gl11.glDrawElements(GL10.GL_TRIANGLES, mIndexCount, GL10.GL_UNSIGNED_SHORT, 0); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); checkGLError(gl); }
@Override public void glBindBuffer(int target, int buffer) { gl.glBindBuffer(target, buffer); }
public void generateBuffers(GL10 gl) { ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); FloatBuffer vertexBuffer = vbb.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); // Set up Indexbuffer ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); ibb.order(ByteOrder.nativeOrder()); ShortBuffer indexBuffer = ibb.asShortBuffer(); indexBuffer.put(indices); indexBuffer.position(0); // Set up Texturebuffer ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4); tbb.order(ByteOrder.nativeOrder()); FloatBuffer textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(textureCoords); textureBuffer.position(0); // Initialize gl buffers if (gl instanceof GL11) { GL11 gl11 = (GL11) gl; int[] buffer = new int[1]; // Allocate and fill the vertex buffer. gl11.glGenBuffers(1, buffer, 0); vertexbufferId = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexbufferId); final int vertexSize = vertexBuffer.capacity() * 4; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL11.GL_STATIC_DRAW); // Allocate and fill the texture coordinate buffer. gl11.glGenBuffers(1, buffer, 0); texturebufferId = buffer[0]; gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, texturebufferId); final int texCoordSize = textureBuffer.capacity() * 4; gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoordSize, textureBuffer, GL11.GL_STATIC_DRAW); // Allocate and fill the index buffer. gl11.glGenBuffers(1, buffer, 0); indexbufferId = buffer[0]; gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexbufferId); // A short is 2 bytes. final int indexSize = indexBuffer.capacity() * 2; gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indexSize, indexBuffer, GL11.GL_STATIC_DRAW); // Unbind the element array buffer. gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); if (vertexbufferId == 0 || texturebufferId == 0 || indexbufferId == 0 || gl.glGetError() != 0) { Log.e( "Grid", "VBO Not available: " + gl.glGetError() + ", " + gl.glGetString(gl.glGetError())); } } }
@Override public void unbind() { GL11 gl = getGLManager().getGL(); gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); }
private void createQuadBuffer(GL10 gl10) { GL11 gl = (GL11) gl10; // // quad = ([vertex x, vertex y, texture x, texture y, red, green, blue, alpha] * 4) = 8 * 4 * // (float=4bytes) = 128 bytes // quads = new float[32 * maxParticles]; indices = new short[maxParticles * 6]; particles = new Particle[maxParticles]; for (int i = 0; i < maxParticles; i++) { indices[i * 6 + 0] = (short) (i * 4 + 0); indices[i * 6 + 1] = (short) (i * 4 + 1); indices[i * 6 + 2] = (short) (i * 4 + 2); indices[i * 6 + 3] = (short) (i * 4 + 2); indices[i * 6 + 4] = (short) (i * 4 + 3); indices[i * 6 + 5] = (short) (i * 4 + 0); } // initialize texture.x, texture.y for (int i = 0; i < maxParticles; i++) { int vi = i * 32; quads[vi + 0] = 0; // vertex x quads[vi + 1] = 0; // vertex y quads[vi + 2] = 0 + getTexelHalfX(); // texture x quads[vi + 3] = getTexture().getMaxT() + getTexelHalfY(); // texture y quads[vi + 4] = 0; // red quads[vi + 5] = 0; // green quads[vi + 6] = 0; // blue quads[vi + 7] = 0; // alpha // ----------------------------- quads[vi + 8] = 0; // vertex x quads[vi + 9] = 1; // vertex y quads[vi + 10] = 0 + getTexelHalfX(); quads[vi + 11] = 0 - getTexelHalfY(); quads[vi + 12] = 0; // red quads[vi + 13] = 0; // green quads[vi + 14] = 0; // blue quads[vi + 15] = 0; // alpha // ----------------------------- quads[vi + 16] = 1; // vertex x quads[vi + 17] = 1; // vertex y quads[vi + 18] = getTexture().getMaxS() - getTexelHalfX(); quads[vi + 19] = 0 - getTexelHalfY(); quads[vi + 20] = 0; // red quads[vi + 21] = 0; // green quads[vi + 22] = 0; // blue quads[vi + 23] = 0; // alpha // ----------------------------- quads[vi + 24] = 1; // vertex x quads[vi + 25] = 0; // vertex y quads[vi + 26] = getTexture().getMaxS() - getTexelHalfX(); quads[vi + 27] = getTexture().getMaxT() + getTexelHalfY(); quads[vi + 28] = 0; // red quads[vi + 29] = 0; // green quads[vi + 30] = 0; // blue quads[vi + 31] = 0; // alpha } quadsBuffer = GLHelper.createFloatBuffer(quads); // Generate the vertices VBO gl.glGenBuffers(1, verticesID, 0); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, verticesID[0]); gl.glBufferData(GL11.GL_ARRAY_BUFFER, 128 * maxParticles, quadsBuffer, GL11.GL_DYNAMIC_DRAW); gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); // By default the particle emitter is active when created active = true; // Set the particle count to zero particleCount = 0; // Reset the elapsed time elapsedTime = 0; indicesBuffer = GLHelper.createShortBuffer(indices); }
public void draw(GL10 gl, boolean useTexture) { if (!mUseHardwareBuffers) { gl.glVertexPointer(3, mCoordinateType, 0, mVertexBuffer); if (useTexture) { gl.glTexCoordPointer(2, mCoordinateType, 0, mTexCoordBuffer); } gl.glDrawElements(GL10.GL_TRIANGLES, mIndexCount, GL10.GL_UNSIGNED_SHORT, mIndexBuffer); } else { GL11 gl11 = (GL11) gl; // draw using hardware buffers gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex); gl11.glVertexPointer(3, mCoordinateType, 0, 0); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex); gl11.glTexCoordPointer(2, mCoordinateType, 0, 0); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex); gl11.glDrawElements(GL11.GL_TRIANGLES, mIndexCount, GL11.GL_UNSIGNED_SHORT, 0); gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); } }