我有一个程序只能在屏幕上显示纹理(我现在正在学习OpenGL ES 2.0)。我有这些着色器:
static const char * vertex =
"attribute vec4 position;"
"attribute vec4 inputTextureCoordinate;"
""
"varying vec2 textureCoordinate;"
""
"void main()"
"{"
" gl_Position = position;"
" textureCoordinate = inputTextureCoordinate.xy;"
"}"
"";
static const char * fragment =
"varying highp vec2 textureCoordinate;"
""
"uniform sampler2D videoFrame;"
""
"void main()"
"{"
" gl_FragColor = texture2D(videoFrame, textureCoordinate);"
"}"
"";
它们可以很好地编译,这可以通过运行时检查来确保。这段代码获取属性和采样器:
TextureID = glGetUniformLocation(program_id, "videoFrame");
pos = glGetAttribLocation(program_id, "position");
input = glGetAttribLocation(program_id, "inputTextureCoordinate");
此代码用于显示我加载的纹理:
static const GLfloat squareVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
static const GLfloat textureVertices[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
glUseProgram(program_id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(TextureID, 0);
glVertexAttribPointer(input, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(0);
glVertexAttribPointer(pos, 2, GL_FLOAT, 0, 0, textureVertices);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
但是屏幕保持空白。我有什么想念吗?
答案 0 :(得分:0)
关注这些行。
glVertexAttribPointer(input, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(0);
您的代码先前已将“输入”分配给纹理坐标属性,但此处传递了位置数据。您还应该将'pos'属性传递给glEnableVertexAttribArray。
glVertexAttribPointer(pos, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(pos);
glVertexAttribPointer(input, 2, GL_FLOAT, 0, 0, textureVertices);
glEnableVertexAttribArray(input);
由于只传递了两个组件,因此您可能还想通过使用vec2而不是vec4在着色器中反映出来。
编辑:我的错,OpenGL ES 2.0不使用布局限定符。