Changeset 9080:6adfe9d305c0 in orange


Ignore:
Timestamp:
10/08/11 22:42:48 (3 years ago)
Author:
matejd <matejd@…>
Branch:
default
Convert:
1e783ac42d4c6b545fa8b12d4cc3ec5e489916e5
Message:

Moved update and draw to c++ (netexplorer3d); refactored out OpenGL extensions

Location:
source/orangeqt
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • source/orangeqt/CMakeLists.txt

    r9057 r9080  
    2626    plot3d.cpp 
    2727    canvas3d.cpp 
     28    glextensions.cpp 
    2829) 
    2930qt4_automoc(${orangeqt_SRCS}) 
  • source/orangeqt/canvas3d.cpp

    r9057 r9080  
    55#include <QtCore/QList> 
    66#include <QtCore/qmath.h> 
    7 #include <QStyleOptionGraphicsItem> 
    87#include <QCoreApplication> 
    98#include <QtCore/QTime> 
    10 #include <QtCore/QParallelAnimationGroup> 
     9 
     10#include "glextensions.h" 
    1111 
    1212#define PI 3.14159265 
     13 
     14qreal _random() 
     15{ 
     16    return ((qreal)(qrand()) / RAND_MAX); 
     17} 
    1318 
    1419class QueueVertex 
     
    6570{ 
    6671    set_index(index); 
    67     set_coordinates(((qreal)(qrand() % 1000)) * 1000, ((qreal)(qrand() % 1000)) * 1000); 
     72    set_coordinates(_random(), _random(), _random()); 
    6873    set_size(1); 
    6974    set_marked(false); 
     
    337342     m_min_node_size = 5; 
    338343     m_max_node_size = 5; 
     344     m_vbos_generated = false; 
    339345} 
    340346 
     
    353359 
    354360    for (; uit != uend; ++uit) 
    355     { 
    356         uit.value()->set_coordinates(((qreal)(qrand() % 1000)) * 1000, ((qreal)(qrand() % 1000)) * 1000); 
    357     } 
     361        uit.value()->set_coordinates(_random(), _random(), _random()); 
    358362 
    359363    return 0; 
     
    10211025void Canvas3D::set_node_marks(const QMap<int, bool>& marks) 
    10221026{ 
    1023     //cancel_all_updates(); 
    10241027    QMap<int, bool>::ConstIterator it; 
    10251028    for (it = marks.constBegin(); it != marks.constEnd(); ++it) 
     
    10311034void Canvas3D::clear_node_marks() 
    10321035{ 
    1033     //cancel_all_updates(); 
    10341036    Nodes::Iterator it; 
    10351037    for (it = m_nodes.begin(); it != m_nodes.end(); ++it) 
     
    10501052        node->set_z(it.value().third); 
    10511053    } 
    1052     //register_points(); 
    10531054} 
    10541055 
    10551056void Canvas3D::set_edge_colors(const QList<QColor>& colors) 
    10561057{ 
    1057     //cancel_all_updates(); 
    10581058    int i; 
    10591059    /*for (i = 0; i < colors.size(); ++i) 
     
    10671067void Canvas3D::set_edge_sizes(double max_size) 
    10681068{ 
    1069     //cancel_all_updates(); 
    1070  
    10711069    double min_size_value = std::numeric_limits<double>::max(); 
    10721070    double max_size_value = std::numeric_limits<double>::min(); 
     
    11121110void Canvas3D::set_edge_labels(const QList<QString>& labels) 
    11131111{ 
    1114     //cancel_all_updates(); 
    11151112    int i; 
    11161113    for (i = 0; i < labels.size(); ++i) 
     
    11701167} 
    11711168 
     1169void Canvas3D::update() 
     1170{ 
     1171    if (m_nodes.size() == 0) 
     1172        return; 
     1173 
     1174    init_gl_extensions(); 
     1175 
     1176    if (m_vbos_generated) 
     1177    { 
     1178        glDeleteBuffers(1, &m_vbo_edges); 
     1179        glDeleteBuffers(1, &m_vbo_nodes); 
     1180    } 
     1181 
     1182    // Build VBOs: 
     1183    int needed_floats = m_edges.size() * 2 * 3; 
     1184    float* vbo_edges_data = new float[needed_floats]; 
     1185    float* dest = vbo_edges_data; 
     1186    memset(vbo_edges_data, 0, needed_floats * sizeof(float)); 
     1187    for (int i = 0; i < m_edges.size(); ++i) 
     1188    { 
     1189        Node3D* node = m_edges[i]->u(); 
     1190        *dest = node->x(); dest++; 
     1191        *dest = node->y(); dest++; 
     1192        *dest = node->z(); dest++; 
     1193 
     1194        node = m_edges[i]->v(); 
     1195        *dest = node->x(); dest++; 
     1196        *dest = node->y(); dest++; 
     1197        *dest = node->z(); dest++; 
     1198    } 
     1199 
     1200    glGenBuffers(1, &m_vbo_edges); 
     1201    glBindBuffer(GL_ARRAY_BUFFER, m_vbo_edges); 
     1202    glBufferData(GL_ARRAY_BUFFER, needed_floats * sizeof(float), vbo_edges_data, GL_STATIC_DRAW); 
     1203    glBindBuffer(GL_ARRAY_BUFFER, 0); 
     1204 
     1205    delete [] vbo_edges_data; 
     1206 
     1207    // Similar for nodes: 
     1208    needed_floats = m_nodes.size() * 3; 
     1209    float* vbo_nodes_data = new float[needed_floats]; 
     1210    dest = vbo_nodes_data; 
     1211    memset(vbo_nodes_data, 0, needed_floats * sizeof(float)); 
     1212    for (int i = 0; i < m_nodes.size(); ++i) 
     1213    { 
     1214        Node3D* node = m_nodes[i]; 
     1215        *dest = node->x(); dest++; 
     1216        *dest = node->y(); dest++; 
     1217        *dest = node->z(); dest++; 
     1218    } 
     1219 
     1220    glGenBuffers(1, &m_vbo_nodes); 
     1221    glBindBuffer(GL_ARRAY_BUFFER, m_vbo_nodes); 
     1222    glBufferData(GL_ARRAY_BUFFER, needed_floats * sizeof(float), vbo_nodes_data, GL_STATIC_DRAW); 
     1223    glBindBuffer(GL_ARRAY_BUFFER, 0); 
     1224 
     1225    delete [] vbo_nodes_data; 
     1226 
     1227    m_vbos_generated = true; 
     1228} 
     1229 
     1230void Canvas3D::draw_edges() 
     1231{ 
     1232    if (!m_vbos_generated) 
     1233        return; 
     1234 
     1235    glBindBuffer(GL_ARRAY_BUFFER, m_vbo_edges); 
     1236    int vertex_size = 3; 
     1237    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertex_size*4, BUFFER_OFFSET(0)); 
     1238    glEnableVertexAttribArray(0); 
     1239    glDrawArrays(GL_LINES, 0, m_edges.size() * 2); 
     1240    glDisableVertexAttribArray(0); 
     1241 
     1242    glBindBuffer(GL_ARRAY_BUFFER, 0); 
     1243} 
     1244 
     1245void Canvas3D::draw_nodes() 
     1246{ 
     1247    if (!m_vbos_generated) 
     1248        return; 
     1249 
     1250    glBindBuffer(GL_ARRAY_BUFFER, m_vbo_nodes); 
     1251    int vertex_size = 3; 
     1252    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertex_size*4, BUFFER_OFFSET(0)); 
     1253    glEnableVertexAttribArray(0); 
     1254    glDrawArrays(GL_POINTS, 0, m_nodes.size()); 
     1255    glDisableVertexAttribArray(0); 
     1256 
     1257    glBindBuffer(GL_ARRAY_BUFFER, 0); 
     1258} 
     1259 
    11721260#include "canvas3d.moc" 
  • source/orangeqt/canvas3d.h

    r9057 r9080  
    22#define CANVAS_3D_H 
    33 
     4#include "triple.h" 
     5 
    46#include <deque> 
    57#include <algorithm> 
    6 #include "triple.h" 
    78 
    89#include <QtGui/QWidget> 
     
    1011#include <QtCore/QMap> 
    1112#include <QtCore/QList> 
     13 
     14#ifdef __APPLE__ 
     15#include <OpenGL/gl.h> 
     16#else 
     17#include <GL/gl.h> 
     18#endif 
    1219 
    1320class Edge3D; 
     
    1926    virtual ~Node3D(); 
    2027 
    21     void set_coordinates(double x, double y, double z=0.0); 
     28    void set_coordinates(double x, double y, double z=0.5); 
    2229    Triple<double, double, double> coordinates() const; 
    2330 
     
    191198    QRectF data_rect() const; 
    192199 
     200    void update(); 
     201    void draw_edges(); 
     202    void draw_nodes(); 
     203 
    193204private: 
    194205    Nodes m_nodes; 
     
    201212    bool m_labels_on_marked_only; 
    202213    bool m_show_component_distances; 
     214 
     215    GLuint m_vbo_edges; 
     216    GLuint m_vbo_nodes; 
     217    bool m_vbos_generated; 
    203218}; 
    204219 
  • source/orangeqt/canvas3d.sip

    r9057 r9080  
    1010    virtual ~Node3D(); 
    1111 
    12     void set_coordinates(double x, double y, double z=0.0); 
     12    void set_coordinates(double x, double y, double z=0.5); 
    1313    Triple<double, double, double> coordinates() const; 
    1414 
     
    148148 
    149149    QRectF data_rect() const; 
     150 
     151    void update(); 
     152    void draw_edges(); 
     153    void draw_nodes(); 
     154 
    150155}; 
  • source/orangeqt/plot3d.cpp

    r9057 r9080  
    11#include "plot3d.h" 
     2 
    23#include <iostream> 
    34#include <limits> 
     
    56#include <numpy/arrayobject.h> 
    67 
    7 #ifdef _WIN32 // Should be defined by most Windows compilers 
    8 #include <GL/glext.h> 
    9 #elif defined __APPLE__ // OpenGL framework 
    10 #include <OpenGL/glext.h> 
    11 #else // Linux 
    12 #include <GL/glx.h> 
    13 #include <GL/glxext.h> 
    14 #include <GL/glext.h> 
    15 #endif 
    16  
    17 #if !defined __APPLE__ // Windows and Linux 
    18 PFNGLGENBUFFERSARBPROC glGenBuffers = NULL; 
    19 PFNGLBINDBUFFERPROC glBindBuffer = NULL; 
    20 PFNGLBUFFERDATAPROC glBufferData = NULL; 
    21 PFNGLENABLEVERTEXATTRIBARRAYARBPROC glEnableVertexAttribArray = NULL; 
    22 PFNGLDISABLEVERTEXATTRIBARRAYARBPROC glDisableVertexAttribArray = NULL; 
    23 typedef void (APIENTRYP PFNGLGETVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer); 
    24 PFNGLGETVERTEXATTRIBPOINTERPROC glVertexAttribPointer = NULL; 
    25 PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = NULL; 
    26 PFNGLUNIFORM2FPROC glUniform2f = NULL; 
    27 PFNGLDELETEBUFFERSPROC glDeleteBuffers = NULL; 
    28 #endif 
    29  
    30 #define BUFFER_OFFSET(i) ((char *)NULL + (i)) 
     8#include "glextensions.h" 
    319 
    3210Plot3D::Plot3D(QWidget* parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) 
     
    9371 
    9472    // Load required extensions (OpenGL context should be up by now). 
    95 #ifdef _WIN32 
    96     glGenBuffers = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffers"); 
    97     glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"); 
    98     glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData"); 
    99     glVertexAttribPointer = (PFNGLGETVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer"); 
    100     glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYARBPROC)wglGetProcAddress("glEnableVertexAttribArray"); 
    101     glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYARBPROC)wglGetProcAddress("glDisableVertexAttribArray"); 
    102     glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"); 
    103     glUniform2f = (PFNGLUNIFORM2FPROC)wglGetProcAddress("glUniform2f"); 
    104     glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)wglGetProcAddress("glDeleteBuffers"); 
    105 #elif defined __APPLE__ 
    106 // Should check if the extensions are available. 
    107 #else 
    108     glGenBuffers = (PFNGLGENBUFFERSARBPROC)glXGetProcAddress((const GLubyte*)"glGenBuffers"); 
    109     glBindBuffer = (PFNGLBINDBUFFERPROC)glXGetProcAddress((const GLubyte*)"glBindBuffer"); 
    110     glBufferData = (PFNGLBUFFERDATAPROC)glXGetProcAddress((const GLubyte*)"glBufferData"); 
    111     glVertexAttribPointer = (PFNGLGETVERTEXATTRIBPOINTERPROC)glXGetProcAddress((const GLubyte*)"glVertexAttribPointer"); 
    112     glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYARBPROC)glXGetProcAddress((const GLubyte*)"glEnableVertexAttribArray"); 
    113     glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYARBPROC)glXGetProcAddress((const GLubyte*)"glDisableVertexAttribArray"); 
    114     glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)glXGetProcAddress((const GLubyte*)"glGetUniformLocation"); 
    115     glUniform2f = (PFNGLUNIFORM2FPROC)glXGetProcAddress((const GLubyte*)"glUniform2f"); 
    116     glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)glXGetProcAddress((const GLubyte*)"glDeleteBuffers"); 
    117 #endif 
     73    init_gl_extensions(); 
    11874} 
    11975 
  • source/orangeqt/plot3d.h

    r9014 r9080  
    22#define PLOT_3D_H 
    33 
     4#include <Python.h> // Complains when not included first 
     5 
    46#include "plot.h" 
     7 
    58#include <QtOpenGL/QGLWidget> 
    69#include <QVector3D> 
    710#include <QVector4D> 
    811#include <QMatrix4x4> 
    9 #include <Python.h> 
    1012 
    11 #ifdef __APPLE__ // Apple OpenGL framework (What if we want to use X11 - then the include should be GL/gl.h) 
     13#ifdef __APPLE__ 
    1214#include <OpenGL/gl.h> 
    1315#else 
    1416#include <GL/gl.h> 
    15 #endif //__APPLE__ 
     17#endif 
    1618 
    1719class Plot3D : public QGLWidget { 
Note: See TracChangeset for help on using the changeset viewer.