Android - 绘制一个圆柱体

时间:2017-05-02 14:36:12

标签: android opengl-es

我使用的是OpenGL,我已经掌握了所有功能。我可以绘制三角形和其他形状,但现在我需要制作一个管。它不需要空心(如果它更容易)。

像这样:Image of the cilinder

如何在3D中定义cilinder?我想我需要x,y,z和Radius。有人可以给我创意从哪里开始?谢谢。

绘制三角形代码:

 class Triangle {

 private final String vertexShaderCode =
         // This matrix member variable provides a hook to manipulate
         // the coordinates of the objects that use this vertex shader
         "uniform mat4 uMVPMatrix;" +
                 "attribute vec4 vPosition;" +
                 "void main() {" +
                 // the matrix must be included as a modifier of gl_Position
                 // Note that the uMVPMatrix factor *must be first* in order
                 // for the matrix multiplication product to be correct.
                 "  gl_Position = uMVPMatrix * vPosition;" +
                 "}";

 // Use to access and set the view transformation
 private int mMVPMatrixHandle;


 private final String fragmentShaderCode =
         "precision mediump float;" +
                 "uniform vec4 vColor;" +
                 "void main() {" +
                 "  gl_FragColor = vColor;" +
                 "}";

 // number of coordinates per vertex in this array
 static final int COORDS_PER_VERTEX = 3;
 static float triangleCoords[] = {   // in counterclockwise order:
         0.0f,  0.622008459f, 0.0f, // top
         -0.5f, -0.311004243f, 0.0f, // bottom left
         0.5f, -0.311004243f, 0.0f  // bottom right
 };

 // Set color with red, green, blue and alpha (opacity) values
 float color[] = { 0.5f, 0.5f, 0.5f, 1.0f };


 private final int mProgram;

private short[] indices = {0,1,2,0,2,3};

private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;

 public Triangle() {

     // initialize vertex byte buffer for shape coordinates
     ByteBuffer bb = ByteBuffer.allocateDirect(
             // (number of coordinate values * 4 bytes per float)
             triangleCoords.length * 4);
     // use the device hardware's native byte order
     bb.order(ByteOrder.nativeOrder());

     // create a floating point buffer from the ByteBuffer
     vertexBuffer = bb.asFloatBuffer();
     // add the coordinates to the FloatBuffer
     vertexBuffer.put(triangleCoords);
     // set the buffer to read the first coordinate
     vertexBuffer.position(0);

     int vertexShader = OpenGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
             vertexShaderCode);
     int fragmentShader = OpenGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
             fragmentShaderCode);

     // create empty OpenGL ES Program
     mProgram = GLES20.glCreateProgram();

     // add the vertex shader to program
     GLES20.glAttachShader(mProgram, vertexShader);

     // add the fragment shader to program
     GLES20.glAttachShader(mProgram, fragmentShader);

     // creates OpenGL ES program executables
     GLES20.glLinkProgram(mProgram);
 }

 private int mPositionHandle;
 private int mColorHandle;

 private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
 private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

 public void drawMatrix(float[] mvpMatrix) { // pass in the calculated transformation matrix


     // Add program to OpenGL ES environment
     GLES20.glUseProgram(mProgram);

     // get handle to vertex shader's vPosition member
     mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

     // Enable a handle to the triangle vertices
     GLES20.glEnableVertexAttribArray(mPositionHandle);

     // Prepare the triangle coordinate data
     GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
             GLES20.GL_FLOAT, false,
             vertexStride, vertexBuffer);

     // get handle to fragment shader's vColor member
     mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

     // Set color for drawing the triangle
     GLES20.glUniform4fv(mColorHandle, 1, color, 0);

     // get handle to shape's transformation matrix
     mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

     // Pass the projection and view transformation to the shader
     GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

     // Draw the triangle
     GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

     // Disable vertex array
     GLES20.glDisableVertexAttribArray(mPositionHandle);
 }

1 个答案:

答案 0 :(得分:0)

无论您如何在内部定义它,您仍然必须为OpenGL ES提供一组三角形,线条或点。 因此,您需要定义两个位于平行平面上的相等的正多边形。他们拥有的顶点越多,它们将成为圆圈。您可以使用以下公式自行查看: Formula should be here R是外接圆的半径,r是内切圆的半径,n是多个顶点。 r越近R,多边形越接近圆。因此,如果您使用更多顶点,多边形将更像圆圈:n→∞π/n→0cos(π/n)→1 r→R

您可以以不同方式划分多边形。例如,像这样:

polygon with the point in the center

这样你就会有n个三角形。

或者你可以这样:

another way

这样你就会有n-2个三角形。

可能有更好的方法,在网上搜索它们。