glEnableVertexAttribArray 中“index”参数的含义以及(可能)OS X OpenGL 实现中的错误

2024-05-05

1)我是否正确理解,要使用顶点数组或VBO进行绘制,我需要所有属性在着色器程序链接之前调用glBindAttribLocation,或者在着色器程序成功链接后调用glGetAttribLocation,然后使用glVertexAttribPointer中的绑定/获得的索引和 glEnableVertexAttribArray 调用?

更具体地说:这三个函数 - glGetAttribLocation、glVertexAttribPointer 和 glEnableVertexAttribArray - 它们都有一个名为“index”的输入参数。这三个的“索引”是相同的吗?它和 glGetAttribLocation 返回的结果是一样的吗?

如果是: 2)我在 OS X 上遇到了一个问题,我在这里描述了它:https://stackoverflow.com/questions/28093919/using-default-attribute-location-doesnt-work-on-osx-osx-opengl-bug https://stackoverflow.com/questions/28093919/using-default-attribute-location-doesnt-work-on-osx-osx-opengl-bug,但遗憾的是没有得到任何回复。

问题是,根据我绑定到属性的属性位置,我在屏幕上看到或看不到任何内容。我仅在运行 OS X 10.9.5 的 MacBook Pro 上看到此行为;我尝试在 Linux 和 Windows 上运行相同的代码,它似乎可以在这些平台上运行,与我的属性绑定到的位置无关。

这是一个显示问题的代码示例(应该在屏幕上绘制一个红色三角形):

#include <iostream>
#include <GLFW/glfw3.h>

GLuint global_program_object;
GLint  global_position_location;
GLint  global_aspect_ratio_location;
GLuint global_buffer_names[1];

int LoadShader(GLenum type, const char *shader_source)
  {
  GLuint shader;
  GLint compiled;
  shader = glCreateShader(type);
  if (shader == 0)
    return 0;
  glShaderSource(shader, 1, &shader_source, NULL);
  glCompileShader(shader);
  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
  if (!compiled)
    {
    GLint info_len = 0;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
    if (info_len > 1)
      {
      char* info_log = new char[info_len];
      glGetShaderInfoLog(shader, info_len, NULL, info_log);
      std::cout << "Error compiling shader" <<  info_log << std::endl;
      delete info_log;
      }
    glDeleteShader(shader);
    return 0; 
    }
  return shader; 
  }

int InitGL()
  {
  char vertex_shader_source[] =
    "attribute vec4  att_position;                       \n"
    "attribute float dummy;\n"
    "uniform   float uni_aspect_ratio;                   \n"
    "void main()                                         \n"
    "  {                                                 \n"
    "  vec4 test = att_position * dummy;\n"
    "  mat4 mat_projection =                             \n"
    "   mat4(1.0 / uni_aspect_ratio, 0.0,  0.0, 0.0,     \n"
    "                           0.0, 1.0,  0.0, 0.0,     \n"
    "                           0.0, 0.0, -1.0, 0.0,     \n"
    "                           0.0, 0.0,  0.0, 1.0);    \n"
    "  gl_Position = att_position;                       \n"
    "  gl_Position *= mat_projection;                    \n"
    "  }                                                 \n";

  char fragment_shader_source[] =
    "void main()                                \n"
    "  {                                        \n"
    "  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n"
    "  }                                        \n";

  GLuint vertex_shader;
  GLuint fragment_shader;
  GLuint program_object;
  GLint  linked;

  vertex_shader   = LoadShader(GL_VERTEX_SHADER  , vertex_shader_source  );
  fragment_shader = LoadShader(GL_FRAGMENT_SHADER, fragment_shader_source);

  program_object = glCreateProgram();
  if(program_object == 0)
    return 1;
  glAttachShader(program_object, vertex_shader  );
  glAttachShader(program_object, fragment_shader);

  // Here any index except 0 results in observing the black screen
  glBindAttribLocation(program_object, 1, "att_position");
  glLinkProgram(program_object);
  glGetProgramiv(program_object, GL_LINK_STATUS, &linked);
  if(!linked)
    {
    GLint info_len = 0;
    glGetProgramiv(program_object, GL_INFO_LOG_LENGTH, &info_len);
    if(info_len > 1)
      {
      char* info_log = new char[info_len];
      glGetProgramInfoLog(program_object, info_len, NULL, info_log);
      std::cout << "Error linking program" <<  info_log << std::endl;
      delete info_log; 
      }
    glDeleteProgram(program_object);
    return 1;
    }
  global_program_object = program_object;
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  glUseProgram(global_program_object);
  global_position_location     = glGetAttribLocation (global_program_object, "att_position");
  global_aspect_ratio_location = glGetUniformLocation(global_program_object, "uni_aspect_ratio");

  GLfloat vertices[] = {-0.5f, -0.5f, 0.0f,
                         0.5f, -0.5f, 0.0f,
                         0.0f,  0.5f, 0.0f};

  glGenBuffers(1, global_buffer_names);
  glBindBuffer(GL_ARRAY_BUFFER, global_buffer_names[0]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, vertices, GL_STATIC_DRAW);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  return 0;
  }

void Render()
  {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  glUseProgram(global_program_object);
  glBindBuffer(GL_ARRAY_BUFFER, global_buffer_names[0]);
  glVertexAttribPointer(global_position_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
  glEnableVertexAttribArray(global_position_location);
  glDrawArrays(GL_TRIANGLES, 0, 3);
  glDisableVertexAttribArray(global_position_location);
  glUseProgram(0);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  }

void FreeGL()
  {
  glDeleteBuffers(1, global_buffer_names);
  glDeleteProgram(global_program_object);
  }

void SetViewport(int width, int height)
  {
  glViewport(0, 0, width, height);
  glUseProgram(global_program_object);
  glUniform1f(global_aspect_ratio_location, static_cast<GLfloat>(width) / static_cast<GLfloat>(height));
  }


int main(void)
  {
  GLFWwindow* window;
  if (!glfwInit())
    return -1;
  window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  if (!window)
    {
    glfwTerminate();
    return -1;
    }
  glfwMakeContextCurrent(window);
  InitGL();

  // Double the resolution to correctly draw with Retina display
  SetViewport(1280, 960);
  while (!glfwWindowShouldClose(window))
    {
    Render();
    glfwSwapBuffers(window);
    glfwPollEvents();
    }
  FreeGL();
  glfwTerminate();
  return 0;
  }

您认为这看起来像是一个错误吗?任何人都可以复制它吗?如果是错误,我应该在哪里报告?

附: 我也尝试过 SDL 而不是 GLFW,行为是相同的......


根据规范,您看到的行为实际上是正确的,MacOSX 与此有关,但只是以一种非常间接的方式。

首先回答问题1):你基本上是正确的。使用现代 GLSL (>=3.30),您还可以通过以下方式指定所需的索引layout(location=...)直接在着色器代码中使用限定符,而不是使用glBindAttribLocation(),但这只是一个旁注。

您面临的问题是您正在使用legacyGL 上下文。您没有指定所需的版本,因此您将获得对旧方式的最大兼容性。现在在 Windows 上,您很可能获得实现支持的最高版本的兼容性配置文件(通常是非古代 GPU 上的 GL3.x 或 GL4.x)。

但是,在 OSX 上,您最多只能使用 GL2.1。这就是症结所在:你的代码是invalid在 GL2.x 中。为了解释这一点,我必须回顾一下 GL 的历史。一开始,有立即模式,所以你确实画了

glBegin(primType);
glColor3f(r,g,b);
glVertex3f(x,y,z);
[...]
glColor3f(r,g,b);
glVertex3f(x,y,z);
glEnd();

请注意,glVertex调用实际上创建了一个顶点。所有其他每顶点属性基本上都是一些当前顶点状态可以随时设置,但调用glVertex将采用所有这些当前属性以及位置来形成馈送到管道的顶点。

现在,当添加顶点数组时,我们得到了类似的函数glVertexPointer(), glColorPointer()等等,每个属性数组可以通过以下方式单独启用或禁用glEnableClientState()。基于数组的绘制调用实际上是根据立即模式定义的OpenGL 2.1 规范 https://www.opengl.org/registry/doc/glspec21.20061201.pdf as glDrawArrays(GLenum mode, GLint first, GLsizei count)相当于

glBegin(mode);
for (i=0; i<count; i++)
    ArrayElement(first + i);
glEnd();

with ArrayElement(i)被定义(这个源自于GL 1.5规格 https://www.opengl.org/registry/doc/glspec15.pdf):

if ( normal_array_enabled )
    Normal3...( <i-th normal value> );
[...] // similiar for all other bultin attribs
if ( vertex_array_enabled)
    Vertex...( <i-th vertex value> );

这个定义有一些微妙的后果:你mustGL_VERTEX_ARRAY启用属性数组,否则不会绘制任何内容,因为没有等效项glVertex生成呼叫。

现在,当 GL2.0 中添加通用属性时,做出了特殊保证:通用属性 0 是内置属性的别名glVertex属性 - 因此两者可以在立即模式和数组中互换使用。所以glVertexAttrib3f(0,x,y,z)以同样的方式“创建”一个顶点glVertex3f(x,y,z)将有。并使用数组glEnableVertexAttribArray(0)一样好glEnableClientState(GL_VERTEX_ARRAY).

在 GL 2.1 中,ArrayElement(i)函数现在如下所示:

if ( normal_array_enabled )
    Normal3...( <i-th normal value> );
[...] // similiar for all other bultin attribs
for (a=1; a<max_attribs; a++) {
    if ( generic_attrib_a_enabled )
        glVertexAttrib...(a, <i-th value of attrib a> );
}
if ( generic_attrib_0_enabled)
    glVertexAttrib...(0, <i-th value of attrib 0> );
else if ( vertex_array_enabled)
    Vertex...( <i-th vertex value> );

现在这就是发生在你身上的事情。你绝对need属性 0(或旧的GL_VERTEX_ARRAY属性)启用此功能可以为管道生成任何顶点。

请注意,理论上应该可以只启用属性 0,不管是否在着色器中使用。您应该确保相应的 attrib 指针指向有效内存,以确保 100% 安全。因此,您只需检查属性索引 0 是否已使用,如果没有,只需将与属性 0 相同的指针设置为真实属性,GL 应该会很高兴。但我没试过这个。

在更现代的 GL 中,这些要求不再存在,并且没有属性 0 的绘图将按预期工作,这就是您在其他系统上看到的情况。也许你应该考虑切换到现代 GL,比如 >= 3.2核心概况,其中不会出现问题(但您需要稍微更新一下代码,包括着色器)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

glEnableVertexAttribArray 中“index”参数的含义以及(可能)OS X OpenGL 实现中的错误 的相关文章

随机推荐