我正在OpenGl中开发一个程序。我的解决方案包括一个四边形和一个我想在其上渲染的纹理。问题是纹理没有渲染到四边形。如果我更改片段着色器以渲染颜色,则会正确渲染。
这是渲染代码:
glfwSwapBuffers(window);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glDrawElements(GL_TRIANGLES, indicies.length, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
glUseProgram(0);
顶点着色器:
#version 400 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textureCoords;
out vec2 pass_TextureCoords;
void main() {
gl_Position = vec4(position, 1.0);
pass_TextureCoords = textureCoords;
}
Fragmen着色器:
#version 400 core
in vec2 pass_textureCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main() {
out_Color = texture(textureSampler, pass_textureCoords);
}
答案 0 :(得分:0)
您需要为textureSampler指定制服。 即,
glUniform1i(textureSamplerHandle, 0);
您从
中检索到的内容textureSamplerHandle = glGetUniformLocation(shaderProgram, "textureSampler");