-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add both gl2 and gl3 versions, fix *glsl.h not being cleaned
- Loading branch information
Showing
69 changed files
with
4,112 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
uniform sampler2D u_atlas_tex; | ||
uniform ivec4 u_atlas_info; | ||
|
||
#define GLYPHY_TEXTURE1D_EXTRA_DECLS , sampler2D _tex, ivec4 _atlas_info, ivec2 _atlas_pos | ||
#define GLYPHY_TEXTURE1D_EXTRA_ARGS , _tex, _atlas_info, _atlas_pos | ||
#define GLYPHY_DEMO_EXTRA_ARGS , u_atlas_tex, u_atlas_info, gi.atlas_pos | ||
|
||
vec4 | ||
glyphy_texture1D_func (int offset GLYPHY_TEXTURE1D_EXTRA_DECLS) | ||
{ | ||
ivec2 item_geom = _atlas_info.zw; | ||
vec2 pos = (vec2 (_atlas_pos.xy * item_geom + | ||
ivec2 (mod (float (offset), float (item_geom.x)), offset / item_geom.x)) + | ||
+ vec2 (.5, .5)) / vec2(_atlas_info.xy); | ||
return texture2D (_tex, pos); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Copyright 2012 Google, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Google Author(s): Behdad Esfahbod | ||
*/ | ||
|
||
#ifdef HAVE_CONFIG_H | ||
#include <config.h> | ||
#endif | ||
|
||
#include "demo-buffer.h" | ||
|
||
struct demo_buffer_t { | ||
unsigned int refcount; | ||
|
||
glyphy_point_t cursor; | ||
std::vector<glyph_vertex_t> *vertices; | ||
glyphy_extents_t ink_extents; | ||
glyphy_extents_t logical_extents; | ||
bool dirty; | ||
GLuint buf_name; | ||
}; | ||
|
||
demo_buffer_t * | ||
demo_buffer_create (void) | ||
{ | ||
demo_buffer_t *buffer = (demo_buffer_t *) calloc (1, sizeof (demo_buffer_t)); | ||
buffer->refcount = 1; | ||
|
||
buffer->vertices = new std::vector<glyph_vertex_t>; | ||
glGenBuffers (1, &buffer->buf_name); | ||
|
||
demo_buffer_clear (buffer); | ||
|
||
return buffer; | ||
} | ||
|
||
demo_buffer_t * | ||
demo_buffer_reference (demo_buffer_t *buffer) | ||
{ | ||
if (buffer) buffer->refcount++; | ||
return buffer; | ||
} | ||
|
||
void | ||
demo_buffer_destroy (demo_buffer_t *buffer) | ||
{ | ||
if (!buffer || --buffer->refcount) | ||
return; | ||
|
||
glDeleteBuffers (1, &buffer->buf_name); | ||
delete buffer->vertices; | ||
free (buffer); | ||
} | ||
|
||
|
||
void | ||
demo_buffer_clear (demo_buffer_t *buffer) | ||
{ | ||
buffer->vertices->clear (); | ||
glyphy_extents_clear (&buffer->ink_extents); | ||
glyphy_extents_clear (&buffer->logical_extents); | ||
buffer->dirty = true; | ||
} | ||
|
||
void | ||
demo_buffer_extents (demo_buffer_t *buffer, | ||
glyphy_extents_t *ink_extents, | ||
glyphy_extents_t *logical_extents) | ||
{ | ||
if (ink_extents) | ||
*ink_extents = buffer->ink_extents; | ||
if (logical_extents) | ||
*logical_extents = buffer->logical_extents; | ||
} | ||
|
||
void | ||
demo_buffer_move_to (demo_buffer_t *buffer, | ||
const glyphy_point_t *p) | ||
{ | ||
buffer->cursor = *p; | ||
} | ||
|
||
void | ||
demo_buffer_current_point (demo_buffer_t *buffer, | ||
glyphy_point_t *p) | ||
{ | ||
*p = buffer->cursor; | ||
} | ||
|
||
void | ||
demo_buffer_add_text (demo_buffer_t *buffer, | ||
const char *utf8, | ||
demo_font_t *font, | ||
double font_size) | ||
{ | ||
FT_Face face = demo_font_get_face (font); | ||
glyphy_point_t top_left = buffer->cursor; | ||
buffer->cursor.y += font_size /* * font->ascent */; | ||
unsigned int unicode; | ||
for (const unsigned char *p = (const unsigned char *) utf8; *p; p++) { | ||
if (*p < 128) { | ||
unicode = *p; | ||
} else { | ||
unsigned int j; | ||
if (*p < 0xE0) { | ||
unicode = *p & ~0xE0; | ||
j = 1; | ||
} else if (*p < 0xF0) { | ||
unicode = *p & ~0xF0; | ||
j = 2; | ||
} else { | ||
unicode = *p & ~0xF8; | ||
j = 3; | ||
continue; | ||
} | ||
p++; | ||
for (; j && *p; j--, p++) | ||
unicode = (unicode << 6) | (*p & ~0xC0); | ||
p--; | ||
} | ||
|
||
if (unicode == '\n') { | ||
buffer->cursor.y += font_size; | ||
buffer->cursor.x = top_left.x; | ||
continue; | ||
} | ||
|
||
unsigned int glyph_index = FT_Get_Char_Index (face, unicode); | ||
glyph_info_t gi; | ||
demo_font_lookup_glyph (font, glyph_index, &gi); | ||
|
||
/* Update ink extents */ | ||
glyphy_extents_t ink_extents; | ||
demo_shader_add_glyph_vertices (buffer->cursor, font_size, &gi, buffer->vertices, &ink_extents); | ||
glyphy_extents_extend (&buffer->ink_extents, &ink_extents); | ||
|
||
/* Update logical extents */ | ||
glyphy_point_t corner; | ||
corner.x = buffer->cursor.x; | ||
corner.y = buffer->cursor.y - font_size; | ||
glyphy_extents_add (&buffer->logical_extents, &corner); | ||
corner.x = buffer->cursor.x + font_size * gi.advance; | ||
corner.y = buffer->cursor.y; | ||
glyphy_extents_add (&buffer->logical_extents, &corner); | ||
|
||
buffer->cursor.x += font_size * gi.advance; | ||
} | ||
|
||
buffer->dirty = true; | ||
} | ||
|
||
void | ||
demo_buffer_draw (demo_buffer_t *buffer) | ||
{ | ||
GLint program; | ||
glGetIntegerv (GL_CURRENT_PROGRAM, &program); | ||
GLuint a_glyph_vertex_loc = glGetAttribLocation (program, "a_glyph_vertex"); | ||
glBindBuffer (GL_ARRAY_BUFFER, buffer->buf_name); | ||
if (buffer->dirty) { | ||
glBufferData (GL_ARRAY_BUFFER, sizeof (glyph_vertex_t) * buffer->vertices->size (), (const char *) &(*buffer->vertices)[0], GL_STATIC_DRAW); | ||
buffer->dirty = false; | ||
} | ||
glEnableVertexAttribArray (a_glyph_vertex_loc); | ||
glVertexAttribPointer (a_glyph_vertex_loc, 4, GL_FLOAT, GL_FALSE, sizeof (glyph_vertex_t), 0); | ||
glDrawArrays (GL_TRIANGLES, 0, buffer->vertices->size ()); | ||
glDisableVertexAttribArray (a_glyph_vertex_loc); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright 2012 Google, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Google Author(s): Behdad Esfahbod, Maysum Panju | ||
*/ | ||
|
||
#ifndef DEMO_COMMON_H | ||
#define DEMO_COMMON_H | ||
|
||
#include <glyphy.h> | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
#include <assert.h> | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
|
||
#define GL_SILENCE_DEPRECATION 1 | ||
|
||
/* Tailor config for various platforms. */ | ||
|
||
#ifdef EMSCRIPTEN | ||
/* https://github.com/kripken/emscripten/issues/340 */ | ||
# undef HAVE_GLEW | ||
/* WebGL shaders are ES2 */ | ||
# define GL_ES_VERSION_2_0 1 | ||
#endif | ||
|
||
#if defined(__ANDROID__) | ||
# define HAVE_GLES2 1 | ||
# define HAVE_GLUT 1 | ||
#endif | ||
|
||
#ifdef _WIN32 | ||
# define HAVE_GL 1 | ||
# define HAVE_GLEW 1 | ||
# define HAVE_GLUT 1 | ||
# define HAVE_FREETYPE2 1 | ||
#endif | ||
|
||
/* Get Glew out of the way. */ | ||
#ifdef HAVE_GLEW | ||
# include <GL/glew.h> | ||
#else | ||
# define GLEW_OK 0 | ||
static inline int glewInit (void) { return GLEW_OK; } | ||
static inline int glewIsSupported (const char *s) | ||
{ return 0 == strcmp ("GL_VERSION_2_0", s); } | ||
#endif /* HAVE_GLEW */ | ||
|
||
/* WTF this block?! */ | ||
#if defined(HAVE_GLES2) | ||
# include <GLES2/gl2.h> | ||
#elif defined(HAVE_GL) | ||
# ifndef HAVE_GLEW | ||
# define GL_GLEXT_PROTOTYPES 1 | ||
# if defined(__APPLE__) | ||
# include <OpenGL/gl.h> | ||
# else | ||
# include <GL/gl.h> | ||
# endif | ||
# endif | ||
# if defined(__APPLE__) | ||
# include <OpenGL/OpenGL.h> | ||
# else | ||
# ifdef HAVE_GLEW | ||
# ifdef _WIN32 | ||
# include <GL/wglew.h> | ||
# else | ||
# include <GL/glxew.h> | ||
# endif | ||
# endif | ||
# endif | ||
#endif /* HAVE_GL */ | ||
|
||
/* Finally, Glut. */ | ||
#ifdef HAVE_GLUT | ||
# if defined(__APPLE__) | ||
# include <GLUT/glut.h> | ||
# else | ||
# include <GL/glut.h> | ||
# endif | ||
#endif | ||
|
||
|
||
|
||
|
||
/* Logging. */ | ||
#ifdef __ANDROID__ | ||
# include <android/log.h> | ||
# define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "glyphy-demo", __VA_ARGS__)) | ||
# define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "glyphy-demo", __VA_ARGS__)) | ||
# define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "glyphy-demo", __VA_ARGS__)) | ||
#else /* !__ANDROID__ */ | ||
# define LOGI(...) ((void) fprintf (stdout, __VA_ARGS__)) | ||
# define LOGW(...) ((void) fprintf (stderr, __VA_ARGS__)) | ||
# define LOGE(...) ((void) fprintf (stderr, __VA_ARGS__), abort ()) | ||
#endif | ||
|
||
|
||
|
||
#define STRINGIZE1(Src) #Src | ||
#define STRINGIZE(Src) STRINGIZE1(Src) | ||
|
||
#define ARRAY_LEN(Array) (sizeof (Array) / sizeof (*Array)) | ||
|
||
|
||
#define MIN_FONT_SIZE 10 | ||
#define TOLERANCE (1./2048) | ||
|
||
|
||
#define gl(name) \ | ||
for (GLint __ee, __ii = 0; \ | ||
__ii < 1; \ | ||
(__ii++, \ | ||
(__ee = glGetError()) && \ | ||
(fprintf (stderr, "gl" #name " failed with error %04X on line %d\n", __ee, __LINE__), abort (), 0))) \ | ||
gl##name | ||
|
||
|
||
static inline void | ||
die (const char *msg) | ||
{ | ||
fprintf (stderr, "%s\n", msg); | ||
exit (1); | ||
} | ||
|
||
template <typename T> | ||
T clamp (T v, T m, T M) | ||
{ | ||
return v < m ? m : v > M ? M : v; | ||
} | ||
|
||
|
||
#if defined(_MSC_VER) | ||
#define DEMO_FUNC __FUNCSIG__ | ||
#else | ||
#define DEMO_FUNC __func__ | ||
#endif | ||
|
||
struct auto_trace_t | ||
{ | ||
auto_trace_t (const char *func_) : func (func_) | ||
{ printf ("Enter: %s\n", func); } | ||
|
||
~auto_trace_t (void) | ||
{ printf ("Leave: %s\n", func); } | ||
|
||
private: | ||
const char * const func; | ||
}; | ||
|
||
#define TRACE() auto_trace_t trace(DEMO_FUNC) | ||
|
||
#endif /* DEMO_COMMON_H */ |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.