c++ - OpenGL 3.0 to 3.2 -


i in process upgrading code opengl 3.0 3.3 (or maybe 4.x) missing something, since screen black.

the closest answer problem triangle in opengl 3.1 nothing in 3.2. solution here using compatibility profile. hint there on implementing proper using core profile "actually using proper core profile gl command sequence full vertex , fragment shaders."

the problem have after sinking entire afternoon reading spec, tutorials , examples, don't see code doing wrong. working on demo recycles code use , can find entire demo, rendering triangle @ gitub.

the juicy bits following:

the vertex code is:

#version 150 in vec3 avertex;  void main() {        gl_position = avertex; } 

the fragment code is:

#version 330 core out vec4 ofragcolor;  void main() {     ofragcolor = vec4(1.0, 1.0, 1.0, 1.0);        } 

compiling shader done follows:

int status = 0; char logstr[256];  const glchar* vbuff[1] = {vertex_code.c_str()};  unsigned int vertex_id = glcreateshader(gl_vertex_shader); glshadersource(vertex_id, 1, vbuff, null); glcompileshader(vertex_id);  glgetshaderinfolog(vertex_id, 256, null, logstr);  glgetshaderiv(vertex_id, gl_compile_status, &status); if(! status) {     gldeleteshader(vertex_id);         throw std::runtime_error(logstr); }  const glchar* fbuff[1] = {fragment_code.c_str()};  unsigned int fragment_id = glcreateshader(gl_fragment_shader); glshadersource(fragment_id, 1, fbuff, null); glcompileshader(fragment_id);  glgetshaderinfolog(fragment_id, 256, null, logstr);  glgetshaderiv(fragment_id, gl_compile_status, &status); if(! status) {                 gldeleteshader(vertex_id);     gldeleteshader(fragment_id);     throw std::runtime_error(logstr); }  program_id = glcreateprogram(); glattachshader(program_id, vertex_id); glattachshader(program_id, fragment_id); gllinkprogram(program_id);  glgetshaderinfolog(program_id, 256, null, logstr);  glgetshaderiv(program_id, gl_link_status, &status); if(! status) {                 gldeleteshader(vertex_id);     gldeleteshader(fragment_id);     gldeleteprogram(program_id);     throw std::runtime_error(logstr); }  gldeleteshader(vertex_id); gldeleteshader(fragment_id);  

the shader bound simple:

gluseprogram(program_id); 

the vertex buffer created follows:

glgenbuffers(5, buffers);  size_t vcount = vertexes.size(); size_t fcount = faces.size();  glbindbuffer(gl_array_buffer, buffers[vertex_buffer]);     glbufferdata(gl_array_buffer, vcount * 3 * sizeof(float), &vertexes[0], gl_static_draw);  glbindbuffer(gl_array_buffer, buffers[normal_buffer]);     glbufferdata(gl_array_buffer, vcount * 3 * sizeof(float), &normals[0], gl_static_draw);  glbindbuffer(gl_array_buffer, buffers[texcoord_buffer]);     glbufferdata(gl_array_buffer, vcount * 2 * sizeof(float), &texcoords[0], gl_static_draw);  glbindbuffer(gl_array_buffer, buffers[tangent_buffer]);     glbufferdata(gl_array_buffer, vcount * 3 * sizeof(float), &tangents[0], gl_static_draw);  glbindbuffer(gl_array_buffer, 0);   glbindbuffer(gl_element_array_buffer, buffers[index_buffer]);     glbufferdata(gl_element_array_buffer, fcount * 3 * sizeof(unsigned int), &faces[0], gl_static_draw);    glbindbuffer(gl_element_array_buffer, 0); 

the vertex buffer rendered following sequence:

int vertex_location   = shader.get_attribute_location("avertex"); int normal_location   = shader.get_attribute_location("anormal"); int texcoord_location = shader.get_attribute_location("atexcoord"); int tangent_location  = shader.get_attribute_location("atangent");  if (vertex_location != -1) {     glbindbuffer(gl_array_buffer, buffers[vertex_buffer]);     glvertexattribpointer(vertex_location, 3, gl_float, gl_false, 0, 0);     glenablevertexattribarray(vertex_location); }  if (normal_location != -1) {     glbindbuffer(gl_array_buffer, buffers[normal_buffer]);     glvertexattribpointer(normal_location, 3, gl_float, gl_false, 0, 0);     glenablevertexattribarray(normal_location); }  if (texcoord_location != -1) {     glbindbuffer(gl_array_buffer, buffers[texcoord_buffer]);     glvertexattribpointer(texcoord_location, 2, gl_float, gl_false, 0, 0);     glenablevertexattribarray(texcoord_location); }  if (tangent_location != -1) {     glbindbuffer(gl_array_buffer, buffers[tangent_buffer]);     glvertexattribpointer(tangent_location, 3, gl_float, gl_false, 0, 0);     glenablevertexattribarray(tangent_location); }  glbindbuffer(gl_array_buffer, 0);  glbindbuffer(gl_element_array_buffer, buffers[index_buffer]);         gldrawelements(gl_triangles, faces.size() * 3, gl_unsigned_int, 0); glbindbuffer(gl_element_array_buffer, 0);   

the attribute location determined with:

return glgetattriblocation(program_id, name.c_str()); 

this code works 3.0 , 3.1. detail did miss breaks starting opengl 3.2 core. (it works in 3.2 compat.)

you need use vaos

create 1 glgenvertexarrays(1, &vao); , bind glbindvertexarray(vao);

move bining code setup code move so

glbindvertexarray(vao); int vertex_location   = shader.get_attribute_location("avertex"); int normal_location   = shader.get_attribute_location("anormal"); int texcoord_location = shader.get_attribute_location("atexcoord"); int tangent_location  = shader.get_attribute_location("atangent");  if (vertex_location != -1) {     glbindbuffer(gl_array_buffer, buffers[vertex_buffer]);     glvertexattribpointer(vertex_location, 3, gl_float, gl_false, 0, 0);     glenablevertexattribarray(vertex_location); }  //etc.  glbindbuffer(gl_array_buffer, 0);  glbindbuffer(gl_element_array_buffer, buffers[index_buffer]);   glbindvertexarray(0); 

then during rendering it's just

glbindevertexarray(vao); gldrawelements(gl_triangles, faces.size() * 3, gl_unsigned_int, 0); glbindevertexarray(0); 

it's best bind attribute locations before linking programs position 1 , normals 2 etc.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -