Efficient way of drawing in OpenGL ES

In my application I draw a lot of cubes through OpenGL ES Api. All the cubes are of same dimensions, only they are located at different coordinates in space. I can think of two ways of drawing them, but I am not sure which is the most efficient one. I am no OpenGL expert, so I decided to ask here.

Method 1, which is what I use now: Since all the cubes are of identical dimensions, I calculate vertex buffer, index buffer, normal buffer and color buffer only once. During a refresh of the scene, I go over all cubes, do bufferData() for same set of buffers and then draw the triangle mesh of the cube using drawElements() call. Since each cube is at different position, I translate the mvMatrix before I draw. bufferData() and drawElements() is executed for each cube. In this method, I probably save a lot of memory, by not calculating the buffers every time. But I am making lot of drawElements() calls.

Method 2 would be: Treat all cubes as set of polygons spread all over the scene. Calculate vertex, index, color, normal buffers for each polygon (actually triangles within the polygons) and push them to graphics card memory in single call to bufferData(). Then draw them with single call to drawElements(). The advantage of this approach is, I do only one bindBuffer and drawElements call. The downside is, I use lot of memory to create the buffers.

My experience with OpenGL is limited enough, to not know which one of the above methods is better from performance point of view.

I am using this in a WebGL app, but it's a generic OpenGL ES question.


I implemented method 2 and it wins by a landslide. The supposed downside of high amount of memory seemed to be only my imagination. In fact the garbage collector got invoked in method 2 only once, while it was invoked for 4-5 times in method 1.

Your OpenGL scenario might be different from mine, but if you reached here in search of performance tips, the lesson from this question is: Identify the parts in your scene that don't change frequently. No matter how big they are, put them in single buffer set (VBOs) and upload to graphics memory minimum number of times. That's how VBOs are meant to be used. The memory bandwidth between client (i.e. your app) and graphics card is precious and you don't want to consume it often without reason.

Read the section "Vertex Buffer Objects" in Ch. 6 of "OpenGL ES 2.0 Programming Guide" to understand how they are supposed to be used. http://opengles-book.com/

I know that this question is already answered, but I think it's worth pointing out the Google IO presentation about WebGL optimization:


They cover, essentially, this exact same issue (lot's of identical shapes with different colors/positions) and talk about some great ways to optimize such a scene (and theirs is dynamic too!)

I propose following approach:

On load:

  1. Generate coordinates buffer (for one cube) and load it into VBO (gl.glGenBuffers, gl.glBindBuffer)

On draw:

  1. Bind buffer (gl.glBindBuffer)
  2. Draw each cell (loop)

    2.1. Move current position to center of current cube (gl.glTranslatef(position.x, position.y, position.z)

    2.2. Draw current cube (gl.glDrawArrays)

    2.3. Move position back (gl.glTranslatef(-position.x, -position.y, -position.z))

Category:opengl es Time:2010-10-02 Views:1

Related post

  • Most efficient way to draw multiple identical objects? 2010-09-26

    I would like to make a game out of many cubes and am planning on putting it on mobile platforms and also on the web using webgl. My problem is when I make a drawelements call per cube I take a hit on the frame rate. Is there a way I can make a single

  • Most efficient way to draw 2d tiles in OpenGL 2012-04-10

    I'm writing a 2D iPad application where the main portion of the screen will be filled using 16x16 pixel tiles, so 3600 in total. The display doesn't have to update particularly smoothly (and only updates when it needs to), but I want the drawing to b

  • What is the most efficient way to draw voxels (cubes) in opengl? 2010-09-25

    I would like to draw voxels by using opengl but it doesn't seem like it is supported. I made a cube drawing function that had 24 vertices (4 vertices per face) but it drops the frame rate when you draw 2500 cubes. I was hoping there was a better way.

  • 2D Drawing In OpenGL ES 2.0 (iOS) 2010-12-26

    I'm learning how to use OpenGL ES 2.0 on iOS. Right now I want to just do some basic 2D animation (e.g. move a rectangle around the screen and change its size). I've started out with the project template for OpenGL ES provided by Apple in Xcode. My d

  • per pixel drawing in OpenGL 2011-10-22

    I used to write small games with 'per pixel' drawing, I mean with some SetPixel(x,y,color) function or such. I am also interested in OpenGL but do not know it much. Is it a good (fast) way to do a per pixel drawing in OpenGL ? It would be good for ex

  • iOS : need inputs in developing efficient ( performance wise ) drawing app 2012-02-13

    I have this app using which one can draw basic shapes like rectangle, eclipse, circle, text etc. I also allow free form drawing, which is stored as set-of-points, on the canvas. Also a user can resize and move around these objects by operating on the

  • Efficient way to draw into a java.awt.graphics object 2011-08-05

    I'm trying to create an animation by dynamically generating frames at each step of the animation. Now as I need to run an algorithm to draw pixel by pixel the new frame and I'm using a BufferedImage that I access through its raster data. However 90%

  • What is the most efficient way to draw a color array in GLUT / OpenGL? 2014-11-02

    I am writing my own 3D renderer engine. I use only one existing thing: create a window and draw a 2D color array into that window. Everything other is my own code, and the result is a(n ugly :-)) 3D game. The language is C++. To create a window and d

  • OpenGL - What is the most efficient way to draw an oval in OpenGL? 2011-04-28

    I have been using TRIANGLE_FAN and drawing like 90 triangles. What's a better way? --------------Solutions------------- Look at this page drawing. It explain 2 different methods. EDIT: I was forgetting about nurbs. You could also use them . Have a lo

  • Efficiency when rearranging arrays in OpenGL 2010-11-10

    Imagine I have a vertex array and an index array. Suppose it is possible to rearrange the elements in the index array so that a single call glDrawElements with GL_TRIANGLE_STRIP draws the desired figure. Another possibility would be to call glDrawEle

  • Efficient method to draw a line with millions of points 2011-01-31

    I'm writing an audio waveform editor in Cocoa with a wide range of zoom options. At its widest, it shows a waveform for an entire song (~10 million samples in view). At its narrowest, it shows a pixel accurate representation of the sound wave (~1 tho

  • More Efficient Method of Drawing Thousands of Particles (Java/Android) 2011-06-15

    So I'm writing a sort of particle simulator, like a "falling sand game" if you know what that is, and I've kind of hit a roadblock now. The way I'm doing this is I have a particle object that basically as of now has an position (int x, int y) and tha

  • Most efficient way to draw a bunch of 3d lines in matlab 2011-08-21

    I need to plot a list of 3d lines in matlab. What is the quickest way to do that? I am currently doing something like %edges is a MX2 matrix, holding the list of edges %points are the vertices' coordinates hold on; %so all the lines will be saved for

  • Most efficient way to draw part of an image in iOS 2011-11-07

    Given an UIImage and a CGRect, what is the most efficient way (in memory and time) to draw the part of the image corresponding to the CGRect (without scaling)? For reference, this is how I currently do it: - (void)drawRect:(CGRect)rect { CGContextRef

  • Efficiency of display lists in OpenGL with Python? 2011-12-12

    I have been teaching myself OpenGL programming using the Python wrapper PyOpenGL, and am now working on my first project with it. This project is a music visualizer (not dissimilar to whitecap) using many independently moving and independently colore

  • Are Paths the most efficient way to draw changing shapes? 2012-01-26

    The app I'm building has to draw around 400 quadrilateral shapes onto the screen with a fluent frame rate. The shapes 255 permutations and are drawn rotated anywhere from 0-360 degrees. I don't think pre-rendering them would efficient, given the amou

  • What's the most efficient way to draw a large CGPath? 2009-07-20

    Alright, I have a UIView which displays a CGPath which is rather wide. It can be scrolled within a UIScrollView horizontally. Right now, I have the view using a CATiledLayer, because it's more than 1024 pixels wide. So my question is: is this efficie

  • Fastest / most efficient way to draw moving speech bubbles on screen - CoreAnimation, Quartz2D? 2010-01-27

    I am adding some functionality to an iPhone app, and could use some help in picking the fastest / most efficient / best practice approach for solving this problem: At the upper-half of my screen, I have speech bubbles (think comic book) that are UIIm

  • Most efficient way to draw circles for polygon outlines 2010-06-14

    I'm using OpenGL and was told I should draw circles at each vertex of my outline to get smoothness. I tried this and it works great. The problem is speed. It crippled my application to draw a circle at each vertex. I'm not sure how else to fix the an

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.826 (s). 13 q(s)