1
\$\begingroup\$

I have a Drawable Class, a Cube Class, and a Texture class. I feel like the texture wrapping isn't being applied due to some values being passed by reference, or just being discarded. My cube just derives from Drawable and only implements the render function and some other small overhead that shouldn't be a factor.

This is the error: http://imgur.com/Dj2hCkp

Drawable:

// A structure to define each vertex of a model
struct Vertex
{
    glm::vec3 Position, Color, Normal;
    glm::vec2 UV;
};

class Drawable
{
public:
    Drawable();
    // cleans up our shaders, textures, and buffers
    virtual ~Drawable();

    // loads the buffers and shaders for our drawable
    virtual void loadBuffers();
    virtual void loadShaders();
    virtual void loadTextures();

    GLSL shaders;

    std::vector<Vertex> vertices;

    glm::mat4 model, view, projection;
    GLint modelLoc, viewLoc, projLoc, textureLocation;

    GLuint vao, vbo;

    GLTexture texture;
};

loadBuffer function:

glCreateVertexArrays(1, &vao);
glGenBuffers(1, &vbo);

glBindVertexArray(vao);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid*>(offsetof(Vertex, Position)));

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid*>(offsetof(Vertex, Color)));

glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid*>(offsetof(Vertex, UV)));

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

My texture is created with this function:

GLuint& GLTexture::createTexture(int texWrap, int textureFiltering, const std::string filePath)
{
    glGenTextures(1, &_texture);
    glBindTexture(GL_TEXTURE_2D, _texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texWrap);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texWrap);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, textureFiltering);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, textureFiltering);

    sf::Image image;
    image.loadFromFile(filePath);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
    return _texture;
}

draw:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture.getTexture());
glUniform1i(textureLocation, 0);

shaders.use();

glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glBindVertexArray(0);
shaders.unuse();

My UV is calculated by clamping the positions from -1 ~ 1 to 0 ~ 1

vert.UV = glm::vec2((vert.Position.x + 1.0f) / 2.0f, (vert.Position.y + 1.0f) / (2.0f));

I feel like i'm doing everything right, based on looking at the source code of two tutorials, yet it ends up warped no matter if i use

GL_CLAMP_TO_EDGE / BORDER
GL_REPEAT

Vertex shader: #version 330 core

layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec3 in_Color;
layout (location = 2) in vec2 in_UV;

out vec2 vs_TexturePos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(in_Position, 1.0f);
    vs_TexturePos = in_UV;
}

Fragment shader:

#version 330 core

in vec2 vs_TexturePos;

uniform sampler2D t_Texture;

out vec4 color;

void main()
{
    color = texture(t_Texture, vs_TexturePos);
}

It might be that the texture parameters aren't being stored, is there a openGL debugger I can use to verify this?

\$\endgroup\$
  • \$\begingroup\$ If you're looking for an OpenGL debugger, RenderDoc is good and maintained. \$\endgroup\$ – SurvivalMachine Jun 12 '16 at 7:31
  • \$\begingroup\$ You're generating your texture coordinates using a planar projection of the vertex coordinates in space. This type of distortion along faces parallel to the projection axis is exactly what we should expect to see when using this method. Your u is an x coordinate, your y is a v coordinate. So for the xz or yz faces you have only one coordinate changing over the entire face, leading to stretching in the other axis. If you want the texture to wrap around the cube, you'll need to use a separate source of UV data. \$\endgroup\$ – DMGregory Jun 14 '16 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.