2017-05-02 60 views
0

私はOpenGLを使用しています。私は三角形や他の形をプロットすることができますが、今はチューブを作る必要があります。中空にする必要はありません。このようなAndroid - 円柱を描く

何か:私は3Dでcilinderを定義することができますどのようにImage of the 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); 
} 

答えて

0

は、どんなにあなたが内部的にそれをどのように定義するか、あなたはまだ、三角形、ラインまたはOpenGL ESのためのポイントのセットを提供する必要がありません。 したがって、平行な平面に置かれた2つの等しい正則なポリゴンを定義する必要があります。コサを持つ頂点が多いほど、サークルになります。あなたは公式でこれをあなた自身で見ることができます: 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三角形を持つことになりますこの方法です。

より良い方法がありますが、ネット上でそれらを検索することができます。