Sunday, 15 February 2015

OpenGL shader with glVertexAttribPointer -



OpenGL shader with glVertexAttribPointer -

i next lazyfoo.net opengl tutorials , having problem in getting textured shaders render @ all. have tried solve issue lastly couple days , need help don't know next. using opengl 2.1 glsl 1.2, have tried on opengl 3.2 glsl 1.5.

from can tell, problem revolves around glvertexattribpointer function , communication shaders. when took out glvertexattribpointer , used glvertexpointer gl_vertex_array (gl_vertex instead of position attribute in shader) able primitive shapes render. textures able render without shaders, want able utilize them.

here shader program:

texture2dprogram::texture2dprogram() { _vertex_position_2d_location = 0; _texture_coordinate_location = 0; _projection_matrix_location = 0; _model_view_matrix_location = 0; _texture_color_location = 0; _texture_unit_location = 0; } bool texture2dprogram::load_program() { //generate programme _program_id = glcreateprogram(); std::string vertex_shader_string = (std::string)("#version 120\n") + "uniform mat4 projection_matrix;\n" + "uniform mat4 model_view_matrix;\n" + "attribute vec2 vertex_position;\n" + "attribute vec2 texture_coordinate;\n" + "varying vec2 texcoord;\n" + "void main() {\n" + "texcoord = texture_coordinate;\n" + "gl_position = projection_matrix * model_view_matrix * vec4( vertex_position.x, vertex_position.y, 0.0, 1.0 );\n" + "}\n"; //load vertex shader gluint vertex_shader = load_shader_from_string( vertex_shader_string, gl_vertex_shader ); //check errors if( vertex_shader == 0 ) { gldeleteprogram( _program_id ); _program_id = 0; homecoming false; } //attach vertex shader programme glattachshader( _program_id, vertex_shader ); std::string fragment_shader_string = (std::string)("#version 120\n") + "uniform vec4 texture_color;\n" + "uniform sampler2d texture_unit;\n" + "varying vec2 texcoord;\n" + "void main() { \n" + "gl_fragcolor = texture2d(texture_unit,texcoord) * texture_color;\n" + "}\n"; //create fragment shader gluint fragment_shader = load_shader_from_string( fragment_shader_string, gl_fragment_shader ); //check errors if( fragment_shader == 0 ) { gldeleteprogram( _program_id ); _program_id = 0; homecoming false; } //attach fragment shader programme glattachshader( _program_id, fragment_shader ); //link programme gllinkprogram( _program_id ); //check errors glint programsuccess = gl_true; glgetprogramiv( _program_id, gl_link_status, &programsuccess ); if( programsuccess != gl_true ) { printf( "error linking programme %d!\n", _program_id ); print_program_log( _program_id ); gldeleteprogram( _program_id ); _program_id = 0; homecoming false; } //get variable locations _vertex_position_2d_location = glgetattriblocation( _program_id, "vertex_position" ); if( _vertex_position_2d_location == -1 ) { printf( "%s not valid glsl programme variable!\n", "vertex_position" ); } _texture_coordinate_location = glgetattriblocation( _program_id, "texture_coordinate" ); if( _texture_coordinate_location == -1 ) { printf( "%s not valid glsl programme variable!\n", "texture_coordinate" ); } _texture_color_location = glgetuniformlocation( _program_id, "texture_color" ); if( _texture_color_location == -1 ) { printf( "%s not valid glsl programme variable!\n", "texture_color" ); } _texture_unit_location = glgetuniformlocation( _program_id, "texture_unit" ); if( _texture_unit_location == -1 ) { printf( "%s not valid glsl programme variable!\n", "texture_unit" ); } _projection_matrix_location = glgetuniformlocation( _program_id, "projection_matrix" ); if( _projection_matrix_location == -1 ) { printf( "%s not valid glsl programme variable!\n", "projection_matrix" ); } _model_view_matrix_location = glgetuniformlocation( _program_id, "model_view_matrix" ); if( _model_view_matrix_location == -1 ) { printf( "%s not valid glsl programme variable!\n", "model_view_matrix" ); } homecoming true; } void texture2dprogram::set_vertex_pointer( glsizei stride, const glvoid* info ) { glvertexattribpointer( _vertex_position_2d_location, 2, gl_float, gl_false, stride, info ); } void texture2dprogram::set_texture_coordinate_pointer( glsizei stride, const glvoid* info ) { glvertexattribpointer( _texture_coordinate_location, 2, gl_float, gl_false, stride, info ); } void texture2dprogram::enable_vertex_pointer() { glenablevertexattribarray( _vertex_position_2d_location ); } void texture2dprogram::disable_vertex_pointer() { gldisablevertexattribarray( _vertex_position_2d_location ); } void texture2dprogram::enable_texture_coordinate_pointer() { glenablevertexattribarray( _texture_coordinate_location ); } void texture2dprogram::disable_texture_coordinate_pointer() { gldisablevertexattribarray( _texture_coordinate_location ); } void texture2dprogram::update_projection_matrix() { gluniformmatrix4fv( _projection_matrix_location, 1, gl_false, glm::value_ptr( _projection_matrix ) ); } void texture2dprogram::update_model_view_matrix() { gluniformmatrix4fv( _model_view_matrix_location, 1, gl_false, glm::value_ptr( _model_view_matrix ) ); } void texture2dprogram::set_texture_color( colorrgba color ) { gluniform4f( _texture_color_location, color.r(), color.g(), color.b(), color.a() ); } void texture2dprogram::set_texture_unit( gluint unit ) { gluniform1i( _texture_unit_location, unit ); }

here initialize vbo , ibo:

void texture::init_vbo() { if (_texture_id != 0 && _vbo == 0) { // texture coords glfloat top = 0.0f; glfloat bottom = (glfloat)_image_height/(glfloat)_texture_height; glfloat left = 0.0f; glfloat right = (glfloat)(_image_width/(glfloat)_texture_width); // vertex coords glfloat quad_width = _image_width; glfloat quad_height = _image_height; // vertex info vertexdata2d vertex_data[4]; vertex_data[0].texture_coordinate.s = left; vertex_data[0].texture_coordinate.t = top; vertex_data[1].texture_coordinate.s = right; vertex_data[1].texture_coordinate.t = top; vertex_data[2].texture_coordinate.s = right; vertex_data[2].texture_coordinate.t = bottom; vertex_data[3].texture_coordinate.s = left; vertex_data[3].texture_coordinate.t = bottom; vertex_data[0].position.x = 0.0f; vertex_data[0].position.y = 0.0f; vertex_data[1].position.x = quad_width; vertex_data[1].position.y = 0.0f; vertex_data[2].position.x = quad_width; vertex_data[2].position.y = quad_height; vertex_data[3].position.x = 0.0f; vertex_data[3].position.y = quad_height; gluint index_data[4]; index_data[0] = 0; index_data[1] = 1; index_data[2] = 2; index_data[3] = 3; // create vbo glgenbuffers(1, &_vbo); glbindbuffer(gl_array_buffer, _vbo); glbufferdata(gl_array_buffer, 16 * sizeof(glfloat), vertex_data, gl_dynamic_draw); // create ibo glgenbuffers(1, &_ibo); glbindbuffer(gl_element_array_buffer, _ibo); glbufferdata(gl_element_array_buffer, 4*sizeof(gluint), index_data, gl_dynamic_draw); //unbind buffers glbindbuffer( gl_array_buffer, null ); glbindbuffer( gl_element_array_buffer, null ); } }

and here render:

void texture::render(glfloat x, glfloat y, rect* clip) { if (_texture_id != 0) { _program->model_view_matrix(glm::mat4()); // move rendering point _program->multiply_model_view_matrix( glm::translate<float>(glm::mat4(), glm::vec3(x, y, 0.0f ))); _program->update_model_view_matrix(); // texture coordinate glbindtexture(gl_texture_2d, _texture_id); _program->enable_texture_coordinate_pointer(); _program->set_texture_coordinate_pointer(sizeof(vertexdata2d), (glvoid*)offsetof(vertexdata2d, texture_coordinate)); // bind vertex array buffer glbindbuffer(gl_array_buffer, _vbo); _program->set_vertex_pointer(sizeof(vertexdata2d), (glvoid*)offsetof(vertexdata2d, position)); _program->enable_vertex_pointer(); // draw quad using vertex info , index info glbindbuffer(gl_element_array_buffer, _ibo); gldrawelements(gl_triangle_fan, 4, gl_unsigned_int, null); _program->disable_vertex_pointer(); _program->disable_texture_coordinate_pointer(); } }

edit

i solved issue, problem programme beingness called.

when using vertex-buffer objects (vbos), lastly parameter glvertexattribpointer byte-offset (i.e., number of bytes) data, , not pointer it. op, should passing 0 lastly parameter.

also, want pass 0 glbindbuffer compared null. while may equate same thing (depending on who's defining value of null), null isn't gluint.

opengl

No comments:

Post a Comment