0

I am trying to apply a texture to a basic triangle in opengl. The problem is my texture doesn't works, it only applies a weird color onto the triangle...

I already looked at the doc, and some youtube tutorials but I really can't make it work.

Here are some samples of the code:

Texture constructor -

Textures::Textures(const std::string& fileName)
{

int width, height, numComponents;
unsigned char * data = stbi_load(
                            fileName.c_str()
                            , &width
                            , &height
                            , &numComponents
                            , 0
                          );

if (data == NULL)
    std::cerr << "Texture loading error: " << fileName << std::endl;

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

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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

if (numComponents == 3)
    glTexImage2D(
        GL_TEXTURE_2D
        , 0
        , GL_RGB
        , width
        , height
        , 0
        , GL_RGB
        , GL_UNSIGNED_BYTE
        , data
    );
else if(numComponents == 4)
    glTexImage2D(
        GL_TEXTURE_2D
        , 0
        , GL_RGBA
        , width
        , height
        , 0
        , GL_RGBA
        , GL_UNSIGNED_BYTE
        , data
    );

glBindTexture(GL_TEXTURE_2D, 0);

stbi_image_free(data);

}

Mesh constructor -

Meshes::Meshes(Vertex* vertices, unsigned int numVertices)
{

m_drawCount = numVertices;

glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);

std::vector<glm::vec3> positions;
std::vector<glm::vec2> texCoord;

positions.reserve(numVertices);
texCoord.reserve(numVertices);

for (unsigned int i = 0; i < numVertices; ++i) {

    positions.push_back( *vertices[i].getPos() );
    texCoord.push_back( *vertices[i].getTexture() );

}

glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(
                GL_ARRAY_BUFFER
                , positions.size() * sizeof(positions[0])
                , &positions[0]
                , GL_STATIC_DRAW
            );

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXTURE_COORD_VB]);
glBufferData(
    GL_ARRAY_BUFFER
    , texCoord.size() * sizeof(texCoord[0])
    , &texCoord[0]
    , GL_STATIC_DRAW
);

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

glBindVertexArray(0);

}

Shader fragment -

#version 120

varying out vec4 diffuseColor;

varying vec2 texCoord0;

uniform sampler2D diffuse;

void main()
{

diffuseColor = texture2D(diffuse, texCoord0 );

}

Shader vertex -

#version 120

attribute vec3 position;
attribute vec2 texCoord;

varying vec2 texCoord0;

void main()
{

gl_Position.xyz = position;
gl_Position.w = 1;
texCoord0 = texCoord;

}

Here is the code on github so you can check it out by yourself: https://github.com/Kosta-Git/Pong/tree/master/Pong

Also I have another problem which is i can't even load the texture.jpg file (bricks.jpg works) but i guess it is due to it not being 512x512.

user7392005
  • 117
  • 1
  • 8
  • I had already done the texCoord it didn't worked i putted it back in since it was only to debug, but your second solution doesnt works either – user7392005 Aug 26 '18 at 19:30
  • i tried with 120 it didn't work either. How can i know if it compiles? – user7392005 Aug 26 '18 at 19:35
  • I get access violation when i try to pass the GLuint of the program to my mesh class – user7392005 Aug 26 '18 at 20:27
  • 1
    Sorry, since you used `glBindAttribLocation`, you don't have to use `glGetAttribLocation.` But the one and only issue is in the constructor of `class Shaders`. The shader program is not linked correctly, because `m_shaders[0]` is set twice, but `m_shaders[1]` is never set. So this should be closed, because of typo. – Rabbid76 Aug 26 '18 at 21:04

1 Answers1

0

The issue is in the constructor of class Shaders. The shader program is not linked correctly, because m_shaders[0] is set twice, but m_shaders[1] is never set.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user7392005
  • 117
  • 1
  • 8