Skip to content

Instantly share code, notes, and snippets.

@Zoxc
Created July 9, 2010 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zoxc/469759 to your computer and use it in GitHub Desktop.
Save Zoxc/469759 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "swl.h"
#include "gles.hpp"
#include "OpenGL.Texture.hpp"
#include <ft2build.h>
#include FT_FREETYPE_H
FT_Library library;
const int width = 800;
const int height = 480;
GLuint program;
GLuint program_gamma;
GLuint TextureUniform;
GLuint ColorUniform;
GLuint TextureUniformGamma;
GLuint ColorUniformGamma;
void draw_quad(int x, int y, int width, int height)
{
GLubyte TextureCoordinate[] = {
0, 1,
0, 0,
1, 1,
1, 0
};
GLshort Positions[] = {
x, y + height,
x, y,
x + width, y + height,
x + width, y
};
glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE, 0, Positions);
glVertexAttribPointer(1, 2, GL_UNSIGNED_BYTE, GL_FALSE, 0, TextureCoordinate);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
GLuint texture_from_aa(FT_Face face)
{
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
int width = face->glyph->bitmap.width;
int height = face->glyph->bitmap.rows;
unsigned int* PixelData = new unsigned int[width * height];
unsigned int* Pixel = PixelData;
unsigned int* PixelEnd = PixelData + width * height;
unsigned char* Buffer = face->glyph->bitmap.buffer;
while(Pixel != PixelEnd)
{
*Pixel = 0x80808080;
/*((unsigned char*)Pixel)[1] = *Buffer;
((unsigned char*)Pixel)[2] = *Buffer;
((unsigned char*)Pixel)[3] = 128;*/
Pixel++;
Buffer++;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, PixelData);
glBindTexture(GL_TEXTURE_2D, tex);
delete[] PixelData;
return tex;
}
void draw_char_aa(FT_Face face, int *x, int y, const char c)
{
if(FT_Load_Char(face, c, FT_LOAD_TARGET_LIGHT))
printf("FT_Load_Glyph failed");
if(FT_Render_Glyph(face->glyph, FT_RENDER_MODE_LIGHT))
printf("FT_Render_Glyph failed");
GLuint tex = texture_from_aa(face);
glBindTexture(GL_TEXTURE_2D, tex);
draw_quad(*x, y, face->glyph->bitmap.width, face->glyph->bitmap.rows);
glDeleteTextures(1, &tex);
*x += face->glyph->bitmap.width + 1;
}
// FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO))
typedef void (*draw_char_t)(FT_Face face, int *x, int y, const char c);
void draw_text(FT_Face face, int *x, int y, const char *text, draw_char_t draw_char)
{
const char *c = text;
while(*c)
{
draw_char(face, x, y, *c);
c++;
}
}
void draw_texts(FT_Face face, int *x, int y, const char *text, bool gamma)
{
if(gamma)
{
glUseProgram(program_gamma);
glUniform1i(TextureUniformGamma, 0);
}
else
{
glUseProgram(program);
glUniform1i(TextureUniform, 0);
}
glActiveTexture(GL_TEXTURE0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
draw_text(face, x, y, text, draw_char_aa);
}
void draw_font(int *y, const char *filename)
{
FT_Face face;
if(FT_New_Face(library, filename, 0, &face))
printf("FT_New_Face failed.");
if(FT_Set_Char_Size(face, 0, 12 * 64, 96 * 3, 96))
printf("FT_Set_Char_Size failed.");
int x = 10;
const char *text = "Indication. ";
draw_texts(face, &x, *y, text, false);
draw_texts(face, &x, *y, text, true);
FT_Done_Face(face);
}
int main(void)
{
if(FT_Init_FreeType(&library))
{
printf("FreeType setup failed");
return -1;
}
enum swl_result result = swl_init("blending", width, height, true);
if(result != SWLR_OK)
{
printf("Unable to setup window... %d", result);
return -1;
}
const char* fragment_shader =
"precision highp float;\
varying vec2 VCord;\
uniform sampler2D Texture;\
uniform vec4 Color;\
void main (void)\
{\
vec4 dest = vec4(0.9, 0.9, 0.9, 1); \
vec4 mask = texture2D(Texture, VCord); \
vec4 frag = mask * Color.a;\
gl_FragColor = Color * frag + (vec4(1, 1, 1, 1) - frag) * dest;\
gl_FragColor = texture2D(Texture, VCord);\
}";
const char* fragment_shader_gamma =
"precision highp float;\
varying vec2 VCord;\
uniform sampler2D Texture;\
uniform vec4 Color;\
void main (void)\
{\
vec4 dest = vec4(0.9, 0.9, 0.9, 1); \
vec4 mask = texture2D(Texture, VCord); \
vec4 frag = mask * Color.a;\
gl_FragColor = Color * frag + (vec4(1, 1, 1, 1) - frag) * dest;\
gl_FragColor = vec4(1, 0, 0, 1);\
}";
const char* vertex_shader =
"precision highp float;\
attribute vec2 APoint;\
attribute vec2 ACord;\
varying vec2 VCord;\
void main(void)\
{\
gl_Position.x = APoint.x / 400.0 - 1.0;\
gl_Position.y = -(APoint.y / 240.0 - 1.0);\
gl_Position.z = 0.0;\
gl_Position.w = 1.0;\
VCord = ACord;\
}";
GLfloat color[4] = {0.1, 0.1, 0.1, 1};
program = glCreateProgram();
gluCompileShader(program, GL_FRAGMENT_SHADER, fragment_shader);
gluCompileShader(program, GL_VERTEX_SHADER, vertex_shader);
glBindAttribLocation(program, 0, "APoint");
glBindAttribLocation(program, 1, "ACord");
gluLinkProgram(program);
glUseProgram(program);
TextureUniform = glGetUniformLocation(program, "Texture");
ColorUniform = glGetUniformLocation(program, "Color");
glUniform1i(TextureUniform, 0);
glUniform4f(ColorUniform, color[0], color[1], color[2], color[3]);
program_gamma = glCreateProgram();
gluCompileShader(program_gamma, GL_FRAGMENT_SHADER, fragment_shader_gamma);
gluCompileShader(program_gamma, GL_VERTEX_SHADER, vertex_shader);
glBindAttribLocation(program_gamma, 0, "APoint");
glBindAttribLocation(program_gamma, 1, "ACord");
gluLinkProgram(program_gamma);
glUseProgram(program_gamma);
TextureUniformGamma = glGetUniformLocation(program_gamma, "Texture");
ColorUniformGamma = glGetUniformLocation(program_gamma, "Color");
glUniform1i(TextureUniformGamma, 0);
glUniform4f(ColorUniformGamma, color[0], color[1], color[2], color[3]);
GLenum error = glGetError();
if(error != GL_NO_ERROR)
{
printf("OpenGL failed with error 0x%x.\n", error);
return 0;
}
struct swl_event event;
while(1)
{
while(swl_query(&event))
{
switch(event.type)
{
case SWLE_QUIT:
goto quit;
case SWLE_RESIZE:
glViewport(0, 0, event.size_event.width, event.size_event.height);
break;
default:
break;
}
}
glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
int y = 10;
draw_font(&y, "fonts\\DroidSans.ttf");
swl_swap();
if(error != GL_NO_ERROR)
{
printf("OpenGL failed in scene with error 0x%x.\n", error);
return 0;
}
}
quit:
swl_quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment