17
17
#define MATH_3D_IMPLEMENTATION
18
18
#include "third-party/math_3d.h"
19
19
20
+ #define FONTSTASH_IMPLEMENTATION
21
+ #include "third-party/fontstash.h"
22
+
23
+ #define GLFONTSTASH_IMPLEMENTATION
24
+ #include "third-party/gl3fontstash.h"
25
+
20
26
#define WINDOW_NAME "blocks"
21
27
#define WINDOW_WIDTH 1024
22
28
#define WINDOW_HEIGHT 768
@@ -97,6 +103,12 @@ static camera Camera;
97
103
static GLuint Texture ;
98
104
static GLuint TextureID ;
99
105
106
+ // FONTS
107
+ static GLfloat mat [16 ];
108
+ static FONScontext * fontcontext ;
109
+ static int DroidRegular = 0 ;
110
+ static float lineHeight = 0.0f ;
111
+
100
112
int render_init () {
101
113
// Ensure we can capture the escape key being pressed below
102
114
glfwSetInputMode (window , GLFW_STICKY_KEYS , GL_TRUE );
@@ -139,23 +151,43 @@ int render_init() {
139
151
// Set Projection matrix to perspective, with 1 rad FoV
140
152
Projection = m4_perspective (80.0f , width / height , 0.1f , 200.0f );
141
153
142
- // Enable face culling
143
- glEnable (GL_CULL_FACE );
154
+ // Set text rendering matrix
155
+ memset (mat , 0 , 16 * sizeof (GLfloat ));
156
+ mat [0 ] = 2.0 / width ;
157
+ mat [5 ] = -2.0 / height ;
158
+ mat [10 ] = 2.0 ;
159
+ mat [12 ] = -1.0 ;
160
+ mat [13 ] = 1.0 ;
161
+ mat [14 ] = -1.0 ;
162
+ mat [15 ] = 1.0 ;
144
163
145
- // Enable depth test
146
- glEnable (GL_DEPTH_TEST );
164
+ glEnable (GL_BLEND );
165
+ glBlendEquation (GL_FUNC_ADD );
166
+ glBlendFunc (GL_SRC_ALPHA ,GL_ONE_MINUS_SRC_ALPHA );
147
167
148
168
// Accept fragment if it closer to the camera than the former one
149
169
glDepthFunc (GL_LESS );
150
170
171
+ // Add fonts
172
+ fontcontext = gl3fonsCreate (1024 , 1024 , FONS_ZERO_TOPLEFT );
173
+ DroidRegular = fonsAddFont (fontcontext , "sans" , "assets/DroidSerif-Regular.ttf" );
174
+
175
+ fonsSetColor (fontcontext , gl3fonsRGBA (255 ,255 ,255 ,255 )); // white
176
+ fonsSetSize (fontcontext , 36.0f ); // 16 point font
177
+ fonsSetAlign (fontcontext , FONS_ALIGN_LEFT | FONS_ALIGN_TOP ); // left/top aligned
178
+ fonsVertMetrics (fontcontext , NULL , NULL , & lineHeight );
179
+
151
180
check_opengl_error ();
152
181
153
182
return 0 ;
154
183
}
155
184
156
185
static unsigned char BLOCKS_DEBUG = 0 ;
157
186
158
- void render () {
187
+ void render (double starttime ) {
188
+ // Use our shaders
189
+ glUseProgram (program );
190
+
159
191
// Clear the screen
160
192
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
161
193
@@ -165,6 +197,12 @@ void render() {
165
197
GLint projection_view = glGetUniformLocation (program , "ProjectionView" );
166
198
glUniformMatrix4fv (projection_view , 1 , GL_FALSE , (GLfloat * ) & ProjectionView );
167
199
200
+ // Enable face culling
201
+ glEnable (GL_CULL_FACE );
202
+
203
+ // Enable depth test
204
+ glEnable (GL_DEPTH_TEST );
205
+
168
206
// Add timer
169
207
glUniform1f (glGetUniformLocation (program , "timer" ), glfwGetTime ());
170
208
@@ -182,6 +220,21 @@ void render() {
182
220
render_chunk (program , Chunks [i ]);
183
221
}
184
222
}
223
+ // Enable face culling
224
+ glDisable (GL_CULL_FACE );
225
+
226
+ // Enable depth test
227
+ glDisable (GL_DEPTH_TEST );
228
+
229
+ // Render text
230
+ gl3fonsProjection (fontcontext , mat );
231
+
232
+ double endtime = glfwGetTime ();
233
+ char performance [256 ];
234
+ sprintf (performance , "frametime: %2.4fms" , (endtime - starttime ) * 1000 );
235
+
236
+ float dx = 10.0 , dy = 10.0f ;
237
+ fonsDrawText (fontcontext , dx , dy , performance , NULL );
185
238
186
239
// Swap buffers
187
240
glfwSwapBuffers (window );
@@ -262,7 +315,9 @@ int main(void) {
262
315
ongoingTime += deltaTime ;
263
316
}
264
317
265
- render ();
318
+ double starttime = glfwGetTime ();
319
+ render (starttime );
320
+
266
321
glfwPollEvents ();
267
322
}
268
323
0 commit comments