ios - OpenGL ES Texture Not Rendering -
i attempting render texture plane opengl es on iphone. have worked opengl before i'm not sure why isn't working.
when run code plane rendered black square , not textured square. believe problem may when loading texture although see no errors when run code.
hopefully spot problem , able help. in advance.
here code mesh.
// 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 };
i need vertices , tex coords right i'm using.
next texture loading.
- ( id ) init: ( nsstring* ) filename { if ( self = [ super init ] ) { cgimageref spriteimage = [ uiimage imagenamed: filename ].cgimage; if ( !spriteimage ) { nslog( @"failed 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 ); }
i used texture loading code this tutorial.
then here rendering code.
- ( 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 ); }
vertex shader.
attribute vec3 position; attribute vec2 texcoord; varying lowp vec2 texcoord0; uniform mat4 modelviewprojectionmatrix; void main() { texcoord0 = texcoord; gl_position = modelviewprojectionmatrix * vec4( position, 1.0 ); }
and lastly fragment shader.
varying lowp vec2 texcoord0; uniform sampler2d sampler; void main() { gl_fragcolor = texture2d( sampler, texcoord0.st ); }
as mentioned in comment, can check power of 2 (pot) texture. in addition, there extensions enable support nonpot (npot) textures gl_img_texture_npot, refer discussion in thread (non power of 2 textures in ios), , thread (http://aras-p.info/blog/2012/10/17/non-power-of-two-textures/).
Comments
Post a Comment