supereight
Loading...
Searching...
No Matches
draw.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2011-2013 Gerhard Reitmayr, TU Graz
3 * SPDX-FileCopyrightText: 2014 University of Edinburgh, Imperial College, University of Manchester
4 * SPDX-FileCopyrightText: 2016-2019 Emanuele Vespa
5 * SPDX-FileCopyrightText: 2021 Smart Robotics Lab, Imperial College London, Technical University of Munich
6 * SPDX-FileCopyrightText: 2021 Nils Funk
7 * SPDX-FileCopyrightText: 2021 Sotiris Papatheodorou
8 * SPDX-License-Identifier: MIT
9 */
10
11#ifndef SE_DRAW_HPP
12#define SE_DRAW_HPP
13
14#include <Eigen/Core>
15#include <se/common/rgb.hpp>
16#include <se/common/rgba.hpp>
17
18#ifdef SE_GLUT
19# include <algorithm>
20# include <cstdint>
21
22# ifdef __APPLE__
23# include <GLUT/glut.h>
24# else
25# include <GL/glut.h>
26# endif
27
28
29template<typename T>
30struct gl;
31
32template<>
33struct gl<float> {
34 static const int format = GL_LUMINANCE;
35 static const int type = GL_FLOAT;
36};
37
38template<>
39struct gl<uint8_t> {
40 static const int format = GL_LUMINANCE;
41 static const int type = GL_UNSIGNED_BYTE;
42};
43
44template<>
45struct gl<uint16_t> {
46 static const int format = GL_LUMINANCE;
47 static const int type = GL_UNSIGNED_SHORT;
48};
49
50template<>
51struct gl<uint32_t> {
52 static const int format = GL_RGBA;
53 static const int type = GL_UNSIGNED_BYTE;
54};
55
56template<>
57struct gl<se::RGB> {
58 static const int format = GL_RGB;
59 static const int type = GL_UNSIGNED_BYTE;
60};
61
62template<>
63struct gl<se::RGBA> {
64 static const int format = GL_RGBA;
65 static const int type = GL_UNSIGNED_BYTE;
66};
67
68
69
70template<typename T>
71void drawit(const T* scene, const Eigen::Vector2i& res)
72{
73 const Eigen::Vector2i content_res(res);
74
75 // Create a GLUT window if one does not already exist.
76 if (glutGetWindow() == 0) {
77 int argc = 1;
78 char* argv = (char*) "supereight";
79 glutInit(&argc, &argv);
80 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
81 glutInitWindowSize(content_res.x(), content_res.y());
82 glutCreateWindow("supereight display");
83
84 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
85 glPixelStorei(GL_UNPACK_ROW_LENGTH, content_res.x());
86
87 // Change raster coordinates from [-1, 1] to [0, 1].
88 glMatrixMode(GL_PROJECTION);
89 gluOrtho2D(0.0, 1.0, 0.0, 1.0);
90 glMatrixMode(GL_MODELVIEW);
91 }
92
93 // Get the window resolution and the scaling factor to scale the content to the
94 // window.
95 const Eigen::Vector2i window_res =
96 Eigen::Vector2i(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
97 const float width_factor = (float) window_res.x() / content_res.x();
98 const float height_factor = (float) window_res.y() / content_res.y();
99 const float factor = std::min(width_factor, height_factor);
100
101 glViewport(0, 0, window_res.x(), window_res.y());
102
103 glClear(GL_COLOR_BUFFER_BIT);
104
105 if (scene != nullptr) {
106 // Draw the image at the top left.
107 glRasterPos2i(0, 1);
108 // Scale the image and flip it up-down.
109 glPixelZoom(factor, -factor);
110 glDrawPixels(res.x(), res.y(), gl<T>::format, gl<T>::type, scene);
111 }
112
113 glutSwapBuffers();
114}
115
116#else // SE_GLUT
117
118template<typename T>
119void drawit(const T*, const Eigen::Vector2i&)
120{
121 // Don't draw anything if GLUT isn't available.
122}
123
124#endif // SE_GLUT
125
126#endif // SE_DRAW_HPP
void drawit(const T *, const Eigen::Vector2i &)
Definition draw.hpp:119
Helper wrapper to allocate and de-allocate octants in the octree.
Definition bounded_vector.hpp:14