我需要您的帮助,以便简单的事情(我希望(通过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); }
这些代码不起作用,您是否知道我错了。
非常感谢您的回答!
好吧,我明白了。
我的错误很简单。我只在第二个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个顶点的矩阵?