PDA

View Full Version : Problem drawing a 3d dimand shaped object



Spiltbeer
09-29-2009, 11:37 AM
Hi,

I'm very new to Open GL. I'm attempting to draw a diamond shape that is missing a side. I have verifed that the points are drawn clockwise.

I believe it is an issue with the way I'm using the buffer but I can't figure out what I'm doing wrong. I have a feeling my eyes are glazing over a typo.



public void onDrawFrame(GL10 gl) {
// Back color
gl.glClearColor(0f, 0.8f, 0f, 1.0f);

// reset the matrix
gl.glLoadIdentity();

//gl.glClearColor(red, green, blue, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

// which part of the triangle should be visible?
gl.glEnable(GL10.GL_CULL_FACE);
gl.glFrontFace(GL10.GL_CCW);
gl.glCullFace(GL10.GL_FRONT);

//TRIANGLE CODE START
// prepare to use vertex arrays we want to draw
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

float[] coords = {
0f, 0f, 1f, //0
0f, 0.25f, 0f, //1
0f, 0f, -0.25f, //2
0.25f, -0.25f, 0f, //3
-0.25f, -0.25f, 0f, //4
};
int vertexCount = coords.length;

float[] colors = {
1f, 0f, 0f, 1f, // red
1f, 1f, 0f, 1f, // yellow
1f, 1f, 1f, 1f, // white
1f, 1f, 0f, 1f, // yellow
1f, 1f, 0f, 1f, // yellow
};

short[] indices = new short[] {
0, 1, 4,
0, 4, 3,
0, 3, 1,
2, 1, 3,
2, 4, 1,
2, 3, 4,
};

// float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();

// short has 2 bytes
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();


// float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();

vertexBuffer.put(coords); //all the points
indexBuffer.put(indices); //draw order
colorBuffer.put(colors);

vertexBuffer.position(0);
indexBuffer.position(0);
colorBuffer.position(0);

// define the vertices we want to draw. 3 points in a triangle
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

//4 points in the color def
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);

// finally draw the vertices
gl.glDrawElements(GL10.GL_TRIANGLES, vertexCount, GL10.GL_UNSIGNED_SHORT, indexBuffer);
}


The code above produces the following:
http://yfrog.com/elcroppercapture8p
http://yfrog.com/27missingfrontp
http://yfrog.com/04trianglekj


Any help is greatly appreciated. Thanks in advance.

dletozeun
09-29-2009, 02:17 PM
Have you tried with culling disabled? Do you see the same thing with culling enabled/disabled?

I don't understand what I see in the screenshot, sorry perhaps it is too late for me.

Spiltbeer
09-29-2009, 03:05 PM
Have you tried with culling disabled? Do you see the same thing with culling enabled/disabled?

I don't understand what I see in the screenshot, sorry perhaps it is too late for me.

Thanks for the response. I disabled culling however the triangle is still not drawn.

The last triangle identified in the indices array isn't being drawn:


short[] indices = new short[] {
0, 1, 4,
0, 4, 3,
0, 3, 1,
2, 1, 3,
2, 4, 1,
2, 3, 4,
};


The triangle made from the points 2, 3, 4 are not being drawn. I've changed the draw order in the array. Its always the last triangle specified in the indices array that isn't drawn which makes me believe I'm doing something wrong with the buffer.

dletozeun
09-29-2009, 03:30 PM
You have to give the indices count to glDrawElements not the vertices count. What does "coord.length" returns exactly? Ithink it is the number of float elements in the array of float and in this case the name "vertexCount" is not suitable.

Try with 18 (6x3) as the number of elements in DrawElements since you have 18 elements if I am correct.

Spiltbeer
09-29-2009, 06:55 PM
Thanks for your help. Removing the vetrtexCount and replacing it with indices.length worked.



gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);