Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: Trouble with loading textures.

  1. #11
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    Quote Originally Posted by geomazzix View Post
    Oh by the way, what should I push into the sampler2D?
    Nothing. By default it will be 0, which is the first texture unit. And since you don't change the texture unit on CPU side, this is well.

    For the fragment shader, just try this:

    Code :
    colour = texture(diffuse, UV)

    If it still outputs nothing, try to put 255 everywhere in your texture data and see if the result becomes white.

  2. #12
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    18
    Nope, it doesn't change anything, which means he does asign the texture, but he doesn't display it for some reason.

  3. #13
    Junior Member Regular Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    106
    Here's a function call from one of my projects:

    Code :
    glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);

    Jeff

  4. #14
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    Quote Originally Posted by geomazzix View Post
    Nope, it doesn't change anything, which means he does asign the texture, but he doesn't display it for some reason.
    You tried both ?

    Also, check for OpenGL errors (glGetError) and if such errors happen, try to figure where is the gl statement responsible of that error.

  5. #15
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    466
    i dont see anywhere what "Texture::Bind(0)" actually does.
    Code :
    //Draws the mesh.
    void Mesh:Draw()
    {
        glBindVertexArray(_VAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
        _Texture.Bind(0);
    ...

    just to reiterate:
    to draw a cube in a transformed position/orientation
    Code :
    glUseProgram(myprogram);
    glBindVertexArray(myvao);
     
    mat4 mymatrix = mat4(1);//or some other transform * view * perspective
    glUniformMatrix4fv(location, 1, GL_FALSE, value_ptr(mymatrix));
     
    glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_BYTE/*or shhort/int*/, nullptr);
     
    //glBindVertexArray(0);
    //glUseProgram(0);

    thats all, to do texturing, your fragmentshader needs a "uniform sampler2D mytexture;". in addition, you c/c++ code needs to tell the "myprogram" from what texture unit (lets say 3) it is sourced:
    Code :
    GLint location = glGetUniformLocation(myprogram, "mytexture");
    if (location == -1)
        /* handle error*/;
    else
        glUniform1i(location, 3);

    and right before drawing (that is the "glDraw*whatever" command), you bind the necessary texture to that unit:
    Code :
    glBindTextureUnit(3, sometexture);

    then your cube will be textured according to the texcoords that you've sent to the fragmentshader. note that you dont need to bind (again) the element buffer or the vertex buffer since that info is stored in the VAO. take a look at this example, or read any of the tutorials found on the internet.

    thats part 1
    --------------------------------------------------------
    part 2: texture data

    if stbi_load(...) doen NOT return 0, then it has succeded, so you have to generate a texture, allocate memory for it and fill it with that data, as usual by calling:
    Code :
    GLuint texture = 0;
    glGenTextures(1, &tetxure);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, ... etc ...);

    for testing purposes, you can skip the loading part and just set a 2x2 texture with 4 different texel colors:
    Code :
    struct Texel {
        unsigned char r, g, b, a;
    };
     
    Texel texturedata[] = {
        //                blue                                    yellow
        { 0x00, 0x00, 0xFF, 0xFF }, { 0xFF, 0xFF, 0x00, 0xFF },
        //                red                                    green
        { 0xFF, 0x00, 0x00, 0xFF }, { 0x00, 0xFF, 0x00, 0xFF },
    };
    Last edited by john_connor; 12-13-2017 at 07:04 AM.

  6. #16
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    18
    it's a function in the Texture.cpp file, it binds the texture to a unit texture, after that it binds it completely.

  7. #17
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    18
    I know I try to use them as frequently as possible.

  8. #18
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    18
    I followed through the code of jhon_connor, but this did not change anything. I have no clue what the problem might be....

  9. #19
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    466
    Quote Originally Posted by geomazzix View Post
    I followed through the code of jhon_connor, but this did not change anything. I have no clue what the problem might be....
    me neither ...

    so lets guess: what are the "symptoms" ? whats not working ? is your cube/rectangle completely black or what color has it ? in case it is black, replace the fragmentshader output with:

    out_color = vec4(0, 1, 0, 1);

    the cube/rectangle/whatever should be green, is it ?
    if yes, then texturing doesnt work as supposed ...

    replace the fragmentshader output with:

    out_color = texelFetch(mytexture, ivec2(0, 0), 0);

    and put just 1 texel in your texture, as i did in the example code linked above, bind it ...

    what color has your cube now ?

    in texture.cpp:
    Code :
    //Allocate texture memory.
    	glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, textureWidth, textureHeight);
     
    	//Set texture data.
    glTexImage2D(GL_TEXTURE_2D, 0, 0, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
    that allocates texture memory twice, you're supposed to set the data with ...
    either ...
    glTexImage2D(...)
    or ...
    glTexStorage2D(...) + glTexSubImage2D(...)

    whats the difference ?
    glTexStorage2D allocates an "immutable" texture, glTexImage2D doesnt ...

    https://www.khronos.org/opengl/wiki/...utable_storage
    https://www.khronos.org/opengl/wiki/...utable_storage
    Last edited by john_connor; 12-13-2017 at 02:09 PM.

  10. #20
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    22
    Quote Originally Posted by geomazzix View Post
    I followed through the code of jhon_connor, but this did not change anything. I have no clue what the problem might be....
    have you tried?
    Code :
    glTexImage2D(GL_TEXTURE_2D, 0, 0, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, &imageData[0]);

    Also what are the dimensions of the image you are trying to load? I know in 1.1 it had some problems with some images of certain sizes but I'm not sure if any other versions had the same issues.
    Last edited by didydriver; 12-13-2017 at 04:37 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •