From f94a8de6c405cd518bf259c1371030d1bf7187e9 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Mon, 3 Mar 2014 21:33:54 -0500 Subject: [PATCH] Adding backdrop --- src/backdrop.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/backdrop.h | 17 +++++++++++++++++ src/canvas.cpp | 4 ++++ src/canvas.h | 2 ++ src/fstl.pro | 6 ++++-- src/window.cpp | 2 ++ 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/backdrop.cpp create mode 100644 src/backdrop.h diff --git a/src/backdrop.cpp b/src/backdrop.cpp new file mode 100644 index 0000000..7975f72 --- /dev/null +++ b/src/backdrop.cpp @@ -0,0 +1,42 @@ +#include "backdrop.h" + +Backdrop::Backdrop() +{ + shader.addShaderFromSourceFile(QGLShader::Vertex, ":/gl/quad.vert"); + shader.addShaderFromSourceFile(QGLShader::Fragment, ":/gl/quad.frag"); + shader.link(); + + float vbuf[] = { + -1, -1, 0.00, 0.10, 0.15, + -1, 1, 0.03, 0.21, 0.26, + 1, -1, 0.00, 0.12, 0.18, + 1, 1, 0.06, 0.26, 0.30}; + + vertices.create(); + vertices.bind(); + vertices.allocate(vbuf, sizeof(vbuf)); + vertices.release(); +} + +void Backdrop::draw() +{ + shader.bind(); + vertices.bind(); + + const GLuint vp = shader.attributeLocation("vertex_position"); + const GLuint vc = shader.attributeLocation("vertex_color"); + + glEnableVertexAttribArray(vp); + glEnableVertexAttribArray(vc); + + glVertexAttribPointer(vp, 2, GL_FLOAT, false, + 5 * sizeof(GLfloat), 0); + glVertexAttribPointer(vc, 3, GL_FLOAT, false, + 5 * sizeof(GLfloat), + (GLvoid*)(2 * sizeof(GLfloat))); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 8); + + vertices.release(); + shader.release(); +} diff --git a/src/backdrop.h b/src/backdrop.h new file mode 100644 index 0000000..53e0074 --- /dev/null +++ b/src/backdrop.h @@ -0,0 +1,17 @@ +#ifndef BACKDROP_H +#define BACKDROP_H + +#include +#include + +class Backdrop +{ +public: + Backdrop(); + void draw(); +private: + QGLShaderProgram shader; + QGLBuffer vertices; +}; + +#endif // BACKDROP_H diff --git a/src/canvas.cpp b/src/canvas.cpp index 3468907..290885b 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -2,6 +2,7 @@ #include #include "canvas.h" +#include "backdrop.h" #include "glmesh.h" #include "mesh.h" @@ -39,6 +40,8 @@ void Canvas::initializeGL() mesh_shader.addShaderFromSourceFile(QGLShader::Fragment, ":/gl/mesh.frag"); mesh_shader.link(); + backdrop = new Backdrop(); + glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); } @@ -47,6 +50,7 @@ void Canvas::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + backdrop->draw(); if (mesh) draw_mesh(); } diff --git a/src/canvas.h b/src/canvas.h index e867898..c094e5b 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -8,6 +8,7 @@ class GLMesh; class Mesh; +class Backdrop; class Canvas : public QGLWidget { @@ -40,6 +41,7 @@ private: QGLShaderProgram quad_shader; GLMesh* mesh; + Backdrop* backdrop; QVector3D center; float scale; diff --git a/src/fstl.pro b/src/fstl.pro index ac58ee1..a5a0f0f 100644 --- a/src/fstl.pro +++ b/src/fstl.pro @@ -9,14 +9,16 @@ SOURCES += \ mesh.cpp \ glmesh.cpp \ loader.cpp \ - window.cpp + window.cpp \ + backdrop.cpp HEADERS += \ canvas.h \ mesh.h \ glmesh.h \ loader.h \ - window.h + window.h \ + backdrop.h CONFIG += c++11 diff --git a/src/window.cpp b/src/window.cpp index 55669a6..b4d7c99 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -13,6 +13,8 @@ Window::Window(QWidget *parent) : canvas = new Canvas(format, this); setCentralWidget(canvas); + + resize(600, 400); } void Window::load_stl(const QString &filename) -- 2.39.5