我一直在开发3D创作者,但由于我无法使用VBO绘制,因此我仍处于第一阶段。
这是我的glwidget.cpp代码片段,其中绘制了所有对象
void GLWidget::initializeGL()
{
#define PROGRAM_VERTEX_ATTRIBUTE 0
#define PROGRAM_NORMALS_ATTRIBUTE 1
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
vShader= new QGLShader (QGLShader::Vertex, this);
vShader->compileSourceFile("../src/shaders/editorVshader.glsl");
fShader= new QGLShader (QGLShader::Fragment, this);
fShader->compileSourceFile("../src/shaders/editorFshader.glsl");
editor= new QGLShaderProgram (this);
editor->addShader(vShader);
editor->addShader(fShader);
editor->bindAttributeLocation("vertices", PROGRAM_VERTEX_ATTRIBUTE);
editor->bindAttributeLocation("normals", PROGRAM_NORMALS_ATTRIBUTE);
editor->link();
editor->bind();
}
void GLWidget::paintGL()
{
glClearColor(0.4765625, 0.54296875, 0.6171875, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
*/
editor->setUniformValue("projectMatrix", controller->getProjectionMatrix());
editor->setUniformValue("viewMatrix", controller->getViewMatrix());
/** Ahora para las operaciones especificas de cada objeto **/
for (int i=0; i<Objects.size(); i++)
{
Objects[i].modelmatrix.scale(1.0, 1.0, 1.0);
Objects[i].modelmatrix.rotate(1.0, 0.0, 0.0, 1.0);
editor->setUniformValue("modelMatrix", Objects.at(i).modelmatrix);
glEnableClientState(GL_VERTEX_ARRAY);
Objects[i].vertexbuffer->bind();
glVertexPointer(3, GL_FLOAT, 0, Objects.at(i).indexed_vertices.data());
editor->enableAttributeArray("vertices");
editor->setAttributeBuffer("vertices", GL_FLOAT, 0, Objects[i].indexed_vertices.size() * sizeof(vertex)); // (PROGRAM_VERTEX_ATTRIBUTE, Objects[i].vertices.data());
/*
Objects[i].normalbuffer.bind();
//glVertexPointer(3, GL_FLOAT, 3, Objects.at(i).indexed_normals.data());
glEnableClientState(GL_NORMAL_ARRAY);
editor->enableAttributeArray(PROGRAM_NORMALS_ATTRIBUTE);
editor->setAttributeBuffer (PROGRAM_NORMALS_ATTRIBUTE, GL_FLOAT, 0, Objects[i].indexed_normals.size() * sizeof(vertex));*/
//glDrawArrays(GL_QUADS, 0, Objects[i].vertices.size());
Objects[i].elementbuffer->bind();
glDrawElements(GL_QUADS, Objects[i].indices.size(), GL_UNSIGNED_SHORT, (void*)0);
}
}
它在尝试执行glDrawElements指令时爆炸。我一直在追查这个问题,但我找不到什么问题。我甚至怀疑在Qt中使用QGLBuffer的正确方法。任何人都可以帮助我吗?
答案 0 :(得分:0)
我解决了。问题是我使用setAttributeBuffer指令的“tuplasize”。
是
Objects[i].indexed_vertices.size() * sizeof(vertex)
现在我将其更改为3并开始将法线作为顶点传递,结果代码为:
void GLWidget::paintGL()
{
glClearColor(0.4765625, 0.54296875, 0.6171875, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
*/
editor->setUniformValue("projectMatrix", controller->getProjectionMatrix());
editor->setUniformValue("viewMatrix", controller->getViewMatrix());
/** Ahora para las operaciones especificas de cada objeto **/
for (int i=0; i<Objects.size(); i++)
{
Objects[i].modelmatrix.scale(1.0, 1.0, 1.0);
Objects[i].modelmatrix.rotate(1.0, 0.0, 0.0, 1.0);
editor->setUniformValue("modelMatrix", Objects.at(i).modelmatrix);
glEnableClientState(GL_VERTEX_ARRAY);
editor->enableAttributeArray("vertices");
Objects[i].vertexbuffer->bind();
//glVertexPointer(3, GL_FLOAT, 0, Objects.at(i).indexed_vertices.data());
editor->setAttributeBuffer("vertices", GL_FLOAT, 0, 3); // (PROGRAM_VERTEX_ATTRIBUTE, Objects[i].vertices.data());
//glDisableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
editor->enableAttributeArray("normals");
Objects[i].normalbuffer->bind();
//glVertexPointer(3, GL_FLOAT, 3, Objects.at(i).indexed_normals.data());
editor->setAttributeBuffer ("normals", GL_FLOAT, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
//glDrawArrays(GL_QUADS, 0, Objects[i].vertices.size());
Objects[i].elementbuffer->bind();
glDrawElements(GL_QUADS, Objects[i].indices.size(), GL_UNSIGNED_SHORT, (void*)0);
}
}
这可以标记为已解决。