OpenGL dropping or clipping triangles - SOLVED

Problems with Canvas, OpenGL, etc...

OpenGL dropping or clipping triangles - SOLVED

Postby warrenbaltz » Sun Aug 08, 2010 1:33 am

I am doing some very basic drawing of triangles in OpenGL, but I am seeing strange rendering errors. When I draw a textured square (two triangles) part of the square is missing or the texture is stretched or corrupted. When I draw a square with no texture, part of the square is missing sometimes.

I do not see a pattern to when it works and when it doesn't. It might be broken one run, then after a back button and rerun, it works the next.

The triangle that's missing is not one of the triangles used to draw the square. Rather it is part of the first triangle and part of the second triangle. The triangles are the bottom right half and the top left half of the square, but the part missing is the bottom left half of the square.

The problem occurs on both the Droid and on the emulator.


Here is the texture image, it is a power of 2 (128x128)
Image

Here is a typical result, although this changes. There are also white squares drawn in front of the textured square.
http://img806.imageshack.us/i/screenshoto.png
Image

You can see that the lower left triangle has a distorted texture. And the lower left triangle of the first white square is missing altogether. The problems come and go, but it is always the lower left triangle.



The code is mostly the same as the Google examples.
Important parts are below with most of the unrelated code removed.


/*** Surface View Setup ***/

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_TEXTURE_2D);
slotGame.initGl(gl, context);
}



public void onSurfaceChanged(GL10 gl, int width, int height) {
float aspectRatio = 1.5f;
int originalWidth = width;
int originalHeight = height;

if(width > (height*aspectRatio)){
width = (int) (height*aspectRatio);
}else if(height > (width/aspectRatio)){
height = (int) (width/aspectRatio);
}

int halfDeltaWidth = (originalWidth - width)/2;
int halfDeltaHeight = (originalHeight - height)/2;

gl.glViewport(halfDeltaWidth, halfDeltaHeight, width, height);

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, width, height, 0);
}


public void onDrawFrame(GL10 gl) {
gl.glClearColor(0.39215f, 0.79607f, 0.88235f, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

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

//--> call to draw(gl) shown below
}




/*** Drawing Code ***/
void init(){
float textureCoords[] = {
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,

0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};

byte indices[] = {
1,2,3,
4,5,6
};

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);

ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length*4);
tbb.order(ByteOrder.nativeOrder());
mTextureBuffer = tbb.asFloatBuffer();
mTextureBuffer.put(textureCoords);
mTextureBuffer.position(0);

mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}



public void draw(GL10 gl){
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
}
Last edited by warrenbaltz on Mon Aug 09, 2010 2:49 pm, edited 1 time in total.
warrenbaltz
Junior Developer
Junior Developer
 
Posts: 21
Joined: Tue May 12, 2009 1:12 am
Location: Iowa

Top

Re: OpenGL dropping or clipping triangles

Postby warrenbaltz » Mon Aug 09, 2010 2:48 pm

For those interested, I have solved this problem. It was a dumb mistake.

The indices are zero-indexed, not one-indexed.

byte indices[] = {
1,2,3,
4,5,6
};

should be

byte indices[] = {
0,1,2,
3,4,5
};

But there must have been garbage in that last slot, and since that particular part of code is unmanaged C (I think) no error was thrown. That's why sometimes triangles rendered and sometimes they didn't.
warrenbaltz
Junior Developer
Junior Developer
 
Posts: 21
Joined: Tue May 12, 2009 1:12 am
Location: Iowa

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 3 guests