Shiokaze Framework
A research-oriented fluid solver for computer graphics
graphics_utility.h
Go to the documentation of this file.
1 /*
2 ** graphics_utility.h
3 **
4 ** This is part of Shiokaze, a research-oriented fluid solver for computer graphics.
5 ** Created by Ryoichi Ando <rand@nii.ac.jp> on Jan 15, 2018.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining a copy of
8 ** this software and associated documentation files (the "Software"), to deal in
9 ** the Software without restriction, including without limitation the rights to use,
10 ** copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
11 ** Software, and to permit persons to whom the Software is furnished to do so,
12 ** subject to the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included in all copies
15 ** or substantial portions of the Software.
16 **
17 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18 ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19 ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 ** CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 ** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 //
25 #ifndef SHKZ_GRAPHICS_UTILITY_H
26 #define SHKZ_GRAPHICS_UTILITY_H
27 //
28 #include <cmath>
30 #include <shiokaze/core/console.h>
31 #include <shiokaze/math/vec.h>
32 //
34 //
36 class graphics_utility {
39 private:
40  using ge = graphics_engine;
41 public:
56  template<class T> static void draw_circle ( graphics_engine &g, const T *p, double r, ge::MODE mode, unsigned num_v=20 ) {
57  g.begin(mode);
58  for( unsigned t=0; t<num_v; t++ ) {
59  double theta = 2.0 * M_PI * t / (double)num_v;
60  g.vertex2(p[0]+r*cos(theta),p[1]+r*sin(theta));
61  }
62  g.end();
63  }
74  template<class T> static void draw_arrow( graphics_engine &g, const T *p0, const T *p1 ) {
75  double y_vec_x = p1[0]-p0[0];
76  double y_vec_y = p1[1]-p0[1];
77  double y_vec_len = hypot(y_vec_x,y_vec_y);
78  if( y_vec_len ) {
79  //
80  y_vec_x /= y_vec_len;
81  y_vec_y /= y_vec_len;
82  //
83  double x_vec_x = -y_vec_y;
84  double x_vec_y = y_vec_x;
85  double k = 0.25*y_vec_len;
86  //
88  g.vertex2v(p0);
89  g.vertex2v(p1);
90  g.end();
91  //
92  vec2d p2(p1[0]+k*0.8*x_vec_x-k*y_vec_x,p1[1]+k*0.8*x_vec_y-k*y_vec_y);
93  vec2d p3(p1[0]-k*0.8*x_vec_x-k*y_vec_x,p1[1]-k*0.8*x_vec_y-k*y_vec_y);
94  vec2d mid = (vec2d(p1[0],p1[1])+p2+p3)/3.0;
96  g.vertex2v(p1);
97  g.vertex2v(p2.v);
98  g.vertex2v(mid.v);
99  g.end();
101  g.vertex2v(p1);
102  g.vertex2v(p3.v);
103  g.vertex2v(mid.v);
104  g.end();
105  }
106  }
113  static void draw_wired_box( graphics_engine &g, double scale=1.0 ) {
114  const double p0[] = { 0.0, 0.0, 0.0 };
115  const double p1[] = { scale, scale, scale };
116  draw_wired_box(g,p0,p1);
117  }
128  template<class T> static void draw_wired_box( graphics_engine &g, const T *p0, const T *p1 ) {
129  //
131  g.vertex3(p0[0],p0[1],p0[2]);
132  g.vertex3(p1[0],p0[1],p0[2]);
133  g.vertex3(p1[0],p1[1],p0[2]);
134  g.vertex3(p0[0],p1[1],p0[2]);
135  g.end();
136  //
138  g.vertex3(p0[0],p0[1],p1[2]);
139  g.vertex3(p1[0],p0[1],p1[2]);
140  g.vertex3(p1[0],p1[1],p1[2]);
141  g.vertex3(p0[0],p1[1],p1[2]);
142  g.end();
143  //
145  g.vertex3(p0[0],p0[1],p0[2]);
146  g.vertex3(p0[0],p0[1],p1[2]);
147  g.vertex3(p0[0],p1[1],p0[2]);
148  g.vertex3(p0[0],p1[1],p1[2]);
149  g.vertex3(p1[0],p1[1],p0[2]);
150  g.vertex3(p1[0],p1[1],p1[2]);
151  g.vertex3(p1[0],p0[1],p0[2]);
152  g.vertex3(p1[0],p0[1],p1[2]);
153  g.end();
154  }
155 };
156 //
158 //
159 #endif
160 //
graphics_engine::vertex2v
void vertex2v(const T *v)
Equivalebt to glVertex. See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glVertex....
Definition: graphics_engine.h:235
graphics_utility::draw_wired_box
static void draw_wired_box(graphics_engine &g, double scale=1.0)
Draw a wired box of a unit size.
Definition: graphics_utility.h:113
graphics_engine::MODE::LINE_LOOP
@ LINE_LOOP
Closed lines.
graphics_engine::end
virtual void end()=0
Equivalebt to glEnd. See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glEnd....
graphics_utility
Class that provides various utility functions for graphics.
Definition: graphics_utility.h:38
graphics_engine::vertex3
void vertex3(double x, double y, double z)
Equivalebt to glVertex. See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glVertex....
Definition: graphics_engine.h:181
console.h
graphics_engine::MODE::LINES
@ LINES
Independent lines segmemts.
vec::v
T v[D]
Vector value array.
Definition: vec.h:44
graphics_engine::vertex2
void vertex2(double x, double y)
Equivalebt to glVertex. See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glVertex....
Definition: graphics_engine.h:173
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
graphics_engine::MODE
MODE
List of drawing mode that is to be specified with begin() function. See https://www....
Definition: graphics_engine.h:188
vec
Fixed sized vector structure.
Definition: vec.h:38
graphics_utility::draw_wired_box
static void draw_wired_box(graphics_engine &g, const T *p0, const T *p1)
Draw a wired box.
Definition: graphics_utility.h:128
vec.h
graphics_engine::MODE::TRIANGLES
@ TRIANGLES
Independent triangles.
graphics_engine::begin
virtual void begin(MODE mode)=0
Equivalebt to glBegin. See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glBegin....
graphics_utility::draw_circle
static void draw_circle(graphics_engine &g, const T *p, double r, ge::MODE mode, unsigned num_v=20)
Draw a circle.
Definition: graphics_utility.h:56
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
graphics_engine
Interface for handling drawing operations.
Definition: graphics_engine.h:38
graphics_utility::draw_arrow
static void draw_arrow(graphics_engine &g, const T *p0, const T *p1)
Draw an arrow.
Definition: graphics_utility.h:74
graphics_engine.h