Draw_3D_API
OpenGL_API.hh
Go to the documentation of this file.
1 /*
2 Copyright 2021 Krystian Szpakowski
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 
6 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 
8 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 */
10 
11 #ifndef OPENGL_API_OPENGL_API_HH
12 #define OPENGL_API_OPENGL_API_HH
13 #include <GL/glut.h>
14 #include <GL/glu.h>
15 #include "Draw3D_api_interface.hh"
16 #include <cmath>
17 #include <array>
18 #include <vector>
19 #include <sstream>
20 #include <iostream>
21 #include <thread>
22 #include <cstdlib>
23 #include <mutex>
24 
25 
26 namespace drawNS {
27 
28 
29  struct data { //used to store shapes data
30  uint shape_id;
31  uint shape_type; //1-draw_line, 2-polygonal_chain, 3-draw_polyhedron, 4-draw_surface
32  uint shape_size; //how many points does one shape contain
33  uint line_size {}; //used for drawing surface
34  std::string color = "black";
35  };
36 
37 
38  class APIopenGL3D : public drawNS::Draw3DAPI {
39  /*
40  * responsible for correct data transfer
41  */
42  static std::mutex mt;
43  /*
44  * \brief used to redraw once with ref_rate == 0
45  * Method Draw_scene_with_delay() would redraw once then reset control bit
46  */
47  static bool control_bit;
48  /*
49  * \brief limits of X, Y, Z axis
50  */
51  static double limits[6];
52  /*
53  * \brief stores vertexes
54  */
55  static std::vector<Point3D> vertexes;
56  /*
57  * \brief stores data about each shape
58  */
59  static std::vector<data> shapes;
60  /*
61  * \brief used when ref_rate != 0 to load image in correct moment
62  */
63  static std::vector<Point3D> vertex_buffer;
64  /*
65  * \brief used when ref_rate != 0 to load image in correct moment
66  */
67  static std::vector<data> shape_buffer;
68  /*
69  * \brief stores thread PID
70  * Used in class destructor to close process in class destructor, therefore destructor must be called in main
71  */
72  static pid_t thread_pid;
73  /*
74  * \brief holds value in ms
75  */
76  static int ref_rate;
77  /*
78  * \brief holds old X coordinate of mouse position
79  */
80  static int mouseoldx;
81  /*
82  * \brief holds old Y coordinate of mouse position
83  */
84  static int mouseoldy;
85  /*
86  * \brief holds angle of rotation by X axis
87  */
88  static double rotationXangle;
89  /*
90  * \brief holds angle of rotation by Z axis
91  */
92  static double rotationZangle;
93  /*
94  * \brief holds value to allow only left-click rotation
95  */
96  static int state_of_mouse;
97  /*
98  * \brief checks is ref_rate is gone, when yes it draws
99  * Used to allow smooth rotation even if ref_rate is high
100  */
101  static bool if_time_gone;
102  /*
103  * \brief is time is gone, sets it for true to draw
104  */
105  static bool if_draw;
106  /*
107  * \brief stores id for load_color method
108  */
109  static int vec_id_for_color_change;
110  /*
111  * \brief stores color for load_color method
112  */
113  static std::string new_color;
114  public:
115  /*
116  * \brief holds max values of axes
117  * needed for static func
118  */
119  APIopenGL3D() = delete;
120  /*
121  * \brief creates scene
122  */
123  APIopenGL3D(double minX,double maxX,double minY,double maxY,double minZ,double maxZ,int ref_time_ms,int *argc,char **argv);
124  ~APIopenGL3D() override {stop_drawing();};
125 
126  /*
127  * TO ALL DRAWING METHODS RETURNING VALUES
128  * They are interface: store vertexes in std::vector, add new shapes to shapes vector
129  * They return value of shape id
130  */
131  uint draw_line(const Point3D & point1, const Point3D & point2, const std::string & color) override;
132  uint draw_polygonal_chain(const std::vector<Point3D> & points, const std::string & color) override;
133  uint draw_polyhedron(const std::vector<std::vector<Point3D> > & points_map, const std::string & color) override;
134  uint draw_surface(const std::vector<std::vector<Point3D> > & points_map, const std::string & color) override;
135  void erase_shape(uint id) override;
136  void change_shape_color(uint id, const std::string & color) override;
137  void change_ref_time_ms(int ref_time_ms) override;
138  void redraw() override;
139  /*
140  * TO ALL STATIC VOID DRAWING METHODS
141  * They are responsible for drawing, data for them are prepared by their equivalent uint methods
142  * They are used in draw_all_shapes() method
143  */
144  private:
145  static void draw_line_void(uint offset);
146  static void draw_polygonal_chain_void(uint offset, uint size);
147  static void draw_polyhedron_void(uint offset, uint size);
148  static void draw_surface_void(uint offset, uint size, uint line_size);
149  /*
150  * \brief draws all shapes
151  * This method is included in Draw_scene()
152  */
153  static void draw_all_shapes();
154  /*
155  * \brief set window size, starting position, bg color
156  */
157  static void Initialize(int *argc,char **argv);
158  /*
159  * \brief draws scene
160  */
161  static void Draw_scene();
162  /*
163  * \brief draws with delay given during initialization
164  */
165  static void Draw_scene_with_delay();
166  /*
167  * \brief draws axes
168  */
169  static void Draw_axes();
170  /*
171  * \brief kill drawing process
172  */
173  static void stop_drawing();
174  /*
175  * \brief loads vertexes and shapes from buffer
176  * Used when ref_rate != 0
177  */
178  static void load_from_buffer();
179  /*
180  * \brief loads color, when needed
181  * Used when ref_rate != 0 -> when dragging mouse color won't be overwritten
182  */
183  static void load_color();
184  /*
185  * \brief after time is gone sets bool variables
186  * Called by thread
187  */
188  static void wait(int time);
189  /*
190  * \brief summing how many points where before shape with id = shape_id
191  * used for:
192  * 1. setting offset which is used to delete vertexes while deleting shapes
193  * 2. to let program know from which vertexes program shall draw, when there is more than one shape
194  */
195  static uint sum_points_before(uint shape_id);
196  /*
197  * \brief sets color for drawing next shape
198  */
199  static void set_color(const std::string &color);
200  /*
201  * \brief scale coordinates to values on which program operates (1 unit for each direction: i. e. on X axis
202  * values are from -1 to +1)
203  */
204  static drawNS::Point3D scale (const drawNS::Point3D &point);
205  /*
206  * \brief takes double and displays it at given 3d vector, displays to 1st place after comma
207  */
208  static void renderString(double x, double y, double z, double var);
209  /*
210  * \brief responsible for scaling scene when windows is resized
211  * It makes coordinate system staying always in center of window and prevent deforming scales by
212  * changing ratio of windows' height and width for other than 1
213  */
214  static void changeSize(int w, int h);
215  /*
216  * \brief reacts when mouse is clicked
217  */
218  static void mouse(int button, int state, int x, int y);
219  /*
220  * \brief rotate coordinate system by mouse dragging
221  * Uses "mouse" method, left-click + drag allows to rotate coordinate system by X and Z axes
222  * right-click reset the rotation, especially handy when ref_rate is high value
223  */
224  static void mouse_drag(int x, int y);
225  }; //end of class
226 
227 
228 } //end of namespace
229 
230 #endif //OPENGL_API_OPENGL_API_HH
Definition: OpenGL_API.hh:29
uint shape_type
Definition: OpenGL_API.hh:31
Definition: OpenGL_API.hh:38
~APIopenGL3D() override
Definition: OpenGL_API.hh:124
Abstract interface for drawing class Abstract class used as interface for 3D drawing class implementa...
Definition: Draw3D_api_interface.hh:87
uint shape_id
Definition: OpenGL_API.hh:30
uint shape_size
Definition: OpenGL_API.hh:32
uint line_size
Definition: OpenGL_API.hh:33
Point in 3D Class represents point in 3D.
Definition: Draw3D_api_interface.hh:35
Namespace for drawing tools - abstract interface and 3D point Namespace for drawing tools - abstract ...
Definition: Dr3D_gnuplot_api.hh:24
std::string color
Definition: OpenGL_API.hh:34