OpenGL 3.1, VAO/VBO

This commit is contained in:
Sergiusz Bazanski 2018-06-22 20:16:49 +01:00
parent 0508fb3627
commit ac1fcefdb1
3 changed files with 100 additions and 21 deletions

View File

@ -116,8 +116,8 @@ DesignWidget::DesignWidget(Context *_ctx, QWidget *parent)
QList<QTreeWidgetItem *> bel_items;
for (auto bel : ctx->getBels()) {
auto name = ctx->getBelName(bel);
bel_items.append(
new BelTreeItem(name, ElementType::BEL, QString(name.c_str(ctx))));
bel_items.append(new BelTreeItem(name, ElementType::BEL,
QString(name.c_str(ctx))));
}
bel_root->addChildren(bel_items);
@ -140,8 +140,8 @@ DesignWidget::DesignWidget(Context *_ctx, QWidget *parent)
treeWidget->insertTopLevelItem(0, pip_root);
for (auto pip : ctx->getPips()) {
auto name = ctx->getPipName(pip);
pip_items.append(
new PipTreeItem(name, ElementType::PIP, QString(name.c_str(ctx))));
pip_items.append(new PipTreeItem(name, ElementType::PIP,
QString(name.c_str(ctx))));
}
pip_root->addChildren(pip_items);

View File

@ -173,6 +173,20 @@ bool LineShader::compile(void)
program_->log().toStdString().c_str());
return false;
}
if (!vao_.create())
log_abort();
vao_.bind();
if (!buffers_.position.create())
log_abort();
if (!buffers_.normal.create())
log_abort();
if (!buffers_.miter.create())
log_abort();
if (!buffers_.index.create())
log_abort();
attributes_.position = program_->attributeLocation("position");
attributes_.normal = program_->attributeLocation("normal");
attributes_.miter = program_->attributeLocation("miter");
@ -180,44 +194,84 @@ bool LineShader::compile(void)
uniforms_.projection = program_->uniformLocation("projection");
uniforms_.color = program_->uniformLocation("color");
vao_.release();
return true;
}
void LineShader::draw(const LineShaderData &line, const QMatrix4x4 &projection)
{
auto gl = QOpenGLContext::currentContext()->functions();
vao_.bind();
program_->bind();
buffers_.position.bind();
buffers_.position.allocate(&line.vertices[0],
sizeof(Vertex2DPOD) * line.vertices.size());
buffers_.normal.bind();
buffers_.normal.allocate(&line.normals[0],
sizeof(Vertex2DPOD) * line.normals.size());
buffers_.miter.bind();
buffers_.miter.allocate(&line.miters[0],
sizeof(GLfloat) * line.miters.size());
buffers_.index.bind();
buffers_.index.allocate(&line.indices[0],
sizeof(GLuint) * line.indices.size());
program_->setUniformValue(uniforms_.projection, projection);
program_->setUniformValue(uniforms_.thickness, line.thickness);
program_->setUniformValue(uniforms_.color, line.color.r, line.color.g,
line.color.b, line.color.a);
buffers_.position.bind();
program_->enableAttributeArray("position");
gl->glVertexAttribPointer(attributes_.position, 2, GL_FLOAT, GL_FALSE, 0,
&line.vertices[0]);
(void *)0);
buffers_.normal.bind();
program_->enableAttributeArray("normal");
gl->glVertexAttribPointer(attributes_.normal, 2, GL_FLOAT, GL_FALSE, 0,
&line.normals[0]);
(void *)0);
buffers_.miter.bind();
program_->enableAttributeArray("miter");
gl->glVertexAttribPointer(attributes_.miter, 1, GL_FLOAT, GL_FALSE, 0,
&line.miters[0]);
gl->glEnableVertexAttribArray(0);
gl->glEnableVertexAttribArray(1);
gl->glEnableVertexAttribArray(2);
(void *)0);
buffers_.index.bind();
gl->glDrawElements(GL_TRIANGLES, line.indices.size(), GL_UNSIGNED_INT,
&line.indices[0]);
(void *)0);
program_->disableAttributeArray("miter");
program_->disableAttributeArray("normal");
program_->disableAttributeArray("position");
gl->glDisableVertexAttribArray(2);
gl->glDisableVertexAttribArray(1);
gl->glDisableVertexAttribArray(0);
program_->release();
vao_.release();
}
FPGAViewWidget::FPGAViewWidget(QWidget *parent)
: QOpenGLWidget(parent), moveX_(0), moveY_(0), zoom_(10.0f),
lineShader_(this)
{
ctx = qobject_cast<BaseMainWindow *>(getMainWindow())->getContext();
ctx_ = qobject_cast<BaseMainWindow *>(getMainWindow())->getContext();
auto fmt = format();
fmt.setMajorVersion(3);
fmt.setMinorVersion(1);
setFormat(fmt);
fmt = format();
printf("FPGAViewWidget running on OpenGL %d.%d\n", fmt.majorVersion(),
fmt.minorVersion());
if (fmt.majorVersion() < 3) {
printf("Could not get OpenGL 3.0 context. Aborting.\n");
log_abort();
}
if (fmt.minorVersion() < 1) {
printf("Could not get OpenGL 3.1 context - trying anyway...\n ");
}
}
QMainWindow *FPGAViewWidget::getMainWindow()
@ -320,15 +374,15 @@ void FPGAViewWidget::paintGL()
// Draw Bels.
auto bels = LineShaderData(0.02f, QColor("#b000ba"));
for (auto bel : ctx->getBels()) {
for (auto &el : ctx->getBelGraphics(bel))
for (auto bel : ctx_->getBels()) {
for (auto &el : ctx_->getBelGraphics(bel))
drawElement(bels, el);
}
lineShader_.draw(bels, matrix);
// Draw Frame Graphics.
auto frames = LineShaderData(0.02f, QColor("#0066ba"));
for (auto &el : ctx->getFrameGraphics()) {
for (auto &el : ctx_->getFrameGraphics()) {
drawElement(frames, el);
}
lineShader_.draw(frames, matrix);

View File

@ -24,6 +24,7 @@
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLWidget>
#include <QPainter>
@ -162,6 +163,15 @@ class LineShader
GLuint miter;
} attributes_;
// GL buffers
struct
{
QOpenGLBuffer position;
QOpenGLBuffer normal;
QOpenGLBuffer miter;
QOpenGLBuffer index;
} buffers_;
// GL uniform locations.
struct
{
@ -173,8 +183,23 @@ class LineShader
GLuint color;
} uniforms_;
QOpenGLVertexArrayObject vao_;
public:
LineShader(QObject *parent) : parent_(parent), program_(nullptr) {}
LineShader(QObject *parent) : parent_(parent), program_(nullptr)
{
buffers_.position = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
buffers_.position.setUsagePattern(QOpenGLBuffer::StaticDraw);
buffers_.normal = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
buffers_.normal.setUsagePattern(QOpenGLBuffer::StaticDraw);
buffers_.miter = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
buffers_.miter.setUsagePattern(QOpenGLBuffer::StaticDraw);
buffers_.index = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
buffers_.index.setUsagePattern(QOpenGLBuffer::StaticDraw);
}
static constexpr const char *vertexShaderSource_ =
"attribute highp vec2 position;\n"
@ -238,7 +263,7 @@ class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions
float startDragX_;
float startDragY_;
Context *ctx;
Context *ctx_;
};
NEXTPNR_NAMESPACE_END