HOG2
GLUtil.h
Go to the documentation of this file.
1 /*
2  * $Id: glUtil.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 6/8/05.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "FPUtil.h"
13 #include <ostream>
14 #include <iostream>
15 #include <sstream>
16 #include <iomanip>
17 
18 // #ifdef __APPLE__
19 // #include "TargetConditionals.h"
20 // #endif
21 
22 #ifdef NO_OPENGL
23 #include "gl.h"
24 #include "glut.h"
25 #else
26 
27 //#ifdef TARGET_OS_IPHONE
28 //#include <OpenGLES/ES1/gl.h>
29 //#include <OpenGLES/ES1/glext.h>
30 //#define GLdouble GLfloat
31 //#else
32 
33 #ifdef OS_MAC
34 #include <OpenGL/gl.h>
35 //#include <OpenGL/glu.h>
36 //#include <GLUT/glut.h>
37 //#include <AGL/agl.h>
38 #else
39 
40 #include <GL/gl.h>
41 //#include <GL/glu.h>
42 //#include <GL/glut.h>
43 #endif
44 
45 //#endif
46 #endif
47 
48 #ifndef GLUTIL_H
49 #define GLUTIL_H
50 
51 class point3d;
52 
53 #include "Colors.h"
54 #include "Graphics.h"
55 
56 //#pragma mark -
57 //#pragma mark OpenGL structures:
58 
59 static const double ONE = 1.0;
60 static const double TWO = 2.0;
61 static const double ROOT_TWO = 1.414213562;//1.5f?
62 static const double ONE_OVER_ROOT_TWO = 1.0/ROOT_TWO;//0.707106781f;
63 static const double ROOT_THREE = 1.732050808;
64 
65 static const double TWOPI = 6.283185307179586476925287;
66 static const double PI = 3.141592653589793238462643;
67 static const double PID180 = PI/180; // radian conversion
68 static const double PID2 = PI/2; // 90degree
69 static const double PID3 = PI/3; // 60degree
70 static const double PID4 = PI/4; // 45degree
71 static const double PID6 = PI/6; // 30degree
72 static const double PID8 = PI/8; // 22.25degree
73 static const double ROOT2D2 = 0.7071067811865475;
74 
78 class recVec {
79 public:
80  recVec() { x = y = z = 0; }
81  recVec(GLdouble x_i, GLdouble y_i, GLdouble z_i) :x(x_i), y(y_i), z(z_i) {}
82  void normalise();
83  double length() const;
85  {
86  recVec n;
87 
88  v.normalise();
89  this->normalise();
90 
91  n.x = this->y * v.z - this->z * v.y;
92  n.y = this->z * v.x - this->x * v.z;
93  n.z = this->x * v.y - this->y * v.x;
94  n.normalise();
95 
96  return n;
97  }
98  GLdouble x,y,z;
100  { x += v.x; y += v.y; z += v.z; return *this; }
102  { x -= v.x; y -= v.y; z -= v.z; return *this; }
104  { recVec n = *this; n-=v; return n; }
105  recVec &operator*=(GLdouble val)
106  { x *= val; y *= val; z *= val; return *this; }
107  recVec operator*(const recVec &val) const
108  {
109  recVec result;
110  result.x = this->y*val.z - this->z*val.y;
111  result.y = this->z*val.x - this->x*val.z;
112  result.z = this->x*val.y - this->y*val.x;
113  result.normalise();
114  return result;
115  }
116 };
117 
118 
119 bool operator==(const recVec &l1, const recVec &l2);
120 
121 std::ostream& operator<<(std::ostream &out, const recVec &loc);
122 
123 #define point3d Graphics::point
124 // * A point in 3d space. (OpenGL)
126 // */
127 //class point3d {
128 //public:
129 // point3d() {}
130 // point3d(Graphics::point p):x(p.x), y(p.y), z(p.z) {}
131 // point3d(GLfloat a, GLfloat b, GLfloat c=0) :x(a), y(b), z(c) {}
132 // GLfloat x, y, z;
133 //
134 // point3d operator+(const point3d &v) const
135 // { point3d p(*this); p+=v; return p; }
136 // point3d operator-(const point3d &v) const
137 // { point3d p(*this); p-=v; return p; }
138 // point3d &operator+=(const point3d &v)
139 // { x += v.x; y += v.y; z += v.z; return *this; }
140 // point3d &operator-=(const point3d &v)
141 // { x -= v.x; y -= v.y; z -= v.z; return *this; }
142 // point3d &operator+=(const float v)
143 // { x += v; y += v; z += v; return *this; }
144 // point3d &operator-=(const float v)
145 // { x -= v; y -= v; z -= v; return *this; }
146 // point3d &operator*=(const float v)
147 // { x *= v; y *= v; z *= v; return *this; }
148 // point3d &operator/=(const float v)
149 // { x /= v; y /= v; z /= v; return *this; }
150 //
151 //};
152 //inline std::ostream &operator<<(std::ostream &o, const point3d&r)
153 //{ o << "(" << r.x << ", " << r.y << ", " << r.z << ")"; return o; }
154 
155 class line2d {
156 public:
157  line2d() {}
158  line2d(recVec a, recVec b) :start(a), end(b) {}
159  bool crosses(line2d which) const;
162 };
163 
164 
168 void DrawPyramid(GLfloat x, GLfloat y, GLfloat z, GLfloat height, GLfloat width);
169 void DrawBox(GLfloat x, GLfloat y, GLfloat z, GLfloat radius);
170 void DrawBoxFrame(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad);
171 void DrawCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, int segments = 32, float rotation = 0);
172 void FrameCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, GLdouble lineWidth, int segments = 32, float rotation = 0);
173 void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius);
174 void DrawSquare(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius);
175 void DrawCylinder(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat innerRad, GLfloat outerRad, GLfloat height);
176 void OutlineRect(GLdouble left, GLdouble top, GLdouble right, GLdouble bottom, double zz);
177 
178 void DrawText(double x, double y, double z, double scale, const char *res);
179 void DrawTextCentered(double x, double y, double z, double scale, const char *res);
180 
181 void SetLighting(GLfloat ambientf = 0.2f, GLfloat diffusef = 1.0f, GLfloat specularf = 1.0f);
182 
183 // Code from: https://stackoverflow.com/questions/16605967/set-precision-of-stdto-string-when-converting-floating-point-values/16606128#16606128
184 template <typename T>
185 std::string to_string_with_precision(const T a_value, const int n = 6)
186 {
187  std::ostringstream out;
188  out.precision(n);
189  out << std::fixed << a_value;
190  return out.str();
191 }
192 
193 
194 
195 #endif
line2d::start
recVec start
Definition: GLUtil.h:160
recVec::GetNormal
recVec GetNormal(recVec v)
Definition: GLUtil.h:84
recVec::operator-=
recVec & operator-=(const recVec &v)
Definition: GLUtil.h:101
DrawTextCentered
void DrawTextCentered(double x, double y, double z, double scale, const char *res)
Definition: GLUtil.cpp:542
recVec
A generic vector (essentially the same as a point, but offers normalization)
Definition: GLUtil.h:78
recVec::z
GLdouble z
Definition: GLUtil.h:98
DrawPyramid
void DrawPyramid(GLfloat x, GLfloat y, GLfloat z, GLfloat height, GLfloat width)
Draw a pyramid with the tip at the given location, given height, and width from center to edge as wid...
Definition: GLUtil.cpp:185
ONE
static const double ONE
Definition: GLUtil.h:59
line2d::end
recVec end
Definition: GLUtil.h:161
FPUtil.h
line2d::line2d
line2d()
Definition: GLUtil.h:157
width
int width
Definition: SFML_HOG.cpp:54
rotation
float rotation[3][3]
Definition: RC.cpp:21
recVec::operator-
recVec operator-(const recVec &v)
Definition: GLUtil.h:103
DrawBoxFrame
void DrawBoxFrame(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat rad)
Definition: GLUtil.cpp:252
recVec::length
double length() const
Definition: GLUtil.cpp:56
recVec::recVec
recVec()
Definition: GLUtil.h:80
DrawCircle
void DrawCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, int segments=32, float rotation=0)
Definition: GLUtil.cpp:421
DrawBox
void DrawBox(GLfloat x, GLfloat y, GLfloat z, GLfloat radius)
Definition: GLUtil.cpp:285
SetLighting
void SetLighting(GLfloat ambientf=0.2f, GLfloat diffusef=1.0f, GLfloat specularf=1.0f)
Definition: GLUtil.cpp:565
line2d::line2d
line2d(recVec a, recVec b)
Definition: GLUtil.h:158
DrawCylinder
void DrawCylinder(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat innerRad, GLfloat outerRad, GLfloat height)
Definition: GLUtil.cpp:309
to_string_with_precision
std::string to_string_with_precision(const T a_value, const int n=6)
Definition: GLUtil.h:185
loc
Definition: MapGenerators.cpp:296
FrameCircle
void FrameCircle(GLdouble _x, GLdouble _y, GLdouble tRadius, GLdouble lineWidth, int segments=32, float rotation=0)
Definition: GLUtil.cpp:407
DrawSphere
void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
Definition: GLUtil.cpp:433
recVec::operator+=
recVec & operator+=(const recVec &v)
Definition: GLUtil.h:99
Colors.h
PI
static const double PI
Definition: GLUtil.h:66
DrawSquare
void DrawSquare(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
Definition: GLUtil.cpp:396
point3d
#define point3d
Definition: GLUtil.h:123
line2d
‍**
Definition: GLUtil.h:155
operator<<
std::ostream & operator<<(std::ostream &out, const recVec &loc)
Definition: GLUtil.cpp:27
ROOT_TWO
static const double ROOT_TWO
Definition: GLUtil.h:61
DrawText
void DrawText(double x, double y, double z, double scale, const char *res)
Definition: GLUtil.cpp:526
PID2
static const double PID2
Definition: GLUtil.h:68
height
int height
Definition: SFML_HOG.cpp:54
PID8
static const double PID8
Definition: GLUtil.h:72
line2d::crosses
bool crosses(line2d which) const
Definition: GLUtil.cpp:61
Graphics.h
TWO
static const double TWO
Definition: GLUtil.h:60
OutlineRect
void OutlineRect(GLdouble left, GLdouble top, GLdouble right, GLdouble bottom, double zz)
Definition: GLUtil.cpp:384
recVec::recVec
recVec(GLdouble x_i, GLdouble y_i, GLdouble z_i)
Definition: GLUtil.h:81
PID3
static const double PID3
Definition: GLUtil.h:69
PID180
static const double PID180
Definition: GLUtil.h:67
ROOT_THREE
static const double ROOT_THREE
Definition: GLUtil.h:63
recVec::operator*=
recVec & operator*=(GLdouble val)
Definition: GLUtil.h:105
TWOPI
static const double TWOPI
Definition: GLUtil.h:65
ROOT2D2
static const double ROOT2D2
Definition: GLUtil.h:73
ONE_OVER_ROOT_TWO
static const double ONE_OVER_ROOT_TWO
Definition: GLUtil.h:62
recVec::y
GLdouble y
Definition: GLUtil.h:98
operator==
bool operator==(const recVec &l1, const recVec &l2)
Definition: GLUtil.cpp:22
PID6
static const double PID6
Definition: GLUtil.h:71
recVec::x
GLdouble x
Definition: GLUtil.h:98
recVec::normalise
void normalise()
Normalize a vector.
Definition: GLUtil.cpp:39
PID4
static const double PID4
Definition: GLUtil.h:70
recVec::operator*
recVec operator*(const recVec &val) const
Definition: GLUtil.h:107