Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓-画一个圆柱体_Android_Opengl Es - Fatal编程技术网

Android 安卓-画一个圆柱体

Android 安卓-画一个圆柱体,android,opengl-es,Android,Opengl Es,我正在使用OpenGL,我已经把所有东西都安装好并运行起来了。我可以画三角形和其他形状,但现在我需要做一个管子。它不需要中空(如果这样更容易) 大概是这样的: 如何在3D中定义cilinder?我想我需要x,y,z和半径。有人能告诉我从哪里开始吗?多谢各位 绘制三角形代码: class Triangle { private final String vertexShaderCode = // This matrix member variable provides a h

我正在使用OpenGL,我已经把所有东西都安装好并运行起来了。我可以画三角形和其他形状,但现在我需要做一个管子。它不需要中空(如果这样更容易)

大概是这样的:

如何在3D中定义cilinder?我想我需要x,y,z和半径。有人能告诉我从哪里开始吗?多谢各位

绘制三角形代码:

 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);
 }

无论您在内部如何定义它,您仍然必须为OpenGL ES提供一组三角形、线或点。 因此,您需要定义两个位于平行平面上的相等正多边形。顶点越多,它们与圆的距离就越大。您可以通过公式自己看到这一点:
R
是外切圆的半径,
R
是内切圆的半径,
n
是顶点数。
r
距离
r
越近,多边形距离圆越近。因此,如果使用更多顶点,多边形将更像一个圆:当
n→∞
π/n→0
cos(π/n)→1
so
r→R

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

这样您将拥有
n
三角形

或者你可以走这条路:

这样,您将拥有
n-2个
三角形

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