OpenGL ES 2:VBO和纹理

时间:2014-10-21 13:48:22

标签: android opengl-es textures vbo

我的计划确实:

  1. 将纹理加载到GPU。
  2. 将uv坐标的VBO加载到GPU。
  3. 将模式坐标的VBO加载到GPU。
  4. 将GPU的VBO加载到GPU。
  5. 绑定纹理。
  6. 绑定到uv坐标VBO。
  7. 绑定到模型坐标VBO。
  8. 绑定指数协调员VBO。
  9. DRAW
  10. 但实际上只绘制了每个纹理的一部分。如果我使用所有相同的数组并且在没有VBO的情况下进行绘制(从客户端每次绘制移动)它就会很好......

    有什么想法吗?现在已经抓了我几个小时。 我在下面发布了相关代码:

    加载到GPU:

        this.texture = new int[1];
        GLES20.glGenTextures(1, this.texture, 0);
    
        //GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.texture[0]);
    
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    
        // Generate VBOs for UV data and model data. My indece VBO is done elswhere
        this.vbo = new int[2];
        GLES20.glGenBuffers(2,vbo,0);
    
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.vbo[0] );
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, uvsBuff.capacity() * FH_Utilities.SHORT_SIZE,
                uvsBuff, GLES20.GL_STATIC_DRAW);
    
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.vbo[1]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, posBuff.capacity() * FH_Utilities.SHORT_SIZE,
                posBuff, GLES20.GL_STATIC_DRAW);
    
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    

    附图中:

    GLES20.glUseProgram(this.terrainShader.mProgram);

        int mtrxhandle = GLES20.glGetUniformLocation(this.terrainShader.mProgram, "uMVPMatrix");
        GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, mvpMatrix, 0);
    
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.texture[0]);
        int mSamplerLoc = GLES20.glGetUniformLocation (this.terrainShader.mProgram, "s_texture" );
        GLES20.glUniform1i ( mSamplerLoc, 0);
    
        int mTexCoordLoc = GLES20.glGetAttribLocation(this.terrainShader.mProgram, "a_texCoord");
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.vbo[0]);
        GLES20.glEnableVertexAttribArray(mTexCoordLoc);
        GLES20.glVertexAttribPointer(mTexCoordLoc, 2,
                GLES20.GL_SHORT, false,
                0, 0);
    
        int mPositionHandle = GLES20.glGetAttribLocation(this.terrainShader.mProgram, "vPosition");
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.vbo[1]);
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, 3, 
                GLES20.GL_SHORT, false, 0, 0);
    
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.drawHandles[0][0]);
    
        GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, this.drawHandles[0][1],     GLES20.GL_UNSIGNED_SHORT, 0);
    
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
    
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mTexCoordLoc);
    

    GENERATE INDECES:

    private int[][] genIndeces(int tileSize, int Nindexes){
    
            int[][] indeceHandles = new int[Nindexes][2];
    
            int         indMaxLen       = ((tileSize-1) * ((tileSize*2) +2))-2;
            int         indMaxBytes     = indMaxLen * FH_Utilities.SHORT_SIZE;
            short[][]   index           = new short[tileSize][tileSize];
            short[]     IDA             = new short[indMaxLen];
            ByteBuffer bInd;
            int nIndeces;
            int x,y;
    
            /* Build an Indece Buffer for course triangle strips */
            nIndeces = 0;
            for(y=0;y<tileSize;y++){
                for(x=0;x<tileSize;x++){
                    index[y][x] = (short)nIndeces;
                    nIndeces++;
                }
            }
    
            /*Run through however many resolutions desired */
    
            bInd = ByteBuffer.allocateDirect(indMaxBytes).order(ByteOrder.nativeOrder());
            ShortBuffer sInd = bInd.asShortBuffer();
    
            nIndeces=0;
    
    
            for(y=0;y<tileSize-1;y++){
                for(x=0;x<tileSize;x++){
    
                    IDA[nIndeces] = index[y+1][x];
                    nIndeces++;
                    IDA[nIndeces] = index[y][x]; 
                    nIndeces++;
    
                    if(x==tileSize-1 && y != tileSize -2) { 
                        IDA[nIndeces] = index[y][x];
                        nIndeces++;
                        IDA[nIndeces] = index[y+2][0];
                        nIndeces++;
                    }
    
                }
            }
    
            sInd.clear();
            sInd.put(IDA, 0, nIndeces);
            sInd.position(0);
    
            int tempHandle[] = new int[1];
    
            GLES20.glGenBuffers(1,tempHandle,0);
            if(GLES20.glGetError()!=GLES20.GL_NO_ERROR){
                System.out.println("FH_ERROR- "+GLES20.glGetError());
                System.exit(1);
            }
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, tempHandle[0]);
            if(GLES20.glGetError()!=GLES20.GL_NO_ERROR){
                System.out.println("FH_ERROR- "+GLES20.glGetError());
                System.exit(1);
            }  
            GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, sInd.capacity() * FH_Utilities.SHORT_SIZE, sInd, GLES20.GL_STATIC_DRAW);
            if(GLES20.glGetError()!=GLES20.GL_NO_ERROR){
                System.out.println("FH_ERROR- "+GLES20.glGetError());
                System.exit(1);
            }
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
            if(GLES20.glGetError()!=GLES20.GL_NO_ERROR){
                System.out.println("FH_ERROR- "+GLES20.glGetError());
                System.exit(1);
            }
    
            indeceHandles[0][0] = tempHandle[0];
            indeceHandles[0][1] = nIndeces;
    
    
    
            bInd.limit(0);
            sInd.limit(0);
            bInd=null;
            sInd=null;
            IDA=null;
            index=null;
            System.gc();
    
            return indeceHandles;
    
    
        }
    

0 个答案:

没有答案