簡體   English   中英

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 );
}

我使用了教程中的紋理加載代碼。

這是我的渲染代碼。

    - ( 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 );
}

如評論中所述,您可以使用2的冪(POT)紋理進行檢查。 此外,還有一些擴展支持諸如GL_IMG_texture_npot之類的NonPOT(NPOT)紋理,請參閱該線程( iOS中兩個紋理的非冪)的討論以及該線程( http://aras-p.info/blog / 2012/10/17 / non-of-of-two-textures / )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM