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 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Trouble with loading textures.

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    17

    Trouble with loading textures.

    I am trying to load a texture but it doesn't want to load in. I have tried it with both SOIL2 and stb_image. They both show no data. I have been stuck at this for a few days now and do not see what the main problem is.

    You can download my project at https://github.com/Geomazzix/2D-OpenGL-platformer
    You can find the problem in the Texture.h and the Texture.cpp
    They get called from the Mesh.cpp and Mesh.h

    Hope you can help me.
    Last edited by geomazzix; 12-08-2017 at 06:22 AM. Reason: Missing content

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    460
    Quote Originally Posted by geomazzix View Post
    I am trying to load a texture but it doesn't want to load in. I have tried it with both SOIL2 and stb_image. They both show no data.
    could you post some actual code here that shows your problem ?

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    17
    Sure, there are multiple places where it can go wrong.

    First one is the texture itself, only the init function does something really big:
    (Problem: he doesn't want to log any image data)

    Code cpp:
    //Initialize the texture.
    void Texture::Init(const std::string filepath)
    {
        //Read image.
        int textureWidth, textureHeight, components;
        unsigned char* imageData = stbi_load(("../2D Platformer/Textures/" + filepath).c_str(), &textureWidth, &textureHeight, &components, 4);
        if (imageData == NULL)
        {
            std::cout << "Image failed to load in." << std::endl;
            FatalError();
        }
     
        //Create texture.
        glGenTextures(1, &_Texture);
        glBindTexture(GL_TEXTURE_2D, _Texture);
     
     
        //Set the sampling for the S(u) and T(v) of the image.
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     
        //Set the filterering for the image. (min: smaller image) (mag: bigger image).
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
     
        //Set texture data.
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
        stbi_image_free(imageData);
    }
     
     
    Secondly the mesh itself:
    Mesh::Mesh()
    {
        //Vertex Positions
        _Vertices[0].position.x = -0.5f;
        _Vertices[0].position.y = -0.5f;
        _Vertices[0].position.z = 0.0f;
     
        _Vertices[1].position.x = -0.5f;
        _Vertices[1].position.y = 0.5f;
        _Vertices[1].position.z = 0.0f;
     
        _Vertices[2].position.x = 0.5f;
        _Vertices[2].position.y = 0.5f;
        _Vertices[2].position.z = 0.0f;
     
        _Vertices[3].position.x = 0.5f;
        _Vertices[3].position.y = -0.5f;
        _Vertices[3].position.z = 0.0f;
     
     
        //Vertex Colours
        _Vertices[0].colour.r = 0;
        _Vertices[0].colour.g = 255;
        _Vertices[0].colour.b = 0;
        _Vertices[0].colour.a = 255;
     
        _Vertices[1].colour.r = 0;
        _Vertices[1].colour.g = 255;
        _Vertices[1].colour.b = 255;
        _Vertices[1].colour.a = 255;
     
        _Vertices[2].colour.r = 0;
        _Vertices[2].colour.g = 255;
        _Vertices[2].colour.b = 0;
        _Vertices[2].colour.a = 255;
     
        _Vertices[3].colour.r = 0;
        _Vertices[3].colour.g = 255;
        _Vertices[3].colour.b = 255;
        _Vertices[3].colour.a = 255;
     
     
        //Set UV coordinates.
        _Vertices[0].uv.u = 0.0f;
        _Vertices[0].uv.v = 0.0f;
     
        _Vertices[1].uv.u = 1.0f;
        _Vertices[1].uv.v = 0.0f;
     
        _Vertices[2].uv.u = 1.0f;
        _Vertices[2].uv.v = 1.0f;
     
        _Vertices[3].uv.u = 0.0f;
        _Vertices[3].uv.v = 1.0f;
    }
     
     
    //Initialize all mesh components.
    void Mesh::Init()
    {
        //Create and enable the vertrex array object.
        glGenVertexArrays(1, &_VAO);
        glBindVertexArray(_VAO);
     
        //Create and enable the vertex object buffer.
        glGenBuffers(1, &_VBO);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(_Vertices), _Vertices, GL_STATIC_DRAW);
     
        //Assign the vertex attributes.
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
        glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, colour));
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
     
        //Create a vertex element array for so the program can reuse vertices.
        glGenBuffers(1, &_EBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_Triangles), _Triangles, GL_STATIC_DRAW);
     
        _Texture.Init("cat.png");
    }
     
     
    //Draws the mesh.
    void Mesh:Draw()
    {
        glBindVertexArray(_VAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
        _Texture.Bind(0);
     
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
     
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
     
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
     
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

    *Leaving garbage disposal out of this*

    Lastly the shaders I use on the mesh:

    *Vertexshader:
    Code glsl:
    #version 450
     
    layout(location=0) in vec2 position;
    layout(location=1) in vec4 colour;
    layout(location=2) in vec2 uv;
     
    out vec4 Colour;
    out vec2 UV;
     
    void main()
    {
        Colour = colour;
        UV = uv;
        gl_Position = vec4(position, 0.0, 1.0);
    }

    *Fragmentshader
    Code glsl:
    #version 450
     
    in vec4 Colour;
    in vec2 UV;
     
    out vec4 colour;
     
    uniform sampler2D diffuse;
     
    void main()
    {
        colour = Colour;
        colour = texture(diffuse, UV) * Colour;
    }
    Last edited by Dark Photon; 12-09-2017 at 06:33 PM.

  4. #4
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    96
    Greetings, I downloaded the entire project, but I can't find this Texture::Init function anywhere, which file is it in? Do I need an updated version of the project?

    Texture.h and texture.cpp are missing from the GitHub linked project.

    Alrighty,

    Jeff

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    17
    No this is entirely my fault, apparently the last push failed to reach the server. I will push the new classes right away!

  6. #6
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    460
    Few things to check:

    If you give a wrong path to stbi_load (ie a name that does not point to any existing file), does it return an error ? This is just for ensuring that your file is actually correctly found and loaded.

    What is
    no data
    ? Is it all black ?

    Do you make any call to glActiveTexture ? Do you set a specific value to the diffuse sampler2D ?

    Even if your integer colors look to be well defined, have you tried to display only the texture in the shader ?

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    17
    - The data of the stbi_load is not equal to NULL, but when I log it, it doesn't show anything (maybe he just logs a space).
    - No have nog checked glActiveTexture and no I did not set a specific value in the diffuse sampler2D (I have no clue how I would do that).
    - No I have no clue how I would do that.

  8. #8
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    96
    "diffuse" is your uniform variable for the texture, but I can't find it in your code. it's only in the shader, are you sending that info to the shader?

    Jeff

    Also here's a link to my youtube opengl tutorials, follow along as I learn opengl:
    https://www.youtube.com/channel/UCzx..._as=subscriber

    P.S. I also wanted to comment, that I really like and admire your coding style, it's very organized, with all the .cpp and .h files separate, and the class management in separate files, etc. Eventually, I hope to find myself coding like this, but I still find it slightly confusing to track where the code is going from start to finish. I might be too old school...! lol Thanks again also, for sharing your project on GitHub.
    Last edited by OceanJeff40; 12-11-2017 at 09:00 PM.

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    17
    Well thanks for that compliment, if you want to get such a clean way of coding you should try to work with an game engine i.e. unity or unreal engine. They use a Object Oriented Programming approach (OOP). This is a commen way to code this type of programs. It basicly means every instance or component of an object has it's own responsibility.

  10. #10
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    17
    Oh by the way, what should I push into the sampler2D?

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
  •