Amrmsmb
Amrmsmb

Reputation: 1

Drawing Triangle using OpenGl, does not appear?

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();
}

}

Upvotes: 3

Views: 6773

Answers (3)

Nathaniel
Nathaniel

Reputation: 11

This android tutorial mentions the requirement of a vertexShader, fragmentShader and Program. I may have missed it, but I don't see anything related to that in your code. In my case the fix for the invisible triangle was to fix a type-o in my fragmentShader.

Upvotes: 1

Shivan Dragon
Shivan Dragon

Reputation: 15219

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.

Upvotes: 4

datenwolf
datenwolf

Reputation: 162164

Your triangles gets clipped by the near clipping plane. Try moving it into the range between the near and far values of gluPerspective.

Upvotes: 2

Related Questions