Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:58:40+00:00 2026-05-31T04:58:40+00:00

I’m trying to make my first App. based on OpenGl, i’m trying to draw

  • 0

I’m trying to make my first App. based on OpenGl, i’m trying to draw a triangle, and wHEN

RUNNING THE App. it just displays black screen with no triangle.

1-I dont know where my mistake is?
2-is there any good book/tutorials for beginners to opengl es Android?

Triangle Class:

public class Triangle {

private FloatBuffer vertxBuffer;

protected static byte indices[] = {
    //Face definition:
    0,1,3, //lower-right triangle of the face is drawn with vertices vertices[0]->vertices[1]->vertices[3] (->vertices[0])
    0,3,2  //upper-right triangle of the face is drawn with vertices vertices[0]->vertices[3]->vertices[2] (->vertices[0])          
};

public float vertices[] = {
        -0.5f, -0.5f, 0.0f,
         0.5f, -0.5f, 0.0f,
         0.0f,  0.5f, 0.0f
};

public Triangle() {

    // float has 4 bytes, so we allocate for each coordinate 4 bytes.
    //what is the difference between ByteBuffer.allocateDirect AND ByteBuffer.allocate???
    ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    vertexByteBuffer.order(ByteOrder.nativeOrder());

    // allocate the memory from the byte buffer
    vertxBuffer = vertexByteBuffer.asFloatBuffer();

    //fill the vertex buffer with the vertices
    vertxBuffer.put(vertices);

    // set the cursor position to the beginning of the buffer
    vertexByteBuffer.position(0);       
}
protected static ByteBuffer indexBuffer;    
static {
    indexBuffer = ByteBuffer.allocateDirect(indices.length);
    indexBuffer.put(indices);
    indexBuffer.position(0);
}
public void draw(GL10 gl) {
    // Because we store the Triangle vertices " Coordinates " in a FloatBuffer
    // we need to enable OpenGL to read from it.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    //set the color for the Triangle (r, g, b, alpha) alpha is between 0-1
    gl.glColor4f(2.0f, 1.0f, 0.0f, 0.5f);

    // point to our vertex buffer to extract the vertices from it.
    //(numberOfVertices, Which type of data the buffer Holds, offset, our Buffer containing the Vertices)
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertxBuffer);

    //draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3 );

    gl.glDrawElements(gl.GL_TRIANGLES, indices.length, gl.GL_UNSIGNED_BYTE, indexBuffer);

    //disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

}

GLRenderer Class:

public class GLRenderer implements Renderer{

private Triangle triangle;

public GLRenderer() {
    this.triangle = new Triangle();
}

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub

    // clear screen and depth buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // reset the mode view matrix
    gl.glLoadIdentity();

    // Drawing 
    gl.glTranslatef(0.0f, 0.0f, -5.0f);
    triangle.draw(gl); 
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub

    if (height == 0) {
        height = 1;
    }

    //Reset the current View Port
    gl.glViewport(0, 0, width, height);

    //Select the Projection Matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);

    // Reset the Projection Matrix
    gl.glLoadIdentity();

    // calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width/(float) height, 0.1f , 100.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub

    gl.glClearColor(0, 0, 0, 1.0f);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glDisable(GL10.GL_DITHER);

}

}

OpenGlRenderActivity Class:

public class OpenGLRenderActivity extends Activity {
/** Called when the activity is first created. */

private GLSurfaceView gLSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    gLSurfaceView = new GLSurfaceView(this);
    gLSurfaceView.setRenderer(new GLRenderer());
    setContentView(gLSurfaceView);
}

@Override
protected void onResume() {
    super.onResume();
    gLSurfaceView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    gLSurfaceView.onPause();
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:58:41+00:00Added an answer on May 31, 2026 at 4:58 am

    I think you need to declare the indexes for your vertex (in order to compose the lines and faces):

    protected static byte indices[] = {
            //Face definition:
            0,1,3, //lower-right triangle of the face is drawn with vertices vertices[0]->vertices[1]->vertices[3] (->vertices[0])
            0,3,2  //upper-right triangle of the face is drawn with vertices vertices[0]->vertices[3]->vertices[2] (->vertices[0])          
        };
    
    protected static ByteBuffer indexBuffer;    
        static {
            indexBuffer = ByteBuffer.allocateDirect(indices.length);
            indexBuffer.put(indices);
            indexBuffer.position(0);
        }
    

    then pass those indexes to your draw call:

    (replace

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3 );
    

    with

    gl.glDrawElements(GL.GL_TRIANGLES, indices.length, GL.GL_UNSIGNED_BYTE, indexBuffer);

    )

    As for tutorials you’ve got great Android versions of the Nehe tutorials here:

    http://insanitydesign.com/wp/projects/nehe-android-ports/

    It’s more code than textual explanations, but they start you off real slow, and the code is well comented. Actually I think one of the first tutrials simply shows how to draw a triangle just like you’re trying here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.