【发布时间】:2013-12-20 14:10:00
【问题描述】:
这是我的顶点着色器:
attribute vec4 a_position;
uniform mat4 u_projection;
uniform vec4 u_origin_translation;
uniform vec4 u_translation;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
uniform vec4 u_color;
varying vec4 v_color;
attribute vec4 a_color;
void main()
{
vec4 pos = a_position + u_origin_translation + u_translation;
gl_Position = u_projection * pos;
v_texCoord = a_texCoord;
v_color = a_color * u_color;
}
和像素着色器:
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
varying vec4 v_color;
void main()
{
vec4 texColor = texture2D(s_texture, v_texCoord);
gl_FragColor = texColor * v_color;
}
我尝试在 Marmalade 7.1 平台上的 OpenGL ES 2.0 上下文中渲染纹理四边形;
将矩阵数据(投影统一“u_projection”)发送到着色器的源代码:
GLint projectionUniformLocation = glGetUniformLocation(m_ShaderProgram->GetHandle(), m_ShaderProgram->GetProjectionUniformName().c_str());
glUniformMatrix4fv(projectionUniformLocation, 1, 0, &m_Ortho[0]);
我的追踪码:
glGetActiveUniform(m_ShaderProgram->GetHandle(), projectionUniformLocation, maxUniformLength, 0, &size, &type, &buffer[0]);
TRACE_OUT("Context::ApplyOrtho: type is matrix " << (type == GL_FLOAT_MAT4));
TRACE_OUT("Context::ApplyOrtho: type is " << type);
TRACE_OUT("Context::ApplyOrtho: buffer is " << &buffer[0]);
TRACE_OUT("Context::ApplyOrtho: projectionUniformLocation is " << projectionUniformLocation);
整个程序在 Windows 的模拟器中运行良好。它显示了带纹理的四边形,并且跟踪我的 projectionUniformLocation 是 GL_FLOAT_MAT4 的类型,所以它是正确的。
但是!!!当我在 ipad 3 上运行它时,相同的代码以另一种方式工作。 跟踪显示我的 projectionUniformLocation 不是 GL_FLOAT_MAT4,但它是 GL_SAMPLER_2D 并且输出缓冲区消息包含“s_texture”! !! 可见结果是没有纹理的黑色四边形。
更新:在 Windows 上模拟器 projectionUniformLocation 索引是 3,但在 ipad 上它的索引是 2;
更新 2: 模拟器中的活动制服列表:
Context::PrintActiveUniforms: active is s_texture and it's location is 0
Context::PrintActiveUniforms: active is u_color and it's location is 1
Context::PrintActiveUniforms: active is u_origin_translation and it's location is 2
Context::PrintActiveUniforms: active is u_projection and it's location is 3
Context::PrintActiveUniforms: active is u_translation and it's location is 4
并从 ipad 列出:
Context::PrintActiveUniforms: active is u_origin_translation and it's location is 0
Context::PrintActiveUniforms: active is u_color and it's location is 1
Context::PrintActiveUniforms: active is s_texture and it's location is 7
Context::PrintActiveUniforms: active is u_projection and it's location is 2
Context::PrintActiveUniforms: active is u_translation and it's location is 6
Update3:更多关于 ipad 3 制服的信息(Andon M. Coleman 感谢您提供源代码):
eTB_GLSL__print_uniforms: (loc=0) type=vec4 name=u_origin_translation size=1
eTB_GLSL__print_uniforms: (loc=1) type=vec4 name=u_color size=1
eTB_GLSL__print_uniforms: (loc=7) type=sampler2D name=s_texture size=1
eTB_GLSL__print_uniforms: (loc=2) type=mat4 name=u_projection size=1
eTB_GLSL__print_uniforms: (loc=6) type=vec4 name=u_translation size=1
为什么 glGetActiveUniform 说我的 u_projection 不是 mat4 以及为什么我看不到我的纹理?
有什么想法吗?
【问题讨论】:
-
你考虑过追踪
projectionUniformLocation的实际值吗?您可能没有注意到,但glGetActiveUniform (...)期望统一索引的GLuint值,但glGetUniformLocation (...)在失败时返回一个有符号整数 (-1)。万一它失败了,它将返回 -1,如果您直接传递给glGetActiveUniform (...),它将被重新解释为0xffffffff,假设系统使用 2 的补码作为有符号数。 -
我更新了我的问题。 projectionUniformLocation 为正值;
-
我在answer 中有一些代码可以回答另一个类似的问题,您可能会觉得有帮助。它与桌面 GL 相关,因此许多常量将是未定义的,但如果您将它们注释掉并运行该代码,它应该真的有助于清除问题。
-
安东谢谢!我很乐意提供任何帮助;)
标签: c++ opengl-es-2.0 shader marmalade