Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture loaded and bound, but fragment shader doesn't render it

Tags:

opengl

glsl

I'm trying to draw a cube with an FBO texture. The texture is rendered to correctly (which I can see in gDebugger), but it doesn't render on the cube. I thought that this might be a problem with the FBO texture so I loaded an ordinary texture and tried it as well, but I get the same problem, just a black cube.

Stepping through the program in gDebugger, I can see that the texture is both loaded correctly and bound, but nothing is drawn. There also doesn't seem to be any problems with the texcoords. Note that I've also tried to output a solid color in the fragment shader which works correctly.

This is my vertex shader:

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

And this is my fragment shader:

#version 420

uniform sampler2D tex;

in vec2 f_texcoord;

out vec4 gl_FragColor;

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

And this is where I draw the cube:

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

I must be blind, because I see no errors.

(Also, please note that I am using SFML and GLEW for context creation and extensions.)

EDIT:

I don't know if this will be helpful, but this is how I set up the cube VBO, uniforms, etc.:

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);
like image 493
Merigrim Avatar asked Apr 21 '12 22:04

Merigrim


1 Answers

===EDIT===

It looks like you're missing the texture setup for the minification filter, which is a problem if you're not using a mipmapped texture. See here: http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture

like image 121
Tim Avatar answered Mar 08 '23 09:03

Tim