added '-m' option, added radial module

This commit is contained in:
Jarcode
2017-12-03 01:03:53 -08:00
parent bb9fbfc4fc
commit 058d34ae52
4 changed files with 118 additions and 15 deletions

23
glava.c
View File

@@ -141,22 +141,25 @@ static const char* help_str =
"Opens a window with an OpenGL context to draw an audio visualizer.\n"
"\n"
"Available arguments:\n"
"-h, --help show this help and exit\n"
"-v, --verbose enables printing of detailed information about execution\n"
"-e, --entry=NAME specifies the name of the file to look for when loading shaders,\n"
" by default this is \"rc.glsl\".\n"
"-C, --copy-config creates copies and symbolic links in the user configuration\n"
" directory for glava, copying any files in the root directory\n"
" of the installed shader directory, and linking any modules.\n"
"-h, --help show this help and exit\n"
"-v, --verbose enables printing of detailed information about execution\n"
"-m, --force-mod=NAME forces the specified module to load instead, ignoring any\n"
" `#request mod` instances in the entry point.\n"
"-e, --entry=NAME specifies the name of the file to look for when loading shaders,\n"
" by default this is \"rc.glsl\".\n"
"-C, --copy-config creates copies and symbolic links in the user configuration\n"
" directory for glava, copying any files in the root directory\n"
" of the installed shader directory, and linking any modules.\n"
"\n"
"GLava (glava) " GLAVA_VERSION "\n"
" -- Copyright (C) 2017 Levi Webb\n";
static const char* opt_str = "hve:C";
static const char* opt_str = "hve:Cm:";
static struct option p_opts[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"entry", required_argument, 0, 'e'},
{"force-mod", required_argument, 0, 'm'},
{"copy-config", no_argument, 0, 'C'},
{0, 0, 0, 0 }
};
@@ -167,6 +170,7 @@ int main(int argc, char** argv) {
const char* install_path = SHADER_INSTALL_PATH;
const char* user_path = SHADER_USER_PATH;
const char* entry = "rc.glsl";
const char* force = NULL;
const char* system_shader_paths[] = { user_path, install_path, NULL };
bool verbose = false;
bool copy_mode = false;
@@ -177,6 +181,7 @@ int main(int argc, char** argv) {
case 'v': verbose = true; break;
case 'C': copy_mode = true; break;
case 'e': entry = optarg; break;
case 'm': force = optarg; break;
case '?': exit(EXIT_FAILURE); break;
default:
case 'h':
@@ -190,7 +195,7 @@ int main(int argc, char** argv) {
exit(EXIT_SUCCESS);
}
renderer* r = rd_new(system_shader_paths, entry);
renderer* r = rd_new(system_shader_paths, entry, force);
float b0[r->bufsize_request], b1[r->bufsize_request];
size_t t;

View File

@@ -587,7 +587,7 @@ static struct gl_bind_src* lookup_bind_src(const char* str) {
return NULL;
}
struct renderer* rd_new(const char** paths, const char* entry) {
struct renderer* rd_new(const char** paths, const char* entry, const char* force_mod) {
renderer* r = malloc(sizeof(struct renderer));
*r = (struct renderer) {
@@ -666,7 +666,8 @@ struct renderer* rd_new(const char** paths, const char* entry) {
int shader_version = 330;
char* module = NULL, * xwintype = NULL;
const char* module = force_mod;
char* xwintype = NULL;
bool loading_module = true;
struct gl_sfbo* current = NULL;
size_t t_count = 0;
@@ -685,10 +686,11 @@ struct renderer* rd_new(const char** paths, const char* entry) {
{
.name = "mod", .fmt = "s",
.handler = RHANDLER(name, args, {
if (loading_module) {
if (loading_module && !force_mod) {
size_t len = strlen((char*) args[0]);
module = malloc(sizeof(char) * (strlen((char*) args[0]) + 1));
strncpy(module, (char*) args[0], len + 1);
char* str = malloc(sizeof(char) * (strlen((char*) args[0]) + 1));
strncpy(str, (char*) args[0], len + 1);
module = str;
}
})
},
@@ -878,7 +880,7 @@ struct renderer* rd_new(const char** paths, const char* entry) {
printf("Loading module: '%s'\n", module);
free(module);
if (!force_mod) free((void*) module);
loading_module = false;
/* Iterate through shader passes in the shader directory and build textures, framebuffers, and

21
shaders/radial.glsl Normal file
View File

@@ -0,0 +1,21 @@
/* center radius (pixels) */
#define C_RADIUS 64
/* center line thickness (pixels) */
#define C_LINE 1.5
/* outline color */
#define OUTLINE vec4(0.20, 0.20, 0.20, 1)
/* number of bars (use even values for best results) */
#define NBARS 92
/* width (in pixels) of each bar*/
#define BAR_WIDTH 3.5
/* outline color */
#define BAR_OUTLINE OUTLINE
/* outline width (in pixels, set to 0 to disable outline drawing) */
#define BAR_OUTLINE_WIDTH 0
/* Inverse horizontal scale, larger means less higher frequencies displayed */
#define WSCALE 11
/* Amplify magnitude of the results each bar displays */
#define AMPLIFY 200
/* Bar color */
#define COLOR (vec4(0.8, 0.2, 0.2, 1) * ((d / 40) + 1))

75
shaders/radial/1.frag Normal file
View File

@@ -0,0 +1,75 @@
layout(pixel_center_integer) in vec4 gl_FragCoord;
#request uniform "screen" screen
uniform ivec2 screen;
#request uniform "audio_sz" audio_sz
uniform int audio_sz;
#request setavgframes 6
#request setavgwindow true
#request setgravitystep 5.2
#request uniform "audio_l" audio_l
#request transform audio_l "window"
#request transform audio_l "fft"
#request transform audio_l "gravity"
#request transform audio_l "avg"
uniform sampler1D audio_l;
#request uniform "audio_r" audio_r
#request transform audio_r "window"
#request transform audio_r "fft"
#request transform audio_r "gravity"
#request transform audio_r "avg"
uniform sampler1D audio_r;
out vec4 fragment;
#include "../radial.glsl"
#define TWOPI 6.28318530718
#define PI 3.14159265359
void main() {
float /* translate (x, y) to use (0, 0) as the center of the screen */
dx = gl_FragCoord.x - (screen.x / 2),
dy = (screen.y / 2) - gl_FragCoord.y;
float theta = atan(dy, dx); /* fragment angle with the center of the screen as the origin */
float d = sqrt((dx * dx) + (dy * dy)); /* distance */
if (d > C_RADIUS - (float(C_LINE) / 2F) && d < C_RADIUS + (float(C_LINE) / 2F)) {
fragment = OUTLINE;
return;
} else if (d > C_RADIUS) {
float section = (TWOPI / NBARS); /* range (radians) for each bar */
float m = mod(theta, section); /* position in section (radians) */
float center = ((TWOPI / NBARS) / 2F); /* center line angle */
float ym = d * sin(center - m); /* distance from center line (cartesian coords) */
if (abs(ym) < BAR_WIDTH / 2) { /* if within width, draw audio */
/* texture lookup */
float v = texture(theta > 0 ? audio_l : audio_r,
log(int(abs(theta) / section) / float(NBARS / 2)) / WSCALE).r * AMPLIFY;
d -= C_RADIUS + (float(C_LINE) / 2F); /* offset to fragment distance from inner circle */
if (d <= v - BAR_OUTLINE_WIDTH) {
#if BAR_OUTLINE_WIDTH > 0
if (abs(ym) < (BAR_WIDTH / 2) - BAR_OUTLINE_WIDTH)
fragment = COLOR;
else
fragment = BAR_OUTLINE;
#else
fragment = COLOR;
#endif
return;
}
#if BAR_OUTLINE_WIDTH > 0
if (d <= v) {
fragment = BAR_OUTLINE;
return;
}
#endif
}
}
fragment = vec4(0, 0, 0, 0); /* default frag color */
}