citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

gl_state.cc (2625B)


      1 /*
      2  * Copyright 2012 Google, Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  *
     16  * Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski
     17  */
     18 #include "gl_state.h"		// citrun::gl_state
     19 
     20 
     21 citrun::gl_state::gl_state() :
     22 	program(shader.create_program()),
     23 	atlas(2048, 1024, 64, 8),
     24 	u_debug(false),
     25 	u_contrast(1.0),
     26 	u_gamma_adjust(1.0),
     27 	u_outline(false),
     28 	u_outline_thickness(1.0),
     29 	u_boldness(0.)
     30 {
     31 	TRACE();
     32 }
     33 
     34 citrun::gl_state::~gl_state()
     35 {
     36 	glDeleteProgram(program);
     37 }
     38 
     39 static void
     40 set_uniform(GLuint program, const char *name, double *p, double value)
     41 {
     42 	*p = value;
     43 	glUniform1f(glGetUniformLocation(program, name), value);
     44 	LOGI("Setting %s to %g\n", name + 2, value);
     45 }
     46 
     47 #define SET_UNIFORM(name, value) set_uniform (program, #name, &name, value)
     48 
     49 void
     50 citrun::gl_state::setup()
     51 {
     52 	glUseProgram(program);
     53 
     54 	atlas.set_uniforms();
     55 
     56 	SET_UNIFORM(u_debug, u_debug);
     57 	SET_UNIFORM(u_contrast, u_contrast);
     58 	SET_UNIFORM(u_gamma_adjust, u_gamma_adjust);
     59 	SET_UNIFORM(u_outline, u_outline);
     60 	SET_UNIFORM(u_outline_thickness, u_outline_thickness);
     61 	SET_UNIFORM(u_boldness, u_boldness);
     62 
     63 	glEnable(GL_BLEND);
     64 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     65 }
     66 
     67 citrun::gl_atlas &
     68 citrun::gl_state::get_atlas()
     69 {
     70 	return atlas;
     71 }
     72 
     73 void
     74 citrun::gl_state::scale_gamma_adjust(double factor)
     75 {
     76 	SET_UNIFORM(u_gamma_adjust, clamp(u_gamma_adjust * factor, .1, 10.));
     77 }
     78 
     79 void
     80 citrun::gl_state::scale_contrast(double factor)
     81 {
     82 	SET_UNIFORM(u_contrast, clamp(u_contrast * factor, .1, 10.));
     83 }
     84 
     85 void
     86 citrun::gl_state::toggle_debug()
     87 {
     88 	SET_UNIFORM(u_debug, 1 - u_debug);
     89 }
     90 
     91 void
     92 citrun::gl_state::set_matrix(float mat[16])
     93 {
     94 	glUniformMatrix4fv(glGetUniformLocation(program, "u_matViewProjection"), 1, GL_FALSE, mat);
     95 }
     96 
     97 void
     98 citrun::gl_state::toggle_outline()
     99 {
    100 	SET_UNIFORM(u_outline, 1 - u_outline);
    101 }
    102 
    103 void
    104 citrun::gl_state::scale_outline_thickness(double factor)
    105 {
    106 	SET_UNIFORM(u_outline_thickness, clamp(u_outline_thickness * factor, .5, 3.));
    107 }
    108 
    109 void
    110 citrun::gl_state::adjust_boldness(double adjustment)
    111 {
    112 	SET_UNIFORM (u_boldness, clamp (u_boldness + adjustment, -.2, .7));
    113 }