升级到 OS 10.11、Xcode 7.0.1 后 GLSL 纹理采样器不工作

GLSL Texture Sampler not Working after Upgrade to OS 10.11, Xcode 7.0.1

我有一个在 Xcode 中构建的 OpenGL (4.1) 应用程序,在升级前 运行 没问题,但现在没有向 OpenGL 视图写入任何内容。应用程序构建没有错误或警告,GLSL 着色器编译和 link 没有错误。

我已经 运行 进行了一些测试以查看着色器是否基本正常工作:

如果片段着色器的最后一行是:

fragColor = vec4(1.0, 0.0, 0.0, 1.0);

OpenGL window 完全是红色的,它应该是红色的。我认为这也证实了顶点数据被正确传递。我可以为视图设置动画以绕任何轴旋转,我再次可以看到顶点数据(包括深度)正在正确传递。

如果片段着色器的最后一行是:

fragColor = vec4(texCoord[0], 0.0, 0.0, 1.0);

fragColor = vec4(texCoord[1], 0.0, 0.0, 1.0);

我在 x 或 y 方向上得到红色渐变,这表明纹理坐标也被正确传递。

所以我认为纹理数据本身由于某种原因没有被传递到采样器中。这是相关代码。

创建纹理的代码是:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures( 1, appController->appDelegate->wispTexture);
glBindTexture(GL_TEXTURE_2D, appController->appDelegate->wispTexture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)TEXTUREWIDTH, (GLsizei)TEXTUREHEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, appController->appDelegate->wispTexture[0]);

glTexImage2D 调用的数据源目前为空,因为我稍后会在应用程序中加载它。该应用程序构建纹理图像数据,但现在只是为了测试着色器的操作,我将其加载为纯白色:

for(int i = 0; i < NUMTEXTUREPOINTS; i++)
{
    appController->textureManager->wispBitmapData[i].red = 255;
    appController->textureManager->wispBitmapData[i].green = 255;
    appController->textureManager->wispBitmapData[i].blue = 255;
    appController->textureManager->wispBitmapData[i].alpha = 255;
}

其中 wispBitmapData 声明为:

AlphaPixelBytes wispBitmapData[NUMTEXTUREPOINTS];

AlphaPixelBytes 被定义为一个结构:

typedef struct
{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
} AlphaPixelBytes;

绘制图像的代码是:

glBindTexture(GL_TEXTURE_2D, appDelegate->wispTexture[0]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, (GLsizei)TEXTUREWIDTH, (GLsizei)TEXTUREHEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, appController->textureManager->wispBitmapData);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispVertexBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispVertices), wispVertices);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispTexCoordBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispTextureCoordinates), wispTextureCoordinates);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispHitsBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispScreenHits), wispScreenHits);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispNormalBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispNormals), wispNormals);

glUseProgram(appDelegate->wispShaders);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispVertexBuffer[0]);
glVertexAttribPointer(appDelegate->wispPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispPositionLoc);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispTexCoordBuffer[0]);
glVertexAttribPointer(appDelegate->wispTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispTexCoordLoc);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispHitsBuffer[0]);
glVertexAttribPointer(appDelegate->wispHitsLoc, 1, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispHitsLoc);

glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispNormalBuffer[0]);
glVertexAttribPointer(appDelegate->wispNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispNormalLoc);

glUniform1i(appDelegate->wispFilterModeLoc, coloringdata->filteringMode);
glUniform1i(appDelegate->wispMaxHitsLoc, maxScreenHits);
glUniform1i(appDelegate->wispMaxNumSamplesLoc, coloringdata->maxNumSamples);
glUniform1f(appDelegate->wispSampleSizeFactorLoc, coloringdata->sampleSizeFactor);
glUniform1i(appDelegate->wispDisplayModeLoc, coloringdata->displayMode);
glUniform1i(appDelegate->wispLightOnLocation, coloringdata->lightingOn);

glUniformMatrix4fv(appDelegate->wispModelMatrixLocation, 1, GL_FALSE, appDelegate->modelMatrix.m);
glUniformMatrix4fv(appDelegate->wispViewMatrixLocation, 1, GL_FALSE, appDelegate->viewMatrix.m);
glUniformMatrix4fv(appDelegate->wispProjectionMatrixLocation, 1, GL_FALSE, appDelegate->projectionMatrix.m);
glUniformMatrix3fv(appDelegate->wispNormalMatrixLocation, 1, GL_FALSE, appDelegate->normalMatrix.m);
glUniform3fv(appDelegate->wispLightPositionLocation, 1, coloringdata->lightPosition);

glActiveTexture(GL_TEXTURE0);
glUniform1i(appDelegate->wispTextureLoc, 0);
glBindTexture(GL_TEXTURE_2D, appDelegate->wispTexture[0]);

// *********************************** Draw the Image to The Screen

glBindVertexArray(appDelegate->wispVertexArray[0]);
glBindVertexArray(appDelegate->wispTexCoordArray[0]);
glBindVertexArray(appDelegate->wispHitsArray[0]);
glBindVertexArray(appDelegate->wispNormalArray[0]);

glDrawArrays(GL_POINTS, 0, NUMSCREENPOINTS);

glDisableVertexAttribArray(appDelegate->wispPositionLoc);
glDisableVertexAttribArray(appDelegate->wispTexCoordLoc);
glDisableVertexAttribArray(appDelegate->wispHitsLoc);
glDisableVertexAttribArray(appDelegate->wispNormalLoc);

[[appController->wispView openGLContext] flushBuffer];

有人能看出这段代码有什么问题吗?我已经检查过很多次了,并且已经检查了我能想到的所有文档。

在片段着色器中,一些声明:

in vec2 texCoord;
uniform sampler2D Texture;
out vec4 fragColor;

但是

fragColor = texture(Texture, texCoord);

呈现黑屏。

我将有关着色器的代码保持简短,因为它们实际上非常复杂。

我现在创建了非常简单的新测试着色器:

顶点着色器:

#version 410

in vec4 Position;
in vec2 TexCoord;

uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;

out vec2 texCoord;

void main()
{

    texCoord = TexCoord;

    gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * Position;
}

片段着色器:

#version 410

in vec2 texCoord;

uniform sampler2D Texture;

out vec4 fragColor;

void main()
{
    fragColor = texture(Texture, texCoord);
}

通过上述各种测试,我仍然得到相同的结果。

我还跟踪了每一步的 GL 错误代码测试,并且有 none。

我感谢那些为 Whosebug 贡献时间和经验的好人,我确信赏金中提供的声望点对任何可以帮助我解决这个问题的人来说都没有任何意义。只是明白我知道并感谢你的帮助。

我正在回答我自己的问题,因为没有其他答复,我最终解决了我所有的问题。我只是对此做了一些总结。

跟踪代码后,在我看来 OpenGL 代码没有问题,但是在进行某些调用时计时出现问题。

我将我所有的应用程序初始化代码从:

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification

至:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

这解决了所有问题(包括我现在理解的其他一些烦人的问题),并且应用程序又 运行 正常了。