Problem coloring a cylinder

Problems with Canvas, OpenGL, etc...

Problem coloring a cylinder

Postby Jux » Mon May 31, 2010 2:30 pm

I am very new to OpenGL programming and I seek some help.

What I have done so far is a 3d cylinder object. Next I want to color it so that the top and bottom would be blue and the side would be red. I have no idea how to achieve that.

I know that there are two types (or maybe more) coloring techniques: flat coloring (with gl.glColor4f()) and smooth coloring (with gl.glColorPointer() using the colorBuffer).

I think what I want to use is flat coloring. I have done it so:

Code: Select all
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

// blue top and bottom
gl.glColor4f(0.3f, 0.1f, 0.9f, 1f);
gl.glDrawElements(GL10.GL_TRIANGLES, numberOfIndeces1,
      GL10.GL_UNSIGNED_SHORT, indexBuffer1);

// red side
gl.glColor4f(0.9f, 0.1f, 0.1f, 1f);
gl.glDrawElements(GL10.GL_TRIANGLES, numberOfIndeces2,
      GL10.GL_UNSIGNED_SHORT, indexBuffer2);


The indexBuffer1 contains all the indices of top and bottom, and indexBuffer2 contains the indices of the cylinder side wall.

The problem is that when I draw top/bottom and side separately, to change the color, strange thing happens. The red side is overlapping on the blue surface one, just as if the blue would be transparent. If I draw blue parts afterward, the effect is reversed.

Result looks like that:
Image

All the help is very welcome.
Jux
Freshman
Freshman
 
Posts: 5
Joined: Mon May 31, 2010 1:58 pm

Top

Re: Problem coloring a cylinder

Postby Jux » Mon May 31, 2010 3:04 pm

OK, I decided draw top, side and bottom separately. The result is better, but not as I like.
Code: Select all
// Blue top
gl.glColor4f(0.3f, 0.1f, 0.9f, 1f);
gl.glDrawElements(GL10.GL_TRIANGLES, numberOfIndeces1a,
      GL10.GL_UNSIGNED_SHORT, indexBuffer1a);

// Red side
gl.glColor4f(0.9f, 0.1f, 0.1f, 1f);
gl.glDrawElements(GL10.GL_TRIANGLES, numberOfIndeces2,
      GL10.GL_UNSIGNED_SHORT, indexBuffer2);

// Blue bottom
gl.glColor4f(0.3f, 0.1f, 0.9f, 1f);
gl.glDrawElements(GL10.GL_TRIANGLES, numberOfIndeces1b,
      GL10.GL_UNSIGNED_SHORT, indexBuffer1b);


Now the top and side is visible, but bottom surface is not:
From one side, top and side are ok:
Image

From other side, bottom is not visible and part of side also:
Image
Jux
Freshman
Freshman
 
Posts: 5
Joined: Mon May 31, 2010 1:58 pm

Re: Problem coloring a cylinder

Postby Jux » Tue Jun 01, 2010 9:45 am

Okidoky. As it turns out, it is a typical culling problem. I had to enable culling (gl.glEnable(GL10.GL_CULL_FACE)) and then rearange the indeces. It works now.
Jux
Freshman
Freshman
 
Posts: 5
Joined: Mon May 31, 2010 1:58 pm

Re: Problem coloring a cylinder

Postby seed » Tue Jun 01, 2010 7:45 pm

Jux wrote:Okidoky. As it turns out, it is a typical culling problem. I had to enable culling (gl.glEnable(GL10.GL_CULL_FACE)) and then rearange the indeces. It works now.


It isn't typical to cull the face. Culling the back face is more typical. The winding of your indices is important as to which side of the triangle you see. Counter Clockwise winding is typical. From your first post, we were seeing the back face of the botton and seeing right through the front face of the top. I don't think you wanted to change the culling. I think you just needed to wind your trinangle indices in the opposite order. Hope this helps.
Visit Exit 4 Gaming - http://www.exit4games.com/
Home of LavaBall - http://exit4games.com/?page_id=3
Home of Rebound - http://exit4games.com/?page_id=138
Home of Tap Crazy - http://exit4games.com/?page_id=219
Download "Caveman Pool" From the Market Today!
seed
Senior Developer
Senior Developer
 
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm

Top

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

Who is online

Users browsing this forum: No registered users and 6 guests