我使用三角形绘制曲面(Android-Studio opengl es 1)如何在此曲面上加载一个纹理

时间:2016-04-15 16:56:45

标签: opengl-es

Illustration

来源: 公共类GLsufaceT {     private FloatBuffer vertBuff,texBuffer;在这里输入代码     私人ShortBuffer pBuff;

private float [] vertices ={ //Coordinates
         -4,-2,0  
        ,-3,-2,0
        ,-2,-3,0
        ,-1,-3,0
        ,0,-3,0
        ,1,-3,0
        ,2,-3,0
        ,3,-3,0
        ,4,-3,0
        ,-4,-2,0
        ,-3,-2,0
        ,-2,-2,0
        ,-1,-2,0
        ,0,-2,0
        ,1,-2,0
        ,2,-2,0
        ,3,-2,0
        ,4,-2,0
        ,-4,-1,0
        ,-3,-1,0
        ,-2,-1,0
        ,-1,-1,0
        ,0,-1,0
        ,1,-1,0
        ,2,-1,0
        ,3,-1,0
        ,4,-1,0
        ,-4, 0,0
        ,-3, 0,0
        ,-2, 0,0
        ,-1, 0,0
        ,0, 0,0
        ,1, 0,0
        ,2, 0,0
        ,3, 0,0
        ,4, 0,0
        ,-4, 1,0
        ,-3, 1,0
        ,-2, 1,0
        ,-1, 1,0
        ,0, 1,0
        ,1, 1,0
        ,2, 1,0
        ,3, 1,0
        ,4, 1,0
        ,-4, 2,0
        ,-3, 2,0
        ,-2, 2,0
        ,-1, 2,0
        ,0, 2,0
        ,1, 2,0
        ,2, 2,0
        ,3, 2,0
        ,4, 2,0
        ,-4, 3,0
        ,-3, 3,0
        ,-2, 3,0
        ,-1, 3,0
        ,0, 3,0
        ,1, 3,0
        ,2, 3,0
        ,3, 3,0
        ,4, 3,0



};
float[] texCoords = {

        0.0f, 0.0f,
        1f,  0.0f,
        0.0f, 1f,
        1f,  1f,
};

private short [] pindex = {0,1,9 //三角形                             ,1,9,10                              ,1,2,10                              ,2,10,11                              ,2,3,11                              ,-3,11,12-                              ,3,4,12                              ,4,12,13                              ,4,5,13                              ,5,13,​​14                              ,5,6,14                              ,6,14,15                              ,6,7,15                              ,7,15,16                              ,7,8,16                              ,8,16,17                              ,9,10,18                              ,10,18,19                              ,10,11,19                              ,11,19,20                              ,-11,12,20-                              ,12,20,21                              ,12,13,21                              ,13,21,22                              ,13,14,22                              ,14,22,23                              ,14,15,23                              ,15,23,24                              ,15,16,24                              ,16,24,25                              ,16,17,25                              ,17,25,26                              ,18,19,27                              ,19,27,28                              ,19,20,28                              ,20,28,29                              ,20,21,29                              ,21,29,30                              ,21,22,30                              ,22,30,31                              ,22,23,31                              ,23,31,32                              ,23,24,32                              ,24,32,33                              ,24,25,33                              ,25,33,34                              ,25,26,34                              ,26,34,35                              ,27,28,36                              ,28,36,37                              ,28,29,37                              ,29,37,38                              ,29,30,38                              ,30,38,39                              ,30,31,39                              ,31,39,40                              ,31,32,40                              ,32,40,41                              ,32,33,41                              ,33,41,42                              ,33,34,42                              ,34,42,43                              ,34,35,43                              ,35,43,44                              ,36,37,45                              ,37,45,46                              ,37,38,46                              ,38,46,47                              ,38,39,47                              ,39,47,48                              ,39,40,48                              ,40,48,49                              ,40,41,49                              ,41,49,50                              ,41,42,50                              ,42,50,51                              ,42,43,51                              ,43,51,52                              ,43,44,52                              ,44,52,53                              ,45,46,54                              ,46,54,55                              ,46,47,55                              ,47,55,56                              ,47,48,56                              ,48,56,57                              ,48,49,57                              ,49,57,58                              ,49,50,58                              ,50,58,59                              ,50,51,59                              ,51,59,60                              ,51,52,60                              ,52,60,61                              ,52,53,61                              ,53,61,62}; * /     int [] textureIDs = new int 1; // 1个纹理ID的数组(新)

public GLsufaceT() {
    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);
    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pindex.length * 2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pindex);
    pBuff.position(0);
    ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    texBuffer = tbb.asFloatBuffer();
    texBuffer.put(texCoords);
    texBuffer.position(0);

}
public void draw(GL10 gl) {
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
   gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, pindex.length,
            GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_FRONT);
}

public void loadTexture(GL10 gl, Context context) {
    gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); // Bind to texture ID


    // Set up texture filters
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    // Construct an input stream to texture image
    InputStream istream = context.getResources().openRawResource(R.drawable.jay);
    Bitmap bitmap;
    try {
        // Read and decode input as bitmap
        bitmap = BitmapFactory.decodeStream(istream);
    } finally {
        try {
            istream.close();
        } catch (IOException e) {
        }
    }
    // Build Texture from loaded bitmap for the currently-bind texture ID
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();
}

}

0 个答案:

没有答案