How did you draw a cylinder with OpenGLES?

How do you draw a cylinder with OpenGLES?

+3
source share
5 answers

You will need to do this by loading the object. You cannot invoke 3D form primitives using Open GL ES.

Check out Jeff Lamarche's blog, there are many really good resources on how to place objects there. link text

+1
source

- , . . , . , , 64. .

for (i = 0; i < 64; i++)
{
    angle = 360 * i / 63;  // Or perhaps 2 * PI * i / 63
    cx[i] = sin(angle);
    cy[i] = cos(angle);
}

for (i = 0; i < 63; i++)
{
    v0 = Vertex(cx[i], cy[i], 0);
    v1 = Vertex(cx[i + 1], cy[i + 1], 0);
    v2 = Vertex(cx[i], cy[i], 1);
    v3 = Vertex(cx[i + 1], cy[i + 1], 1);

    DrawTriangle(v0, v1, v2);
    DrawTriangle(v1, v3, v2);
    // If you have it:  DrawQuad(v0, v1, v3, v2);
}

. , , , , .

, , .

+3

OpenGL ES, . GLUT | ES (, ..) glutes_geometry.c. , glBegin() glEnd(), OpenGL ES.

OpenGL ES .

+1

, , OpenGLES 2.0 Android.

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Cylinder {

    public Cylinder(int n) {

        this.numOfVertex = n;

        float[] vertex = new float[3 * (n + 1) * 2];
        byte[] baseIndex = new byte[n];
        byte[] topIndex = new byte[n];
        byte[] edgeIndex = new byte[n*2 + 2];

        double perAngle = 2 * Math.PI / n;

        for (int i = 0; i < n; i++) {
            double angle = i * perAngle;
            int offset = 6 * i;

            vertex[offset + 0] = (float)(Math.cos(angle) * radious) + cx;
            vertex[offset + 1] = -height;
            vertex[offset + 2] = (float)(Math.sin(angle) * radious) + cy;

            vertex[offset + 3] = (float)(Math.cos(angle) * radious) + cx;
            vertex[offset + 4] = height;
            vertex[offset + 5] = (float)(Math.sin(angle) * radious) + cy;

            topIndex[i] = (byte)(2*i);

            baseIndex[i] = (byte)(2*i +1);

            edgeIndex[2*i + 1] = baseIndex[i];
            edgeIndex[2*i] = topIndex[i];

        }


        edgeIndex[2*n] = topIndex[0];
        edgeIndex[2*n+1] = baseIndex[0];

        ByteBuffer vbb = ByteBuffer
                .allocateDirect(vertex.length * 4)
                .order(ByteOrder.nativeOrder());

        mFVertexBuffer = vbb.asFloatBuffer();
        mFVertexBuffer.put(vertex);
        mFVertexBuffer.position(0);

        normalBuffer = mFVertexBuffer;

        mCircleBottom = ByteBuffer.allocateDirect(baseIndex.length);
        mCircleBottom.put(baseIndex);
        mCircleBottom.position(0);

        mCircleTop = ByteBuffer.allocateDirect(topIndex.length);
        mCircleTop.put(topIndex);
        mCircleTop.position(0);

        mEdge = ByteBuffer.allocateDirect(edgeIndex.length);
        mEdge.put(edgeIndex);
        mEdge.position(0);
    }

    public void draw(GL10 gl)
    {
        gl.glCullFace(GL10.GL_BACK);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glPushMatrix();

        gl.glColor4f(1f, 0, 0, 0);
        gl.glDrawElements( GL10.GL_TRIANGLE_STRIP, numOfVertex * 2 + 2, GL10.GL_UNSIGNED_BYTE, mEdge);
        gl.glPopMatrix();
        gl.glPushMatrix();

        gl.glColor4f(0.9f, 0, 0, 0);
        gl.glDrawElements( GL10.GL_TRIANGLE_FAN, numOfVertex, GL10.GL_UNSIGNED_BYTE, mCircleTop);
        gl.glPopMatrix();

        gl.glPushMatrix();

        gl.glTranslatef(0, 2*height, 0);
        gl.glRotatef(-180, 1, 0, 0); 

        gl.glColor4f(0.9f,0, 0, 0);
        gl.glDrawElements( GL10.GL_TRIANGLE_FAN, numOfVertex , GL10.GL_UNSIGNED_BYTE, mCircleBottom);
        gl.glPopMatrix();

    }

    private FloatBuffer mFVertexBuffer;
    private FloatBuffer normalBuffer;
    private ByteBuffer mCircleBottom;
    private ByteBuffer mCircleTop;
    private ByteBuffer mEdge;
    private int numOfVertex;
    private int cx = 0;
    private int cy = 0;
    private int height = 1;
    private float radious = 1;
}
+1

, . , , , , , . , .

I created a module for Unity3D in C # that does just that and allows you to tweak the settings. You should be able to easily convert to C or C ++, since the calculation of geometry is the same everywhere. Watch the video to find out what it means and download the code from GitHub .

0
source

Source: https://habr.com/ru/post/1711464/


All Articles