473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

draw trinagle in android openGL 2.0

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
1 2011
Nepomuk
3,112 Expert 2GB
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

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

Similar topics

9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
2
by: George | last post by:
I have marked the areas with comments as to what I would like to do and what the code does. The two problem areas are in the key pressing and mousing clicking areas. thanks for the help. This...
2
by: Rameshika | last post by:
Hi All I am new to 3D modelling in VISUAL .NET C#.I am planning to draw the entire roof (showing the roof material, roof angle, roof shape,ceiling shape, ceiling material) of the house and...
0
by: Scott Chang | last post by:
Hi all, I tried to use Managed C++ coding of VC++.NET (2002)and OpenGL version 1.2 to draw a hexagon shape of benzene. My OpenGLdrawBenzene.cpp is the following: // This is the main project file...
1
by: Scott Chang | last post by:
Hi All I tried to use the attached OpenGL code to draw a Star-shaped figure on my Microsoft Visual C++ .NET 2002-Windows XP Pro PC. When I did 'Build' in Debug Mode, I got an error C2065:...
14
by: Jessica Weiner | last post by:
I am writing an application in C# which need to plot graphs and simple shapes (polygons, circles, squares etc). Which library is better for this purpose and why? Thanks.
4
by: tobfon | last post by:
I'm creating a scientific visualization application with rather high demands on performance. I've created a nice rendering engine for it in C++/OpenGL and a python interface to the rendering...
1
by: linksterman | last post by:
hey, I was experimenting with pyglet, and was trying out their opengl, but cant figure out how to scale images... #!/usr/bin/python # $Id:$ '''Demonstrates one way of fixing the display...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.