简体   繁体   中英

OpenGL 3.3: Issues Displaying 2D Texture over 3D scene

I have been trying to display a 2D image over a 3D scene and have had absolutely no results (as in nothing is displayed over the 3D scene at all). I have tried normal gdb debugging, and using APITrace to make sure all the OpenGL calls are working properly. I have no idea why the image is not displaying. After some more tests, I believe the issue is not the texture itself, but something about the rendering of the 2 triangles which the texture is on. Below is all the relevant code:

// 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 Vertex Shader:

#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 Fragment Shader:

#version 330 core

in vec2 UV;

uniform sampler2D tex;

out vec3 color;

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

The code is actually quite a bit more scattered than this, but this is what I have confirmed it comes down to. See the full code in our VCS (this is just a random game my friend and I decided to develop to practice OpenGL, we dont even have a real name for it yet).

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

Those look like the same point. Zero-(screenspace-)area triangles generally aren't rasterized.

Try changing f2 to this:

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

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

Try changing that 10000 to 6 . Since you only have 6 vertices in your VBOs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM