我对OpenGL很新,并且在使用ColorPointer函数和DrawArrays时遇到了一些麻烦......我似乎无法使用颜色渲染对象..当我使用glColor4f函数但是这个有效..我认为这与我如何构建我的颜色FloatBuffer有关...但是我似乎无法弄清楚这一点。
请查看我的一些代码并让我知道我在这里做错了什么......这也是我的第一个Stack Over Flow Post(:
// function draws the vertices in 3D space
public void draw(GL10 gl){
// Enable client side access since arrays are store on client side heap
// and OpenGl is considered server side
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnable(GL10.GL_COLOR_ARRAY);
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
gl.glEnable(GL10.GL_COLOR_MATERIAL);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, binaryStlParser.getVertices());
gl.glNormalPointer(GL10.GL_FLOAT, 0, binaryStlParser.getNormalVectors());
gl.glColorPointer(4, GL10.GL_FLOAT, 0, binaryStlParser.colours);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, (binaryStlParser.getNumOfFacets() * 3));
//gl.glShadeModel(GL10.GL_SHININESS);
// disables access given from client side heap for OpenGL
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
gl.glDisable(GL10.GL_COLOR_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_COLOR_MATERIAL);
}
来自另一个班级
numOfFacets = ByteBuffer.wrap(numberOfFacets).order(ByteOrder.nativeOrder()).getInt();
// Allocate memory for the FloatBuffers since we now know the number of Facets
// we use a ByteBuffer to ensure native ordering .........
// * 3 for each vertex point in 3D (* 3) space. Size of float is 4 bytes
vertices = ByteBuffer.allocateDirect((numOfFacets * 3 * 3) * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
// * 3 for each coordinate in vector. Size of float is 4 bytes
normalVectors = ByteBuffer.allocateDirect((numOfFacets * 3) * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
// * 3 for each vertex point in 4D (* 4) RBGA space. Size of float is 4 bytes
colours = ByteBuffer.allocateDirect((numOfFacets * 3 * 4) * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
for(int i = 0 ; i < numOfFacets*3; i++){
colours.put(new float[] {0.0f, 1.0f, 0.0f, 0.5f});
}
colours.position(0);
答案 0 :(得分:2)
我想出来了!只是想发布答案,这有助于其他人(:
愚蠢的错误...看起来像我在使用:
gl.glEnable(GL10.GL_COLOR_ARRAY);
而不是
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
FloatBuffers存储在客户端堆上,而我认为OpenGL被认为是服务器端。