OpenGL ES 纹理不渲染

OpenGL ES Texture Not Rendering

我正在尝试在 iPhone 上使用 OpenGL ES 将纹理渲染到平面。我以前使用过 OpenGL,但我不确定为什么它不起作用。

当我 运行 代码时,平面呈现为黑色正方形而不是纹理正方形。我相信问题可能出在加载纹理时,尽管我在 运行 代码时没有看到任何错误。

希望有人能发现问题并提供帮助。提前致谢。

这是我的网格代码。

// Mesh loading
- ( id ) init {
    if ( self = [ super init ] ) {
        glGenVertexArraysOES( 1, &m_vertexArray );
        glBindVertexArrayOES( m_vertexArray );

        glGenBuffers( 1, &m_vertexBuffer );
        glBindBuffer( GL_ARRAY_BUFFER, m_vertexBuffer );
        glBufferData( GL_ARRAY_BUFFER, sizeof( g_vertices ), g_vertices, GL_STATIC_DRAW );

        glGenBuffers( 1, &m_texCoordBuffer );
        glBindBuffer( GL_ARRAY_BUFFER, m_texCoordBuffer );
        glBufferData( GL_ARRAY_BUFFER, sizeof( g_texCoords ), g_texCoords, GL_STATIC_DRAW );
    }
    return self;
}

- ( void ) render {
    glBindBuffer( GL_ARRAY_BUFFER, m_vertexBuffer );
    glVertexAttribPointer( GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, ( GLvoid* ) 0 );

    glBindBuffer( GL_ARRAY_BUFFER, m_texCoordBuffer );
    glVertexAttribPointer( GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, ( GLvoid* ) 0 );

    glDrawArrays( GL_TRIANGLES, 0, sizeof( g_vertices ) / sizeof( g_vertices[ 0 ] ) );
}

const GLfloat g_vertices[] = {
    -1.0, -1.0,  0.0,
     1.0,  1.0,  0.0,
    -1.0,  1.0,  0.0,

    -1.0, -1.0,  0.0,
     1.0, -1.0,  0.0,
     1.0,  1.0,  0.0
};

const GLfloat g_texCoords[] = {
    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0
};

我现在只需要我的顶点和纹理坐标,所以这就是我正在使用的全部。

接下来是我的纹理加载。

- ( id ) init: ( NSString* ) filename {
    if ( self = [ super init ] ) {
        CGImageRef spriteImage = [ UIImage imageNamed: filename ].CGImage;
        if ( !spriteImage ) {
            NSLog( @"Failed to load image %@", filename );
            exit( 1 );
        }

        size_t width = CGImageGetWidth( spriteImage );
        size_t height = CGImageGetHeight( spriteImage );

        GLubyte *spriteData = ( GLubyte* ) calloc( width * height * 4, sizeof( GLubyte ) );
        CGContextRef spriteContext = CGBitmapContextCreate( spriteData, width, height, 8, 4 * width, CGImageGetColorSpace( spriteImage ), kCGImageAlphaPremultipliedLast );

        CGContextDrawImage( spriteContext, CGRectMake( 0, 0, width, height ), spriteImage );
        CGContextRelease( spriteContext );

        glGenTextures( 1, &m_texture );
        glBindTexture( GL_TEXTURE_2D, m_texture );

        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, ( GLuint ) width, ( GLuint ) height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData );

        free( spriteData );
    }
    return self;
}

- ( void ) bind {
    glActiveTexture( GL_TEXTURE0 );
    glBindTexture( GL_TEXTURE_2D, m_texture );
}

我使用了 this 教程中的纹理加载代码。

那么这是我的渲染代码。

    - ( void ) glkView: ( GLKView* ) view drawInRect: ( CGRect ) rect {
    glClearColor( 0.65f, 0.65f, 0.65f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glUseProgram( m_shaderProgram );

    [ m_texture bind ];
     glUniform1i( uniforms[ UNIFORM_SAMPLER ], 0 );

    GLKMatrix4 mvp = GLKMatrix4Multiply( GLKMatrix4Multiply( m_projectionMatrix, m_viewMatrix ), m_modelMatrix );

    glUniformMatrix4fv( uniforms[ UNIFORM_MODELVIEWPROJECTION_MATRIX ], 1, 0, mvp.m );

    glEnableVertexAttribArray( GLKVertexAttribPosition );
    glEnableVertexAttribArray( GLKVertexAttribTexCoord0 );

    [ m_plane render ];

    glDisableVertexAttribArray( GLKVertexAttribTexCoord0 );
    glDisableVertexAttribArray( GLKVertexAttribPosition );
}

顶点着色器。

attribute vec3 position;
attribute vec2 texCoord;

varying lowp vec2 texCoord0;

uniform mat4 modelViewProjectionMatrix;

void main()
{
    texCoord0 = texCoord;
    gl_Position = modelViewProjectionMatrix * vec4( position, 1.0 );
}

最后是片段着色器。

varying lowp vec2 texCoord0;

uniform sampler2D sampler;

void main()
{
    gl_FragColor = texture2D( sampler, texCoord0.st );
}

如评论中所述,您可以使用二的幂 (POT) 纹理进行检查。此外,还有支持 NonPOT (NPOT) 纹理的扩展,如 GL_IMG_texture_npot,请参阅此线程中的讨论 (Non power of two textures in iOS), and this thread (http://aras-p.info/blog/2012/10/17/non-power-of-two-textures/)。