C++ OpenGL Shaders not working -
i've started learning c++ , i'm trying make program shaders draws triangle colors.
this vertex shader:
const glchar* vertexsource = "#version 150 core\n" "in vec4 position;" "in vec4 color;" "out vec4 color;" "out vec4 gl_position;" "void main(){" " gl_position = position;" " color = color;" "}";
and fragment shader:
const glchar* fragmentsource = "#version 150 core\n" "in vec4 color;" "out vec4 gl_fragcolor" "void main(){" " gl_fragcolor = color;" "}";
i have list of values co-ords , colors respectively:
float vertices[]{ -1.0f, -1.0f, 0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f, 1.0f, -1.0f, 0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f, 0.0f, 1.0f, 0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f, };
i initialise 'vertex buffer object' , 'vertex array object'
gluint vao; //initialise vertex array object glgenvertexarrays(1, &vao); glbindvertexarray(vao); gluint vbo; //initialise vertex buffer object glgenbuffers(1,&vbo); glbindbuffer(gl_array_buffer,vbo); glbufferdata(gl_array_buffer,sizeof(vertices),vertices,gl_static_draw);
i compile shader program:
gluint vertexshader = glcreateshader(gl_vertex_shader); glshadersource(vertexshader, 1, &vertexsource, null); glcompileshader(vertexshader); gluint fragmentshader = glcreateshader(gl_fragment_shader); glshadersource(fragmentshader, 1, &fragmentsource, null); glcompileshader(fragmentshader); gluint shaderprogram = glcreateprogram(); glattachshader(shaderprogram, vertexshader); glattachshader(shaderprogram, fragmentshader); gllinkprogram(shaderprogram); gluseprogram(shaderprogram);
i define attributes shaders:
glbindattriblocation(shaderprogram, 0, "position"); glvertexattribpointer(0, 4, gl_float, gl_false, 8 * sizeof(float), 0); glenablevertexattribarray(0); glbindattriblocation(shaderprogram, 1 ,"color"); glvertexattribpointer(1, 4, gl_float, gl_false, 8 * sizeof(float), (void*)(4 * sizeof(float))); glenablevertexattribarray(1);
and draw triangle in 'do, while' loop
gldrawarrays(gl_triangles, 0, 3);
however, doesn't print triangles, instead shows black screen.
i got of code tutorials online, , i'm trying rewrite out understand how works.
could see went wrong? advice appreciated :)
edit:
i've added code check errors:
if (status == gl_false) { glint infologlength; glgetshaderiv(shaderprogram, gl_info_log_length, &infologlength); glchar* strinfolog = new glchar[infologlength + 1]; glgetprograminfolog(shaderprogram, infologlength, null, strinfolog); std::cout << strinfolog; delete[] strinfolog; }
...but nothing prints
gl_position
pre defined built in variable , glsl compiler surely complaining trying redefining , errors out. gl_fragcolor
in reserved namespace.
of course code lacks queries of shader info log, don't see compiler output; addendum queries program linker log.
the solution easy omitting out vec4 gl_position
, out vec4 gl_fragcolor
shader sources.