Problem with displaying textures

Hi all,
This is my first post here :smiley:

Even if I did this multiple time with my new engine I have some problem with displaying textures…
My entire code is here: GitHub - newincpp/G_Rand: A tiny portable OpenGL (for now) library with minimal dependency at execution in the case the error don’t come from where I expect…

Well, here is what I did in my fragment shader (the interesting part):

layout(location = 8)uniform int textureAmount;
uniform sampler2D tex[8];
...
void main() {
    int i = 0;
    outColor = lighting();                                                                                                                                                                      
    while (i < textureAmount) {
        outColor *= texture(tex[i], fUVCoord);
    }
 }

For my texture loading I use DevIL but because I had nothing displayed I use an hard coded texture instead to be sure it’s not a loading problem… Here is my code:

void GRand::Texture::load() noexcept {
    if (!_textureId) {
         glDeleteTextures(1, &_textureId);
         _textureId = 0;
    }
    glGenTextures(1, &_textureId);
    glBindTexture(GL_TEXTURE_2D, _textureId);
    if (_filename.size() == 0) { // hardCoded texture part....
        std::cout << "\e[33;1mno texture file name, using the default one\e[0m" << std::endl;
        float pixels[] = {
            .7f, .7f, .7f,   .7f, .0f, .0f,
            .7f, .0f, .0f, .7f, .7f, .7f
        };
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);
        return;
    }
    std::cout << "loading file: " << _filename << std::endl;
    ilBindImage(_imgId);
    ilEnable(IL_ORIGIN_SET);
    ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
    if (!ilLoadImage(_filename.c_str())) {
        std::cout << "\e[31;1mfailed to load: " << _filename << "\e[0m" << std::endl;
        return;
    }
    ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, ilGetData());
    GLenum errCode;
    if ((errCode = glGetError()) != GL_NO_ERROR) {
        std::cout << _glErrorToString[errCode - GL_INVALID_ENUM] << std::endl;
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //glGenerateMipmap(GL_TEXTURE_2D);
    _loaded = true;
}

my bind function is pretty simple:

inline void bind(GLenum textureUnit_) const noexcept {
   glActiveTexture(textureUnit_);
   glBindTexture(GL_TEXTURE_2D, _textureId);
}

and To finish because here is how I use my textures:

void GRand::Material::use() const noexcept {
    unsigned int i = 0;
    for (decltype(_textures)::value_type t : _textures) { // _textures is a std::vector<const Texture*>
        t->bind(GL_TEXTURE0 + i);
        glUniform1i(glGetUniformLocation(_shaderProgram, _StexStringArray_[i]), 0);
    }
    _uTextureAmount.upload(); // _uTextureAmount is a representation of the uniform textureAmount on my fragment... upload it to syncronize it
    glUseProgram(_shaderProgram);
}

I want to say that this is not a mapping problem because when I do this:
outColor = vec4(fUVCoord, 0,1);
In my fragment I can see my texture map like that:
[ATTACH=CONFIG]1118[/ATTACH]

If you want to download the code to test things you have to know it only work of linux right now and compile only with clang. the dependency are only libjpeg libpng libtiff liblGLU libXrandr libXi libGL libpthread liblX11 and libXxf86vm I’ve included the rest in the repo. (note: you will need sse sse2 and sse3…)

There is an infinite loop in your fragment shader bolded below. Increment i in the while loop or do something like this

while(i++ < textureAmount) { ... }

Other than that I would suggest using one texture to start with. See if you get something with this simple fragment shader


void main() {
    outColor = texture(tex[0], fUVCoord);    
}

[QUOTE=NeWinn;1270318]Hi all,

layout(location = 8)uniform int textureAmount;
uniform sampler2D tex[8];
...
void main() {
    int i = 0;
    outColor = lighting();                                                                                                                                                                      
    [b]while (i < textureAmount) {
        outColor *= texture(tex[i], fUVCoord);
    }[/b]
 }


[/QUOTE]

Thanks for replying mobeen.

I’ve already tried “outColor = texture(tex[0], fUVCoord);” in my fragment shader and it make my suzane all black.
To be honest I’ve tried a lot of thing in my shader to understand where this bug come from and it’s when I introduce this (really dumb) infinite loop :whistle:

After 3 week with this bug I’ve found why.

It wasn’t in the code I’ve posted before. Actually it was because of my architecture itself.
I do every graphics things in a special thread and my texture code was not executed in this thread so it tried to load and generate before my windows was even open…

Sorry for this dumb question =/