title: Mesh doesn't get drawn if it's added from certain method of a certain class (OpenGL ES 2)
body: <p>So, I'm trying to make a simple 3d engine based on OpenGL ES 2 (Android). First (apart from many tutorials and such), I copy-pasted code from <a href="https://developer.android.com/guide/topics/graphics/opengl.html" rel="nofollow" title="developer.android.com tutorial">developer.android.com tutorial</a> step-by-step. Everything worked fine. Then I started to modify it. I changed Mesh class' fields and constructor so that vertex coordinates, shader codes, color and number of vertices are not preset (all the code is at the end of the question).
  Changed a Mesh field in GL20Renderer class to <code>private List&lt;Mesh&gt; meshs = new ArrayList&lt;Mesh&gt;();</code>. Created functions for adding and removing meshs. The only add function used at the moment in my code is the following:</p>
  
  <pre><code>public void addMesh( Mesh meshToAdd ) {
    meshs.add( meshToAdd );
  }
  </code></pre>
  
  <p>The only change in GL20Renderer.onDrawFrame() is that the code there loops through mesh list and calls Mesh.draw() method for every mesh:</p>
  
  <pre><code>for( Mesh meshToDraw : meshs ) {
    meshToDraw.draw(scratch);
  }
  </code></pre>
  
  <p>Then I tried to add one mesh from GL20Renderer.onSurfaceCreated() method - it worked fine. Add another mesh from the same method - works fine.
  And then I tried to add a mesh from GL20Activity.onCreate() method (while removing the same code from GL20Renderer.onSurfaceCreated()):</p>
  
  <pre><code>float[] vertexCoords = { 0.0f, 0.622008459f, 0.0f, -0.5f, -0.311004243f, 0.0f, 0.5f, -0.311004243f, 0.0f };
  int[] drawOrder = { 1, 2, 3 };
  float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
  String vertexShaderCode = "uniform mat4 uMVPMatrix;" + "attribute vec4 vPosition;" + "void main() {" + "  gl_Position = uMVPMatrix * vPosition;" + "}";
  String fragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() {" + "  gl_FragColor = vColor;" + "}";
  renderer.addMesh( new Mesh( 3, vertexCoords, drawOrder, 1, color, vertexShaderCode, fragmentShaderCode) );
  </code></pre>
  
  <p>Mesh gets added to mesh list but doesn't get displayed for some reason (it's properties are the same as the tutorial's mesh's ones). If meshs are added from both GL20Activity.onCreate() and Renderer.onSurfaceCreated() - none of them gets displayed, but they're still on mesh list. Then I added onRendererInitialized() method to GL20Activity, which is called from the very end of GL20Renderer.onCreate() method. onRendererInited() just adds a mesh, and it gets displayed, but only if no mesh is added from GL20Activity.onCreate().
    The question is: Why does that mesh not get drawn if it's added from GL20Activity.onCreate() method? I tried changing vertices' coordinates so they are not the cause of a problem. I have also tried to add some code to GL20Renderer.onDrawFrame() to make sure that <code>meshToDraw.draw( scratch );</code> line of code runs properly, and it does run properly, but still mesh doesn't get drawn. But since mesh with same vertices' coordinates and color added from GL20Activity.onRendererInitialized() and/or from GL20Renderer.onSurfaceCreated() gets drawn, there shouldn't be any problem with drawing mesh, which is added from GL20Activity.onCreate().</p>
  
  <p>MainActivity.java:</p>
  
  
  
  <pre><code>package com.Reaper.VisionEngine;
  import android.app.*;
  import android.content.*;
  import android.os.*;
  
  public class MainActivity extends Activity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState {
      super.onCreate(savedInstanceState);
      Intent intent = new Intent( this, GL20Activity.class );
      startActivity( intent );
    }
  }
  </code></pre>
  
  <p>GL20Activity.java:</p>
  
  
  
  <pre><code>package com.Reaper.VisionEngine;
  import android.app.*;
  import android.os.*;
  import android.widget.*;
  import com.Reaper.VisionEngine.GLRenderer.*;
  import com.Reaper.VisionEngine.GLSurfaceView.*;
  import com.Reaper.VisionEngine.Mesh.*;
  import java.io.*;
  
  public class GL20Activity extends Activity {
    GL20SurfaceView GLView;
    GL20Renderer renderer;
  
    @Override
    protected void onCreate( Bundle savedInstanceState ) {
      super.onCreate(savedInstanceState);
      GLView = new GL20SurfaceView(this);
      setContentView(GLView);
      renderer = GLView.getRenderer();
      float[] vertexCoords = { 0.0f, 0.622008459f, 0.0f, -0.5f, -0.311004243f, 0.0f, 0.5f, -0.311004243f, 0.0f };
      int[] drawOrder = { 1, 2, 3 };
      float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
      String vertexShaderCode = "uniform mat4 uMVPMatrix;" + "attribute vec4 vPosition;" + "void main() {" + "  gl_Position = uMVPMatrix * vPosition;" + "}";
      String fragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() {" + "  gl_FragColor = vColor;" + "}";
      renderer.addMesh( new Mesh( 3, vertexCoords, drawOrder, 1, color, vertexShaderCode, fragmentShaderCode) );
    }
  </code></pre>
  
  <p>GL20Renderer.java:</p>
  
  
  
  <pre><code>package com.Reaper.VisionEngine.GLRenderer;
  import android.opengl.*;
  import com.Reaper.VisionEngine.*;
  import com.Reaper.VisionEngine.Mesh.*;
  import java.util.*;
  import javax.microedition.khronos.egl.*;
  import javax.microedition.khronos.opengles.*;
  import javax.microedition.khronos.egl.EGLConfig;
  
  public class GL20Renderer implements GLSurfaceView.Renderer {
    private List&lt;Mesh&gt; meshs = new ArrayList&lt;Mesh&gt;();
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];
    private float[] mRotationMatrix = new float[16];
    public volatile float mAngle;
    private List&lt;Mesh&gt; meshQueue = new ArrayList&lt;Mesh&gt;();
    private GL20Activity activity;
  
    public void onSurfaceCreated(GL10 gl, EGLConfig glConfig) {
      GLES20.glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
    }
  
    public void onDrawFrame(GL10 gl) {
      GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
      Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
      Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
      float[] scratch = new float[16];
      Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
      Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
      for( Mesh meshToDraw : meshs ) {
        meshToDraw.draw(scratch);
      }
    }
  
    public void onSurfaceChanged(GL10 gl, int width, int height) {
      GLES20.glViewport(0, 0, width, height);
      float ratio = (float) width / height;
      Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
    }
  
    public static int loadShader(int type, String shaderCode){
      int shader = GLES20.glCreateShader(type);
      GLES20.glShaderSource(shader, shaderCode);
      GLES20.glCompileShader(shader);
      return shader;
    }
  
    public void addMesh( Mesh meshToAdd ) {
      meshs.add( meshToAdd );
    }
  </code></pre>
  
  <p>GL20SurfaceView.java:</p>
  
  
  
  <pre><code>package com.Reaper.VisionEngine.GLSurfaceView;
  import android.content.*;
  import android.opengl.*;
  import android.view.*;
  import com.Reaper.VisionEngine.*;
  import com.Reaper.VisionEngine.GLRenderer.*;
  
  public class GL20SurfaceView extends GLSurfaceView {
    private final GL20Renderer Renderer;
  
    public GL20SurfaceView(Context context) {
      super(context);
      setEGLContextClientVersion(2);
      Renderer = new GL20Renderer();
      setRenderer(Renderer);
      setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
  
    public boolean onTouchEvent(MotionEvent e) { // TODO: CHANGE THIS
      switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
        requestRender();
      }
      return true;
    }
  
    public GL20Renderer getRenderer() {
      return Renderer;
    }
  }
  </code></pre>
  
  <p>Mesh.java:</p>
  
  
  
  <pre><code>package com.Reaper.VisionEngine.Mesh;
  import android.opengl.*;
  import com.Reaper.VisionEngine.GLRenderer.*;
  import java.nio.*;
  
  public class Mesh {
    private FloatBuffer vertexBuffer;
    public final int GL20Program;
    private int PositionHandle;
    private int ColorHandle;
    private int MVPMatrixHandle;
    private int colorOverridesTexture = 0;
    private int vertexCount = 1;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
    private int[] drawOrder; // TODO: Implement
    static final int COORDS_PER_VERTEX = 3;
    private float vertexCoords[];
    float color[] = new float[3];
    private String vertexShaderCode;
    private String fragmentShaderCode;
    private final String defaultVertexShaderCode = "uniform mat4 uMVPMatrix;" + "attribute vec4 vPosition;" + "void main() }" + "  gl_Position = uMVPMatrix * vPosition;" + "}";
    private final String defaultFragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() }" + "  gl_FragColor = vColor;" + "}";
  
    public Mesh( int newVertexCount, float[] newVertexCoords, int[] newDrawOrder, int useColor, float[] newColor, String newVertexShaderCode, String newFragmentShaderCode ) {
      vertexCount = newVertexCount;
      vertexCoords = newVertexCoords;
      drawOrder = newDrawOrder;
      colorOverridesTexture = useColor;
      color = newColor;
      vertexShaderCode = newVertexShaderCode;
      fragmentShaderCode = newFragmentShaderCode;
      ByteBuffer bb = ByteBuffer.allocateDirect( vertexCoords.length * 4 );
      bb.order(ByteOrder.nativeOrder());
      vertexBuffer = bb.asFloatBuffer();
      vertexBuffer.put( vertexCoords );
      vertexBuffer.position( 0 );
      int vertexShader = GL20Renderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
      int fragmentShader = GL20Renderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
      GL20Program = GLES20.glCreateProgram();
      GLES20.glAttachShader(GL20Program, vertexShader);
      GLES20.glAttachShader(GL20Program, fragmentShader);
      GLES20.glLinkProgram(GL20Program);
    }
  
    public void draw(float[] mvpMatrix) {
      MVPMatrixHandle = GLES20.glGetUniformLocation(GL20Program, "uMVPMatrix");
      GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
      GLES20.glUseProgram(GL20Program);
      PositionHandle = GLES20.glGetAttribLocation(GL20Program, "vPosition");
      GLES20.glEnableVertexAttribArray(PositionHandle);
      GLES20.glVertexAttribPointer(PositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
      ColorHandle = GLES20.glGetUniformLocation(GL20Program, "vColor");
      GLES20.glUniform4fv(ColorHandle, 1, color, 0);
      GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
      GLES20.glDisableVertexAttribArray(PositionHandle);
    }
  }
  </code></pre>
  
  <p>Sorry for bad english.</p>