是什么导致基于OpenGL 2.0的着色器处理代码在::glBegin()中崩溃?

是什么导致基于OpenGL 2.0的着色器处理代码在::glBegin()中崩溃?,opengl,segmentation-fault,glsl,shader,Opengl,Segmentation Fault,Glsl,Shader,为了它的价值: 我的图形驱动程序(ATI Mobility Radeon 4570)是最新的 我之前已经设法让基于ARB扩展的着色器处理代码在同一配置上工作(尽管在我回到着色器之前已经有一段时间了) 我在我的设置中遵循Lighthouse 3D教程(…松散地说:着色器编译日志代码是从一个SO问题中删除的,很抱歉没有学分,我现在似乎找不到它) 基本上,我所有的OpenGL操作似乎都能成功执行;着色器编译和程序链接。正如您在下面看到的,我在每次调用gl***后都检查了OpenGL错误。着色器本身

为了它的价值:

  • 我的图形驱动程序(ATI Mobility Radeon 4570)是最新的
  • 我之前已经设法让基于ARB扩展的着色器处理代码在同一配置上工作(尽管在我回到着色器之前已经有一段时间了)
  • 我在我的设置中遵循Lighthouse 3D教程(…松散地说:着色器编译日志代码是从一个SO问题中删除的,很抱歉没有学分,我现在似乎找不到它)
基本上,我所有的OpenGL操作似乎都能成功执行;着色器编译和程序链接。正如您在下面看到的,我在每次调用gl***后都检查了OpenGL错误。着色器本身是微不足道的

当然,我可能又忽略了一件可笑的简单事情

#define GLOP(operation) operation; if(!GFX::CheckError(#operation)) exit(1)
// GFX::CheckError() prints an error message, if any.

  GFX::Init(640, 480, 24, 0);
  GLOP(Texture2D::Enable());

  // shader setup
  GLuint  hFrag = GLOP(::glCreateShader(GL_FRAGMENT_SHADER));
  GLuint  hVert = GLOP(::glCreateShader(GL_VERTEX_SHADER));
  GLuint  hProg = GLOP(::glCreateProgram());

  std::ifstream inFile;
  ReadOpen("shader.frag", inFile);
  std::string  str(ReadFile(inFile));
  const char* pSource = str.c_str();
  GLOP(::glShaderSource(hFrag, 1, &pSource, 0));
  GLOP(::glCompileShader(hFrag));

  GLint logLength;
  glGetShaderiv(hFrag, GL_INFO_LOG_LENGTH, &logLength);
  if (logLength > 0) {
      GLchar* log = (GLchar*)malloc(logLength);
      glGetShaderInfoLog(hFrag, logLength, &logLength, log);
      printf("Shader compile log:\n%s\n", log);
      free(log);
  }

  XR::ReadOpen(core.GetPath() + "shader.vert", inFile);
  XRLOG(XR::GetFileSize(inFile) << " bytes in file." << std::endl);
  str = XR::ReadFile(inFile);
  pSource = str.c_str();
  GLOP(::glShaderSource(hVert, 1, &pSource, 0));
  GLOP(::glCompileShader(hVert));

  glGetShaderiv(hVert, GL_INFO_LOG_LENGTH, &logLength);
  if (logLength > 0) {
      GLchar* log = (GLchar*)malloc(logLength);
      glGetShaderInfoLog(hVert, logLength, &logLength, log);
      printf("Shader compile log:\n%s\n", log);
      free(log);
  }

  GLOP(::glAttachShader(hProg, hFrag));
  GLOP(::glAttachShader(hProg, hVert));
  GLOP(::glLinkProgram(hProg));

  glGetProgramiv(hProg, GL_INFO_LOG_LENGTH, &logLength);
  if (logLength > 0) {
      GLchar* log = (GLchar*)malloc(logLength);
      glGetProgramInfoLog(hProg, logLength, &logLength, log);
      printf("Program link log:\n%s\n", log);
      free(log);
  }

  GLOP(::glUseProgram(hProg));

  // get uniform's location
  GLint locTex0 = GLOP(::glGetUniformLocation(hProg, "tex0"));

  /// [loading image, creating texture goes here. works perfectly.]

  while(core.IsRunning())
  {
    GLOP(::glActiveTexture(GL_TEXTURE0));
    GLOP(pTex->Bind());
    GLOP(::glUniform1i(locTex0, GL_TEXTURE0));
    GLOP(::glPushMatrix());
    GLOP(::glTranslatef(GFX::GetFlopWidth() / 2, GFX::GetHeight() / 2, .0f)); // still no errors
    ::glBegin(GL_TRIANGLE_FAN);  // crash
    ::glTexCoord2f(.0f, 1.0f);
    ::glVertex2f(-100.0f, -100.0f);
    ::glTexCoord2f(1.0f, 1.0f);
    ::glVertex2f(100.0f, -100.0f);
    ::glTexCoord2f(1.0f, .0f);
    ::glVertex2f(100.0f, 100.0f);
    ::glTexCoord2f(.0f, .0f);
    ::glVertex2f(-100.0f, 100.0f);
    ::glEnd();
    ::glPopMatrix();
  }
片段着色器:

uniform sampler2D tex0;

void  main()
{
  vec2  texCoords = gl_TexCoord[0].st;
  vec3  pixel = texture2D(tex0, texCoords).xyz;
  gl_FragColor = vec4(pixel, 1.0);
}
输出:

Successfully set video mode 640x480@24
Shader compile log:
Fragment shader was successfully compiled to run on hardware.

Shader compile log:
Vertex shader was successfully compiled to run on hardware.

Program link log:
Vertex shader(s) linked, fragment shader(s) linked.

它会在第一帧时崩溃吗


我有点怀疑这和你的崩溃有什么关系,但是你应该知道你调用glVertex/glTexcoord的顺序不对。glVertex终止一个顶点,因此在您的情况下,第一个glVertex没有texcoord集,最后一个texcoord将应用于下一个循环。

核心配置文件中不支持这样提交顶点的即时模式,因此我想知道您在设置OpenGL上下文时是否指定了核心配置文件。这就解释了为什么它在glBegin死亡。

似乎不太可能导致它严重崩溃。另外,他在几行之前接到了glPushMatrix的电话,这也不是核心问题。说得好。此外,我在自己的设置中尝试了这一点,使用核心配置文件执行命令也很好。他们只是不做任何有用的事。所以我现在认为其他地方有一个问题,可能是由其他原因导致的某种堆栈损坏。是的,它在第一帧崩溃。感谢您指出glVertex2f和glTexCoord2f的错误顺序-这是匆忙写出纹理四元渲染的产物;我现在已经编辑过了。无论如何,本例中的运行时在崩溃之前都不会到达那里。
Successfully set video mode 640x480@24
Shader compile log:
Fragment shader was successfully compiled to run on hardware.

Shader compile log:
Vertex shader was successfully compiled to run on hardware.

Program link log:
Vertex shader(s) linked, fragment shader(s) linked.