• Advertisement
Sign in to follow this  

Problem with gles 2 textures

This topic is 1973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm learning gles 2 android ndk at the moment, but I can't display a texture. I expect my code to render a black texture on the screen, but all I get is some color garbage. This is my rendering routine:
[source lang="cpp"] gl_context::create_context( s );

glViewport( 0 , 0 , gl_context::width() , gl_context::height() );
glClearColor( 0.0f , 0.0f , 0.0f , 0.0f );

GLuint shader_program = 0;
GLuint position = 0;
GLuint texture_coord = 1;
GLuint sampler = 0;

const char* vertex_shader_source =
"attribute vec4 position; \n"
"attribute vec2 a_texture_coord; \n"
"varying vec2 v_texture_coord; \n"
"void main() \n"
"{ \n"
" gl_Position = position; \n"
" v_texture_coord = a_texture_coord; \n"
"} \n";

const char* fragment_shader_source =
"precision mediump float; \n"
"uniform sampler2D sampler; \n"
"varying vec2 v_texture_coord; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D( sampler , v_texture_coord );"
//" gl_FragColor = vec4( 1.0 , 0.0 , 0.0 , 1.0 );" //uncommenting this results in a red screen, so I guess I have a valid gles 2 context
"} \n";

GLuint vertex_shader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertex_shader , 1 , &vertex_shader_source , 0 );
glCompileShader( vertex_shader );

GLuint fragment_shader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragment_shader , 1 , &fragment_shader_source , 0 );
glCompileShader( fragment_shader );

shader_program = glCreateProgram();
glAttachShader( shader_program , vertex_shader );
glAttachShader( shader_program , fragment_shader );

glBindAttribLocation( shader_program , position , "position" );
glBindAttribLocation( shader_program , texture_coord , "a_texture_coord" );

glLinkProgram( shader_program );

glUseProgram( shader_program );

sampler = glGetUniformLocation( shader_program , "sampler" );

glActiveTexture( GL_TEXTURE0 );

glBindTexture( GL_TEXTURE_2D , 0 ) ;
std::vector< GLubyte > buffer( 64 * 64 * 3 , 0 );
glTexImage2D( GL_TEXTURE_2D , 0 , GL_RGB , 64 , 64 , 0 , GL_RGB , GL_UNSIGNED_BYTE , buffer.data() );

glUniform1i( sampler , 0 );

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


const GLfloat vertex_positions[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};

const GLfloat texture_coords[] =
{
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
};

glVertexAttribPointer( position , 2 , GL_FLOAT , GL_FALSE , 0 , vertex_positions );
glEnableVertexAttribArray( position );

glVertexAttribPointer( texture_coord , 2 , GL_FLOAT , GL_FALSE , 0 , texture_coords );
glEnableVertexAttribArray( texture_coord );

glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );


glDisableVertexAttribArray( position );
glDisableVertexAttribArray( texture_coord );


gl_context::swap_buffers();[/source]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement