12 #ifndef GLVIS_TYPES_HPP
13 #define GLVIS_TYPES_HPP
22 #ifdef __INTEL_COMPILER
25 #define GLM_FORCE_CXX98
27 #include <glm/mat4x4.hpp>
28 #include <glm/vec3.hpp>
29 #include <glm/gtc/matrix_access.hpp>
30 #include <glm/gtc/matrix_transform.hpp>
31 #include <glm/gtc/matrix_inverse.hpp>
32 #include <glm/gtc/type_ptr.hpp>
46 template<
void(*GLFinalizer)(GLu
int)>
53 ~Handle() {
if (hnd) { GLFinalizer(hnd); } }
55 : hnd{other.hnd} { other.hnd = 0; }
65 operator GLuint()
const {
return hnd; }
70 glDeleteBuffers(1, &vbo_hnd);
75 glDeleteLists(dlist, 1);
80 glDeleteProgram(prgm);
90 glDeleteVertexArrays(1, &vao);
95 glDeleteTextures(1, &tex);
100 glDeleteFramebuffers(1, &fbo);
105 glDeleteRenderbuffers(1, &rbo);
124 void rotate(
float angle,
double x,
double y,
double z)
126 mtx = glm::rotate(mtx, glm::radians(angle), glm::vec3(x,y,z));
137 mtx = glm::translate(mtx, glm::vec3(x, y, z));
141 void scale(
double x,
double y,
double z)
143 mtx = glm::scale(mtx, glm::vec3(x, y, z));
154 mtx = glm::ortho(left, right, bottom, top, z_near, z_far);
158 void perspective(
double fov,
double aspect,
double z_near,
double z_far)
160 mtx = glm::perspective(glm::radians(fov), aspect, z_near, z_far);
166 mtx = glm::mat4(1.0);
181 inline std::array<uint8_t, 4>
ColorU8(
float rgba[])
185 (rgba[0] >= 1.0) ? (uint8_t) 255 : (uint8_t)(rgba[0] * 256.),
186 (rgba[1] >= 1.0) ? (uint8_t) 255 : (uint8_t)(rgba[1] * 256.),
187 (rgba[2] >= 1.0) ? (uint8_t) 255 : (uint8_t)(rgba[2] * 256.),
188 (rgba[3] >= 1.0) ? (uint8_t) 255 : (uint8_t)(rgba[3] * 256.),
192 inline std::array<uint8_t, 4>
ColorU8(
float r,
float g,
float b,
float a)
196 (r >= 1.0) ? (uint8_t) 255 : (uint8_t)(r * 256.),
197 (g >= 1.0) ? (uint8_t) 255 : (uint8_t)(g * 256.),
198 (b >= 1.0) ? (uint8_t) 255 : (uint8_t)(b * 256.),
199 (a >= 1.0) ? (uint8_t) 255 : (uint8_t)(a * 256.),
209 return Vertex {(float) d[0], (
float) d[1], (float) d[2]};
275 std::array<float, 3> coords;
276 std::array<float, 3> norm;
277 std::array<uint8_t, 4> color;
278 std::array<float, 2> texcoord;
284 void saveVertex(
const FFState& v);
297 if (e == GL_LINES || e == GL_LINE_STRIP || e == GL_LINE_LOOP)
312 if (render_as == GL_LINES && count % 2 != 0)
316 if (render_as == GL_TRIANGLES)
318 for (
int i = 0; i < count % 3; i++)
323 if (render_as == GL_LINE_LOOP && count > 2)
326 saveVertex(saved[0]);
327 saveVertex(saved[1]);
334 curr.coords = { (float) x, (
float) y, (float) z };
335 if (render_as == GL_LINES || render_as == GL_TRIANGLES)
350 saveVertex(saved[1]);
355 else if (render_as == GL_QUADS)
360 saveVertex(saved[0]);
361 saveVertex(saved[1]);
362 saveVertex(saved[2]);
363 saveVertex(saved[0]);
364 saveVertex(saved[2]);
373 else if (render_as != GL_NONE)
378 saveVertex(saved[0]);
379 saveVertex(saved[1]);
381 if (render_as == GL_TRIANGLE_STRIP)
398 glVertex3d(d[0], d[1], d[2]);
404 curr.norm = { (float) nx, (
float) ny, (float) nz };
407 void glNormal3dv(
const double * d) { glNormal3d(d[0], d[1], d[2]); }
418 (r >= 1.0) ? (uint8_t) 255 : (uint8_t)(r * 256.),
419 (g >= 1.0) ? (uint8_t) 255 : (uint8_t)(g * 256.),
420 (b >= 1.0) ? (uint8_t) 255 : (uint8_t)(b * 256.),
421 (a >= 1.0) ? (uint8_t) 255 : (uint8_t)(a * 256.),
425 void glColor3f(
float r,
float g,
float b) { glColor4f(r, g, b, 1.f); }
427 void glColor4fv(
float * cv) { glColor4f(cv[0], cv[1], cv[2], cv[3]); }
436 curr.texcoord = { coord_u, coord_v };
451 virtual void clear() = 0;
453 virtual size_t count()
const = 0;
455 virtual GLenum getShape()
const = 0;
457 virtual size_t getStride()
const = 0;
459 virtual const void* getData()
const = 0;
467 std::vector<T> vertex_data;
475 virtual void clear() { vertex_data.clear(); }
476 virtual size_t count()
const {
return vertex_data.size(); }
478 virtual GLenum
getShape()
const {
return primitive; }
484 virtual const void*
getData()
const {
return vertex_data.data(); }
489 vertex_data.emplace_back(vertex);
495 vertex_data.insert(vertex_data.end(), verts.begin(), verts.end());
502 virtual const std::vector<int>& getIndices()
const = 0;
510 std::vector<T> vertex_data;
511 std::vector<int> vertex_indices;
520 vertex_indices.clear();
523 virtual size_t count()
const {
return vertex_data.size(); }
525 virtual GLenum
getShape()
const {
return primitive; }
531 virtual const void*
getData()
const {
return vertex_data.data(); }
533 const std::vector<int>&
getIndices()
const {
return vertex_indices; }
535 void addVertices(
const std::vector<T>& verts,
const std::vector<int>& ids)
537 int index_offset = vertex_data.size();
538 std::copy(verts.begin(), verts.end(), std::back_inserter(vertex_data));
539 int old_end = vertex_indices.size();
540 std::copy(ids.begin(), ids.end(), std::back_inserter(vertex_indices));
541 for (
size_t i = old_end; i < vertex_indices.size(); i++)
543 vertex_indices[i] += index_offset;
557 Entry(
float x,
float y,
float z,
int ox,
int oy,
const std::string& txt)
558 : rx(x), ry(y), rz(z), ox(ox), oy(oy), text(txt) { }
563 std::vector<Entry> vertex_data;
572 void addText(
float x,
float y,
float z,
int ox,
int oy,
573 const std::string& text)
575 vertex_data.emplace_back(x, y, z, ox, oy, text);
576 num_chars += text.length();
587 virtual void clear() { vertex_data.clear(); num_chars = 0; }
588 virtual size_t count()
const {
return num_chars * 6; };
589 virtual GLenum
getShape()
const {
return GL_TRIANGLES; };
590 virtual size_t getStride()
const {
return sizeof(float) * 8; };
591 virtual const void*
getData()
const {
return nullptr; }
597 const static size_t NUM_SHAPES = 2;
598 std::unique_ptr<IVertexBuffer> buffers[
NUM_LAYOUTS][NUM_SHAPES];
599 std::unique_ptr<IIndexedBuffer> indexed_buffers[
NUM_LAYOUTS][NUM_SHAPES];
604 friend class ::VisualizationScene;
606 template<
typename Vert>
610 if (shape == GL_LINES)
614 else if (shape == GL_TRIANGLES)
622 if (!buffers[Vert::layout][idx])
627 (buffers[Vert::layout][idx].get());
631 template<
typename Vert>
635 if (shape == GL_LINES)
639 else if (shape == GL_TRIANGLES)
647 if (!indexed_buffers[Vert::layout][idx])
649 indexed_buffers[Vert::layout][idx]
650 .reset(
new IndexedVertexBuffer<Vert>(shape));
652 return static_cast<IndexedVertexBuffer<Vert>*
>
653 (indexed_buffers[Vert::layout][idx].get());
658 void addText(
float x,
float y,
float z,
const std::string& text)
660 text_buffer.addText(x, y, z, 0, 0, text);
665 void addText(
float x,
float y,
float z,
int ox,
int oy,
666 const std::string& text)
668 text_buffer.addText(x, y, z, ox, oy, text);
671 template<
typename Vert>
674 getBuffer<Vert>(GL_LINES)->addVertex(v1);
675 getBuffer<Vert>(GL_LINES)->addVertex(v2);
678 template<
typename Vert>
681 getBuffer<Vert>(GL_LINES)->addVertices(verts);
684 template<
typename Vert>
687 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v1);
688 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v2);
689 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v3);
692 template<
typename Vert>
693 void addQuad(
const Vert& v1,
const Vert& v2,
const Vert& v3,
const Vert& v4)
695 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v1);
696 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v2);
697 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v3);
698 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v1);
699 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v3);
700 getBuffer<Vert>(GL_TRIANGLES)->addVertex(v4);
703 template<
typename Vert>
705 const std::vector<int>& inds)
707 getIndexedBuffer<Vert>(GL_TRIANGLES)->addVertices(verts, inds);
710 template<
typename Vert>
712 const std::vector<int>& inds)
714 std::vector<int> new_inds;
715 for (
size_t i = 0; i < inds.size() / 4; i++)
717 new_inds.emplace_back(inds[4*i]);
718 new_inds.emplace_back(inds[4*i + 1]);
719 new_inds.emplace_back(inds[4*i + 2]);
720 new_inds.emplace_back(inds[4*i]);
721 new_inds.emplace_back(inds[4*i + 2]);
722 new_inds.emplace_back(inds[4*i + 3]);
724 getIndexedBuffer<Vert>(GL_TRIANGLES)->addVertices(verts, new_inds);
727 void addCone(
float x,
float y,
float z,
728 float vx,
float vy,
float vz,
729 float cone_scale = 0.075);
741 for (
size_t j = 0; j < NUM_SHAPES; j++)
745 buffers[i][j]->clear();
747 if (indexed_buffers[i][j])
749 indexed_buffers[i][j]->clear();
759 #endif // GLVIS_TYPES_HPP
virtual size_t count() const
Gets the number of vertices contained in the buffer.
std::array< float, 3 > coord
void glColor4fv(float *cv)
Entry(float x, float y, float z, int ox, int oy, const std::string &txt)
void identity()
Sets the matrix to the identity matrix.
virtual const void * getData() const
virtual void clear()
Clears the data stored in the vertex buffer.
virtual GLenum getShape() const
Gets the primitive type contained by the vertex buffer.
void addLines(const std::vector< Vert > &verts)
void translate(double x, double y, double z)
Applies a translation transform to the matrix.
void addText(float x, float y, float z, int ox, int oy, const std::string &text)
Adds a string at the given position in object coordinates with an offset in pixels.
void glVertex3d(double x, double y, double z)
ConstIterator end() const
virtual size_t getStride() const
Gets the stride between vertices.
std::array< float, 2 > texCoord
void glColor3f(float r, float g, float b)
virtual void clear()
Clears the data stored in the vertex buffer.
virtual GLenum getShape() const
Gets the primitive type contained by the vertex buffer.
void addLine(const Vert &v1, const Vert &v2)
void shdrCleanup(GLuint shdr)
void prgmCleanup(GLuint prgm)
static Vertex create(double *d)
void fboCleanup(GLuint fbo)
void addText(float x, float y, float z, int ox, int oy, const std::string &text)
void glNormal3dv(const double *d)
std::array< uint8_t, 4 > color
std::vector< T >::const_iterator ConstIterator
const std::vector< int > & getIndices() const
GlBuilder(GlDrawable *buf)
std::array< uint8_t, 4 > ColorU8(float rgba[])
IndexedVertexBuffer(GLenum shape)
std::array< float, 3 > coord
virtual size_t count() const
Gets the number of vertices contained in the buffer.
ConstIterator begin() const
void addText(float x, float y, float z, const std::string &text)
Adds a string at the given position in object coordinates.
void vaoCleanup(GLuint vao)
virtual size_t getStride() const
Gets the stride between vertices.
virtual GLenum getShape() const
Gets the primitive type contained by the vertex buffer.
void addVertices(const std::vector< T > &verts)
Add vertices to a buffer.
ConstIterator end() const
std::vector< T >::const_iterator ConstIterator
std::array< float, 3 > coord
void setHandle(int dev_hnd)
void glNormal3d(double nx, double ny, double nz)
std::array< float, 3 > coord
virtual size_t count() const
Gets the number of vertices contained in the buffer.
std::array< float, 2 > texCoord
void addTriangleIndexed(const std::vector< Vert > &verts, const std::vector< int > &inds)
GlBuilder createBuilder()
void addVertex(const T &vertex)
Add a vertex to the buffer.
void ortho(double left, double right, double bottom, double top, double z_near, double z_far)
Sets the matrix to an orthographic projection.
void dspListCleanup(GLuint dlist)
Iterator end()
Gets an iterator pointing to the last text entry.
Iterator begin()
Gets an iterator referencing the first text entry.
void scale(double x, double y, double z)
Applies a scale transform to the matrix.
void rotate(float angle, double x, double y, double z)
Applies a rotation transform to the matrix.
void glTexCoord2f(float coord_u, float coord_v)
std::array< uint8_t, 4 > color
ConstIterator end() const
std::array< float, 3 > coord
void perspective(double fov, double aspect, double z_near, double z_far)
Sets the matrix to a perspective projection.
ConstIterator begin() const
Crude fixed-function OpenGL emulation helper.
std::array< float, 3 > coord
void addQuadIndexed(const std::vector< Vert > &verts, const std::vector< int > &inds)
void texCleanup(GLuint tex)
virtual size_t getStride() const
Gets the stride between vertices.
virtual const void * getData() const
std::vector< Entry >::iterator Iterator
void addVertices(const std::vector< T > &verts, const std::vector< int > &ids)
void addTriangle(const Vert &v1, const Vert &v2, const Vert &v3)
std::array< float, 3 > norm
void glVertex3dv(const double *d)
std::array< float, 3 > norm
void glColor4f(float r, float g, float b, float a)
std::vector< Entry >::const_iterator ConstIterator
void addQuad(const Vert &v1, const Vert &v2, const Vert &v3, const Vert &v4)
void boCleanup(GLuint vbo_hnd)
ConstIterator begin() const
void clear()
Clears the drawable object.
std::array< float, 3 > norm
virtual const void * getData() const
void rboCleanup(GLuint rbo)
VertexBuffer(GLenum shape)
virtual void clear()
Clears the data stored in the vertex buffer.