7

我正在尝试用 FBO 纹理绘制一个立方体。纹理被正确渲染(我可以在 gDebugger 中看到),但它不会在立方体上渲染。我认为这可能是 FBO 纹理的问题,所以我加载了一个普通纹理并尝试了它,但我遇到了同样的问题,只是一个黑色立方体。

单步执行 gDebugger 中的程序,我可以看到纹理已正确加载和绑定,但没有绘制任何内容。texcoords似乎也没有任何问题。请注意,我还尝试在片段着色器中输出一个可以正常工作的纯色。

这是我的顶点着色器:

#version 420

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

layout(location = 0) in vec3 vertex;
layout(location = 1) in vec2 texcoord;

out vec2 f_texcoord;

void main() {
    gl_Position = projection * view * model * vec4(vertex, 1.0);
    f_texcoord = texcoord;
}

这是我的片段着色器:

#version 420

uniform sampler2D tex;

in vec2 f_texcoord;

out vec4 gl_FragColor;

void main() {
    gl_FragColor = texture2D(tex, f_texcoord);
}

这就是我绘制立方体的地方:

ShaderManager::Get("world")->Use();
glBindVertexArray(cube_vao);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);

我一定是盲目的,因为我看不到任何错误。

(另外,请注意我使用 SFML 和 GLEW 来创建和扩展上下文。)

编辑:

我不知道这是否会有所帮助,但这就是我设置立方体 VBO、制服等的方式:

glClearColor(1.0, 0.0, 0.0, 1.0);

glEnable(GL_DEPTH_TEST);

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
sf::Image img;
img.LoadFromFile("test.png");
img.FlipVertically();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.GetPixelsPtr());
glBindTexture(GL_TEXTURE_2D, 0);

ShaderTime = 0.0;
camz = -4.0f;
rotation_y = 0.0f;

Shader* shader = ShaderManager::Load("shader", "fx.vert", "fx.frag");
Shader* world = ShaderManager::Load("world", "world.vert", "world.frag");
shader->Use();
glUniform1f(glGetUniformLocation(shader->GetId(), "time"), ShaderTime);
world->Use();
glm::mat4 proj = glm::perspective(60.0f, (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 1000.0f);
glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, camz));
glm::mat4 model;
glUniformMatrix4fv(glGetUniformLocation(world->GetId(), "projection"), 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(glGetUniformLocation(world->GetId(), "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(world->GetId(), "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform1i(glGetUniformLocation(world->GetId(), "tex"), 0);

glGenVertexArrays(1, &cube_vao);
glBindVertexArray(cube_vao);
glGenBuffers(1, &cube_vbo);
glBindBuffer(GL_ARRAY_BUFFER, cube_vbo);

GLfloat* data = new GLfloat[5 * 36] {
    // Front
    -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,     -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,      1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f, 1.0f,       1.0f, -1.0f, 1.0f, 1.0f, 0.0f,      -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,

    // Back
    -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,    -1.0f, 1.0f, -1.0f, 0.0f, 1.0f,     1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, -1.0f, 1.0f, 1.0f,      1.0f, -1.0f, -1.0f, 1.0f, 0.0f,     -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,

    // Top
    -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,      -1.0f, 1.0f, -1.0f, 0.0f, 1.0f,     1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, -1.0f, 1.0f, 1.0f,      1.0f, 1.0f, 1.0f, 1.0f, 1.0f,       -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,

    // Bottom
    -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,     -1.0f, -1.0f, -1.0f, 0.0f, 1.0f,        1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
    1.0f, -1.0f, -1.0f, 1.0f, 1.0f,     1.0f, -1.0f, 1.0f, 1.0f, 1.0f,      -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,

    // Left
    -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,    -1.0f, 1.0f, -1.0f, 0.0f, 1.0f,     -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
    -1.0f, 1.0f, 1.0f, 0.0f, 1.0f,      -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,     -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,

    // Right
    1.0f, -1.0f, -1.0f, 0.0f, 0.0f,     1.0f, 1.0f, -1.0f, 0.0f, 1.0f,      1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 0.0f, 1.0f,       1.0f, -1.0f, 1.0f, 0.0f, 0.0f,      1.0f, -1.0f, -1.0f, 0.0f, 0.0f
};

glBufferData(GL_ARRAY_BUFFER, (5 * 36) * sizeof(GLfloat), data, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (GLvoid*)(0));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (GLvoid*)(sizeof(GLfloat) * 3));

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
4

1 回答 1

7

===编辑===

看起来您缺少缩小过滤器的纹理设置,如果您不使用 mipmapped 纹理,这是一个问题。见这里:http ://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture

于 2012-04-21T22:40:54.443 回答