title: OpenGLES - Create objects at runtime
body: <p>I'm new to OpenGL and started with the small tutorial from dev.android.com. The sample code includes this <code>Square</code> class for a square geometry. The object will be created in the <code>onSurfaceCreated()</code> method and drawn every frame using <code>onDrawFrame()</code>. Here is the example code of the <code>Square</code> (constructor and draw-method):</p>
  
  <pre><code>   public Square() {
      // initialize vertex byte buffer for shape coordinates
  
      ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
      bb.order(ByteOrder.nativeOrder());
      vertexBuffer = bb.asFloatBuffer();
      vertexBuffer.put(squareCoords);
      vertexBuffer.position(0);
  
      // initialize byte buffer for the draw list
      ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
      dlb.order(ByteOrder.nativeOrder());
      drawListBuffer = dlb.asShortBuffer();
      drawListBuffer.put(drawOrder);
      drawListBuffer.position(0);
  
      // prepare shaders and OpenGL program
      int vertexShader = MyGLRenderer.loadShader(
              GLES20.GL_VERTEX_SHADER,
              vertexShaderCode);
  
      int fragmentShader = MyGLRenderer.loadShader(
              GLES20.GL_FRAGMENT_SHADER,
              fragmentShaderCode);
  
      mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
      GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
      GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
      GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables
  }
  
  
  public void draw(float[] mvpMatrix) {
      // Add program to OpenGL environment
      GLES20.glUseProgram(mProgram);
  
      // get handle to vertex shader's vPosition member
      mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
  
      // Enable a handle to the triangle vertices
      GLES20.glEnableVertexAttribArray(mPositionHandle);
  
      // Prepare the triangle coordinate data
      GLES20.glVertexAttribPointer(
              mPositionHandle, COORDS_PER_VERTEX,
              GLES20.GL_FLOAT, false,
              vertexStride, vertexBuffer);
  
      // get handle to fragment shader's vColor member
      mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
  
      // Set color for drawing the triangle
      GLES20.glUniform4fv(mColorHandle, 1, color, 0);
  
      // get handle to shape's transformation matrix
      mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
      MyGLRenderer.checkGlError("glGetUniformLocation");
  
      // Apply the projection and view transformation
      GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
      MyGLRenderer.checkGlError("glUniformMatrix4fv");
  
      // Draw the square
      GLES20.glDrawElements(
              GLES20.GL_TRIANGLES, drawOrder.length,
              GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
  
      // Disable vertex array
      GLES20.glDisableVertexAttribArray(mPositionHandle);
  }
  </code></pre>
  
  <p>My question is now: how can I create the object not at <code>onSurfaceCreated()</code> but after a touch event? </p>
  
  <p>I tried to define a <code>Square</code> variable but not initialize it at <code>onSurfaceCreated()</code>, then check if the object is null before drawing it. After the touch I called: </p>
  
  <pre><code>mSquare = new Square();
  </code></pre>
  
  <p>I know it's not a good way of implementing this, but I just wanted to try if it works. I would have created a list of drawable elements and run through it in the <code>onDrawFrame()</code> method, calling every <code>draw()</code> from the list objects. But since this method causes the program to crash, I don't know how to go on.</p>