3dphotoshoot

changeset 22:d7fe157c402d

fonts
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 13 Jun 2015 05:32:07 +0300
parents 4ca4e3c5a754
children a94af102872f
files android/manifest.xml.in libs/drawtext/drawtext.h libs/drawtext/font.c sdr/font.p.glsl sdr/vertex.glsl src/assman.c src/assman.h src/game.cc src/game.h src/shader.cc src/shader.h src/text.c src/text.h
diffstat 13 files changed, 373 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/android/manifest.xml.in	Thu Jun 11 04:56:33 2015 +0300
     1.2 +++ b/android/manifest.xml.in	Sat Jun 13 05:32:07 2015 +0300
     1.3 @@ -14,7 +14,8 @@
     1.4  		<!-- android:icon="@drawable/ic_launcher" -->
     1.5  
     1.6  		<activity android:name="MainActivity" android:label="$APPTITLE"
     1.7 -				android:screenOrientation="landscape">
     1.8 +			android:screenOrientation="landscape"
     1.9 +			android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    1.10  
    1.11  			<meta-data android:name="android.app.lib_name" android:value="$APPNAME"/>
    1.12  
     2.1 --- a/libs/drawtext/drawtext.h	Thu Jun 11 04:56:33 2015 +0300
     2.2 +++ b/libs/drawtext/drawtext.h	Sat Jun 13 05:32:07 2015 +0300
     2.3 @@ -20,6 +20,7 @@
     2.4  
     2.5  #include <stdio.h>
     2.6  #include <stdlib.h>
     2.7 +#include "assman.h"
     2.8  
     2.9  struct dtx_font;
    2.10  struct dtx_glyphmap;
    2.11 @@ -90,7 +91,7 @@
    2.12   * freetype support.
    2.13   */
    2.14  struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
    2.15 -struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
    2.16 +struct dtx_glyphmap *dtx_load_glyphmap_stream(ass_file *fp);
    2.17  int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap);
    2.18  int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap);
    2.19  
     3.1 --- a/libs/drawtext/font.c	Thu Jun 11 04:56:33 2015 +0300
     3.2 +++ b/libs/drawtext/font.c	Sat Jun 13 05:32:07 2015 +0300
     3.3 @@ -31,6 +31,7 @@
     3.4  #include <ft2build.h>
     3.5  #include FT_FREETYPE_H
     3.6  #endif
     3.7 +#include "assman.h"
     3.8  #include "drawtext.h"
     3.9  #include "drawtext_impl.h"
    3.10  
    3.11 @@ -333,18 +334,18 @@
    3.12  
    3.13  struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
    3.14  {
    3.15 -	FILE *fp;
    3.16 +	ass_file *fp;
    3.17  	struct dtx_glyphmap *gmap;
    3.18  
    3.19 -	if(!(fp = fopen(fname, "rb"))) {
    3.20 +	if(!(fp = ass_fopen(fname, "rb"))) {
    3.21  		return 0;
    3.22  	}
    3.23  	gmap = dtx_load_glyphmap_stream(fp);
    3.24 -	fclose(fp);
    3.25 +	ass_fclose(fp);
    3.26  	return gmap;
    3.27  }
    3.28  
    3.29 -struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
    3.30 +struct dtx_glyphmap *dtx_load_glyphmap_stream(ass_file *fp)
    3.31  {
    3.32  	char buf[512];
    3.33  	int hdr_lines = 0;
    3.34 @@ -364,7 +365,7 @@
    3.35  
    3.36  	while(hdr_lines < 3) {
    3.37  		char *line = buf;
    3.38 -		if(!fgets(buf, sizeof buf, fp)) {
    3.39 +		if(!ass_fgets(buf, sizeof buf, fp)) {
    3.40  			fperror("unexpected end of file");
    3.41  			goto err;
    3.42  		}
    3.43 @@ -471,13 +472,13 @@
    3.44  	}
    3.45  
    3.46  	for(i=0; i<num_pixels; i++) {
    3.47 -		long c = fgetc(fp);
    3.48 +		long c = ass_fgetc(fp);
    3.49  		if(c == -1) {
    3.50  			fprintf(stderr, "unexpected end of file while reading pixels\n");
    3.51  			goto err;
    3.52  		}
    3.53  		gmap->pixels[i] = 255 * c / max_pixval;
    3.54 -		fseek(fp, 2, SEEK_CUR);
    3.55 +		ass_fseek(fp, 2, SEEK_CUR);
    3.56  	}
    3.57  
    3.58  	gmap->cstart = min_code;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/sdr/font.p.glsl	Sat Jun 13 05:32:07 2015 +0300
     4.3 @@ -0,0 +1,12 @@
     4.4 +precision mediump float;
     4.5 +
     4.6 +uniform sampler2D tex;
     4.7 +uniform vec4 color;
     4.8 +
     4.9 +varying vec4 tex_coords;
    4.10 +
    4.11 +void main()
    4.12 +{
    4.13 +	vec4 texel = texture2D(tex, tex_coords.xy);
    4.14 +	gl_FragColor = vec4(color.xyz, texel.a);
    4.15 +}
     5.1 --- a/sdr/vertex.glsl	Thu Jun 11 04:56:33 2015 +0300
     5.2 +++ b/sdr/vertex.glsl	Sat Jun 13 05:32:07 2015 +0300
     5.3 @@ -1,12 +1,13 @@
     5.4 -attribute vec4 attr_vertex, attr_texcoord;
     5.5 +attribute vec4 attr_vertex, attr_texcoord, attr_color;
     5.6  
     5.7  uniform mat4 matrix_modelview, matrix_projection, matrix_texture;
     5.8  
     5.9 -varying vec4 tex_coords;
    5.10 +varying vec4 tex_coords, color;
    5.11  
    5.12  void main()
    5.13  {
    5.14  	mat4 mvp = matrix_projection * matrix_modelview;
    5.15  	gl_Position = mvp * attr_vertex;
    5.16  	tex_coords = matrix_texture * attr_texcoord;
    5.17 +	color = attr_color;
    5.18  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/assman.c	Sat Jun 13 05:32:07 2015 +0300
     6.3 @@ -0,0 +1,30 @@
     6.4 +#include "assman.h"
     6.5 +
     6.6 +int ass_fgetc(ass_file *fp)
     6.7 +{
     6.8 +	char c;
     6.9 +
    6.10 +	if(ass_fread(&c, 1, 1, fp) < 1) {
    6.11 +		return -1;
    6.12 +	}
    6.13 +	return c;
    6.14 +}
    6.15 +
    6.16 +char *ass_fgets(char *s, int size, ass_file *fp)
    6.17 +{
    6.18 +	int i, c;
    6.19 +	char *ptr = s;
    6.20 +
    6.21 +	if(!size) return 0;
    6.22 +
    6.23 +	for(i=0; i<size - 1; i++) {
    6.24 +		if((c = ass_fgetc(fp)) == -1) {
    6.25 +			break;
    6.26 +		}
    6.27 +		*ptr++ = c;
    6.28 +
    6.29 +		if(c == '\n') break;
    6.30 +	}
    6.31 +	*ptr = 0;
    6.32 +	return ptr == s ? 0 : s;
    6.33 +}
     7.1 --- a/src/assman.h	Thu Jun 11 04:56:33 2015 +0300
     7.2 +++ b/src/assman.h	Sat Jun 13 05:32:07 2015 +0300
     7.3 @@ -5,6 +5,10 @@
     7.4  
     7.5  typedef void ass_file;
     7.6  
     7.7 +#ifdef __cplusplus
     7.8 +extern "C" {
     7.9 +#endif
    7.10 +
    7.11  ass_file *ass_fopen(const char *fname, const char *mode);
    7.12  void ass_fclose(ass_file *fp);
    7.13  long ass_fseek(ass_file *fp, long offs, int whence);
    7.14 @@ -12,5 +16,12 @@
    7.15  
    7.16  size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp);
    7.17  
    7.18 +/* convenience functions, derived from the above */
    7.19 +int ass_fgetc(ass_file *fp);
    7.20 +char *ass_fgets(char *s, int size, ass_file *fp);
    7.21 +
    7.22 +#ifdef __cplusplus
    7.23 +}
    7.24 +#endif
    7.25  
    7.26  #endif	/* ASSMAN_H_ */
     8.1 --- a/src/game.cc	Thu Jun 11 04:56:33 2015 +0300
     8.2 +++ b/src/game.cc	Sat Jun 13 05:32:07 2015 +0300
     8.3 @@ -8,10 +8,12 @@
     8.4  #include "sanegl.h"
     8.5  #include "texture.h"
     8.6  #include "shader.h"
     8.7 +#include "text.h"
     8.8  
     8.9  static void draw_quad(float hsz, float vsz);
    8.10  
    8.11 -static int win_width, win_height;
    8.12 +int win_width, win_height;
    8.13 +
    8.14  static float win_aspect;
    8.15  static int video_width, video_height;
    8.16  static float video_aspect;
    8.17 @@ -21,10 +23,8 @@
    8.18  
    8.19  extern "C" int game_init(void)
    8.20  {
    8.21 -	unsigned int vsdr, psdr_cam, psdr_tex;
    8.22 -
    8.23  	//glEnable(GL_DEPTH_TEST);
    8.24 -	glEnable(GL_CULL_FACE);
    8.25 +	//glEnable(GL_CULL_FACE);
    8.26  
    8.27  	glClearColor(0.4, 0.4, 0.4, 1);
    8.28  
    8.29 @@ -102,10 +102,19 @@
    8.30  
    8.31  	glEnable(GL_BLEND);
    8.32  	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    8.33 +	draw_quad(0.5, 0.5);
    8.34 +	glDisable(GL_BLEND);
    8.35  
    8.36 -	draw_quad(0.5, 0.5);
    8.37 +	gl_matrix_mode(GL_TEXTURE);
    8.38 +	gl_load_identity();
    8.39  
    8.40 -	glDisable(GL_BLEND);
    8.41 +
    8.42 +	// draw some text
    8.43 +	text_color(1, 1, 1, 1);
    8.44 +	for(int i=0; i<25; i++) {
    8.45 +		text_position(0, i);
    8.46 +		text_printf("%d%s line of text", i, i == 1 ? "st" : (i == 2 ? "nd" : (i == 3 ? "rd" : "th")));
    8.47 +	}
    8.48  }
    8.49  
    8.50  static void draw_quad(float hsz, float vsz)
     9.1 --- a/src/game.h	Thu Jun 11 04:56:33 2015 +0300
     9.2 +++ b/src/game.h	Sat Jun 13 05:32:07 2015 +0300
     9.3 @@ -1,6 +1,8 @@
     9.4  #ifndef GAME_H_
     9.5  #define GAME_H_
     9.6  
     9.7 +extern int win_width, win_height;
     9.8 +
     9.9  #ifdef __cplusplus
    9.10  extern "C" {
    9.11  #endif
    10.1 --- a/src/shader.cc	Thu Jun 11 04:56:33 2015 +0300
    10.2 +++ b/src/shader.cc	Sat Jun 13 05:32:07 2015 +0300
    10.3 @@ -187,6 +187,122 @@
    10.4  	return true;
    10.5  }
    10.6  
    10.7 +void SdrProg::set_uniform(const char *name, int count, const int *val) const
    10.8 +{
    10.9 +	if(!bind()) return;
   10.10 +
   10.11 +	int loc = glGetUniformLocation(prog, name);
   10.12 +	if(loc == -1) return;
   10.13 +
   10.14 +	switch(count) {
   10.15 +	case 1:
   10.16 +		glUniform1iv(loc, 1, val);
   10.17 +		break;
   10.18 +	case 2:
   10.19 +		glUniform2iv(loc, 1, val);
   10.20 +		break;
   10.21 +	case 3:
   10.22 +		glUniform3iv(loc, 1, val);
   10.23 +	case 4:
   10.24 +		glUniform4iv(loc, 1, val);
   10.25 +	default:
   10.26 +		break;
   10.27 +	}
   10.28 +}
   10.29 +
   10.30 +void SdrProg::set_uniform(const char *name, int count, const float *val) const
   10.31 +{
   10.32 +	if(!bind()) return;
   10.33 +
   10.34 +	int loc = glGetUniformLocation(prog, name);
   10.35 +	if(loc == -1) return;
   10.36 +
   10.37 +	switch(count) {
   10.38 +	case 1:
   10.39 +		glUniform1fv(loc, 1, val);
   10.40 +		break;
   10.41 +	case 2:
   10.42 +		glUniform2fv(loc, 1, val);
   10.43 +		break;
   10.44 +	case 3:
   10.45 +		glUniform3fv(loc, 1, val);
   10.46 +	case 4:
   10.47 +		glUniform4fv(loc, 1, val);
   10.48 +	default:
   10.49 +		break;
   10.50 +	}
   10.51 +}
   10.52 +
   10.53 +void SdrProg::set_uniform(const char *name, const Vector4 &v) const
   10.54 +{
   10.55 +	set_uniform(name, 4, &v.x);
   10.56 +}
   10.57 +
   10.58 +
   10.59 +void SdrProg::set_uniform1i(const char *name, int x) const
   10.60 +{
   10.61 +	set_uniform(name, 1, &x);
   10.62 +}
   10.63 +
   10.64 +void SdrProg::set_uniform2i(const char *name, int x, int y) const
   10.65 +{
   10.66 +	int v[] = {x, y};
   10.67 +	set_uniform(name, 2, v);
   10.68 +}
   10.69 +
   10.70 +void SdrProg::set_uniform3i(const char *name, int x, int y, int z) const
   10.71 +{
   10.72 +	int v[] = {x, y, z};
   10.73 +	set_uniform(name, 3, v);
   10.74 +}
   10.75 +
   10.76 +void SdrProg::set_uniform4i(const char *name, int x, int y, int z, int w) const
   10.77 +{
   10.78 +	int v[] = {x, y, z, w};
   10.79 +	set_uniform(name, 4, v);
   10.80 +}
   10.81 +
   10.82 +void SdrProg::set_uniform1f(const char *name, float x) const
   10.83 +{
   10.84 +	set_uniform(name, 1, &x);
   10.85 +}
   10.86 +
   10.87 +void SdrProg::set_uniform2f(const char *name, float x, float y) const
   10.88 +{
   10.89 +	float v[] = {x, y};
   10.90 +	set_uniform(name, 2, v);
   10.91 +}
   10.92 +
   10.93 +void SdrProg::set_uniform3f(const char *name, float x, float y, float z) const
   10.94 +{
   10.95 +	float v[] = {x, y, z};
   10.96 +	set_uniform(name, 3, v);
   10.97 +}
   10.98 +
   10.99 +void SdrProg::set_uniform4f(const char *name, float x, float y, float z, float w) const
  10.100 +{
  10.101 +	float v[] = {x, y, z, w};
  10.102 +	set_uniform(name, 4, v);
  10.103 +}
  10.104 +
  10.105 +void SdrProg::set_uniform_matrix(const char *name, const float *m) const
  10.106 +{
  10.107 +	if(!bind()) return;
  10.108 +
  10.109 +	int loc = glGetUniformLocation(prog, name);
  10.110 +	if(loc == -1) return;
  10.111 +
  10.112 +	glUniformMatrix4fv(loc, 1, 0, m);
  10.113 +}
  10.114 +
  10.115 +void SdrProg::set_uniform_matrix(const char *name, const Matrix4x4 &m) const
  10.116 +{
  10.117 +	Matrix4x4 tmp = m.transposed();
  10.118 +	set_uniform_matrix(name, tmp[0]);
  10.119 +}
  10.120 +
  10.121 +
  10.122 +
  10.123  unsigned int get_shader(const char *name, unsigned int type)
  10.124  {
  10.125  	std::map<std::string, unsigned int>::const_iterator it = sdrdb.find(name);
    11.1 --- a/src/shader.h	Thu Jun 11 04:56:33 2015 +0300
    11.2 +++ b/src/shader.h	Sat Jun 13 05:32:07 2015 +0300
    11.3 @@ -2,6 +2,7 @@
    11.4  #define SHADER_H_
    11.5  
    11.6  #include <vector>
    11.7 +#include "vmath/vmath.h"
    11.8  
    11.9  enum SdrDefaultAttrib {
   11.10  	SDR_ATTR_VERTEX,
   11.11 @@ -42,6 +43,25 @@
   11.12  
   11.13  	bool bind() const;
   11.14  
   11.15 +	// helper functions for setting uniforms
   11.16 +	void set_uniform(const char *name, int count, const int *val) const;
   11.17 +	void set_uniform(const char *name, int count, const float *val) const;
   11.18 +	void set_uniform(const char *name, const Vector4 &v) const;
   11.19 +
   11.20 +	void set_uniform1i(const char *name, int x) const;
   11.21 +	void set_uniform2i(const char *name, int x, int y) const;
   11.22 +	void set_uniform3i(const char *name, int x, int y, int z) const;
   11.23 +	void set_uniform4i(const char *name, int x, int y, int z, int w) const;
   11.24 +
   11.25 +	void set_uniform1f(const char *name, float x) const;
   11.26 +	void set_uniform2f(const char *name, float x, float y) const;
   11.27 +	void set_uniform3f(const char *name, float x, float y, float z) const;
   11.28 +	void set_uniform4f(const char *name, float x, float y, float z, float w) const;
   11.29 +
   11.30 +	void set_uniform_matrix(const char *name, const float *m) const;
   11.31 +	void set_uniform_matrix(const char *name, const Matrix4x4 &m) const;
   11.32 +
   11.33 +
   11.34  	unsigned int get_globj() const { return prog; }
   11.35  };
   11.36  
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/text.c	Sat Jun 13 05:32:07 2015 +0300
    12.3 @@ -0,0 +1,135 @@
    12.4 +#include <stdio.h>
    12.5 +#include <stdarg.h>
    12.6 +#include <alloca.h>
    12.7 +#include "game.h"
    12.8 +#include "text.h"
    12.9 +#include "sdr.h"
   12.10 +#include "opengl.h"
   12.11 +#include "sanegl.h"
   12.12 +#include "drawtext/drawtext.h"
   12.13 +
   12.14 +#define FONT_NAME	"consolas_s24.glyphmap"
   12.15 +#define FONT_SIZE	24
   12.16 +
   12.17 +static struct dtx_font *font;
   12.18 +static unsigned int prog;
   12.19 +static int uloc_color = -1;
   12.20 +static int aloc_vertex, aloc_texcoords;
   12.21 +static float color[4];
   12.22 +static int cpos[2];
   12.23 +static int line_height;
   12.24 +
   12.25 +static int init(void)
   12.26 +{
   12.27 +	if(font) return 0;	/* already initialized */
   12.28 +
   12.29 +	if(!(font = dtx_open_font_glyphmap("data/" FONT_NAME))) {
   12.30 +		fprintf(stderr, "failed to open font\n");
   12.31 +		return -1;
   12.32 +	}
   12.33 +	line_height = dtx_line_height();
   12.34 +
   12.35 +	if(!(prog = create_program_load("sdr/vertex.glsl", "sdr/font.p.glsl"))) {
   12.36 +		dtx_close_font(font);
   12.37 +		font = 0;
   12.38 +		return -1;
   12.39 +	}
   12.40 +	aloc_vertex = glGetAttribLocation(prog, "attr_vertex");
   12.41 +	aloc_texcoords = glGetAttribLocation(prog, "attr_texcoord");
   12.42 +	uloc_color = glGetUniformLocation(prog, "color");
   12.43 +
   12.44 +	return 0;
   12.45 +}
   12.46 +
   12.47 +void text_color(float r, float g, float b, float a)
   12.48 +{
   12.49 +	color[0] = r;
   12.50 +	color[1] = g;
   12.51 +	color[2] = b;
   12.52 +	color[3] = a;
   12.53 +}
   12.54 +
   12.55 +void text_position(int x, int y)
   12.56 +{
   12.57 +	cpos[0] = x;
   12.58 +	cpos[1] = y;
   12.59 +}
   12.60 +
   12.61 +static int pre_print(void)
   12.62 +{
   12.63 +	int top = win_height - line_height;
   12.64 +
   12.65 +	if(init() == -1) {
   12.66 +		return -1;
   12.67 +	}
   12.68 +
   12.69 +	gl_matrix_mode(GL_TEXTURE);
   12.70 +	gl_push_matrix();
   12.71 +	gl_load_identity();
   12.72 +	gl_matrix_mode(GL_PROJECTION);
   12.73 +	gl_push_matrix();
   12.74 +	gl_load_identity();
   12.75 +	gl_ortho(0, win_width, 0, win_height, -1, 1);
   12.76 +	gl_matrix_mode(GL_MODELVIEW);
   12.77 +	gl_push_matrix();
   12.78 +	gl_load_identity();
   12.79 +	gl_translatef(cpos[0] * dtx_glyph_width(' '), top - cpos[1] * line_height, 0);
   12.80 +
   12.81 +	glUseProgram(prog);
   12.82 +	glUniform4fv(uloc_color, 1, color);
   12.83 +
   12.84 +	gl_apply_xform(prog);
   12.85 +
   12.86 +	dtx_use_font(font, FONT_SIZE);
   12.87 +	dtx_vertex_attribs(aloc_vertex, aloc_texcoords);
   12.88 +	return 0;
   12.89 +}
   12.90 +
   12.91 +static void post_print(void)
   12.92 +{
   12.93 +	gl_matrix_mode(GL_TEXTURE);
   12.94 +	gl_pop_matrix();
   12.95 +	gl_matrix_mode(GL_PROJECTION);
   12.96 +	gl_pop_matrix();
   12.97 +	gl_matrix_mode(GL_MODELVIEW);
   12.98 +	gl_pop_matrix();
   12.99 +}
  12.100 +
  12.101 +void text_print(const char *s)
  12.102 +{
  12.103 +	if(pre_print() == -1) {
  12.104 +		return;
  12.105 +	}
  12.106 +
  12.107 +	dtx_string(s);
  12.108 +
  12.109 +	post_print();
  12.110 +}
  12.111 +
  12.112 +void text_printf(const char *fmt, ...)
  12.113 +{
  12.114 +	va_list ap;
  12.115 +	int buf_size;
  12.116 +	char *buf, tmp;
  12.117 +
  12.118 +	if(pre_print() == -1) {
  12.119 +		return;
  12.120 +	}
  12.121 +
  12.122 +	va_start(ap, fmt);
  12.123 +	buf_size = vsnprintf(&tmp, 0, fmt, ap);
  12.124 +	va_end(ap);
  12.125 +
  12.126 +	if(buf_size == -1) {
  12.127 +		buf_size = 512;
  12.128 +	}
  12.129 +
  12.130 +	buf = alloca(buf_size + 1);
  12.131 +	va_start(ap, fmt);
  12.132 +	vsnprintf(buf, buf_size + 1, fmt, ap);
  12.133 +	va_end(ap);
  12.134 +
  12.135 +	dtx_string(buf);
  12.136 +
  12.137 +	post_print();
  12.138 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/text.h	Sat Jun 13 05:32:07 2015 +0300
    13.3 @@ -0,0 +1,17 @@
    13.4 +#ifndef TEXT_H_
    13.5 +#define TEXT_H_
    13.6 +
    13.7 +#ifdef __cplusplus
    13.8 +extern "C" {
    13.9 +#endif
   13.10 +
   13.11 +void text_color(float r, float g, float b, float a);
   13.12 +void text_position(int x, int y);
   13.13 +void text_print(const char *s);
   13.14 +void text_printf(const char *fmt, ...);
   13.15 +
   13.16 +#ifdef __cplusplus
   13.17 +}
   13.18 +#endif
   13.19 +
   13.20 +#endif	/* TEXT_H_ */