Search code examples
opengltexture-mapping

How to correctly map UV coords to those of a quad made of two triangles?


I'm having a difficult time in figuring out how to map the UV coords of a texture to those of a quad made up by two triangles. Can someone explain me where the error is and what should be the correct way of doing it using glDrawArrays()? My code is

#include <GL/glew.h>
#include "QOGLWidget.h"

QOGLWidget::QOGLWidget(QWidget* parent)
    : QGLWidget(parent)
{
    m_nPoints = 6;
    m_nDims = 3;
    m_points = new GLfloat[m_nPoints * m_nDims];

    m_texCoords = new GLfloat[m_nPoints * 2];
}

QOGLWidget::~QOGLWidget()
{
}

void QOGLWidget::initializeGL()
{
    glewInit();

    m_program.addShaderFromSourceFile(QGLShader::Vertex, "vshader.glsl");
    m_program.addShaderFromSourceFile(QGLShader::Fragment, "fshader.glsl");
    m_program.link();

    m_points[0] = 0.75f;
    m_points[1] = 0.5f;
    m_points[2] = -0.75f;

    m_points[3] = -0.75f;
    m_points[4] = 0.5f;
    m_points[5] = -0.75f;

    m_points[6] = -0.75f;
    m_points[7] = 0.0f;
    m_points[8] = 0.75f;

    m_points[9] = 0.75f;
    m_points[10] = 0.5f;
    m_points[11] = -0.75f;

    m_points[12] = -0.75;
    m_points[13] = 0.0f;
    m_points[14] = 0.75f;

    m_points[15] = 0.75f;
    m_points[16] = 0.0f;
    m_points[17] = 0.75f;

    glGenVertexArrays(1, &m_vao);
    glBindVertexArray(m_vao);
        glGenBuffers(1, &m_vbo);
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glBufferData(GL_ARRAY_BUFFER, m_nPoints*m_nDims*sizeof(GLfloat), m_points, GL_STATIC_DRAW);

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, m_nDims, GL_FLOAT, GL_FALSE, 0, 0);
    glBindVertexArray(0);

    m_texCoords[0] = 1.0f;
    m_texCoords[1] = 1.0f;

    m_texCoords[2] = 0.0f;
    m_texCoords[3] = 1.0f;

    m_texCoords[4] = 0.0f;
    m_texCoords[5] = 0.0f;

    m_texCoords[6] = 1.0f;
    m_texCoords[7] = 0.0f;

    QImage img;
    img.load("plane.png");
    img = QGLWidget::convertToGLFormat(img);
    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);

    glBindVertexArray(m_vao);
        glGenBuffers(1, &m_texVbo);
        glBindBuffer(GL_ARRAY_BUFFER, m_texVbo);
        glBufferData(GL_ARRAY_BUFFER, m_nPoints*2*sizeof(GLuint), m_texCoords, GL_STATIC_DRAW);
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glBindVertexArray(0);

    glViewport(0, 0, width(), height());
}

void QOGLWidget::paintGL()
{
    glClearColor(0.85f, 0.85f, 0.85f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    m_program.bind();
    glBindTexture(GL_TEXTURE_2D, m_texture);
    glBindVertexArray(m_vao);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
    m_program.release();
}

void QOGLWidget::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
}

My shaders look like

// VERTEX SHADER
#version 330 core

layout (location = 0) in vec4 vPosition;
layout (location = 1) in vec2 vTexCoord;

out vec2 fTexCoord;

void main()
{
    gl_Position = vPosition;
    fTexCoord = vTexCoord;
}

// FRAGMENT SHADER
#version 330 core

uniform sampler2D imgTexture;

in vec2 fTexCoord;

out vec4 color;

void main()
{
    color = texture(imgTexture, fTexCoord);
} 

with this code, the result is the upper triangle being correctly mapped while the lower one just appears grayish.

enter image description here


Solution

  • You have 6 vertices, but provide only tex coords for the first four of them. Both your m_texCoords array and the GL buffer object you use for them are big enough to hold data for 6 vertices, but you leave the last two uninizitalized, so the actual mapping you get for the second triangle is just more or less random.