0
\$\begingroup\$

Hello all I have been trying learning opengl and have rendered a colorful cube and decided to texture it, now when I run the program the textures render on all of the faces except for the back face and bottom face where I just get a very light brown color her is a picture to show what is happening : Unrendered Textures

I think it may have something to do with my texture coordinates but I have the same texture coordinates for every vertex, here is the Vertex data I pass in :

static const Vertex VertexBufferData[] =
{
    //front face

    //1st triangle
    Vertex(glm::vec3(-1.0f, 1.0f, 0.0f), 
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(-1.0f, -1.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)), 

    Vertex(glm::vec3(1.0f, -1.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)), 

    //second triangle
    Vertex(glm::vec3(1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(-1.0f, 1.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //left face

    //1st Triangle
    Vertex(glm::vec3(-1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(-1.0f, -1.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(-1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //2nd triangle

    Vertex(glm::vec3(-1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(-1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(-1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //right face

    //1st triangle

    Vertex(glm::vec3(1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //2nd triangle

    Vertex(glm::vec3(1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //Top face

    //1st triangle

    Vertex(glm::vec3(-1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, 1.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //2nd triangle

    Vertex(glm::vec3(-1.0f, 1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(-1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //Bottom face

    //1st triangle

    Vertex(glm::vec3(-1.0f, -1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //2nd triangle

    Vertex(glm::vec3(-1.0f, -1.0f, 0.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(-1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),


    //Back face

    //1st triangle

    Vertex(glm::vec3(-1.0f, 1.0f, -2.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),


    Vertex(glm::vec3(1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(-1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),

    //2nd triangle

    Vertex(glm::vec3(-1.0f, -1.0f, -2.0f),
    glm::vec3(1.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),

    Vertex(glm::vec3(1.0f, -1.0f, -2.0f),
    glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),

    Vertex(glm::vec3(1.0f, 1.0f, -2.0f),
    glm::vec3(0.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f)),


};

The paramaters for Vertex are : Vertex(glm::vec3 pos, glm::vec3 color, glm::vec2 textCoord) Here is my texture code :

Texture::Texture(std::string filename)
{
    int Width, Height, NumCompononents;

    m_Texture = 0;
    if (filename.empty()) return;
    unsigned char * imageData = (unsigned char*)stbi_load(filename.c_str(), &Width, &Height, &NumCompononents, 4);
    if (imageData == nullptr)
    {
        std::cerr << "Failed to load Texture for : "<< filename << std::endl;
        return;
    }
    else std::cout << "Texture successfully loaded!!!" << std::endl;

    glEnable(GL_TEXTURE_2D);


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


    glTextureParameterf(m_Texture, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameterf(m_Texture, GL_TEXTURE_WRAP_S, GL_REPEAT);

    glTextureParameteri(m_Texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTextureParameteri(m_Texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    stbi_image_free(imageData);
}
void Texture::Bind(unsigned int unit)
{

    assert(unit >= 0 && unit <= 31);
    glActiveTexture(GL_TEXTURE0 + unit);
    glBindTexture(GL_TEXTURE_2D, m_Texture);
}

Texture::~Texture()
{
    glDeleteTextures(1, &m_Texture);
}

Here is the Object file where I render the Cube:

#include "Object.h"
#include <glm\glm.hpp>
#include <glm\gtx\transform.hpp>
#include <iostream>


Object::Object(const Vertex * VertexBufferData, unsigned int numVertices)
{
    m_NumVertices = numVertices;
//  std::cout << m_NumVertices<< std::endl;
    std::vector<glm::vec3> Colors;
    std::vector<glm::vec3> Positions;
    std::vector<glm::vec2> TexCoords;

    for (unsigned int i = 0; i < numVertices; i++)
    {
        Colors.push_back(VertexBufferData[i].m_Color);
        Positions.push_back(VertexBufferData[i].m_VertPos);
        TexCoords.push_back(VertexBufferData[i].m_texCoord);
    }
    glGenVertexArrays(1, &m_VertexArrayID);
    glBindVertexArray(m_VertexArrayID);
    // Generate 1 buffer, put the resulting identifier in Vertexbuffers
    glGenBuffers(1, m_Vertexbuffers);

    SendPositionData(Positions);
    SendColorData(Colors);
    SendTextureData(TexCoords);

}

void Object::SendColorData(std::vector<glm::vec3> Colors)
{

    glBindBuffer(GL_ARRAY_BUFFER, m_Vertexbuffers[1]);
    glBufferData(GL_ARRAY_BUFFER, m_NumVertices * sizeof(Colors[0]), Colors.data(), GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);

    glVertexAttribPointer(
        1,                  // attribute 1 which is the color of the vertices
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // is it normalized
        0,                  // stride
        (void*)0
        );

    glBindVertexArray(1);
}
void Object::SendPositionData(std::vector<glm::vec3> Positions)
{
    // send the vertices to OpenGL
    glBindBuffer(GL_ARRAY_BUFFER, m_Vertexbuffers[0]);
    glBufferData(GL_ARRAY_BUFFER, m_NumVertices * sizeof(Positions[0]), Positions.data(), GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(
        0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // is it normalized
        0,                  // stride
        (void*)0
        );

    glBindVertexArray(0);
}
void Object::SendTextureData(std::vector<glm::vec2>TexCoords)
{
    glBindBuffer(GL_ARRAY_BUFFER, m_Vertexbuffers[2]);
    glBufferData(GL_ARRAY_BUFFER, m_NumVertices * sizeof(TexCoords[0]), TexCoords.data(), GL_STATIC_DRAW);
    glEnableVertexAttribArray(2);

    glVertexAttribPointer(
        2,                  // attribute 2 the TexCoordinates
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // is it normalized
        0,                  // stride
        (void*)0
        );

    glBindVertexArray(2);
}

void Object::Draw()
{
    glDrawArrays(GL_TRIANGLES, 0, m_NumVertices); // Draw all triangles starting with vertex0 to m_NumVertices
}
Object::~Object()
{

}
\$\endgroup\$

closed as off-topic by Kromster, Josh Jan 21 '15 at 16:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Kromster, Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Anyone know what I did wrong? I can't see where I went wrong \$\endgroup\$ – user3000477 Jan 20 '15 at 4:26
  • \$\begingroup\$ From the first sight the code looks okay. Look for the bug in other related parts too. \$\endgroup\$ – Kromster Jan 20 '15 at 5:03
  • \$\begingroup\$ @KromStern I added the Object Class, I have been looking everywhere to see where I messed up but to no avail maybe it's in here but I just don't see it \$\endgroup\$ – user3000477 Jan 20 '15 at 5:08
  • \$\begingroup\$ You need to learn to debug the code. Inspect variables, cut the code out until you find the offending line. \$\endgroup\$ – Kromster Jan 20 '15 at 5:22
1
\$\begingroup\$

Usage of glVertexAttribPointer is suspicious.

glVertexAttribPointer(
    2,                  // 
    3,                  // Mistake in TexCoords, should be 2
    GL_FLOAT,           // 
    GL_FALSE,           // 
    0,                  // Should be size of your vertex
    (void*)0            // Should be offset within vertex
);

Check the documentation to see if you use it right. Examples: https://www.opengl.org/wiki/VBO_-_just_examples

\$\endgroup\$
  • \$\begingroup\$ Well the stride and offset are only if you are using one array for all the data which I am not and I did set the TexCoords size to 2 but and changed around some of my glsl code which I realised I didn't write correctly but now all I get is a brown cube no texture I know it's sending the data correctly because I set it to send the color instead of the textures and that gave me the correct output so it must be in my texture class but I am not sure what it is \$\endgroup\$ – user3000477 Jan 21 '15 at 3:49
  • \$\begingroup\$ You are on the right track with trying different thing. Keep on debugging the code! :-) \$\endgroup\$ – Kromster Jan 21 '15 at 5:57
  • \$\begingroup\$ I figured it out I kept binding the vertex array every time I sent the data to the GPU when I only need to do it every time I draw it and I have to enable it and disable it now it works :D \$\endgroup\$ – user3000477 Jan 21 '15 at 10:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.