Oolite
Loading...
Searching...
No Matches
OOOpenGL.h
Go to the documentation of this file.
1/*
2
3OOOpenGL.h
4
5Do whatever is appropriate to get gl.h, glu.h and glext.h included.
6
7Also declares OpenGL-related utility functions.
8
9
10Oolite
11Copyright (C) 2004-2013 Giles C Williams and contributors
12
13This program is free software; you can redistribute it and/or
14modify it under the terms of the GNU General Public License
15as published by the Free Software Foundation; either version 2
16of the License, or (at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26MA 02110-1301, USA.
27
28*/
29
30#import "OOFoundation.h"
31#import "OOOpenGLOnly.h"
32
33typedef enum {
34 // NOTE: numerical values are available to scripts.
40
41#define NULL_SHADER ((GLhandleARB)0)
42
43// GLhandleARB is a pointer type on macOS; convert to a GLuint for the core GL 2.0+ shader API.
44#define OOGLProgramName(handle) ((GLuint)(uintptr_t)(handle))
45
46// Whether to use state verifier. Will be changed to equal OO_CHECK_GL_HEAVY in future.
47#ifdef NDEBUG
48#define OO_GL_STATE_VERIFICATION 0
49#else
50#define OO_GL_STATE_VERIFICATION 1
51#endif
52
53/*
54 OOSetOpenGLState(stateType)
55
56 Set OpenGL state to one of two standard states.
57
58 In Deployment builds, this only modifies the state that differs between
59 the two standard states, and only when the last set state is not the same
60 as the specified target.
61
62 In Debug builds, it tests many different OpenGL state variables, complains
63 if they aren't in the expected state, and corrects them.
64
65
66 The correct procedure for using these functions is:
67 1. Call OOSetOpenGLState(<most appropriate state>)
68 2. Make any state changes needed
69 3. Draw
70 4. Reverse state changes from stage 2
71 5. Call OOVerifyOpenGLState()
72 6. Call OOCheckOpenGLErrors().
73
74
75 The states are:
76 OPENGL_STATE_OPAQUE - standard state for drawing solid objects like OOMesh
77 and planets. This can be considered the Oolite baseline state.
78 Differs from standard GL state as follows:
79 GL_LIGHTING is on
80 GL_LIGHT1 is on
81 GL_TEXTURE_2D is on
82 Blend mode is GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA (but GL_BLEND is off)
83 GL_FOG may be either on or off (as it's set in Universe's draw loop)
84 GL_VERTEX_ARRAY is on
85 GL_NORMAL_ARRAY is on
86 GL_DEPTH_TEST is on
87 GL_CULL_FACE is on
88
89 OPENGL_STATE_TRANSLUCENT_PASS is used during the translucent phase of the
90 universe draw loop. This usage needs to be cleaned up.
91 Differs from OPENGL_STATE_OPAQUE as follows:
92 GL_LIGHTING is off
93 GL_TEXTURE_2D is off
94 GL_VERTEX_ARRAY is off
95 GL_NORMAL_ARRAY is off
96 GL_DEPTH_WRITEMASK is off
97
98 OPENGL_STATE_ADDITIVE_BLENDING is used for glowy special effects.
99 Differs from OPENGL_STATE_OPAQUE as follows:
100 GL_LIGHTING is off
101 GL_TEXTURE_2D is off
102 GL_BLEND is on
103 Blend mode is GL_SRC_ALPHA, GL_ONE
104 GL_NORMAL_ARRAY is off
105 GL_DEPTH_WRITEMASK is off
106 GL_CULL_FACE is off
107
108 OPENGL_STATE_OVERLAY is used for UI elements, which are unlit and don't use
109 z-buffering.
110 Differs from OPENGL_STATE_OPAQUE as follows:
111 GL_LIGHTING is off
112 GL_TEXTURE_2D is off
113 GL_BLEND is on
114 GL_FOG is off
115 GL_NORMAL_ARRAY is off
116 GL_DEPTH_TEST is off
117 GL_DEPTH_WRITEMASK is off
118 GL_CULL_FACE is off
119*/
128
129#if OO_GL_STATE_VERIFICATION
130void OOSetOpenGLState_(OOOpenGLStateID state, const char* function, unsigned line);
131void OOVerifyOpenGLState_(const char* function, unsigned line);
132#define OOSetOpenGLState(STATE) OOSetOpenGLState_(STATE, __FUNCTION__, __LINE__)
133#define OOVerifyOpenGLState() OOVerifyOpenGLState_(__FUNCTION__, __LINE__)
134#else
136#define OOVerifyOpenGLState() \
137 do { \
138 } while (0)
139#endif
140
141// Inform the SetState/VerifyState mechanism that the OpenGL context has been reset to its initial state.
142void OOResetGLStateVerifier(void);
143
144/* OOCheckOpenGLErrors()
145 Check for and log OpenGL errors, and returns YES if an error occurred.
146 NOTE: this is controlled by the log message class rendering.opengl.error.
147 If logging is disabled, no error checking will occur. This is done
148 because glGetError() is quite expensive, requiring a full OpenGL
149 state sync.
150*/
151BOOL OOCheckOpenGLErrors(NSString* format, ...);
152
153/* LogOpenGLState()
154 Write a bunch of OpenGL state information to the log.
155*/
156void LogOpenGLState(void);
157
158/* GLScaledLineWidth()
159 GLScaledPointSize()
160 GLGetDisplayScaleFactor()
161 GLSetDisplayScaleFactor()
162
163 These functions wrap glLineWidth() and glPointSize(), and multiply the
164 specified size by a "display scale factor". This is currently used to
165 support Retina display modes in Mac OS X 10.7 and later.
166
167 The default display scale factor is 1.0.
168*/
169void GLScaledLineWidth(GLfloat width);
170void GLScaledPointSize(GLfloat size);
171GLfloat GLGetDisplayScaleFactor(void);
172void GLSetDisplayScaleFactor(GLfloat factor);
173
174/* OOGLWireframeModeOn()
175 OOGLWireframeModeOff()
176 Enable/disable polygon-to-lines wireframe rendering with line width of 1.
177*/
178void OOGLWireframeModeOn(void);
179void OOGLWireframeModeOff(void);
180
181/* GLDrawBallBillboard()
182 Draws a circle corresponding to a sphere of given radius at given distance.
183 Assumes Z buffering will be disabled.
184*/
185void GLDrawBallBillboard(GLfloat radius, GLfloat step, GLfloat z_distance);
186
187/* GLDrawOval(), GLDrawFilledOval()
188 Draw axis-alligned ellipses, as outline and fill respectively.
189*/
190void GLDrawOval(GLfloat x, GLfloat y, GLfloat z, NSSize siz, GLfloat step);
191void GLDrawFilledOval(GLfloat x, GLfloat y, GLfloat z, NSSize siz, GLfloat step);
192
193/* GLDrawPoints(), GLDrawFilledPoints()
194 Draw array of points, as outline and fill respectively.
195*/
196
197typedef struct {
198 GLfloat x;
199 GLfloat y;
200 GLfloat z;
201} OOGLVector;
202
203void GLDrawPoints(OOGLVector* points, int n);
204void GLDrawFilledPoints(OOGLVector* points, int n);
205void GLDrawQuadStrip(OOGLVector* points, int n);
206
207/* OO_CHECK_GL_HEAVY and error-checking stuff
208
209 If OO_CHECK_GL_HEAVY is non-zero, the following error-checking facilities
210 come into play:
211 OOGL(foo) checks for GL errors before and after performing the statement foo.
212 OOGLBEGIN(mode) checks for GL errors, then calls glBegin(mode).
213 OOGLEND() calls glEnd(), then checks for GL errors.
214 CheckOpenGLErrorsHeavy() checks for errors exactly like OOCheckOpenGLErrors().
215
216 If OO_CHECK_GL_HEAVY is zero, these macros don't perform error checking,
217 but otherwise continue to work as before, so:
218 OOGL(foo) performs the statement foo.
219 OOGLBEGIN(mode) calls glBegin(mode);
220 OOGLEND() calls glEnd().
221 CheckOpenGLErrorsHeavy() does nothing (including not performing any parameter side-effects).
222*/
223#ifndef OO_CHECK_GL_HEAVY
224#define OO_CHECK_GL_HEAVY 0
225#endif
226
227#if OO_CHECK_GL_HEAVY
228
229#if OO_GL_STATE_VERIFICATION
230void OOGLNoteCurrentFunction(const char* func, unsigned line);
231#else
232#define OOGLNoteCurrentFunction(FUNC, line) \
233 do { \
234 } while (0)
235#endif
236
237NSString* OOLogAbbreviatedFileName(const char* inName);
238#define OOGL_PERFORM_CHECK(label, code) OOCheckOpenGLErrors(@"%s %@:%u (%s)%s", label, OOLogAbbreviatedFileName(__FILE__), __LINE__, __PRETTY_FUNCTION__, code)
239#define OOGL(statement) \
240 do { \
241 OOGLNoteCurrentFunction(__FUNCTION__, __LINE__); \
242 OOGL_PERFORM_CHECK("PRE", " -- " #statement); \
243 statement; \
244 OOGL_PERFORM_CHECK("POST", " -- " #statement); \
245 } while (0)
246#define CheckOpenGLErrorsHeavy OOCheckOpenGLErrors
247#define OOGLBEGIN(mode) \
248 do { \
249 OOGLNoteCurrentFunction(__FUNCTION__, __LINE__); \
250 OOGL_PERFORM_CHECK("PRE-BEGIN", " -- " #mode); \
251 glBegin(mode); \
252 } while (0)
253#define OOGLEND() \
254 do { \
255 glEnd(); \
256 OOGLNoteCurrentFunction(__FUNCTION__, __LINE__); \
257 OOGL_PERFORM_CHECK("POST-END", ""); \
258 } while (0)
259
260#else
261
262#define OOGL(statement) \
263 do { \
264 statement; \
265 } while (0)
266#define CheckOpenGLErrorsHeavy(...) \
267 do { \
268 } while (0)
269#define OOGLBEGIN glBegin
270#define OOGLEND glEnd
271
272#endif
273
274enum {
276};
277
278// Programmer-readable shader mode strings.
281// Localized shader mode strings.
283
284#ifndef NDEBUG
285
286NSString* OOGLColorToString(GLfloat color[4]);
287NSString* OOGLEnumToString(GLenum value);
288NSString* OOGLFlagToString(bool value);
289
290#endif
NSString * OOLogAbbreviatedFileName(const char *inName)
Definition OOLogging.m:762
static void OOGLNoteCurrentFunction(const char *function, unsigned line)
NSString * OOGLEnumToString(GLenum value)
Definition OOOpenGL.m:471
void OOGLWireframeModeOn(void)
Definition OOOpenGL.m:85
OOShaderSetting
Definition OOOpenGL.h:33
@ SHADERS_FULL
Definition OOOpenGL.h:38
@ SHADERS_SIMPLE
Definition OOOpenGL.h:37
@ SHADERS_OFF
Definition OOOpenGL.h:36
@ SHADERS_NOT_SUPPORTED
Definition OOOpenGL.h:35
@ kOOShaderSettingDefault
Definition OOOpenGL.h:275
void GLDrawFilledOval(GLfloat x, GLfloat y, GLfloat z, NSSize siz, GLfloat step)
Definition OOOpenGL.m:158
void OOGLWireframeModeOff(void)
Definition OOOpenGL.m:100
OOOpenGLStateID
Definition OOOpenGL.h:120
@ OPENGL_STATE_INTERNAL_USE_ONLY
Definition OOOpenGL.h:126
@ OPENGL_STATE_TRANSLUCENT_PASS
Definition OOOpenGL.h:122
@ OPENGL_STATE_ADDITIVE_BLENDING
Definition OOOpenGL.h:123
@ OPENGL_STATE_OVERLAY
Definition OOOpenGL.h:124
@ OPENGL_STATE_OPAQUE
Definition OOOpenGL.h:121
NSString * OOGLColorToString(GLfloat color[4])
Definition OOOpenGL.m:270
void GLDrawPoints(OOGLVector *points, int n)
Definition OOOpenGL.m:167
void OOResetGLStateVerifier(void)
void GLDrawQuadStrip(OOGLVector *points, int n)
Definition OOOpenGL.m:193
OOShaderSetting OOShaderSettingFromString(NSString *string)
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:38
void GLScaledLineWidth(GLfloat width)
Definition OOOpenGL.m:206
void GLDrawOval(GLfloat x, GLfloat y, GLfloat z, NSSize siz, GLfloat step)
Definition OOOpenGL.m:149
NSString * OOStringFromShaderSetting(OOShaderSetting setting)
void GLSetDisplayScaleFactor(GLfloat factor)
Definition OOOpenGL.m:223
void GLDrawBallBillboard(GLfloat radius, GLfloat step, GLfloat z_distance)
Definition OOOpenGL.m:107
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
void LogOpenGLState(void)
Definition OOOpenGL.m:239
void OOVerifyOpenGLState_(const char *function, unsigned line)
void GLScaledPointSize(GLfloat size)
Definition OOOpenGL.m:212
void OOSetOpenGLState_(OOOpenGLStateID state, const char *function, unsigned line)
void GLDrawFilledPoints(OOGLVector *points, int n)
Definition OOOpenGL.m:180
NSString * OOGLFlagToString(bool value)
Definition OOOpenGL.m:541
GLfloat GLGetDisplayScaleFactor(void)
Definition OOOpenGL.m:218
NSString * OODisplayStringFromShaderSetting(OOShaderSetting setting)
float y
float x
voidpf void uLong size
Definition ioapi.h:126
GLfloat y
Definition OOOpenGL.h:199
GLfloat x
Definition OOOpenGL.h:198
GLfloat z
Definition OOOpenGL.h:200