Can't draw triangle using Opengl

前端 未结 2 767
死守一世寂寞
死守一世寂寞 2021-01-28 14:34

in this code i want to draw a simple triangle on a blue background using openGL however when i compile and run the code only a window with the blue background appears (without t

2条回答
  •  暖寄归人
    2021-01-28 15:25

    You have provided invalid buffer size in the following line:

    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(vertex), vertex, GL_STATIC_DRAW);
    

    Because sizeof(vertex) should return the total size of the array (e.g 36) not the underlying type, thus no need to multiply by 9. If you still want to use multiplication try following:

    9 * sizeof(float)
    

    I have to write this down... You should separate your initialization and draw cycle like in genpfault's answer.

提交回复
热议问题