OpenGL2 2D纹理显示黑色,但在OpenGL2 ES上工作

时间:2014-04-24 18:44:04

标签: opengl

我有一些适用于OpenGL 2 ES(iOS)的OpenGL代码,但是当代码在OpenGL 2(OS X)上运行时,我的纹理会变黑(或者如果我在着色器中强制使用其他颜色)。地理位置是正确的,我只是得到了黑色像素。

以下是我的着色器:它们正在编译而没有错误。

static const char *shader_source[2][2] =
{
{ NULL, NULL },

{
    /* Vertex shader */
    "uniform mat4 modelViewProj;\n"
    "uniform mat4 textureMatrix;\n"
    "attribute vec4 position;\n"
    "attribute vec2 texcoord;\n"
    "varying vec2 varyingUV;\n"
    "void main()\n"
    "{\n"
    "   gl_Position = modelViewProj * vec4( position.xyz, 1 );\n"
    "   varyingUV = (textureMatrix * vec4( texcoord.xy, 0, 1 )).xy;\n"
    "}\n",
    /* Fragment shader */
#ifdef PLATFORM_IOS
    "uniform lowp vec4 color;\n"
    "uniform sampler2D colorMap;\n"
    "varying lowp vec2 varyingUV;\n"
    "void main()\n"
    "{\n"
    "   lowp vec4 texColor = texture2D( colorMap, varyingUV );\n"
    "   gl_FragColor = color * texColor;\n"
    "}"
#endif
#ifdef PLATFORM_OSX
    "uniform vec4 color;\n"
    "uniform sampler2D colorMap;\n"
    "varying vec2 varyingUV;\n"
    "void main()\n"
    "{\n"
    "   vec4 texColor = texture2D( colorMap, varyingUV );\n"
    "   gl_FragColor = color * texColor;\n"
    "}"
#endif
}
};

以下代码用于渲染。我查看了所有变量,_vertices,_blendFuncSource等,它们在iOS和OS X上是相同的,所以没有什么奇怪的。假设此代码应该在OpenGL ES和非ES(桌面)上运行,这是正确的吗?

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, _texture->glTexture());

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _filter);

if (_useBlending) glEnable(GL_BLEND); else glDisable(GL_BLEND);

glBlendFunc(_blendFuncSource, _blendFuncDestination);

_shaderProgram->useProgram();

glUniformMatrix4fv(_shaderProgram->uniformModelViewProj, 1, 0, (GLfloat *)GGProjectTopMatrix());
glUniformMatrix4fv(_shaderProgram->uniformTextureMatrix, 1, 0, (GLfloat *)&IDENTITY_MATRIX);
glUniform1i(_shaderProgram->uniformColorMap, 0);

if (_premultAlpha)
    glUniform4fv(_shaderProgram->uniformColor, 1, (GLfloat *)&_colorPreMultipliedAlpha);
else
    glUniform4fv(_shaderProgram->uniformColor, 1, (GLfloat *)&_color);

glVertexAttribPointer(0, 2, GL_FLOAT, 0, 0, &_vertices);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, 0, 0, &_textureCoordinates);
glEnableVertexAttribArray(1);

// Draw the vertices to the screen
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glUseProgram(0);

窗口和GL上下文是使用桌面上的SDL创建的,但不是在iOS上创建的。 Image(RGBA .png)正在通过SDL_Image加载(在桌面上),一切似乎都是正确的。如果我使用SDL显示图像,它会正确显示,而不是当我尝试通过OpenGL执行所有操作时。

当我在SDL OpenGL表面绘制简单的三角形时,它们会正确显示(我的黑色纹理在顶部),所以我知道正在显示像素。同样,我的图像显示正确的大小,在正确的位置,它们只是一个黑色方块,所以我认为它是一些着色器问题。我可以强制片段着色器中的颜色,黑色图像会改变颜色。

0 个答案:

没有答案