Pioneer
Graphics.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _GRAPHICS_H
5 #define _GRAPHICS_H
6 
7 #include "RenderTarget.h"
8 #include "matrix4x4.h"
9 #include <memory>
10 #include <vector>
11 
12 /*
13  * bunch of reused 3d drawy routines.
14  * XXX most of this is to be removed
15  */
16 namespace Graphics {
17 
18  enum RendererType {
22  };
23 
24  const char *RendererNameFromType(const RendererType rType);
25 
26  // requested video settings
27  struct Settings {
29  bool fullscreen;
30  bool hidden;
35  int vsync;
37  int height;
38  int width;
39  const char *iconFile;
40  const char *title;
41  };
42 
43  class Renderer;
44 
45  typedef Renderer *(*RendererCreateFunc)(const Settings &vs);
47 
48  //for querying available modes
49  struct VideoMode {
50  VideoMode(int w, int h) :
51  width(w),
52  height(h) {}
53 
54  int width;
55  int height;
56  };
57 
58  // Lightweight representation of viewport bounds to simplify viewport state management
59  struct ViewportExtents {
61  x(0),
62  y(0),
63  w(0),
64  h(0) {}
65 
66  ViewportExtents(int32_t _x, int32_t _y, int32_t _w, int32_t _h) :
67  x(_x),
68  y(_y),
69  w(_w),
70  h(_h) {}
71 
72  int32_t x, y, w, h;
73  bool operator!=(const ViewportExtents &rhs) const { return !(*this == rhs); }
74  bool operator==(const ViewportExtents &rhs) const
75  {
76  return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h;
77  }
78  };
79 
80  class Material;
81  extern Material *vtxColorMaterial;
82 
83  int GetScreenWidth();
84  int GetScreenHeight();
85 
86  float GetFov();
87  void SetFov(float);
88  float GetFovFactor(); //cached 2*tan(fov/2) for LOD
89 
90  // Project a point in the renderer's current coordinate system to
91  // screenspace as defined by Renderer::GetViewport.
92  // This function applies the current renderer transform to the input point
93  // TODO: find a better place to hang this off of; this is too useful to be tied to a renderer object
94  vector3d ProjectToScreen(const Renderer *r, const vector3d &in);
95  vector3f ProjectToScreen(const Renderer *r, const vector3f &in);
96 
97  // ProjectToScreen handles projecting a point in view-space to 2d viewport space.
98  // It returns the { X, Y } window coordinates and the NDC depth value as Z.
99  // Implements gluProject (see the OpenGL documentation or the Mesa implementation of gluProject)
100  // This implementation is tailored to understand Reverse-Z and our data structures.
101  template <typename T>
102  vector3<T> ProjectToScreen(const vector3<T> &vcam, const matrix4x4<T> &proj, const ViewportExtents &vp)
103  {
104  // compute the effective W component for perspective divide.
105  // This code assumes that it's being passed a 'standard' perspective or ortho matrix.
106  const double w = vcam.z * proj[11] + proj[15];
107 
108  // convert view coordinates -> homogeneous coordinates -> NDC
109  // perspective divide is applied last (left-to-right associativity)
110  const vector3<T> vNDC = proj * vcam / w;
111 
112  // convert -1..1 NDC to 0..1 viewport coordinates
113  const vector3<T> vVP = {
114  vNDC.x * T(0.5) + T(0.5),
115  vNDC.y * T(0.5) + T(0.5),
116  // FIXME: this isn't a proper linearization into viewspace (needs -znear / zNDC)
117  // but it accomplishes the goal of positions behind the camera having z > 0.
118  -vNDC.z // undo reverse-Z coordinate flip
119  };
120 
121  // viewport coord * size + position
122  return vector3<T>{
123  vVP.x * vp.w + vp.x,
124  vVP.y * vp.h + vp.y,
125  vVP.z
126  };
127  }
128 
129  // does SDL video init, constructs appropriate Renderer
130  Renderer *Init(Settings);
131  void Uninit();
132  std::vector<VideoMode> GetAvailableVideoModes();
133 
135  std::unique_ptr<Uint8[]> pixels;
136  Uint32 width;
137  Uint32 height;
138  Uint32 stride;
139  Uint32 bpp;
140  };
141 } // namespace Graphics
142 
143 #endif /* _RENDER_H */
Definition: Renderer.h:44
Definition: matrix4x4.h:15
T y
Definition: vector3.h:18
T x
Definition: vector3.h:18
T z
Definition: vector3.h:18
Definition: Background.h:14
Renderer * Init(Settings vs)
Definition: Graphics.cpp:76
void SetFov(float fov)
Definition: Graphics.cpp:55
Material * vtxColorMaterial
Definition: Graphics.cpp:35
Renderer *(* RendererCreateFunc)(const Settings &vs)
Definition: Graphics.h:45
RendererType
Definition: Graphics.h:18
@ RENDERER_DUMMY
Definition: Graphics.h:19
@ RENDERER_OPENGL_3x
Definition: Graphics.h:20
@ MAX_RENDERER_TYPE
Definition: Graphics.h:21
float GetFovFactor()
Definition: Graphics.cpp:61
vector3f ProjectToScreen(const Renderer *r, const vector3f &in)
Definition: Graphics.cpp:66
float GetFov()
Definition: Graphics.cpp:50
void Uninit()
Definition: Graphics.cpp:136
const char * RendererNameFromType(const RendererType rType)
Definition: Graphics.cpp:16
std::vector< VideoMode > GetAvailableVideoModes()
Definition: Graphics.cpp:146
int GetScreenWidth()
Definition: Graphics.cpp:40
void RegisterRenderer(RendererType type, RendererCreateFunc fn)
Definition: Graphics.cpp:27
int GetScreenHeight()
Definition: Graphics.cpp:45
Definition: Graphics.h:134
std::unique_ptr< Uint8[]> pixels
Definition: Graphics.h:135
Uint32 height
Definition: Graphics.h:137
Uint32 stride
Definition: Graphics.h:138
Uint32 bpp
Definition: Graphics.h:139
Uint32 width
Definition: Graphics.h:136
Definition: Graphics.h:27
bool useAnisotropicFiltering
Definition: Graphics.h:32
RendererType rendererType
Definition: Graphics.h:28
int height
Definition: Graphics.h:37
bool gl3ForwardCompatible
Definition: Graphics.h:34
const char * title
Definition: Graphics.h:40
bool fullscreen
Definition: Graphics.h:29
bool useTextureCompression
Definition: Graphics.h:31
int vsync
Definition: Graphics.h:35
int requestedSamples
Definition: Graphics.h:36
int width
Definition: Graphics.h:38
const char * iconFile
Definition: Graphics.h:39
bool enableDebugMessages
Definition: Graphics.h:33
bool hidden
Definition: Graphics.h:30
Definition: Graphics.h:49
int height
Definition: Graphics.h:55
int width
Definition: Graphics.h:54
VideoMode(int w, int h)
Definition: Graphics.h:50
Definition: Graphics.h:59
bool operator!=(const ViewportExtents &rhs) const
Definition: Graphics.h:73
int32_t h
Definition: Graphics.h:72
ViewportExtents()
Definition: Graphics.h:60
int32_t w
Definition: Graphics.h:72
int32_t y
Definition: Graphics.h:72
bool operator==(const ViewportExtents &rhs) const
Definition: Graphics.h:74
ViewportExtents(int32_t _x, int32_t _y, int32_t _w, int32_t _h)
Definition: Graphics.h:66
int32_t x
Definition: Graphics.h:72