r1 = outer_radius - tooth_depth / 2.0f;
r2 = outer_radius + tooth_depth / 2.0f;
da = 2.0f * (float)Math.PI / teeth / 4.0f;
final Geometry gear = new Geometry();
final ImmediateModeBuffer imb = new ImmediateModeBuffer(1024);
int lastDrawIndex = 0;
//glShadeModel(GL_FLAT);
// draw front face
lastDrawIndex += gear.addDrawCommand(GL_TRIANGLE_STRIP, lastDrawIndex, teeth * 4 + 2);
for ( i = 0; i <= teeth; i++ ) {
angle = i * 2.0f * (float)Math.PI / teeth;
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5f);
if ( i < teeth ) {
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle + 3.0f * da), r1 * sin(angle + 3.0f * da), width * 0.5f);
}
}
// draw front sides of teeth
for ( i = 0; i < teeth; i++ ) {
lastDrawIndex += gear.addDrawCommand(GL_TRIANGLE_STRIP, lastDrawIndex, 4);
angle = i * 2.0f * (float)Math.PI / teeth;
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle + 3.0f * da), r1 * sin(angle + 3.0f * da), width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r2 * cos(angle + 2.0f * da), r2 * sin(angle + 2.0f * da), width * 0.5f);
}
// draw back face
lastDrawIndex += gear.addDrawCommand(GL_TRIANGLE_STRIP, lastDrawIndex, (teeth + 1) * 4);
for ( i = 0; i <= teeth; i++ ) {
angle = i * 2.0f * (float)Math.PI / teeth;
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5f);
}
// draw back sides of teeth
for ( i = 0; i < teeth; i++ ) {
lastDrawIndex += gear.addDrawCommand(GL_TRIANGLE_STRIP, lastDrawIndex, 4);
angle = i * 2.0f * (float)Math.PI / teeth;
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5f);
imb.glNormal3f(0.0f, 0.0f, 1.0f);
imb.glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5f);
}
// draw outward faces of teeth
// OpenGL ES 2.0 note: This needs to be converted to a triangle
// list with face normals to get the flat look of the original.
lastDrawIndex += gear.addDrawCommand(GL_TRIANGLE_STRIP, lastDrawIndex, teeth * 8 + 2);
for ( i = 0; i < teeth; i++ ) {
angle = i * 2.0f * (float)Math.PI / teeth;
imb.glNormal3f(cos(angle), sin(angle), 0.0f);
imb.glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5f);
imb.glNormal3f(cos(angle), sin(angle), 0.0f);
imb.glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5f);
u = r2 * cos(angle + da) - r1 * cos(angle);
v = r2 * sin(angle + da) - r1 * sin(angle);
len = (float)Math.sqrt(u * u + v * v);
u /= len;
v /= len;
imb.glNormal3f(v, -u, 0.0f);
imb.glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5f);
imb.glNormal3f(v, -u, 0.0f);
imb.glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5f);
imb.glNormal3f(cos(angle), sin(angle), 0.0f);
imb.glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5f);
imb.glNormal3f(cos(angle), sin(angle), 0.0f);
imb.glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5f);
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
imb.glNormal3f(v, -u, 0.0f);
imb.glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5f);
imb.glNormal3f(v, -u, 0.0f);
imb.glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5f);
}
imb.glNormal3f(cos(0), sin(0), 0.0f);
imb.glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5f);
imb.glNormal3f(cos(0), sin(0), 0.0f);
imb.glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5f);
//glShadeModel(GL_SMOOTH);
// draw inside radius cylinder
lastDrawIndex += gear.addDrawCommand(GL_TRIANGLE_STRIP, lastDrawIndex, (teeth + 1) * 2);
for ( i = 0; i <= teeth; i++ ) {
angle = i * 2.0f * (float)Math.PI / teeth;
imb.glNormal3f(-cos(angle), -sin(angle), 0.0f);
imb.glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5f);
imb.glNormal3f(-cos(angle), -sin(angle), 0.0f);
imb.glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5f);
}
gear.update(imb.getBuffer());
return gear;
}