commit 4e595e8ab657f1831215cab2df3135ec9885d359
parent 111b043430b7e6d430bce6620baf55450505993c
Author: Kyle Milz <kyle@0x30.net>
Date: Mon, 9 Oct 2017 23:42:50 -0600
bin: wrap and rename demo-glstate
Diffstat:
9 files changed, 186 insertions(+), 236 deletions(-)
diff --git a/bin/Jamfile b/bin/Jamfile
@@ -57,12 +57,12 @@ Main citrun_inst : $(INST_SRCS) ;
#
GL_SRCS =
demo-atlas.cc
- demo-glstate.cc
demo-shader.cc
gl_buffer.cc
gl_font.cc
gl_main.cc
gl_runtime.cc
+ gl_state.cc
gl_view.cc
matrix4x4.c ;
diff --git a/bin/demo-glstate.cc b/bin/demo-glstate.cc
@@ -1,152 +0,0 @@
-/*
- * 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, Wojciech Baranowski
- */
-
-#include "demo-glstate.h"
-
-
-struct demo_glstate_t {
- unsigned int refcount;
-
- GLuint program;
- demo_atlas_t *atlas;
-
- /* Uniforms */
- double u_debug;
- double u_contrast;
- double u_gamma_adjust;
- double u_outline;
- double u_outline_thickness;
- double u_boldness;
-};
-
-demo_glstate_t *
-demo_glstate_create (void)
-{
- TRACE();
-
- demo_glstate_t *st = (demo_glstate_t *) calloc (1, sizeof (demo_glstate_t));
- st->refcount = 1;
-
- st->program = demo_shader_create_program ();
- st->atlas = demo_atlas_create (2048, 1024, 64, 8);
-
- st->u_debug = false;
- st->u_contrast = 1.0;
- st->u_gamma_adjust = 1.0;
- st->u_outline = false;
- st->u_outline_thickness = 1.0;
- st->u_boldness = 0.;
-
- return st;
-}
-
-demo_glstate_t *
-demo_glstate_reference (demo_glstate_t *st)
-{
- if (st) st->refcount++;
- return st;
-}
-
-void
-demo_glstate_destroy (demo_glstate_t *st)
-{
- if (!st || --st->refcount)
- return;
-
- demo_atlas_destroy (st->atlas);
- glDeleteProgram (st->program);
-
- free (st);
-}
-
-
-static void
-set_uniform (GLuint program, const char *name, double *p, double value)
-{
- *p = value;
- glUniform1f (glGetUniformLocation (program, name), value);
- LOGI ("Setting %s to %g\n", name + 2, value);
-}
-
-#define SET_UNIFORM(name, value) set_uniform (st->program, #name, &st->name, value)
-
-void
-demo_glstate_setup (demo_glstate_t *st)
-{
- glUseProgram (st->program);
-
- demo_atlas_set_uniforms (st->atlas);
-
- SET_UNIFORM (u_debug, st->u_debug);
- SET_UNIFORM (u_contrast, st->u_contrast);
- SET_UNIFORM (u_gamma_adjust, st->u_gamma_adjust);
- SET_UNIFORM (u_outline, st->u_outline);
- SET_UNIFORM (u_outline_thickness, st->u_outline_thickness);
- SET_UNIFORM (u_boldness, st->u_boldness);
-
- glEnable (GL_BLEND);
- glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-}
-
-demo_atlas_t *
-demo_glstate_get_atlas (demo_glstate_t *st)
-{
- return st->atlas;
-}
-
-void
-demo_glstate_scale_gamma_adjust (demo_glstate_t *st, double factor)
-{
- SET_UNIFORM (u_gamma_adjust, clamp (st->u_gamma_adjust * factor, .1, 10.));
-}
-
-void
-demo_glstate_scale_contrast (demo_glstate_t *st, double factor)
-{
- SET_UNIFORM (u_contrast, clamp (st->u_contrast * factor, .1, 10.));
-}
-
-void
-demo_glstate_toggle_debug (demo_glstate_t *st)
-{
- SET_UNIFORM (u_debug, 1 - st->u_debug);
-}
-
-void
-demo_glstate_set_matrix (demo_glstate_t *st, float mat[16])
-{
- glUniformMatrix4fv (glGetUniformLocation (st->program, "u_matViewProjection"), 1, GL_FALSE, mat);
-}
-
-void
-demo_glstate_toggle_outline (demo_glstate_t *st)
-{
- SET_UNIFORM (u_outline, 1 - st->u_outline);
-}
-
-void
-demo_glstate_scale_outline_thickness (demo_glstate_t *st, double factor)
-{
- SET_UNIFORM (u_outline_thickness, clamp (st->u_outline_thickness * factor, .5, 3.));
-}
-
-void
-demo_glstate_adjust_boldness (demo_glstate_t *st, double adjustment)
-{
- SET_UNIFORM (u_boldness, clamp (st->u_boldness + adjustment, -.2, .7));
-}
diff --git a/bin/demo-glstate.h b/bin/demo-glstate.h
@@ -1,68 +0,0 @@
-/*
- * 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
- */
-
-#ifndef DEMO_GLSTATE_H
-#define DEMO_GLSTATE_H
-
-#include "demo-common.h"
-#include "gl_buffer.h"
-
-#include "demo-atlas.h"
-#include "demo-shader.h"
-
-typedef struct demo_glstate_t demo_glstate_t;
-
-demo_glstate_t *
-demo_glstate_create (void);
-
-demo_glstate_t *
-demo_glstate_reference (demo_glstate_t *st);
-
-void
-demo_glstate_destroy (demo_glstate_t *st);
-
-
-void
-demo_glstate_setup (demo_glstate_t *st);
-
-demo_atlas_t *
-demo_glstate_get_atlas (demo_glstate_t *st);
-
-void
-demo_glstate_scale_gamma_adjust (demo_glstate_t *st, double factor);
-
-void
-demo_glstate_scale_contrast (demo_glstate_t *st, double factor);
-
-void
-demo_glstate_toggle_debug (demo_glstate_t *st);
-
-void
-demo_glstate_set_matrix (demo_glstate_t *st, float mat[16]);
-
-void
-demo_glstate_toggle_outline (demo_glstate_t *st);
-
-void
-demo_glstate_scale_outline_thickness (demo_glstate_t *st, double factor);
-
-void
-demo_glstate_adjust_boldness (demo_glstate_t *st, double adjustment);
-
-
-#endif /* DEMO_GLSTATE_H */
diff --git a/bin/gl_main.cc b/bin/gl_main.cc
@@ -17,9 +17,8 @@
citrun::gl_main::gl_main()
{
- st = demo_glstate_create();
static_vu = new View(st);
- font = new citrun::gl_font(FONT_PATH, demo_glstate_get_atlas(st));
+ font = new citrun::gl_font(FONT_PATH, st.get_atlas());
static_vu->setup();
glyphy_point_t top_left = { 0, 0 };
diff --git a/bin/gl_main.h b/bin/gl_main.h
@@ -3,6 +3,7 @@
#include "gl_buffer.h" // citrun::gl_buffer
#include "gl_font.h" // citrun::gl_font
#include "gl_runtime.h" // citrun::gl_procfile, citrun::process_dir
+#include "gl_state.h" // citrun::gl_state
#include "gl_view.h"
@@ -12,9 +13,9 @@ class gl_main {
citrun::gl_buffer buffer;
citrun::gl_font *font;
citrun::process_dir m_pdir;
+ citrun::gl_state st;
std::vector<citrun::gl_procfile> drawables;
- demo_glstate_t *st;
glyphy_extents_t extents;
View *static_vu;
public:
diff --git a/bin/gl_state.cc b/bin/gl_state.cc
@@ -0,0 +1,114 @@
+/*
+ * 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, Wojciech Baranowski
+ */
+#include "gl_state.h" // citrun::gl_state
+
+
+citrun::gl_state::gl_state() :
+ program(demo_shader_create_program()),
+ atlas(demo_atlas_create(2048, 1024, 64, 8)),
+ u_debug(false),
+ u_contrast(1.0),
+ u_gamma_adjust(1.0),
+ u_outline(false),
+ u_outline_thickness(1.0),
+ u_boldness(0.)
+{
+ TRACE();
+}
+
+citrun::gl_state::~gl_state()
+{
+ demo_atlas_destroy(atlas);
+ glDeleteProgram(program);
+}
+
+static void
+set_uniform(GLuint program, const char *name, double *p, double value)
+{
+ *p = value;
+ glUniform1f(glGetUniformLocation(program, name), value);
+ LOGI("Setting %s to %g\n", name + 2, value);
+}
+
+#define SET_UNIFORM(name, value) set_uniform (program, #name, &name, value)
+
+void
+citrun::gl_state::setup()
+{
+ glUseProgram(program);
+
+ demo_atlas_set_uniforms(atlas);
+
+ SET_UNIFORM(u_debug, u_debug);
+ SET_UNIFORM(u_contrast, u_contrast);
+ SET_UNIFORM(u_gamma_adjust, u_gamma_adjust);
+ SET_UNIFORM(u_outline, u_outline);
+ SET_UNIFORM(u_outline_thickness, u_outline_thickness);
+ SET_UNIFORM(u_boldness, u_boldness);
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+}
+
+demo_atlas_t *
+citrun::gl_state::get_atlas()
+{
+ return atlas;
+}
+
+void
+citrun::gl_state::scale_gamma_adjust(double factor)
+{
+ SET_UNIFORM(u_gamma_adjust, clamp(u_gamma_adjust * factor, .1, 10.));
+}
+
+void
+citrun::gl_state::scale_contrast(double factor)
+{
+ SET_UNIFORM(u_contrast, clamp(u_contrast * factor, .1, 10.));
+}
+
+void
+citrun::gl_state::toggle_debug()
+{
+ SET_UNIFORM(u_debug, 1 - u_debug);
+}
+
+void
+citrun::gl_state::set_matrix(float mat[16])
+{
+ glUniformMatrix4fv(glGetUniformLocation(program, "u_matViewProjection"), 1, GL_FALSE, mat);
+}
+
+void
+citrun::gl_state::toggle_outline()
+{
+ SET_UNIFORM(u_outline, 1 - u_outline);
+}
+
+void
+citrun::gl_state::scale_outline_thickness(double factor)
+{
+ SET_UNIFORM(u_outline_thickness, clamp(u_outline_thickness * factor, .5, 3.));
+}
+
+void
+citrun::gl_state::adjust_boldness(double adjustment)
+{
+ SET_UNIFORM (u_boldness, clamp (u_boldness + adjustment, -.2, .7));
+}
diff --git a/bin/gl_state.h b/bin/gl_state.h
@@ -0,0 +1,56 @@
+/*
+ * 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
+ */
+#ifndef DEMO_GLSTATE_H
+#define DEMO_GLSTATE_H
+
+#include "demo-common.h"
+#include "demo-atlas.h"
+#include "demo-shader.h"
+
+
+namespace citrun {
+
+class gl_state {
+ GLuint program;
+ demo_atlas_t *atlas;
+
+ /* Uniforms */
+ double u_debug;
+ double u_contrast;
+ double u_gamma_adjust;
+ double u_outline;
+ double u_outline_thickness;
+ double u_boldness;
+public:
+ gl_state();
+ ~gl_state();
+
+ void setup();
+ demo_atlas_t *get_atlas();
+ void scale_gamma_adjust(double);
+ void scale_contrast(double);
+ void toggle_debug();
+ void set_matrix(float[16]);
+ void toggle_outline();
+ void scale_outline_thickness(double);
+ void adjust_boldness(double);
+};
+
+} // namespace citrun
+
+#endif /* DEMO_GLSTATE_H */
diff --git a/bin/gl_view.cc b/bin/gl_view.cc
@@ -25,7 +25,7 @@ extern "C" {
}
-View::View(demo_glstate_t *st) :
+View::View(citrun::gl_state &st) :
refcount(1),
st(st),
fullscreen(false)
@@ -56,13 +56,13 @@ View::reset()
void
View::scale_gamma_adjust(double factor)
{
- demo_glstate_scale_gamma_adjust(st, factor);
+ st.scale_gamma_adjust(factor);
}
void
View::scale_contrast(double factor)
{
- demo_glstate_scale_contrast(st, factor);
+ st.scale_contrast(factor);
}
void
@@ -74,19 +74,19 @@ View::scale_perspective(double factor)
void
View::toggle_outline()
{
- demo_glstate_toggle_outline(st);
+ st.toggle_outline();
}
void
View::scale_outline_thickness(double factor)
{
- demo_glstate_scale_outline_thickness(st, factor);
+ st.scale_outline_thickness(factor);
}
void
View::adjust_boldness(double factor)
{
- demo_glstate_adjust_boldness(st, factor);
+ st.adjust_boldness(factor);
}
void
@@ -162,7 +162,7 @@ View::toggle_fullscreen()
void
View::toggle_debug()
{
- demo_glstate_toggle_debug(st);
+ st.toggle_debug();
}
@@ -431,7 +431,7 @@ View::display(glyphy_extents_t const &extents)
-(extents.max_x + extents.min_x) / 2.,
-(extents.max_y + extents.min_y) / 2., 0);
- demo_glstate_set_matrix(st, mat);
+ st.set_matrix(mat);
glClearColor (1, 1, 1, 1);
glClear (GL_COLOR_BUFFER_BIT);
@@ -440,5 +440,5 @@ View::display(glyphy_extents_t const &extents)
void
View::setup()
{
- demo_glstate_setup(st);
+ st.setup();
}
diff --git a/bin/gl_view.h b/bin/gl_view.h
@@ -2,12 +2,12 @@
#define VIEW_H
#include "demo-common.h"
-#include "demo-glstate.h"
+#include "gl_state.h" // citrun::gl_state
#include <GLFW/glfw3.h>
class View {
public:
- View(demo_glstate_t *);
+ View(citrun::gl_state &);
~View();
void reset();
@@ -43,7 +43,7 @@ private:
unsigned int refcount;
- demo_glstate_t *st;
+ citrun::gl_state &st;
/* Output */
glyphy_bool_t fullscreen;