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