OpenGL 3.3:在3D场景上显示2D纹理的问题

时间:2013-06-28 18:59:49

标签: c++ opengl textures

我一直试图在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);

2D顶点着色器:

#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);
}

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

1 个答案:

答案 0 :(得分: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);
                              ^^^^^

尝试将10000更改为6。由于您的VBO中只有6个顶点。