Guest User

Untitled

a guest
Mar 24th, 2015
15
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.  
  91.     qDebug() << "----------";
  92.     for (int i = 0; i < vertices.size(); i++) {
  93.         qDebug() << "Vertex[" << i << "] = [" << vertices[i].position.x << "," << vertices[i].position.y << "]";
  94.     }
  95.     qDebug() << "----------";
  96.  
  97.     glPointSize(5.0);
  98.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  99.     m_program.bind();
  100.     m_vao.bind();
  101.     glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
  102.     m_vao.release();
  103.     m_program.release();
  104. }
  105.  
  106. void CurveGLWidget::onDrawModeChanged()
  107. {
  108.  
  109. }
  110.  
  111. void CurveGLWidget::mousePressEvent(QMouseEvent* e)
  112. {
  113.  
  114.     float glX = scale(0, width(), -1, 1, e->x());
  115.     float glY = scale(0, height(), -1, 1, e->y());
  116.  
  117.     Vertex v;
  118.     v.position.x = glX;
  119.     v.position.y = glY;
  120.     v.color.r    = 1.0f;
  121.     v.color.g    = 1.0f;
  122.     v.color.b    = 1.0f;
  123.     v.color.a    = 1.0f;
  124.  
  125.  
  126.     vertices.push_back(v);
  127.  
  128.  
  129.  
  130.     updateVertices();
  131.  
  132.     update();
  133. }
  134.  
  135.  
  136. float CurveGLWidget::scale(float domainStart, float domainEnd, float  rangeStart, float rangeEnd, float value)
  137. {
  138.  
  139.     float rangeInterval  = abs(rangeEnd - rangeStart);
  140.     float domainInterval = abs(domainEnd - domainStart);
  141.     float u              = (value - domainStart) / domainInterval;
  142.  
  143.  
  144.     return rangeStart + u * rangeInterval;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment