supereight
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/Dense>
15 
16 #ifdef SE_GLUT
17 # include <algorithm>
18 # include <cstdint>
19 
20 # ifdef __APPLE__
21 # include <GLUT/glut.h>
22 # else
23 # include <GL/glut.h>
24 # endif
25 
26 
27 template<typename T>
28 struct gl;
29 
30 template<>
31 struct gl<float> {
32  static const int format = GL_LUMINANCE;
33  static const int type = GL_FLOAT;
34 };
35 
36 template<>
37 struct gl<uint8_t> {
38  static const int format = GL_LUMINANCE;
39  static const int type = GL_UNSIGNED_BYTE;
40 };
41 
42 template<>
43 struct gl<uint16_t> {
44  static const int format = GL_LUMINANCE;
45  static const int type = GL_UNSIGNED_SHORT;
46 };
47 
48 template<>
49 struct gl<uint32_t> {
50  static const int format = GL_RGBA;
51  static const int type = GL_UNSIGNED_BYTE;
52 };
53 
54 
55 
56 template<typename T>
57 void drawit(const T* scene, const Eigen::Vector2i& res)
58 {
59  const Eigen::Vector2i content_res(res);
60 
61  // Create a GLUT window if one does not already exist.
62  if (glutGetWindow() == 0) {
63  int argc = 1;
64  char* argv = (char*) "supereight";
65  glutInit(&argc, &argv);
66  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
67  glutInitWindowSize(content_res.x(), content_res.y());
68  glutCreateWindow("supereight display");
69 
70  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
71  glPixelStorei(GL_UNPACK_ROW_LENGTH, content_res.x());
72 
73  // Change raster coordinates from [-1, 1] to [0, 1].
74  glMatrixMode(GL_PROJECTION);
75  gluOrtho2D(0.0, 1.0, 0.0, 1.0);
76  glMatrixMode(GL_MODELVIEW);
77  }
78 
79  // Get the window resolution and the scaling factor to scale the content to the
80  // window.
81  const Eigen::Vector2i window_res =
82  Eigen::Vector2i(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
83  const float width_factor = (float) window_res.x() / content_res.x();
84  const float height_factor = (float) window_res.y() / content_res.y();
85  const float factor = std::min(width_factor, height_factor);
86 
87  glViewport(0, 0, window_res.x(), window_res.y());
88 
89  glClear(GL_COLOR_BUFFER_BIT);
90 
91  if (scene != nullptr) {
92  // Draw the image at the top left.
93  glRasterPos2i(0, 1);
94  // Scale the image and flip it up-down.
95  glPixelZoom(factor, -factor);
96  glDrawPixels(res.x(), res.y(), gl<T>::format, gl<T>::type, scene);
97  }
98 
99  glutSwapBuffers();
100 }
101 
102 #else // SE_GLUT
103 
104 template<typename T>
105 void drawit(const T*, const Eigen::Vector2i&)
106 {
107  // Don't draw anything if GLUT isn't available.
108 }
109 
110 #endif // SE_GLUT
111 
112 #endif // SE_DRAW_HPP
void drawit(const T *, const Eigen::Vector2i &)
Definition: draw.hpp:105