当你通过VBO传递Matrices4x4并在着色器(版本4.3)中获取它时,我需要你的帮助来做一件简单的事情(我希望)。 我不知道该怎么做。我有我的代码版本:
void GLWidget::initializeGL()
{
// Set up the rendering context, load shaders and other resources, etc.:
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
/** creation first triangle **/
m_data.push_back(-.1f); m_data.push_back(0.f); m_data.push_back(0.f);
m_data.push_back(0.f); m_data.push_back(.1f); m_data.push_back(0.f);
m_data.push_back(.1f); m_data.push_back(0.f); m_data.push_back(0.f);
m_label = 0;
m_matrixObjects.push_back(QMatrix4x4());
//initialize a triangle and a matrix model in VBOs
m_VAO[0].create();
m_VBO[COORDINATES].create();
m_VBO[MATRICES].create();
m_VAO[0].bind();
m_VBO[COORDINATES].bind();
m_VBO[COORDINATES].allocate(sizeof(float) * NBCOORDSTRIANGLE * MAXNBELEMENTSVBO);
m_VBO[COORDINATES].write(0, m_data.data(), sizeof(float) * m_data.size());
f->glVertexAttribPointer(COORDINATES, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
f->glEnableVertexAttribArray(COORDINATES);
m_VBO[COORDINATES].release();
m_VBO[MATRICES].bind();
m_VBO[MATRICES].allocate(sizeof(float) * SIZEMATRIX4X4 * MAXNBELEMENTSVBO);
m_VBO[MATRICES].write(0, m_matrixObjects[0].data(), sizeof(float) * SIZEMATRIX4X4);
f->glVertexAttribPointer(MATRICES, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)0);
f->glEnableVertexAttribArray(MATRICES);
f->glVertexAttribPointer(MATRICES+1, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)(4 * sizeof(float)));
f->glEnableVertexAttribArray(MATRICES+1);
f->glVertexAttribPointer(MATRICES+2, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)(8 * sizeof(float)));
f->glEnableVertexAttribArray(MATRICES+2);
f->glVertexAttribPointer(MATRICES+3, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)(12 * sizeof(float)));
f->glEnableVertexAttribArray(MATRICES+3);
m_VBO[MATRICES].release();
m_VAO[0].release();
m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, "vertexShader.glsl");
m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, "fragmentShader.glsl");
m_program.link();
m_matrixPUniform = m_program.uniformLocation("P");
m_matrixVUniform = m_program.uniformLocation("V");
m_matrixMUniform = m_program.uniformLocation("M");
m_matrixMObjectsUniform[0] = m_program.uniformLocation("M0");
m_near = .1f;
float far = 100.f, angleInDegrees = 60.f, ratio = 4.f/3.f;
m_eyeOrigin = QVector3D(0.f,0.f,-4.f);
QVector3D target(0.f,0.f,0.f), up(0.f,1.f,0.f);
MVP.V.lookAt(
m_eyeOrigin,
target,
up
);
MVP.P.perspective(
angleInDegrees,
ratio,
m_near,
far
);
m_nbDrawingPoints = 3; }
我的顶点着色器:
#version 430 core
layout (location = 0) in vec3 position;
layout (location = 4) in mat4 M0
uniform mat4 P, V, M;
void main() {
gl_Position = P * V * M * vec4(position.x, position.y, position.z, 1.0); }
这些代码不起作用,你知道我错在哪里。
非常感谢你的答案!
答案 0 :(得分:0)
好的,我明白了。
我的错误很简单。我在第二个VBO中只放了一个矩阵,然后顶点着色器为第一个顶点应用矩阵,之后没有为其他顶点定义矩阵。
我需要为网格的每个点复制3次相同的矩阵。
以下代码:
m_VAO[0].create();
m_VBO[COORDINATES].create();
m_VBO[MATRICES].create();
m_VAO[0].bind();
m_VBO[COORDINATES].bind();
m_VBO[COORDINATES].allocate(sizeof(float) * NBCOORDSTRIANGLE * MAXNBELEMENTSVBO);
m_VBO[COORDINATES].write(0, m_data.data(), sizeof(float) * m_data.size());
f->glVertexAttribPointer(COORDINATES, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
f->glEnableVertexAttribArray(COORDINATES);
m_VBO[COORDINATES].release();
m_VBO[MATRICES].bind();
m_VBO[MATRICES].allocate(sizeof(float) * SIZEMATRIX4X4 * MAXNBELEMENTSVBO * 3);
//first vertex
m_VBO[MATRICES].write(0, m_matrixObjects[0].data(), sizeof(float) * SIZEMATRIX4X4);
//second vertex
m_VBO[MATRICES].write(sizeof(float) * SIZEMATRIX4X4, m_matrixObjects[0].data(), sizeof(float) * SIZEMATRIX4X4);
//third vertex
m_VBO[MATRICES].write(sizeof(float) * SIZEMATRIX4X4 * 2, m_matrixObjects[0].data(), sizeof(float) * SIZEMATRIX4X4);
f->glVertexAttribPointer(MATRICES, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)0);
f->glEnableVertexAttribArray(MATRICES);
f->glVertexAttribPointer(MATRICES+1, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)(4 * sizeof(float)));
f->glEnableVertexAttribArray(MATRICES+1);
f->glVertexAttribPointer(MATRICES+2, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)(8 * sizeof(float)));
f->glEnableVertexAttribArray(MATRICES+2);
f->glVertexAttribPointer(MATRICES+3, 4, GL_FLOAT, GL_FALSE, SIZEMATRIX4X4 * sizeof(float), (GLvoid*)(12 * sizeof(float)));
f->glEnableVertexAttribArray(MATRICES+3);
m_VBO[MATRICES].release();
m_VAO[0].release();
例如在我的情况下,没有办法为3个顶点保留一个矩阵?