繁体   English   中英

在OpenGL ES 2.0中使用多个纹理

[英]working with multiple Textures in OpenGL ES 2.0

我遇到了麻烦的问题。

我有一个DrawableTexturedPlane类。 此类指定带有字节数组的简单Plane作为Texture。 可以通过简单的函数调用来更改数组数据。 好了,该类将特定的着色器程序作为参数。 在我的OpenGL视图中,我需要其中两个平面。 好吧,第一个正确渲染,但是第二个根本不显示。 我很确定我错过了一些东西,但是我不确定代码的哪一部分不正确。 我将不胜感激任何帮助 ;)

TexturedPlaneClass(相关部分):

public DrawableTexturedPlane( float[] fVertices, int nProgrammHandle, int nGLTextureChannel, int nGLTextureID ) 
    {  
        super( nProgrammHandle );
        m_bHasBorder = bHasBorder;

        m_nGLTextureChannel = nGLTextureChannel;
        m_oVertexBuffer = _GetVertexBuffer( fVertices );
        m_oTextureBuffer = _GetTextureCoordinates();



        GLES20.glActiveTexture( m_nGLTextureChannel );  
        // Set filtering
        GLES20.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST );
        GLES20.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST );
        GLES20.glTexParameteri( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
        GLES20.glTexParameteri( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );

        m_cTexture = new byte[ 640 * 480 ];


        Log.i( "Java/DrawableTexturedPlane", "Drawable Textured Plane created!" );
    }

绘制方法:

 @Override
    public void Draw( float[] mvpMatrix )
    {

        GLES20.glActiveTexture( m_nGLTextureChannel );

        GLES20.glEnable(GLES20.GL_TEXTURE_2D);
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);

        GLES20.glUseProgram( m_nProgramHandle );


        m_HTextureUniform = GLES20.glGetUniformLocation( m_nProgramHandle, "uTexture" );
        m_HTextureCoordinate = GLES20.glGetAttribLocation( m_nProgramHandle, "TexCoordinate" );


        // get handle to the vertex shader's vPosition member
        m_nPositionHandle = GLES20.glGetAttribLocation( m_nProgramHandle, "vPosition" );


        ByteBuffer oDataBuf = ByteBuffer.wrap( m_cTexture );
        // Prepare the triangle data
        GLES20.glTexImage2D( GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, 640, 480,
                             0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, oDataBuf );

        // Prepare the triangle data
        GLES20.glVertexAttribPointer( m_nPositionHandle, 3, GLES20.GL_FLOAT, false, 12, m_oVertexBuffer );
        GLES20.glEnableVertexAttribArray( m_nPositionHandle );

        GLES20.glVertexAttribPointer( m_HTextureCoordinate, 2, GLES20.GL_FLOAT, false, 12, m_oTextureBuffer); 
        GLES20.glEnableVertexAttribArray( m_HTextureCoordinate );

        m_nMVPMatrixHandle = GLES20.glGetUniformLocation( m_nProgramHandle, "uMVPMatrix");

        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv( m_nMVPMatrixHandle, 1, false, mvpMatrix, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);

    }

这是创建纹理和2个平面的方法:

 GLES20.glGenTextures( m_nTextureStorage.length, m_nTextureStorage, 0);

        for( int i : m_nTextureStorage )
        {
            GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, i );
        }


        m_oInfoPlane = new DrawableTexturedPlane( oInfoRect.GetAsFloatArray(), true, m_nShaderPrograms[0], GLES20.GL_TEXTURE0, m_nTextureStorage[0] );   
        m_oMainPlane =  new DrawableTexturedPlane( oMainRect.GetAsFloatArray(), true, m_nShaderPrograms[0], GLES20.GL_TEXTURE1, m_nTextureStorage[1] );

好点是,如果我同时初始化GL_TEXTURE0,它就可以正常工作。 (尽管在10到20秒后会出现严重的闪烁,但我不知道为什么)。

如果我按照上面的方法进行初始化,则只能正确显示具有TEXTURE_0的一个,而另一个则为黑色。

我知道我应该写一个TextureManager类,但是对于2个Textures,这是一个简单的矫kill过正。

提前致谢

您没有在正确的时间绑定纹理。 另外,您不能将2种不同的纹理绑定到GL_TEXTURE_2D。

它看起来应该像这样:

GLES20.glActiveTexture( m_nGLTextureChannel ); //Activate the texture channel

  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, nGLTextureID); //Bind here!
//Bind before you draw

我知道我应该写一个TextureManager类,但是对于2个Textures,这是一个简单的矫kill过正。

至于这不是现在,而是整个项目的需求。 以后需要经理吗? 前期设计会有所收获,但不会太过分。 (金达自相矛盾!祝你好运!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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