glDrawArrays not working. Using GtkGLArea in GTK3

时光总嘲笑我的痴心妄想 提交于 2019-11-29 12:31:28

You're using generic vertex attributes. As such you're supposed to use shaders. If you're in a compatibility profile your use of vertex attribute 0 may be interpreted as built-in attribute "vertex position", but that's not a given. So if you want to operate "by the book" you must supply a shader (or stick with the deprecated built-in attributes).

On a side note: Enabling the vertex attribute array and setting the pointer in the initialization code is not recommendable. Anything may happen with the OpenGL context between those two, including binding other vertex arrays, disabling/enabling (other) vertex attribute arrays, setting different buffer pointers, etc.

As a rule of thumb, anything that's directly related to drawing state (i.e. that does not constitute one time data initialization/loading) belongs at the corresponding place of the drawing code. In your case

static gboolean render(GtkGLArea *area, GdkGLContext *context)
{
  glClearColor(0, 0, 0, 0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  //glFrontFace(GL_CW);

  /* added these ---> */
  glBindBuffer(GL_ARRAY_BUFFER, gl_buffer);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
  /* <--- */

  glDrawArrays(GL_TRIANGLES, 0, 3);

  //gtk_widget_get_realized(area);

  return TRUE;
}

Doing not so will create will confuse you in the long run.

You don't appear to have any shaders. You need a fragment and vertex shader.

Here's a tutorial on how to write and use them: https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/loading.php

You are missing the creation of the VAO; GTK+ will create GL core profile contexts, which means you need to create and select the VAO yourself, otherwise your vertex buffer objects won't be used.

When initializing the GL state, add this:

  /* we need to create a VAO to store the other buffers */
  GLuint vao;

  glGenVertexArrays (1, &vao);
  glBindVertexArray (vao);

Before creating the VBOs.

You should check out my blog post about using OpenGL with GTK+, and its related example code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!