Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "curveglwidget.h"
- CurveGLWidget::CurveGLWidget(QWidget* parent):QOpenGLWidget(parent), m_vao(this)
- {
- m_drawMode = GL_LINE_STRIP;
- }
- CurveGLWidget::~CurveGLWidget()
- {
- }
- void CurveGLWidget::initializeGL()
- {
- initializeOpenGLFunctions();
- loadShaders();
- initialize();
- }
- void CurveGLWidget::initialize()
- {
- m_vao.create();
- m_vbo.create();
- m_vbo.setUsagePattern(QOpenGLBuffer::StreamDraw);
- }
- void CurveGLWidget::updateVertices()
- {
- m_vao.bind();
- m_vbo.bind();
- m_vbo.allocate(vertices.size() * sizeof(Vertex));
- m_vbo.write(0, &vertices.begin(), vertices.size() * sizeof(Vertex));
- m_program.enableAttributeArray("position");
- m_program.setAttributeBuffer("position", GL_FLOAT, 0, 2, sizeof(Vertex));
- m_program.enableAttributeArray("color");
- m_program.setAttributeBuffer("color", GL_FLOAT, sizeof(glm::vec2), 4, sizeof(Vertex));
- m_vao.release();
- m_vbo.release();
- }
- void CurveGLWidget::loadShaders()
- {
- if (!m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSrc)) {
- qDebug() << "Error binding vertex shader: " << m_program.log();
- }
- if (!m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSrc)) {
- qDebug() << "Error binding fragment shader: " << m_program.log();
- }
- if (!m_program.link()) {
- qDebug() << "Could not link program: " << m_program.log();
- }
- m_program.attributeLocation("position");
- m_program.attributeLocation("color");
- }
- void CurveGLWidget::resizeGL(int w, int h)
- {
- glViewport(0, 0, w, h);
- }
- void CurveGLWidget::setClearColor(glm::vec4 color)
- {
- m_clearColor = color;
- update();
- }
- void CurveGLWidget::setDrawMode(GLenum mode)
- {
- m_drawMode = mode;
- update();
- }
- void CurveGLWidget::paintGL()
- {
- if (vertices.size() < 2) {
- return;
- }
- updateVertices();
- glClearColor(m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
- qDebug() << "----------";
- for (int i = 0; i < vertices.size(); i++) {
- qDebug() << "Vertex[" << i << "] = [" << vertices[i].position.x << "," << vertices[i].position.y << "]";
- }
- qDebug() << "----------";
- glPointSize(5.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
- m_program.bind();
- m_vao.bind();
- glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
- m_vao.release();
- m_program.release();
- }
- void CurveGLWidget::onDrawModeChanged()
- {
- }
- void CurveGLWidget::mousePressEvent(QMouseEvent* e)
- {
- float glX = scale(0, width(), -1, 1, e->x());
- float glY = scale(0, height(), -1, 1, e->y());
- Vertex v;
- v.position.x = glX;
- v.position.y = glY;
- v.color.r = 1.0f;
- v.color.g = 1.0f;
- v.color.b = 1.0f;
- v.color.a = 1.0f;
- vertices.push_back(v);
- updateVertices();
- update();
- }
- float CurveGLWidget::scale(float domainStart, float domainEnd, float rangeStart, float rangeEnd, float value)
- {
- float rangeInterval = abs(rangeEnd - rangeStart);
- float domainInterval = abs(domainEnd - domainStart);
- float u = (value - domainStart) / domainInterval;
- return rangeStart + u * rangeInterval;
- }
Advertisement
Add Comment
Please, Sign In to add comment