0

我一直在尝试在 3D 场景上显示 2D 图像并且完全没有结果(因为在 3D 场景上根本没有显示任何内容)。我已经尝试过正常的 gdb 调试,并使用APITrace来确保所有的 OpenGL 调用都正常工作。我不知道为什么图像不显示。经过更多测试,我认为问题不在于纹理本身,而在于纹理所在的 2 个三角形的渲染。以下是所有相关代码:

// Constants
const int COORD_LOCATION = 10;
const int UV_LOCATION = 5;

// Program Loading
uint program = loadShaders("2d.fs","2d.vs") // Same function used for loading shaders for 3D objects, so the function itself should work

// Texture creation
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, temp_width, temp_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data); // image_data is generated by libpng earlier, and it is valid according to apitrace
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

/* Later, when texture rendering is started... */
uint UV_BUFFER, cb;

glGenBuffers(1, &UV_BUFFER);
glGenBuffers(1, &cb);

const float data[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f };

glBindBuffer(GL_ARRAY_BUFFER, UV_BUFFER);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);

vec2 f1 = vec2(-0.5f, -0.5f);
vec2 f2 = vec2(0.5f, 0.5f);

// Set the 2d coordinates for the shader
const float data2[] = { f1.x, f1.y, 
                    f1.x, f2.y, 
                    f2.x, f1.y, 
                    f2.x, f1.y, 
                    f1.x, f2.y, 
                    f2.x, f2.y };
// Load into buffer
glBindBuffer(GL_ARRAY_BUFFER, cb);
glBufferData(GL_ARRAY_BUFFER, sizeof(data2), &data2[0], GL_STATIC_DRAW);

// During rendering...
glUseProgram(program);
glEnableVertexAttribArray(COORD_LOCATION);
glBindBuffer(GL_ARRAY_BUFFER, cb);
glVertexAttribPointer(
    COORD_LOCATION,                  // attribute. No particular reason for 0, but must match the layout in the shader.
    2,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);

// Send uniform texture
uint tex = glGetUniformLocation(program, TEXTURE_UNIFORM);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
// Set our "myTextureSampler" sampler to user Texture Unit 0
glUniform1i(tex, 0);

// Send UVs to the location
glEnableVertexAttribArray(UV_LOCATION);
glBindBuffer(GL_ARRAY_BUFFER, UV_BUFFER);
glVertexAttribPointer(
    UV_LOCATION,        // attribute. No particular reason for 0, but must match the layout in the shader.
    2,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDrawArrays(GL_TRIANGLES, 0, 6);

glDisable(GL_BLEND);

glDisableVertexAttribArray(COORD_LOCATION);
glDisableVertexAttribArray(UV_LOCATION);

二维顶点着色器:

#version 330 core

layout(location = 10) in vec2 coords;
layout(location = 5) in vec2 inUV;

out vec2 UV;

void main(){
gl_Position.x = coords.x;
gl_Position.y = coords.y;
gl_Position.z = 0;
gl_Position.w = 1;
UV = inUV;
}

2D 片段着色器:

#version 330 core

in vec2 UV;

uniform sampler2D tex;

out vec3 color;

void main(){
    color = texture(tex, UV);
}

代码实际上比这更分散,但这是我已经确认归结为的。查看我们的 VCS 中的完整代码(这只是一个随机游戏,我和我的朋友决定开发以练习 OpenGL,我们甚至还没有它的真实名称)。

4

1 回答 1

0
vec2 f1 = vec2(-0.5f, -0.5f);
vec2 f2 = vec2(-0.5f, -0.5f);

这些看起来是同一个点。零(屏幕空间)区域的三角形通常不会被光栅化。

尝试更改f2为:

vec2 f2 = vec2(0.5f,  0.5f);

glDrawArrays(GL_TRIANGLES, 0, 10000);
                              ^^^^^

尝试将其更改100006. 因为您的 VBO 中只有 6 个顶点。

于 2013-06-28T19:17:48.613 回答