简体   繁体   中英

How to extrude one flat polygon to 3d object in android opengl es2.0?

I have one 2d flat polygon data. String s4="375.7,216.8 384.4,234.1 397.1,231.6 419.4,275.1";
I can show 2d picture in android opengl es. But now I need extrude it to 3d object. Many friends suggestion I create it according to theirs'or

Way:each vertex in your vertex array, add (0,0,z) to it, and add the result to your vertex array, for a total of 2*n vertices. So, for your example, you will add the vertices (0,0,z), (10,0,z), (10,10,z), (0,10,z) to your vertex array...

float[] vertices = new float[sarr.length*6]; short[] indices = new short[(sarr.length-2)*6+sarr.length*6];
..... But I can't get the correct result.

who can provide codes to help me in extrude in android ?

Lets assume you have a vector of vertices, and a vector of indices.

vector vertices; vector indices;

Step 1: Now, you first need to create the extruded surface, which is a copy of the same surface on the extruded distance and reverse the triangles to face opposite direction.

unsigned int originalVertexCount = vertices.size();
unsigned int originalIndexCount = indices.size();
for (int i = 0; i < vertices.size(); i++) {
    Vertex v = vertices[i];
    v.z += extrude;
    vertices.push_back(v);
}

and the indices is created like this.

for (int i = 0; i < indices.size(); i+=3) {
    unsigned int ind1 = indices[i + 0] + originalVertexCount;
    unsigned int ind2 = indices[i + 1] + originalVertexCount;
    unsigned int ind3 = indices[i + 2] + originalVertexCount;
    indices.push_back(ind3);
    indices.push_back(ind2);
    indices.push_back(ind1);
}

The next step is to fill the sides of the surfaces.

for (int i = 0; i < originalVertexCount; i++) {
    int nextIndex = i + 1;
    if(i + 1 > originalVertexCount)
        nextIndex = 0;
    unsigned int ind1 = indices[i];
    unsigned int ind2 = indices[i] + originalVertexCount;
    unsigned int ind3 = indices[nextIndex] + originalVertexCount;
    indices.push_back(ind1);
    indices.push_back(ind2);
    indices.push_back(ind3);
    unsigned int ind4 = indices[nextIndex] + originalVertexCount;
    unsigned int ind5 = indices[nextIndex];
    unsigned int ind6 = indices[i];
    indices.push_back(ind4);
    indices.push_back(ind5);
    indices.push_back(ind6);
}

The code was not 100% tested but it should work fine as the concept is the same as what we are using for production purpose in our application. Also, this extrusion is for z-axis, and you can apply the same in other axis based on your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM