Guest User

Untitled

a guest
Mar 24th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "curveglwidget.h"
  2.  
  3. CurveGLWidget::CurveGLWidget(QWidget* parent):QOpenGLWidget(parent), m_vao(this)
  4. {
  5.     m_drawMode = GL_LINE_STRIP;
  6. }
  7.  
  8. CurveGLWidget::~CurveGLWidget()
  9. {
  10.  
  11. }
  12.  
  13. void CurveGLWidget::initializeGL()
  14. {
  15.     initializeOpenGLFunctions();
  16.     loadShaders();
  17.     initialize();
  18. }
  19.  
  20. void CurveGLWidget::initialize()
  21. {
  22.     m_vao.create();
  23.     m_vbo.create();
  24.     m_vbo.setUsagePattern(QOpenGLBuffer::StreamDraw);
  25. }
  26.  
  27.  
  28. void CurveGLWidget::updateVertices()
  29. {
  30.     m_vao.bind();
  31.     m_vbo.bind();
  32.  
  33.     m_vbo.allocate(vertices.size() * sizeof(Vertex));
  34.     m_vbo.write(0, &vertices.begin(), vertices.size() * sizeof(Vertex));
  35.  
  36.     m_program.enableAttributeArray("position");
  37.     m_program.setAttributeBuffer("position", GL_FLOAT, 0, 2, sizeof(Vertex));
  38.  
  39.     m_program.enableAttributeArray("color");
  40.     m_program.setAttributeBuffer("color", GL_FLOAT, sizeof(glm::vec2), 4, sizeof(Vertex));
  41.  
  42.     m_vao.release();
  43.     m_vbo.release();
  44. }
  45.  
  46.  
  47.  
  48. void CurveGLWidget::loadShaders()
  49. {
  50.     if (!m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSrc)) {
  51.         qDebug() << "Error binding vertex shader: " << m_program.log();
  52.     }
  53.  
  54.     if (!m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSrc)) {
  55.         qDebug() << "Error binding fragment shader: " << m_program.log();
  56.     }
  57.  
  58.     if (!m_program.link()) {
  59.         qDebug() << "Could not link program: " << m_program.log();
  60.     }
  61.  
  62.     m_program.attributeLocation("position");
  63.     m_program.attributeLocation("color");
  64. }
  65.  
  66. void CurveGLWidget::resizeGL(int w, int h)
  67. {
  68.     glViewport(0, 0, w, h);
  69. }
  70.  
  71. void CurveGLWidget::setClearColor(glm::vec4 color)
  72. {
  73.     m_clearColor = color;
  74.     update();
  75. }
  76.  
  77. void CurveGLWidget::setDrawMode(GLenum mode)
  78. {
  79.     m_drawMode = mode;
  80.     update();
  81. }
  82.  
  83. void CurveGLWidget::paintGL()
  84. {
  85.     if (vertices.size() < 2) {
  86.         return;
  87.     }
  88.     updateVertices();
  89.     glClearColor(m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
  90.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  91.     m_program.bind();
  92.     m_vao.bind();
  93.     glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
  94.     m_vao.release();
  95.     m_program.release();
  96. }
  97.  
  98. void CurveGLWidget::onDrawModeChanged()
  99. {
  100.  
  101. }
  102.  
  103. void CurveGLWidget::mousePressEvent(QMouseEvent* e)
  104. {
  105.  
  106.     float glX = scale(0, width(), -1, 1, e->x());
  107.     float glY = scale(0, height(), -1, 1, e->y());
  108.  
  109.     Vertex v;
  110.     v.position.x = glX;
  111.     v.position.y = glY;
  112.     v.color.r    = 1.0f;
  113.     v.color.g    = 1.0f;
  114.     v.color.b    = 1.0f;
  115.     v.color.a    = 1.0f;
  116.  
  117.  
  118.     vertices.push_back(v);
  119.  
  120.  
  121.  
  122.     updateVertices();
  123.  
  124.     update();
  125. }
  126.  
  127.  
  128. float CurveGLWidget::scale(float domainStart, float domainEnd, float  rangeStart, float rangeEnd, float value)
  129. {
  130.  
  131.     float rangeInterval  = abs(rangeEnd - rangeStart);
  132.     float domainInterval = abs(domainEnd - domainStart);
  133.     float u              = (value - domainStart) / domainInterval;
  134.  
  135.  
  136.     return rangeStart + u * rangeInterval;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment