-1

[Edit2]: Nothing wrong with this code. My shader class didn't load the uniforms correctly.

[Edit]: It seems like I can only use GL_TEXTURE0/texture unit 0 by some reason. What I want is to draw a 2d texture and a 3d texture, but only the texture with texture unit 0(GL_TEXTURE_0) will work. And I use both of them at the same time in the shader I can't see anything using that shader.

This is the fragment shader code I want to use:

#version 330 core

// Interpolated values from the vertex shaders
in vec3 fragmentColor;
in vec3 fragmentPosition;

// Ouput data
out vec3 color;

uniform sampler3D textureSampler3D;

uniform sampler2D textureSampler2D;

float getInputLight(vec3 pos);

void main(){
    // Get the nearest corner
    vec3 cornerPosition = vec3(round(fragmentPosition.x), round(fragmentPosition.y), round(fragmentPosition.z));

    float light = getInputLight(cornerPosition);

    color = (0.5+16*light)*fragmentColor;

}

float getInputLight(vec3 pos) {
    if (pos.z <= 0.f)
        return texture2D(textureSampler2D, vec2(pos.x/16, pos.y/16)).r;

    return texture(textureSampler3D, vec3(pos.x/16, pos.y/16, pos.z/16)).r;
}

But with that I can't see anything made by that shader. If I use this I can see what the 2d textures does.

float getInputLight(vec3 pos) {
    if (pos.z <= 0.f)
        return texture(textureSampler2D, vec2(pos.x/16, pos.y/16)).r;
    return 0.f;
}

If I use this it will work perfectly except that I only have the 3d texture:

float getInputLight(vec3 pos) {
    return texture(textureSampler3D, pos/16).r;
}

That means that I can only use one of the textures on the shaders. When I say that I use the 3d texture then I change getInputLight so I it just used the 3d texture. I do the same thing with 2d textures except that I change it to the other version.

This is the c++ code I use to load the 3d texture:

glGenTextures(1, &m_3dTextureBuffer);
glBindTexture(GL_TEXTURE_3D, m_3dTextureBuffer);

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, voxelMatrixWidth, voxelMatrixHeight, voxelMatrixDepth, 0, GL_RED, GL_UNSIGNED_BYTE, (GLvoid*)m_lightData);

This is the code I use to load the 2d texture: GLuint buffer;

    unsigned char *voidData = new unsigned char[256];
            // With this I can see if the shader has right data.
    for (int i = 0; i < 256; ++i)
        voidData[i] = i%16;

    glGenTextures(1, &buffer);
    glBindTexture(GL_TEXTURE_2D, buffer);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 16, 16, 0, GL_RED, GL_UNSIGNED_BYTE, (GLvoid*)voidData);
            m_3dTextureBuffer = buffer;

This is the code I run before it draws the vertex buffer:

GLint texture3dId = shader->getUniform(1);

GLint texture2dId = shader->getUniform(2);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, m_3dTextureBuffer);
glUniform1i(texture3dId, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_2dTextureBuffer);
//glUniform1i(texture3dId,1);

If I use texture unit 0(TEXTURE0) for both 2d texture and 3d texture I get data/pixels of what I expect. This is a picture of it and it is what I expect:

http://oi57.tinypic.com/3wrbd.jpg

If I use different units I get this random data and it falshes sometimes(every pixel turns black/0 for a frame). The random data doesn't change either. If you look in some direction it doesn't flash and some directions flash faster than others.

http://oi58.tinypic.com/2ltiqoo.jpg

When I swap texture unit of the 3d texture an the 2d texture the same thing happens, but the 2d texture works an the 3d texture fails.

Do you have any idea what it could be?

  • This is quite a lot of code. Are you sure you can't distill it to a simpler example? Can you reproduce the same problem with one or two texture units instead of 6? Is this the absolutely simplest code that illustrates your problem? – Magnus Hoff Jun 18 '14 at 10:01
  • Ok, I'll make it shorter. – user2456635 Jun 18 '14 at 10:05
  • 1
    You probably at least need to load the textures in different texture units. In other words, you should use `GL_TEXTURE1` in the last code snippet like you have in your comment and describe the problems that then appear. – Magnus Hoff Jun 18 '14 at 10:33
  • You cannot sample from the same texture image unit using two kinds of samplers in the same invocation. It is valid to have two different texture targets bound to a texture image unit, just invalid to try and sample both targets in one invocation. So yeah... bind one of your textures to a different unit. – Andon M. Coleman Jun 18 '14 at 11:03

1 Answers1

0

There was nothing wrong with this code. My shader class didn't load the uniforms correctly.