我很难弄清楚如何将纹理的UV坐标映射到由两个三角形组成的四边形。有人可以解释我错误的位置以及使用glDrawArrays()执行此操作的正确方法是什么? 我的代码是
.....
Login::Login(QNetworkAccessManager *manager, QObject *parent) : QObject(parent)
{
m_manager = manager;
connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotRequestFinished(QNetworkReply*)));
}
.....
我的着色器看起来像
#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);
}
使用此代码,结果是上三角形正确映射,而下面的三角形显示为灰色。
答案 0 :(得分:1)
你有6个顶点,但只提供前四个顶点的tex坐标。您用于它们的m_texCoords
数组和GL缓冲区对象都足够大,可以保存6个顶点的数据,但是您将最后两个保留为未通过化,因此您获得的第二个三角形的实际映射只是或多或少随机的。