我正在尝试使用Android应用程序绘制一些我在此处找到的简单OpenGL代码。 http://www.iphonemobilephones.com/opengl-es-from-the-ground-up-part-4-let-there-be-light.html 对于我的生活,它不想为我有多个三角形的任何形状做绘图。如果我有一个三角形,它工作正常。
我希望有更多了解OpenGL的人会非常友好地看看是否有任何明显的错误。
谢谢, MJ
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glRotatef(1.0f, 1.0f, 1.0f, 0f);
float vertices[]= {
0f, -0.525731f, 0.850651f, // vertices[0]
0.850651f, 0f, 0.525731f, // vertices[1]
0.850651f, 0f, -0.525731f, // vertices[2]
-0.850651f, 0f, -0.525731f, // vertices[3]
-0.850651f, 0f, 0.525731f, // vertices[4]
-0.525731f, 0.850651f, 0f, // vertices[5]
0.525731f, 0.850651f, 0f, // vertices[6]
0.525731f, -0.850651f, 0f, // vertices[7]
-0.525731f, -0.850651f, 0f, // vertices[8]
0f, -0.525731f, -0.850651f, // vertices[9]
0f, 0.525731f, -0.850651f, // vertices[10]
0f, 0.525731f, 0.850651f // vertices[11]
};
FloatBuffer v = FloatBufferFromFloatArray( vertices, 12*3);
short icosahedronFaces[] = {
1, 2, 6,
1, 7, 2,
3, 4, 5,
4, 3, 8,
6, 5, 11,
5, 6, 10,
9, 10, 2,
10, 9, 3,
7, 8, 9,
8, 7, 0,
11, 0, 1,
0, 11, 4,
6, 2, 10,
1, 6, 11,
3, 5, 10,
5, 4, 11,
2, 7, 9,
7, 1, 0,
3, 9, 8,
4, 8, 0,
};
ShortBuffer i = ShortBufferFromShortArray( icosahedronFaces, 60 );
float normals[] = {
0.000000f, -0.417775f, 0.675974f,
0.675973f, 0.000000f, 0.417775f,
0.675973f, -0.000000f, -0.417775f,
-0.675973f, 0.000000f, -0.417775f,
-0.675973f, -0.000000f, 0.417775f,
-0.417775f, 0.675974f, 0.000000f,
0.417775f, 0.675973f, -0.000000f,
0.417775f, -0.675974f, 0.000000f,
-0.417775f, -0.675974f, 0.000000f,
0.000000f, -0.417775f, -0.675973f,
0.000000f, 0.417775f, -0.675974f,
0.000000f, 0.417775f, 0.675973f
};
FloatBuffer n = FloatBufferFromFloatArray( normals, 3*12 );
float colors[] = {
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.5f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 0.5f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.5f, 1.0f
};
FloatBuffer c = FloatBufferFromFloatArray( colors, 4*12 );
gl.glVertexPointer( 12, GL10.GL_FLOAT, 0, v);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, c);
gl.glNormalPointer(GL10.GL_FLOAT, 0, n);
gl.glDrawElements(GL10.GL_TRIANGLES, 60, GL10.GL_UNSIGNED_SHORT, i);
// helper functions below as well as the surface created function
private ShortBuffer ShortBufferFromShortArray( short[] array, int length )
{
ByteBuffer vbb = ByteBuffer.allocateDirect(length * 4);
vbb.order(ByteOrder.nativeOrder());
ShortBuffer fb = vbb.asShortBuffer();
fb.put(array);
fb.position(0);
return fb;
}
private FloatBuffer FloatBufferFromFloatArray( float[] array, int length )
{
ByteBuffer vbb = ByteBuffer.allocateDirect(length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer fb = vbb.asFloatBuffer();
fb.put(array);
fb.position(0);
return fb;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//
//gl.glEnable(GL10.GL_DEPTH_TEST);
//gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
// Define the ambient component of the first light
float[] light0Ambient = {0.1f, 0.1f, 0.1f, 1.0f};
gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, FloatBufferFromFloatArray(light0Ambient, 4));
// Define the diffuse component of the first light
float[] light0Diffuse = {0.7f, 0.7f, 0.7f, 1.0f};
gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, FloatBufferFromFloatArray(light0Diffuse, 4));
// Define the specular component and shininess of the first light
float[] light0Specular = {0.7f, 0.7f, 0.7f, 1.0f};
float light0Shininess = 0.4f;
//gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, FloatBufferFromFloatArray(light0Specular, 4));
// Define the position of the first light
float[] light0Position = {1.0f, 0.0f, 0.0f, 0.0f};
gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, FloatBufferFromFloatArray(light0Position, 4));
// Define a direction vector for the light, this one points correct down the Z axis
float[] light0Direction = {0.0f, 0.0f, -1.0f};
//gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPOT_DIRECTION, FloatBufferFromFloatArray(light0Direction, 3));
// Define a cutoff angle. This defines a 90° field of vision, since the cutoff
// is number of degrees to each side of an imaginary line drawn from the light's
// position along the vector supplied in GL_SPOT_DIRECTION above
//gl.glLightf(gl.GL_LIGHT0, gl.GL_SPOT_CUTOFF, 180.0f);
//gl.glEnable(GL10.GL_CULL_FACE);
// which is the front? the one which is drawn counter clockwise
//gl.glFrontFace(GL10.GL_CCW);
// which one should NOT be drawn
//gl.glCullFace(GL10.GL_BACK);
initShape();
//gl.glScalef(1.0f, 1.0f, 1.0f);
}