c++ - glDrawElements only draws first triangle specified by indices -


i started learning opengl , having trouble gldrawelements. trying draw 2 triangles using gldrawelements gl_triangle, 1 triangle appears.

what expected: gldrawelements draw 2 triangles vertices

(0,0) (1,1) (-1,1) , (0,0) (-1,-1) (1,-1)

what happens: gldrawelements draw 1 triangle vertices (0,0) (1,1) (-1,1)

short, self contained, correct (compilable), example:

#include <gl/glew.h> #include <glfw/glfw3.h>  int main(int argc, char *argv[]) {     glfwinit();     glfwwindow* window = glfwcreatewindow(640, 480, "sample code not display 2 triangles", null, null);     glfwmakecontextcurrent(window);      glewinit();      glfloat vertices[] {         +0.0f, +0.0f, //0         +1.0f, +1.0f, //1         -1.0f, +1.0f, //2         -1.0f, -1.0f  //3         +1.0f, -1.0f  //4     };      gluint vertexbufferid;     glgenbuffers(1, &vertexbufferid);     glbindbuffer(gl_array_buffer, vertexbufferid);     glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw);     glenablevertexattribarray(0);     glvertexattribpointer(0, 2, gl_float, gl_false, 0, 0);      gluint indexbufferid;     glushort indices[] {0,1,2, 0,3,4};     glgenbuffers(1, &indexbufferid);     glbindbuffer(gl_element_array_buffer, indexbufferid);     glbufferdata(gl_element_array_buffer, sizeof(indices), indices, gl_static_draw);      while (!glfwwindowshouldclose(window))     {         gldrawelements(gl_triangles, 6, gl_unsigned_short, 0);         glfwswapbuffers(window);         glfwpollevents();     }      glfwterminate();     return 0; } 

this sample opens 640x480 glfw window, displays triangle vertices in both upper corners , third vertex in middle of window. other triangle, vertices in both bottom corners , third vertex in middle, missing. why that?

specs:
os: linux mint 17
gpu: nvidia gtx 275
gpu driver: 331.67
glew version: 1.10.0
glfw version: 3.0.4

commas important. this:

glfloat vertices[] = {     +0.0f, +0.0f, //0     +1.0f, +1.0f, //1     -1.0f, +1.0f, //2     -1.0f, -1.0f  //3     +1.0f, -1.0f  //4 }; size_t elements = sizeof( vertices ) / sizeof( glfloat ) = 9(!) 

is not same this:

glfloat vertices[] = {     +0.0f, +0.0f, //0     +1.0f, +1.0f, //1     -1.0f, +1.0f, //2     -1.0f, -1.0f, //3     +1.0f, -1.0f  //4 }; size_t elements = sizeof( vertices ) / sizeof( glfloat ) = 10 

note added comma @ end of line 3.

if vertices has 9 elements when gldrawelements() goes try read fifth vertex x coordinate valid. y coordinate contain garbage if you're lucky, segfault if you're not.

also, shouldn't use generic vertex attribute functions without specifying shaders.

all together:

#include <gl/glew.h> #include <glfw/glfw3.h>  int main(int argc, char *argv[]) {     glfwinit();     glfwwindow* window = glfwcreatewindow(640, 480, "sample code not display 2 triangles", null, null);     glfwmakecontextcurrent(window);      glewinit();      glfloat vertices[] =     {         +0.0f, +0.0f, //0         +1.0f, +1.0f, //1         -1.0f, +1.0f, //2         -1.0f, -1.0f, //3         +1.0f, -1.0f, //4     };      gluint vertexbufferid;     glgenbuffers(1, &vertexbufferid);     glbindbuffer(gl_array_buffer, vertexbufferid);     glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw);     glenableclientstate( gl_vertex_array );     glvertexpointer( 2, gl_float, 0, 0 );      gluint indexbufferid;     glushort indices[] = { 0,1,2, 0,3,4 };     glgenbuffers(1, &indexbufferid);     glbindbuffer(gl_element_array_buffer, indexbufferid);     glbufferdata(gl_element_array_buffer, sizeof(indices), indices, gl_static_draw);      while (!glfwwindowshouldclose(window))     {         glclear( gl_color_buffer_bit | gl_depth_buffer_bit );         gldrawelements(gl_triangles, 6, gl_unsigned_short, 0);         glfwswapbuffers(window);         glfwpollevents();     }      glfwterminate();     return 0; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -