By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

draw trinagle in android openGL 2.0

P: 59
I am trying to create a triangle in openGL 2.0 but some some reason background is shows up but no triangle. I am following this tutorial: http://developer.android.com/trainin...vironment.html
i have the same code as on tutorial but for some reason the triangle is not showing up.
may be i missed some thing or i am doing some thing wrong?

i have two class.
GlRender.java
GLTriangle.java

i think the problem is in GLTriangle draw method.

Expand|Select|Wrap|Line Numbers
  1. public class GlRender implements Renderer {
  2.  
  3.     private GLTriangle glTriangleObj;
  4.  
  5.     private final float[] mModelMatrix = new float[16];
  6.     private final float[] mProjectionMatrix = new float[16];
  7.     private final float[] mViewMatrix = new float[16];
  8.     private final float[] mRotationMatrix = new float[16];
  9.  
  10.     /************************/
  11.     /*** create() method ***/
  12.     /************************/
  13.     @Override
  14.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  15.         // Set the background color
  16.         // 0 to 1
  17.         // 1 = no transparent
  18.         GLES20.glClearColor(0.8f, 0.2f, 0.2f, 1f);
  19.         gl.glClearDepthf(1f);
  20.  
  21.         glTriangleObj = new GLTriangle();
  22.         // glSquare = new Square(); //etc...
  23.  
  24.         // postion the eye behind the origin
  25.         final float eyeX = 0.0f;
  26.         final float eyeY = 0.0f;
  27.         final float eyeZ = 1.5f;
  28.  
  29.         // we are looking toward the distance
  30.         final float lookX = 0.0f;
  31.         final float lookY = 0.0f;
  32.         final float lookZ = -5.0f;
  33.  
  34.         // Set our up vector. This is where our head would be pointing were we
  35.         // holding the camera.
  36.         final float upX = 0.0f;
  37.         final float upY = 1.0f;
  38.         final float upZ = 0.0f;
  39.  
  40.         // Set the view matrix. This matrix can be said to represent the camera
  41.         // position.
  42.         // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination
  43.         // of a model and
  44.         // view matrix. In OpenGL 2, we can keep track of these matrices
  45.         // separately if we choose.
  46.         Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY,
  47.                 lookZ, upX, upY, upZ);
  48.     }
  49.  
  50.     /********************/
  51.     /*** paint method ***/
  52.     /********************/
  53.     @Override
  54.     public void onDrawFrame(GL10 gl) {
  55.         // Redraw background color
  56.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  57.  
  58.         // do a complete rotation every 10 seconds
  59.         // long time = SystemClock.uptimeMillis() % 10000L;
  60.         // float angleInDegree = (360.0f / 10000.0f) * ((int) time);
  61.         // draw the triangle face straight on
  62.         // Matrix.setIdentityM(mModelMatrix, 0);
  63.         // Matrix.rotateM(mModelMatrix, 0, angleInDegree, 0.0f, 0.0f, 1.0f);
  64.  
  65.         // Set the camera position (View matrix)
  66.         Matrix.setLookAtM(mViewMatrix, 0, 0, -2, 0, 0f, 0f, 0f, 0f, 2.0f, 0.0f);
  67.  
  68.         // Calculate the projection and view transformation
  69.         Matrix.multiplyMM(mModelMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
  70.  
  71.         glTriangleObj.draw(mModelMatrix);
  72.     }
  73.  
  74.     /*****************************/
  75.     /*** landscape to portrait ***/
  76.     /*****************************/
  77.     @Override
  78.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  79.         if (height == 0)
  80.             height = 1;
  81.  
  82.         // Set the OpenGL viewport to the same size as the surface.
  83.         GLES20.glViewport(0, 0, width, height);
  84.  
  85.         // while the width will vary as per aspect ratio.
  86.         final float ratio = (float) width / height;
  87.         final float left = -ratio;
  88.         final float right = ratio;
  89.         final float bottom = -1.0f;
  90.         final float top = 1.0f;
  91.         final float near = 1.0f;
  92.         final float far = 25.0f;
  93.  
  94.         Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near,
  95.                 far);
  96.     }
  97. }
  98.  
  99.  


GLTriangle.java

Expand|Select|Wrap|Line Numbers
  1. public class GLTriangle {
  2.     // always same
  3.     private final String vertexShaderCode = "attribute vec4 vPosition;"
  4.             + "void main() {" + "  gl_Position = vPosition;" + "}";
  5.  
  6.     private final String fragmentShaderCode = "precision mediump float;"
  7.             + "uniform vec4 vColor;" + "void main() {"
  8.             + "  gl_FragColor = vColor;" + "}";
  9.  
  10.     // always start at zero when ploting a point as defult
  11.     // trinagle Coordinates - vertex = 3(x y z)
  12.     private float trinagleCoords[] = { 0f, 1f, // V 0 - top
  13.             1f, -1, // V 1 - right bottom
  14.             -1f, 1f // V 2 - left bottom
  15.     };
  16.     private int COORDS_PER_VERTEX = 2; // number of points
  17.     private FloatBuffer vertexBuffer;
  18.  
  19.     private short[] drawOrder = { 0, 1, 2 }; // order
  20.     private ShortBuffer orderBuffer;
  21.  
  22.     float color[] = { 0.5f, 1f, 0f, 1f };
  23.  
  24.     private int vertexCount = trinagleCoords.length / COORDS_PER_VERTEX;
  25.     private int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex
  26.  
  27.     // used to pass in the transformation matrix
  28.     private final int mProgram;
  29.     // used to pass in model poition information
  30.     private int mPositionHandle;
  31.     // used to pass inmodel color information
  32.     private int mColorHandle;
  33.  
  34.     private int mMVPMatrixHandle;
  35.  
  36.     public GLTriangle() {
  37.         // vertices - has to be 4
  38.         ByteBuffer byteBuffer = ByteBuffer
  39.                 .allocateDirect(trinagleCoords.length * 4);
  40.         byteBuffer.order(ByteOrder.nativeOrder());
  41.         vertexBuffer = byteBuffer.asFloatBuffer();
  42.         vertexBuffer.put(trinagleCoords);
  43.         vertexBuffer.position(0);
  44.  
  45.         // draw order - has to be 2
  46.         ByteBuffer pointByteBuffer = ByteBuffer
  47.                 .allocateDirect(drawOrder.length * 2);
  48.         pointByteBuffer.order(ByteOrder.nativeOrder());
  49.         orderBuffer = pointByteBuffer.asShortBuffer();
  50.         orderBuffer.put(drawOrder);
  51.         orderBuffer.position(0);
  52.  
  53.         // shader - always same
  54.         int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  55.         int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
  56.                 fragmentShaderCode);
  57.         // create empty OpenGL ES Program
  58.         mProgram = GLES20.glCreateProgram();
  59.         // add the vertex shader to program
  60.         GLES20.glAttachShader(mProgram, vertexShader);
  61.         // add the fragment shader to program
  62.         GLES20.glAttachShader(mProgram, fragmentShader);
  63.         // creates OpenGL ES program executables
  64.         GLES20.glLinkProgram(mProgram);
  65.     }
  66.  
  67.     /************/
  68.     /*** same ***/
  69.     /************/
  70.     public static int loadShader(int type, String shaderCode) {
  71.  
  72.         // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
  73.         // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
  74.         int shader = GLES20.glCreateShader(type);
  75.  
  76.         // add the source code to the shader and compile it
  77.         GLES20.glShaderSource(shader, shaderCode);
  78.         GLES20.glCompileShader(shader);
  79.  
  80.         return shader;
  81.     }
  82.  
  83.     public void draw(float[] mModelMatrix) {
  84.         // Add program to OpenGL environment
  85.         GLES20.glUseProgram(mProgram);
  86.  
  87.         // get handle to vertex shader's vPosition member
  88.         mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
  89.  
  90.         // Enable a handle to the triangle vertices
  91.         GLES20.glEnableVertexAttribArray(mPositionHandle);
  92.  
  93.         // Prepare the triangle coordinate data
  94.         GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
  95.                 GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
  96.  
  97.         // get handle to fragment shader's vColor member
  98.         mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
  99.  
  100.         // Set color for drawing the triangle
  101.         GLES20.glUniform4fv(mColorHandle, 1, color, 0);
  102.  
  103.         // Draw the triangle
  104.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
  105.  
  106.         // Disable vertex array
  107.         GLES20.glDisableVertexAttribArray(mPositionHandle);
  108.  
  109.         // get handle to shape's transformation matrix
  110.         mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
  111.  
  112.         // Apply the projection and view transformation
  113.         GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mModelMatrix, 0);
  114.  
  115.         // Draw the triangle
  116.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
  117.  
  118.  
  119.     }
  120. }
  121.  
Sep 3 '13 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
What device are you testing this on? Or is it just the emulator? I believe the emulators OpenGL implementation is pretty crappy compared to what most real devices have.
Sep 4 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.