c++ - OpenGL Instanced Rendering with multiple attributes -
so i'm quite new opengl , learning instanced rendering. trying render 3 triangles diffrent positions , colors can't work. here's code:
glfloat verts[] = { -0.5f, -0.5f, -0.5f, +0.5f, 0.0f, -0.5f }; gluint vertexbufferid; glgenbuffers(1, &vertexbufferid); glbindbuffer(gl_array_buffer, vertexbufferid); glbufferdata(gl_array_buffer, sizeof(verts), verts, gl_static_draw); glenablevertexattribarray(0); glvertexattribpointer(0, 2, gl_float, gl_false, 0, 0); glfloat xoffsets[] = { 0.0f, 0.5f, 1.0f }; gluint offsetsbufferid; glgenbuffers(1, &offsetsbufferid); glbindbuffer(gl_array_buffer, offsetsbufferid); glbufferdata(gl_array_buffer, sizeof(xoffsets), xoffsets, gl_static_draw); glenablevertexattribarray(1); glvertexattribpointer(1, 1, gl_float, gl_false, 0, 0); glvertexattribdivisor(1, 1); unsigned int colors[] = { 0xffff0000, 0xff00ff00, 0xff00ff00 }; gluint colorid; glgenbuffers(1, &colorid); glbindbuffer(gl_array_buffer, colorid); glbufferdata(gl_array_buffer, sizeof(colors), colors, gl_static_draw); glenablevertexattribarray(2); glvertexattribpointer(2, 4, gl_unsigned_byte, gl_true, 0, 0); glvertexattribdivisor(1, 1); glushort indices[] = { 0, 1, 2 }; gluint indexarraybufferid; glgenbuffers(1, &indexarraybufferid); glbindbuffer(gl_element_array_buffer, indexarraybufferid); glbufferdata(gl_element_array_buffer, sizeof(indices), indices, gl_static_draw);
vertex shader:
#version 330 core layout(location = 0) in vec2 position; layout(location = 1) in float offset; layout(location = 2) in vec4 color; out vec4 col; void main() { gl_position = vec4(position.x + offset, position.y, 0, 1); col = color; }
fragment:
#version 330 core out vec4 color; in vec4 col; void main() { color = col; }
this how render:
gldrawelementsinstanced(gl_triangles, 3, gl_unsigned_short, 0, 3);
as can see triangles in right position, colors wrong. first triangle should blue , other 2 should green. here seems 1 vertex in every triangle blue. doing wrong here?
Comments
Post a Comment