Changeset 8785:05064d554b96 in orange
 Timestamp:
 08/26/11 01:05:56 (3 years ago)
 Branch:
 default
 Convert:
 2a1960d8fca9f200b77d693b45e1323614d091e1
 Location:
 source/orangeqt
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

source/orangeqt/plot3d.cpp
r8780 r8785 1 #include <iostream> 2 #include <assert.h> 1 3 #include "plot3d.h" 4 5 PFNGLGENBUFFERSARBPROC glGenBuffers = NULL; 6 PFNGLBINDBUFFERPROC glBindBuffer = NULL; 7 PFNGLBUFFERDATAPROC glBufferData = NULL; 8 PFNGLENABLEVERTEXATTRIBARRAYARBPROC glEnableVertexAttribArray = NULL; 9 PFNGLDISABLEVERTEXATTRIBARRAYARBPROC glDisableVertexAttribArray = NULL; 10 typedef void (APIENTRYP PFNGLGETVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer); 11 PFNGLGETVERTEXATTRIBPOINTERPROC glVertexAttribPointer = NULL; 12 13 #define BUFFER_OFFSET(i) ((char *)NULL + (i)) 2 14 3 15 Plot3D::Plot3D(QWidget* parent) 4 16 { 17 vbo_generated = false; 5 18 } 6 19 … … 9 22 } 10 23 24 template<class T> 25 inline T clamp(T value, T min, T max) 26 { 27 if (value > max) 28 return max; 29 if (value < min) 30 return min; 31 return value; 32 } 33 34 void Plot3D::set_symbol_geometry(int symbol, int type, const QList<QVector3D>& geometry) 35 { 36 if (type == 0) 37 geometry_data_2d[symbol] = geometry; 38 else if (type == 1) 39 geometry_data_3d[symbol] = geometry; 40 else 41 geometry_data_edges[symbol] = geometry; 42 } 43 44 void Plot3D::set_data(quint64 array_address, int num_examples, int example_size) 45 { 46 this>data_array = reinterpret_cast<float*>(array_address); // 32bit systems, endianness? 47 this>num_examples = num_examples; 48 this>example_size = example_size; 49 50 // Load required extensions (OpenGL context should be up by now). 51 #ifdef _WIN32 52 // TODO: wglGetProcAddress 53 #else 54 glGenBuffers = (PFNGLGENBUFFERSARBPROC)glXGetProcAddress((const GLubyte*)"glGenBuffers"); 55 glBindBuffer = (PFNGLBINDBUFFERPROC)glXGetProcAddress((const GLubyte*)"glBindBuffer"); 56 glBufferData = (PFNGLBUFFERDATAPROC)glXGetProcAddress((const GLubyte*)"glBufferData"); 57 glVertexAttribPointer = (PFNGLGETVERTEXATTRIBPOINTERPROC)glXGetProcAddress((const GLubyte*)"glVertexAttribPointer"); 58 glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYARBPROC)glXGetProcAddress((const GLubyte*)"glEnableVertexAttribArray"); 59 glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYARBPROC)glXGetProcAddress((const GLubyte*)"glDisableVertexAttribArray"); 60 #endif 61 } 62 63 void Plot3D::update_data(int x_index, int y_index, int z_index, 64 int color_index, int symbol_index, int size_index, int label_index, 65 const QList<QColor>& colors, int num_symbols_used, 66 bool x_discrete, bool y_discrete, bool z_discrete, bool use_2d_symbols) 67 { 68 const float scale = 0.001; 69 float* vbo_data = new float[num_examples * 144 * 13]; 70 float* dest = vbo_data; 71 int size_in_bytes = 0; 72 QMap<int, QList<QVector3D> >& geometry = use_2d_symbols ? geometry_data_2d : geometry_data_3d; 73 74 for (int index = 0; index < num_examples; ++index) 75 { 76 float* example = data_array + index*example_size; 77 float x_pos = *(example + x_index); 78 float y_pos = *(example + y_index); 79 float z_pos = *(example + z_index); 80 81 int symbol = 0; 82 if (num_symbols_used > 1 && symbol_index > 1) 83 symbol = *(example + symbol_index) * num_symbols_used; 84 85 float size = *(example + size_index); 86 if (size_index < 0  size < 0.) 87 size = 1.; 88 89 float color_value = *(example + color_index); 90 int num_colors = colors.count(); 91 QColor color; 92 93 if (num_colors > 0) 94 color = colors[clamp(int(color_value * num_colors), 0, num_colors1)]; // TODO: garbage values sometimes? 95 else if (color_index > 1) 96 color = QColor(0., 0., color_value); 97 else 98 color = QColor(0., 0., 0.8); 99 100 for (int i = 0; i < geometry[symbol].count(); i += 6) { 101 size_in_bytes += 3*13*4; 102 103 for (int j = 0; j < 3; ++j) 104 { 105 // position 106 *dest = x_pos; dest++; 107 *dest = y_pos; dest++; 108 *dest = z_pos; dest++; 109 110 // offset 111 *dest = geometry[symbol][i+j].x()*size*scale; dest++; 112 *dest = geometry[symbol][i+j].y()*size*scale; dest++; 113 *dest = geometry[symbol][i+j].z()*size*scale; dest++; 114 115 // color 116 *dest = color.redF(); dest++; 117 *dest = color.greenF(); dest++; 118 *dest = color.blueF(); dest++; 119 120 // normal 121 *dest = geometry[symbol][i+3+j].x(); dest++; 122 *dest = geometry[symbol][i+3+j].y(); dest++; 123 *dest = geometry[symbol][i+3+j].z(); dest++; 124 125 // index 126 *dest = index; dest++; 127 } 128 } 129 } 130 131 glGenBuffers(1, &vbo_id); 132 glBindBuffer(GL_ARRAY_BUFFER, vbo_id); 133 glBufferData(GL_ARRAY_BUFFER, size_in_bytes, vbo_data, GL_STATIC_DRAW); 134 glBindBuffer(GL_ARRAY_BUFFER, 0); 135 136 delete [] vbo_data; 137 138 num_vertices = size_in_bytes / (13*4); 139 vbo_generated = true; 140 } 141 142 void Plot3D::draw_data() 143 { 144 if (!vbo_generated) 145 return; 146 147 glBindBuffer(GL_ARRAY_BUFFER, vbo_id); 148 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 13*4, BUFFER_OFFSET(0)); 149 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 13*4, BUFFER_OFFSET(3*4)); 150 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 13*4, BUFFER_OFFSET(6*4)); 151 glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 13*4, BUFFER_OFFSET(9*4)); 152 glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 13*4, BUFFER_OFFSET(12*4)); 153 glEnableVertexAttribArray(0); 154 glEnableVertexAttribArray(1); 155 glEnableVertexAttribArray(2); 156 glEnableVertexAttribArray(3); 157 glEnableVertexAttribArray(4); 158 159 glDrawArrays(GL_TRIANGLES, 0, num_vertices); 160 161 glDisableVertexAttribArray(0); 162 glDisableVertexAttribArray(1); 163 glDisableVertexAttribArray(2); 164 glDisableVertexAttribArray(3); 165 glDisableVertexAttribArray(4); 166 167 glBindBuffer(GL_ARRAY_BUFFER, 0); 168 } 169 11 170 #include "plot3d.moc" 
source/orangeqt/plot3d.h
r8780 r8785 3 3 4 4 #include <QtOpenGL/QGLWidget> 5 #include <QVector3D> 6 #include <GL/gl.h> 7 #include <GL/glx.h> 8 #include <GL/glxext.h> // TODO: Windows? 9 #include <GL/glext.h> 5 10 6 11 class Plot3D : public QGLWidget { … … 10 15 explicit Plot3D(QWidget* parent = 0); 11 16 virtual ~Plot3D(); 17 18 void set_symbol_geometry(int symbol, int type, const QList<QVector3D>& geometry); 19 20 void set_data(quint64 array_address, int num_examples, int example_size); 21 void update_data(int x_index, int y_index, int z_index, 22 int color_index, int symbol_index, int size_index, int label_index, 23 const QList<QColor>& colors, int num_symbols_used, 24 bool x_discrete, bool y_discrete, bool z_discrete, bool use_2d_symbols); 25 void draw_data(); 26 27 private: 28 float* data_array; 29 int num_examples; 30 int example_size; 31 int num_vertices; 32 GLuint vbo_id; 33 bool vbo_generated; 34 35 QMap<int, QList<QVector3D> > geometry_data_2d; 36 QMap<int, QList<QVector3D> > geometry_data_3d; 37 QMap<int, QList<QVector3D> > geometry_data_edges; 12 38 }; 13 39 
source/orangeqt/plot3d.sip
r8780 r8785 9 9 Plot3D(QWidget* parent /TransferThis/ = 0); 10 10 virtual ~Plot3D(); 11 12 void set_symbol_geometry(int symbol, int type, const QList<QVector3D>& geometry); 13 14 void set_data(quint64 array_address, int num_examples, int example_size); 15 void update_data(int x_index, int y_index, int z_index, 16 int color_index, int symbol_index, int size_index, int label_index, 17 const QList<QColor>& colors, int num_symbols_used, 18 bool x_discrete, bool y_discrete, bool z_discrete, bool use_2d_symbols); 19 void draw_data(); 11 20 };
Note: See TracChangeset
for help on using the changeset viewer.