Question

I have a problem using opengl on android to draw a simple rectangle. This is what I have done.

I drew a simple rectangle with sketchup. I exported the result using a 3d-model collada .dae file. I then copied the vertices data from the .dae (xml) file and put in an array. I copied the array in native format to a float buffer. I then drew the triangles using stripe mode. The result is nearly a rectangle. It is missing a triangle on each surface.

Here is the relevant portion of code and the result.

public void draw(GL10 gl) {
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
    // Enable color tracking
    gl.glEnable(GL10.GL_COLOR_MATERIAL);
    for (int i=0; i<108/4; i=i+4) {
        myDrawColor(gl,i);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,i,4);// mode, first, count
    }
}

the result is shown here http://imgur.com/a/o3HTP

Was it helpful?

Solution 2

I got the code working. There were three problems.

  1. DrawArrays works forward through the vertex array. ie. you draw one element from the array at a time and then go to the next. You can not hop back and forth thru it.
  2. Using the offset list in the .xml file I created an array that I could use with glDrawElements where it hops around in the vertices list.
  3. You need to use unsigned short of the offset. I was using an integer and fixed which did not work.

Here is the resulting code that works. The problem was mine. Not the .xml file produced from sketchup.

public void draw(GL10 gl) {
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorsBuffer);
    gl.glEnable(GL10.GL_COLOR_MATERIAL); 
// Enable color tracking 
    gl.glEnable(GL10.GL_COLOR_MATERIAL);
    gl.glDrawElements(GL10.GL_TRIANGLES, myoffsets.length, GL10.GL_UNSIGNED_SHORT, mIndicesBuffer); // mode, count, type, indices
}

OTHER TIPS

You probably have your vertexes in the wrong order in your vertex list (which could be the fault of the export). This is what I got from my quads when i had the vertices in the wrong places. You will want to build them counter-clockwise from the outside. It could also be that triangle strip is causing issues with your vertices, for an application this simple you could try GL_QUADS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top