Vinicius Teshima 1 жил өмнө
parent
commit
527d38b54b

+ 0 - 45
shaders/free_font.frag

@@ -1,45 +0,0 @@
-#version 330 core
-
-#define FONT_WIDTH 128
-#define FONT_HEIGHT 64
-#define FONT_COLS 18
-#define FONT_ROWS 7
-#define FONT_CH_W (FONT_WIDTH  / FONT_COLS)
-#define FONT_CH_H (FONT_HEIGHT / FONT_ROWS)
-#define FONT_CH_W_UV (float(FONT_CH_W) / float(FONT_WIDTH))
-#define FONT_CH_H_UV (float(FONT_CH_H) / float(FONT_HEIGHT))
-
-uniform sampler2D font;
-
-uniform float time;
-uniform vec2 resolution;
-
-in vec2 uv;
-
-in vec2 glyph_uv_pos;
-in vec2 glyph_uv_size;
-in vec4 glyph_fg_color;
-in vec4 glyph_bg_color;
-
-float map01(float x)
-{
-	return (x + 1) / 2;
-}
-
-vec3 hsl2rgb( in vec3 c )
-{
-	vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0,
-			  0.0, 1.0 );
-
-	return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
-}
-
-void main() {
-	// vec2 t = glyph_uv_pos + glyph_uv_size * uv;
-	// gl_FragColor = vec4(texture(font, t).x, 0, 0, 1);
-	vec2 t = glyph_uv_pos + glyph_uv_size * uv;
-	vec4 tc = texture(font, t);
-	vec2 frag_uv = gl_FragCoord.xy / resolution;
-	vec4 rainbow = vec4(hsl2rgb(vec3((time + frag_uv.x + frag_uv.y), 0.5, 0.5)), 1.0);
-	gl_FragColor = glyph_bg_color * (1.0 - tc.x) + tc.x * glyph_fg_color * rainbow;
-}

+ 0 - 43
shaders/free_font.vert

@@ -1,43 +0,0 @@
-#version 330 core
-
-#define FONT_WIDTH 128
-#define FONT_HEIGHT 64
-#define FONT_COLS 18
-#define FONT_ROWS 7
-#define FONT_CH_W (FONT_WIDTH / FONT_COLS)
-#define FONT_CH_H (FONT_HEIGHT / FONT_ROWS)
-
-uniform vec2 resolution;
-uniform float time;
-uniform vec2 camera;
-
-layout(location = 0) in vec2 pos;
-layout(location = 1) in vec2 size;
-layout(location = 2) in vec2 uv_pos;
-layout(location = 3) in vec2 uv_size;
-layout(location = 4) in vec4 fg_color;
-layout(location = 5) in vec4 bg_color;
-
-out vec2 uv;
-out vec2 glyph_uv_pos;
-out vec2 glyph_uv_size;
-out vec4 glyph_fg_color;
-out vec4 glyph_bg_color;
-
-vec2 project_point(vec2 point)
-{
-  return (2.0 * (point - camera)) / resolution;
-}
-
-void main()
-{
-	uv = vec2(float(gl_VertexID & 1),
-		  float((gl_VertexID >> 1) & 1));
-
-	gl_Position = vec4(project_point(uv * size + pos), 0.0, 1.0);
-
-	glyph_uv_pos = uv_pos;
-	glyph_uv_size = uv_size;
-	glyph_fg_color = fg_color;
-	glyph_bg_color = bg_color;
-}

+ 47 - 53
src/free_glyph.h

@@ -103,59 +103,53 @@ fga_calc_col_buffer(const struct free_glyph_atlas *fga,
 					  app->buf.cur);
 }
 
-/* struct free_glyph_atlas */
-/* fga_calc(struct free_glyph_atlas fga, struct app app, */
-/* 		       const char *cstr, size_t cstr_size, struct vec2f pos, */
-/* 		       struct vec4f fg_color, struct vec4f bg_color) */
-/* { */
-/* 	struct vec2f pen = pos; */
-/* 	struct vec2f dim = vec2f_from_ui(fga->atlas_dim); */
-/* 	for ( size_t i = 0; i < cstr_size; ++i ) { */
-/* 		char c = cstr[i]; */
-/* 		if ( c == '\n' ) { */
-/* 			pen.x = 0; */
-/* 			pen.y -= dim.y; */
-/* 			continue; */
-/* 		} */
-/* 		struct glyph_info gi = fga->g_infos[(int64_t)c]; */
-/* 		if ( c == '\t' ) { */
-/* 			c = ' '; */
-/* 			gi = fga->g_infos[(int64_t)c]; */
-/* 			gi.a.x *= app.cfg.tab_size; */
-/* 		} */
-
-/* 		float w = (float)gi.b.w; */
-/* 		float h = (float)gi.b.h; */
-
-/* 		struct free_glyph gly = { */
-/* 			.pos = { */
-/* 				pen.x + (float) gi.b.l, */
-/* 				pen.y + (float) gi.b.t */
-/* 			}, */
-/* 			.size = {w, -h}, */
-/* 			.uv_pos = { gi.tx, 0.0f }, */
-/* 			.uv_size = { w / dim.x, h / dim.y }, */
-/* 			.fg_color = fg_color, */
-/* 			.bg_color = bg_color, */
-/* 		}; */
-
-/* 		pen.x += (float) gi.a.x; */
-/* 		pen.y += (float) gi.a.y; */
-
-/* 		DA_APPEND(fga->glyphs, gly); */
-/* 	} */
-/* 	return fga; */
-/* } */
-
-/* struct free_glyph_atlas */
-/* fga_calc_buffer(struct free_glyph_atlas fga, */
-/* 			      struct app app, */
-/* 			      struct vec2f size) */
-/* { */
-/* 	return fga_calc(fga, app, */
-/* 				      app.buf.data.items, app.buf.data.size, */
-/* 				      size, vec4fs(1), vec4fs(0)); */
-/* } */
+void
+fga_calc(struct free_glyph_atlas *fga, struct simple_render *sr,
+	 const struct app *app, const char *cstr, size_t cstr_size,
+	 struct vec2f pos, struct vec4f fg_color, struct vec4f bg_color)
+{
+	(void) sr;
+	(void) fg_color;
+	(void) bg_color;
+	struct vec2f pen = pos;
+	struct vec2f dim = vec2f_from_ui(fga->atlas_dim);
+	for ( size_t i = 0; i < cstr_size; ++i ) {
+		char c = cstr[i];
+		if ( c == '\n' ) {
+			pen.x = 0;
+			pen.y -= dim.y;
+			continue;
+		}
+		struct glyph_info gi = fga->g_infos[(int64_t)c];
+		if ( c == '\t' ) {
+			c = ' ';
+			gi = fga->g_infos[(int64_t)c];
+			gi.a.x *= app->cfg.tab_size;
+		}
+
+		float w = (float)gi.b.w;
+		float h = (float)gi.b.h;
+
+		sr_image_rect(sr,
+			      vec2f(pen.x + (float) gi.b.l,
+				    pen.y + (float) gi.b.t), /* pos */
+			      vec2f(w, -h),	      /* size */
+			      vec2f(gi.tx, 0.f),      /* uv_pos */
+			      vec2f(w/dim.x, h/dim.y) /* uv_size */
+			);
+
+		pen.x += (float) gi.a.x;
+		pen.y += (float) gi.a.y;
+	}
+}
+
+void
+fga_calc_buffer(struct free_glyph_atlas *fga, struct simple_render *sr,
+		const struct app *app, struct vec2f size)
+{
+	fga_calc(fga, sr, app, app->buf.data.items, app->buf.data.size,
+		 size, vec4fs(1), vec4fs(0));
+}
 
 void
 fga_load_texture_atlas_or_exit(struct free_glyph_atlas *fga,

+ 26 - 43
src/main.c

@@ -47,8 +47,6 @@ main(int32_t argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	/* const char *ffont_path = "./fonts/VictorMono-Regular.ttf"; */
-	/* const char *ffont_path = "./fonts/unifont-15.1.05.otf"; */
 	const char *ffont_path = "./fonts/ComicMono-Regular.ttf";
 
 	FT_Face face;
@@ -191,8 +189,6 @@ void
 render_buffer_into_fgr(struct free_glyph_atlas *fga, struct simple_render *sr,
 		       struct app *app)
 {
-	(void) fga;
-	(void) app;
 	uint32_t ticks = SDL_GetTicks();
 	float cur_row = (float)(buffer_calc_cur_row(&app->buf)
 				* fga->atlas_dim.y);
@@ -207,48 +203,35 @@ render_buffer_into_fgr(struct free_glyph_atlas *fga, struct simple_render *sr,
 	app->buf.cam.vel = cvel;
 
 	float time = ((float) ticks) / 1000.0f;
-	/* free_glyph_render_use(*fgr); */
-	/* { */
-	/* 	glUniform1f(fgr->uniforms.time, time); */
-	/* 	glUniform2f(fgr->uniforms.camera, */
-	/* 		    (float)cpos.x, */
-	/* 		    (float)cpos.y); */
-	/* 	glUniform2f(fgr->uniforms.resolution, */
-	/* 		    (float) app->win.w, */
-	/* 		    (float) app->win.h); */
-
-	/* 	fgr->glyphs.size = 0; */
-	/* 	*fgr = free_glyph_render_calc_buffer(*fgr, *app, vec2fs(0)); */
-	/* 	free_glyph_render_sync(*fgr); */
-	/* 	glDrawArraysInstanced(GL_TRIANGLE_STRIP, */
-	/* 			      0, 4, */
-	/* 			      (int32_t) fgr->glyphs.size); */
-	/* } */
 
 	sr_use(sr);
-	sr_set_shader(sr, SIMPLE_SHADER_RAINBOW);
 	{
-		glUniform1f(sr_get_uniform(sr, "time"), time);
-		glUniform2f(sr_get_uniform(sr, "camera"),
-			    (float)cpos.x, (float)cpos.y);
-		glUniform2f(sr_get_uniform(sr, "resolution"),
-			    (float) app->win.w, (float) app->win.h);
-		/* struct vec2f uv = vec2fs(0); */
-		sr->vertexs.size = 0;
-		/* sr_triangle( */
-		/* 	sr, */
-		/* 	vec2f(-50.f,-50.f), vec2f(50.f,-50.f), vec2f(0.0f,50.f), */
-		/* 	vec4f(1,0,0,1), vec4f(0,1,0,1), vec4f(0,0,1,1), */
-		/* 	X3_ARGS(uv)); */
-
-		#if 1
-		uint32_t t = ticks - app->last_press;
-		if ( t < 500 || (t/1000)%2 != 0 ) {
-			sr_solid_rect(sr, vec2_to_f(cur_pos), vec2f(3, 32), vec4fs(1));
+		sr_set_shader(sr, SIMPLE_SHADER_RAINBOW);
+		{
+			glUniform1f(sr_get_uniform(sr, "time"), time);
+			glUniform2f(sr_get_uniform(sr, "camera"),
+				    (float)cpos.x, (float)cpos.y);
+			glUniform2f(sr_get_uniform(sr, "resolution"),
+				    (float) app->win.w, (float) app->win.h);
+			fga_calc_buffer(fga, sr, app, vec2fs(0));
+			sr_draw(sr);
+			sr->vertexs.size = 0;
+		}
+		sr_set_shader(sr, SIMPLE_SHADER_COLOR);
+		{
+			glUniform1f(sr_get_uniform(sr, "time"), time);
+			glUniform2f(sr_get_uniform(sr, "camera"),
+				    (float)cpos.x, (float)cpos.y);
+			glUniform2f(sr_get_uniform(sr, "resolution"),
+				    (float) app->win.w, (float) app->win.h);
+			fga_calc_buffer(fga, sr, app, vec2fs(0));
+			uint32_t t = ticks - app->last_press;
+			if ( t < 500 || (t/1000)%2 != 0 ) {
+				sr_solid_rect(sr, vec2_to_f(cur_pos),
+					      vec2f(3, 32), vec4fs(1));
+			}
+			sr_draw(sr);
+			sr->vertexs.size = 0;
 		}
-		#else
-		sr_image_rect(sr, vec2_to_f(cur_pos), vec2f(3, 32), vec4fs(1));
-		#endif
-		sr_draw(sr);
 	}
 }

+ 10 - 4
src/simple_render.h

@@ -70,8 +70,9 @@ void sr_quad(struct simple_render *sr,
 	     struct vec4f c0, struct vec4f c1, struct vec4f c2, struct vec4f c3,
 	     struct vec2f uv0, struct vec2f uv1,
 	     struct vec2f uv2, struct vec2f uv3);
-void sr_image_rect(struct simple_render *sr, struct vec2f pos,
-		   struct vec2f size, struct vec4f color);
+void sr_image_rect(struct simple_render *sr,
+		   struct vec2f pos, struct vec2f size,
+		   struct vec2f uv_pos, struct vec2f uv_size);
 void sr_solid_rect(struct simple_render *sr, struct vec2f pos,
 		   struct vec2f size, struct vec4f color);
 void sr_draw(const struct simple_render *sr);
@@ -184,7 +185,9 @@ int32_t
 sr_set_uniform(struct simple_render *sr, const char *uniform_name)
 {
 	int32_t uni = get_uniform(sr->progs[sr->cur_shader], uniform_name);
+	#if 0
 	printf("%s: Got Uniform %s -> %d\n", __func__, uniform_name, uni);
+	#endif
 	HT_SET(sr->uniforms, uniform_name, strlen(uniform_name), uni);
 	return uni;
 }
@@ -244,13 +247,16 @@ sr_quad(struct simple_render *sr,
 
 void
 sr_image_rect(struct simple_render *sr, struct vec2f pos, struct vec2f size,
-	      struct vec4f color)
+	      struct vec2f uv_pos, struct vec2f uv_size)
 {
+	struct vec4f color = vec4fs(0);
 	sr_quad(sr,
 		pos, vec2f_add(pos, vec2f(size.x, 0)),
 		vec2f_add(pos, vec2f(0, size.y)), vec2f_add(pos, size),
 		X4_ARGS(color),
-		vec2f(0, 0), vec2f(1, 0), vec2f(0, 1), vec2f(1, 1));
+		uv_pos, vec2f_add(uv_pos, vec2f(uv_size.x, 0)),
+		vec2f_add(uv_pos,vec2f(0, uv_size.y)),
+		vec2f_add(uv_pos, uv_size));
 }
 
 void