1 /* glHelp.h
2 Some misc. help functions for GL commands etc...
3
4 Copyright (C) 2000 Mathias Broxvall
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef GLHELP_H
22 #define GLHELP_H
23
24 #define GL_GLEXT_PROTOTYPES
25 #include <GL/gl.h>
26 #include <GL/glext.h>
27
28 #include "general.h"
29
30 typedef struct _TTF_Font TTF_Font;
31 typedef struct SDL_Surface SDL_Surface;
32 class Map;
33 class Game;
34
35 /* Prototypes */
36 void glHelpInit();
37 void glHelpCleanup();
38 void warnForGLerrors(const char *where_am_i);
39
40 /* displays a 2d text on specific screen coordinates, returning width */
41 int draw2DString(TTF_Font *, const char *, int x, int y, float red, float green, float blue,
42 float alpha, bool outlined, int align, int maxwidth = 0);
43 void update2DStringCache(bool force_wipe);
44 /* Common interface for drawing 2d things on the screen */
45 void draw2DRectangle(GLfloat x, GLfloat y, GLfloat w, GLfloat h, GLfloat tx, GLfloat ty,
46 GLfloat tw, GLfloat th, GLfloat r, GLfloat g, GLfloat b, GLfloat a,
47 GLuint tex = 0);
48 void draw2DQuad(const GLfloat corners[4][2], const GLfloat texture[4][2],
49 const GLfloat color[4][4], GLuint tex = 0);
50
51 void tickMouse(Real td);
52 void drawMousePointer();
53 void drawMouse(int x, int y, int w, int h);
54 size_t packObjectVertex(void *dest, GLfloat x, GLfloat y, GLfloat z, GLfloat tx, GLfloat ty,
55 const Color &color, const GLfloat normal[3]);
56 void configureObjectAttributes();
57 void configureUIAttributes();
packNormal(const GLfloat n[3])58 inline uint32_t packNormal(const GLfloat n[3]) {
59 uint32_t d = (512 + n[2] * 511.f);
60 uint32_t e = (512 + n[1] * 511.f);
61 uint32_t f = (512 + n[0] * 511.f);
62 uint32_t x = 0;
63 // Alpha (<<30) unused
64 x |= d << 20;
65 x |= e << 10;
66 x |= f << 0;
67 return x;
68 }
69 void countObjectSpherePoints(int *ntriangles, int *nvertices, int detail);
70 void placeObjectSphere(void *data, ushort *idxs, ushort first_index, GLfloat const position[3],
71 Matrix3d rotation, GLfloat radius, int detail, const Color &color);
72
73 typedef enum {
74 Shader_Tile,
75 Shader_Object,
76 Shader_Water,
77 Shader_Line,
78 Shader_Reflection,
79 } Shader_Type;
80 /* call markViewChanged when activeView is changed */
81 void markViewChanged();
82 /* setActiveProgramAndUniforms switches program, sets view uniforms, and updates common uniform
83 * locations */
84 void setActiveProgramAndUniforms(Shader_Type shader);
85 /* update object shader uniforms */
86 typedef enum {
87 Lighting_None, /* No shadows, orientation independent */
88 Lighting_NoShadows, /* Orientation dependent colors, no shadows */
89 Lighting_Regular /* Shadows and orientation dependent colors */
90 } Object_Lighting;
91 void setObjectUniforms(Color specular, float sharpness, Object_Lighting lighting);
92 /* if the currently active shader has a uniform with matching name, then this struct provides
93 * the location */
94 struct UniformLocations {
95 GLuint line_color;
96 GLuint refl_color;
97 GLuint tex;
98 GLuint render_stage;
99 GLuint gameTime;
100 GLuint arrtex;
101 GLuint wtex;
102 GLuint specular_color;
103 GLuint sharpness;
104 GLuint ignore_shadow;
105 GLuint use_lighting;
106 // For UI shader
107 GLuint ui_screen_width;
108 GLuint ui_screen_height;
109 GLuint ui_tex;
110 };
111 extern UniformLocations uniformLocations;
112
113 typedef struct _viewpa {
114 Matrix4d modelview;
115 Matrix4d projection;
116
117 int fog_enabled;
118 GLfloat fog_color[3];
119 GLfloat fog_start;
120 GLfloat fog_end;
121
122 GLfloat light_ambient[3];
123 GLfloat light_diffuse[3];
124 GLfloat light_specular[3];
125
126 GLfloat light_position[3];
127 GLfloat sun_direction[3];
128
129 bool day_mode;
130 GLuint cascadeTexsize;
131 GLuint cascadeTexture[3];
132 Matrix4d cascade_proj[3];
133 Matrix4d cascade_model[3];
134
135 GLuint shadowMapTexture;
136 GLuint shadowMapTexsize;
137 bool calculating_shadows;
138
139 bool show_flag_state;
140 } ViewParameters;
141
142 extern ViewParameters activeView;
143
144 void perspectiveMatrix(GLdouble fovy_deg, GLdouble aspect, GLdouble zNear, GLdouble zFar,
145 Matrix4d out);
146 void lookAtMatrix(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX,
147 GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ,
148 Matrix4d out);
149
150 void renderDummyShadowCascade();
151 void renderDummyShadowMap();
152 void renderShadowMap(const Coord3d &focus, Map *mp, Game *gm);
153 void renderShadowCascade(const Coord3d &focus, Map *mp, Game *gm);
154
155 // generates a snapshot
156 int createSnapshot();
157 // displays a semitransparent centered sign with two text rows */
158 void message(char *row1, char *row2);
159 void multiMessage(int nlines, const char *left[], const char *right[]);
160 void displayFrameRate();
161
162 Coord3d crossProduct(const Coord3d &, const Coord3d &);
163 double dotProduct(const Coord3d &, const Coord3d &);
164 double length(const Coord3d &);
165
166 void debugMatrix(Matrix4d);
167 Coord3d useMatrix(Matrix4d, const Coord3d &);
168 Coord3d useMatrix(Matrix3d, const Coord3d &);
169 void identityMatrix(Matrix4d);
170 void assign(const Matrix4d, Matrix4d);
171 void matrixMult(const Matrix4d, const Matrix4d, Matrix4d);
172 void rotateX(double, Matrix4d);
173 void rotateY(double, Matrix4d);
174 void rotateZ(double, Matrix4d);
assign(const GLfloat A[3],GLfloat B[3])175 inline void assign(const GLfloat A[3], GLfloat B[3]) {
176 for (int k = 0; k < 3; k++) B[k] = A[k];
177 }
178 int powerOfTwo(int input);
179 bool testBboxClip(double x1, double x2, double y1, double y2, double z1, double z2,
180 const Matrix4d model_times_proj);
181
182 /* Cache small polygon coordinates */
183 extern const double sin6[6];
184 extern const double sin10[10];
185 extern const double sin12[12];
186 extern const double sin14[14];
187 extern const double cos6[6];
188 extern const double cos10[10];
189 extern const double cos12[12];
190 extern const double cos14[14];
191
192 void Require2DMode();
193 void Enter2DMode();
194 void Leave2DMode();
195 GLuint LoadTexture(SDL_Surface *surface, GLfloat *texcoord);
196
197 // preloads texture from file (if not already loaded); returns texture array position
198 int loadTexture(const char *name);
199 /* Loads image from SHARE_DIR/image/ */
200 SDL_Surface *loadImage(const char *imagename);
201
202 extern GLuint textures[256];
203 extern char *textureNames[256]; // the names of preloaded textures
204 extern int numTextures;
205 extern GLuint textureBlank, textureGlitter, textureMousePointer, textureDizzy, textureWings,
206 textureTrack; // immediate texture lookup
207
208 /* Globals */
209 extern float fps;
210 extern int screenWidth, screenHeight;
211 extern TTF_Font *ingameFont;
212 TTF_Font *menuFontForSize(int sz);
213 extern const GLfloat menuColorSelected[4], menuColor[4];
214
215 /***********************************/
216 /* Inlined vector operations */
217
218 /* |A| */
length(const Coord3d & A)219 inline double length(const Coord3d &A) {
220 return std::sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
221 }
222 /* C <- A x B */
crossProduct(const Coord3d & A,const Coord3d & B)223 inline Coord3d crossProduct(const Coord3d &A, const Coord3d &B) {
224 Coord3d C;
225 C[0] = A[1] * B[2] - A[2] * B[1];
226 C[1] = A[2] * B[0] - A[0] * B[2];
227 C[2] = A[0] * B[1] - A[1] * B[0];
228 return C;
229 }
230
231 /* <- A . B */
dotProduct(const Coord3d & A,const Coord3d & B)232 inline double dotProduct(const Coord3d &A, const Coord3d &B) {
233 return A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
234 }
235
236 /* Inlined helper function */
glUniformC(GLint l,const Color & c)237 inline void glUniformC(GLint l, const Color &c) {
238 glUniform4f(l, c.f0(), c.f1(), c.f2(), c.f3());
239 }
240 #endif
241