调用glVertexAttribPointer时的GL_INVALID_OPERATION

时间:2017-11-25 17:33:00

标签: c opengl gtk

尝试关注此视频教程:YouTube: OpenGL Hello World Triangle我制作了以下不起作用的代码。但之前,我不是100%遵循教程,只是OpenGL部分。在这个视频中,当我使用Gtk时,他使用Qt,当我使用环氧树脂时他也使用了glew,当我在Linux 4.12.12上使用C时,他在Windows上使用C ++进行编程。问题是对glVertexAttribPointer的调用给了我GL_INVALID_OPERATION。我无法理解为什么。

代码:

#include <epoxy/gl.h>
#include <epoxy/glx.h>
#include <gtk/gtk.h>

static void realize (GtkGLArea *area) {
  GLuint buffer;
  GLenum err;
  GLfloat vertices[] = {
    +0.0f, +1.0f,
    -1.0f, -1.0f,
    +1.0f, -1.0f
  };

  gtk_gl_area_make_current (area);
  //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

  g_print("Call to glGenBuffers()\n");
  glGenBuffers(1, &buffer);
  err = glGetError();
  if (err != GL_NO_ERROR) {
    g_print("Error detected: 0x%X\n", err);
  }

  g_print("Call to glBindBuffer()\n");
  glBindBuffer(GL_ARRAY_BUFFER, buffer);
  err = glGetError();
  if (err != GL_NO_ERROR) {
    g_print("Error detected: 0x%X\n", err);
  }
  g_print("Call to glBufferData()\n");
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  err = glGetError();
  if (err != GL_NO_ERROR) {
    g_print("Error detected: 0x%X\n", err);
  }

  g_print("Call to glEnableVertexAttribArray()\n");
  glEnableVertexAttribArray(0);
  err = glGetError();
  if (err != GL_NO_ERROR) {
    g_print("Error detected: 0x%X\n", err);
  }

  g_print("Call to glVertexAttribPointer()\n");
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
  err = glGetError();
  if (err != GL_NO_ERROR) {
    g_print("Error detected: 0x%X\n", err);
  }
}

static gboolean render (GtkGLArea *area, GdkGLContext *context) {
  //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glDrawArrays(GL_TRIANGLES, 0, 3);
  return FALSE;
}

static void activate (GtkApplication* app) {
  GtkWidget *window;
  GtkWidget *glarea;

  window = gtk_application_window_new (app);
  glarea = gtk_gl_area_new ();
  gtk_window_set_title (GTK_WINDOW (window), "GL Window");
  gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  g_signal_connect (glarea, "realize", G_CALLBACK (realize), NULL);
  g_signal_connect (glarea, "render", G_CALLBACK (render), NULL);
  gtk_container_add (GTK_CONTAINER(window), glarea);
  gtk_widget_show_all (window);
}

int main (int argc, char **argv) {
  GtkApplication *app;
  int status;

  app = gtk_application_new ("local.gltest", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

代码中每个注意到的错误都很好看。

Makefile for curious:

CLIBS = `pkg-config --libs gtk+-3.0 epoxy`
CFLAGS = `pkg-config --cflags gtk+-3.0 epoxy`
DEPS = main.o
CC = gcc

run: run.bin
    ./run.bin

run.bin: $(DEPS)
    $(CC) $(CLIBS) $< $(CFLAGS) -o run.bin

%.o: %.c
    $(CC) $(CLIBS) -c $< $(CFLAGS) -o $@

clean:
    rm *.o *.bin

0 个答案:

没有答案