OpenGL(ES) - 使用三角形条绘制圆柱体

时间:2011-05-07 03:04:45

标签: graphics opengl-es

编辑解决方案:  openGL ES中的圆柱:


///Storing a cylinder in a VBO
int verticesCyl = 60;
int indicesCyl = verticesCyl + 2;
float cylHeight = .2f;

void initCylinder(GL10 gl) {
        GL11 gl11 = (GL11) gl;
        FloatBuffer vertBuffer = ByteBuffer.allocateDirect(verticesCyl * 3 * 
                4).order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        ShortBuffer indexBuffer = ByteBuffer.allocateDirect(indicesCyl *               2).order(ByteOrder.nativeOrder())
                .asShortBuffer();

        float vertices[] = new float[verticesCyl * 3];
        short indices[] = new short[indicesCyl];
        for (int j = 0; j < verticesCyl*3 ; j+=3) {
            vertices[j] = (float) (Math.cos(theta));
            vertices[j+1] = (float) (Math.sin(theta));
            vertices[j+2] = j % 2 == 0 ? cylHeight : -cylHeight;
            theta += 2 * Math.PI / (verticesCyl);
        }

        for (int j = 0; j < indicesCyl; j++) {
                indices[j] = (short) (j % verticesCyl) ;
        }

        indexBuffer.put(indices);
        vertBuffer.put(vertices);
        indexBuffer.flip();
        vertBuffer.flip();

        int[] buffer = new int[1];

        gl11.glGenBuffers(1, buffer, 0);
        vertexPointerCyl = buffer[0];
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerCyl);
        gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertBuffer.capacity() * 4, vertBuffer, GL11.GL_STATIC_DRAW);
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
        gl11.glGenBuffers(1, buffer, 0);
        indexPointerCyl = buffer[0];
        gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerCyl);
        gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * 2, indexBuffer, GL11.GL_STATIC_DRAW);
    }

////////////Draw the cylinder
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerCyl);
        gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerCyl);
        gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
        gl11.glPushMatrix();
        gl11.glColor4f(shadowColor,shadowColor,shadowColor, alpha);
        gl11.glTranslatef(mainX, mainY, 0);
        gl11.glScalef(scaleFactor, scaleFactor, 0);
        gl11.glRotatef(rotateCylinder, mainX, mainY, 0);
        gl11.glDrawElements(GL11.GL_TRIANGLE_STRIP, indicesCyl, GL11.GL_UNSIGNED_SHORT, 0);
        gl11.glPopMatrix();

1 个答案:

答案 0 :(得分:3)

要考虑的要点:

  1. 您是否正在“刷新”图形上下文? (我猜您使用的是Android,但我不知道要调用的确切API)
  2. 您是否使用“glBufferData”加载缓冲区?
  3. 您是否使用“glEnableClientState(GL_VERTEX_ARRAY)”启用了顶点客户端状态?