Skip to content

Commit

Permalink
initial VBO + shader support
Browse files Browse the repository at this point in the history
  • Loading branch information
nikp123 committed Aug 12, 2019
1 parent fc13133 commit 2c7c6c1
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 105 deletions.
22 changes: 14 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ if(X11_FOUND)
list(APPEND ADDITIONAL_LIBRARIES "${X11_LIBRARIES}")

# GLX
pkg_check_modules(GL QUIET gl xrender)
pkg_check_modules(GL QUIET glew xrender)
if(GL_FOUND)
add_definitions(-DGLX -DGL)
list(APPEND INCLUDE_DIRS "${GL_INCLUDE_DIRS}")
Expand All @@ -114,13 +114,19 @@ if(MSYS OR MINGW OR MSVC)
if(GLU_LIB)
find_library(DWM_LIB dwmapi)
if(DWM_LIB)
add_definitions(-DWIN -DGL)
list(APPEND ADDITIONAL_SOURCES "src/output/graphical_win.c")
list(APPEND ADDITIONAL_SOURCES "src/input/wasapi.cpp")
list(APPEND ADDITIONAL_LIBRARIES "-lgdi32")
list(APPEND ADDITIONAL_LIBRARIES "-lopengl32")
list(APPEND ADDITIONAL_LIBRARIES "-lglu32")
list(APPEND ADDITIONAL_LIBRARIES "-ldwmapi")
find_library(GLEW_LIB glew32)
if(GLEW_LIB)
add_definitions(-DWIN -DGL)
list(APPEND ADDITIONAL_SOURCES "src/output/graphical_win.c")
list(APPEND ADDITIONAL_SOURCES "src/input/wasapi.cpp")
list(APPEND ADDITIONAL_LIBRARIES "-lgdi32")
list(APPEND ADDITIONAL_LIBRARIES "-lopengl32")
list(APPEND ADDITIONAL_LIBRARIES "-lglu32")
list(APPEND ADDITIONAL_LIBRARIES "-lglew32")
list(APPEND ADDITIONAL_LIBRARIES "-ldwmapi")
else()
message("GLEW library not found")
endif()
else()
message("DWMAPI library not found")
endif()
Expand Down
4 changes: 4 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <sys/stat.h>
#include <math.h>

#ifdef WIN
#include <windows.h>
#endif

#include "output/graphical.h"
#include "config.h"

Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct config_params {
double monstercat, integral, gravity, ignore, sens, logScale, logBegin, logEnd,
eqBalance, foreground_opacity, background_opacity;
unsigned int lowcf, highcf, shdw, shdw_col, inputsize, fftsize, gradients,
bgcol, col;
bgcol, col, bars;
double *smooth;
int smcount, im, om, autobars, stereo, is_bin, ascii_range, vsync,
bit_format, fixedbars, framerate, bw, bs, autosens, overshoot, waves,
Expand Down
154 changes: 79 additions & 75 deletions src/output/graphical.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "graphical.h"
#include "../config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

Expand Down Expand Up @@ -37,84 +39,86 @@ void calculate_win_pos(int *winX, int *winY, int winW, int winH, int scrW, int s
}

#ifdef GL
unsigned int VBObuffer;
float glBars[1600]; // 2*4*200

static unsigned int CompileShader(unsigned int type, char *source) {
unsigned int id = glCreateShader(type);
glShaderSource(id, 1, &source, NULL);
glCompileShader(id);

int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char *message = (char*)alloca(length*sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
fprintf(stderr, "Failed to compile %s shader: %s\n", type==GL_VERTEX_SHADER? "vertex" : "fragment", message);

glDeleteShader(id);
return 0;
}

return id;
}

static unsigned int CreateShader(const char* vertexShader,
const char* fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);

glDeleteShader(vs);
glDeleteShader(fs);

return program;
}

void VBOGLsetup() {
glewInit();

for(int i=0; i<200; i++) glBars[i] = 0.0;
glGenBuffers(1, &VBObuffer);
glBindBuffer(GL_ARRAY_BUFFER, VBObuffer);
glBufferData(GL_ARRAY_BUFFER, 1600*sizeof(float), glBars, GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, 0);

char *vertex = "void main(){ \n"
" gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;\n"
"}\n";
char *fragment = "void main(){\n"
" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";

unsigned int shader = CreateShader(vertex, fragment);
glUseProgram(shader);
}

void VBOGLdestroy() {
//free(glBars);
}

int drawGLBars(int rest, int bars, double colors[8], double gradColors[24], int *f) {
for(int i = 0; i < bars; i++) {
double point[4];
point[0] = rest+(p.bw+p.bs)*i;
point[1] = rest+(p.bw+p.bs)*i+p.bw;
point[2] = (unsigned int)f[i]+p.shdw> (unsigned int)p.h-p.shdw ? (unsigned int)p.h-p.shdw : (unsigned int)f[i]+p.shdw;
point[3] = p.shdw;

glBegin(GL_QUADS);
if(p.shdw) {
// left side
glColor4d(colors[5], colors[6], colors[7], colors[4]);
glVertex2d(point[0], point[2]);
glVertex2d(point[0], point[3]);
glColor4d(0.0, 0.0, 0.0, 0.0);
glVertex2d(point[0]-p.shdw/2, point[3]-p.shdw);
glVertex2d(point[0]-p.shdw/2, point[2]+p.shdw/2);

// right side
glColor4d(colors[5], colors[6], colors[7], colors[4]);
glVertex2d(point[1], point[2]);
glVertex2d(point[1], point[3]);
glColor4d(0.0, 0.0, 0.0, 0.0);
glVertex2d(point[1]+p.shdw, point[3]-p.shdw);
glVertex2d(point[1]+p.shdw, point[2]+p.shdw/2);

// top side
glColor4d(colors[5], colors[6], colors[7], colors[4]);
glVertex2d(point[1], point[2]);
glVertex2d(point[0], point[2]);
glColor4d(0.0, 0.0, 0.0, 0.0);
glVertex2d(point[0]-p.shdw/2, point[2]+p.shdw/2);
glVertex2d(point[1]+p.shdw, point[2]+p.shdw/2);

// bottom side
glColor4d(colors[5], colors[6], colors[7], colors[4]);
glVertex2d(point[1], point[3]);
glVertex2d(point[0], point[3]);
glColor4d(0.0, 0.0, 0.0, 0.0);
glVertex2d(point[0]-p.shdw/2, point[3]-p.shdw);
glVertex2d(point[1]+p.shdw, point[3]-p.shdw);
}

if(p.gradients) {
double progress = (double)(point[2]-p.shdw)/(double)((unsigned int)p.h-p.shdw);
int gcMax = ceil((p.gradients-1.0)*progress);
double cutLenght = ((unsigned int)p.h-p.shdw)/(double)(p.gradients-1.0);
for(int gcPhase=0; gcPhase<gcMax; gcPhase++) {
if(gcPhase==gcMax-1) {
double barProgress = fmod(point[2]-1.0-(double)p.shdw, cutLenght)/cutLenght;
glColor4d(
gradColors[gcPhase*3]+(gradColors[gcPhase*3+3]-gradColors[gcPhase*3])*barProgress,
gradColors[gcPhase*3+1]+(gradColors[gcPhase*3+4]-gradColors[gcPhase*3+1])*barProgress,
gradColors[gcPhase*3+2]+(gradColors[gcPhase*3+5]-gradColors[gcPhase*3+2])*barProgress, colors[3]);
glVertex2d(point[0], point[2]);
glVertex2d(point[1], point[2]);
} else {
glColor4d(gradColors[gcPhase*3+3], gradColors[gcPhase*3+4],
gradColors[gcPhase*3+5], colors[3]);
glVertex2d(point[0], cutLenght*(gcPhase+1)+point[3]);
glVertex2d(point[1], cutLenght*(gcPhase+1)+point[3]);
}

glColor4d(gradColors[gcPhase*3], gradColors[gcPhase*3+1], gradColors[gcPhase*3+2], colors[3]);
glVertex2d(point[1], cutLenght*gcPhase+point[3]);
glVertex2d(point[0], cutLenght*gcPhase+point[3]);
}
} else {
glColor4d(colors[0],colors[1],colors[2],colors[3]);
glVertex2d(point[0], point[2]);
glVertex2d(point[1], point[2]);

glColor4d(colors[0], colors[1], colors[2], colors[3]);
glVertex2d(point[1], point[3]);
glVertex2d(point[0], point[3]);
}
glEnd();
glBars[i*8] = rest+(p.bw+p.bs)*i;
glBars[i*8+6] = glBars[i*8];
glBars[i*8+2] = rest+(p.bw+p.bs)*i+p.bw;
glBars[i*8+4] = glBars[i*8+2];
glBars[i*8+1] = (unsigned int)f[i]+p.shdw> (unsigned int)p.h-p.shdw ? (unsigned int)p.h-p.shdw : (unsigned int)f[i]+p.shdw;
glBars[i*8+3] = glBars[i*8+1];
glBars[i*8+5] = p.shdw;
glBars[i*8+7] = glBars[i*8+5];
}
glBufferSubData(GL_ARRAY_BUFFER, 0, 8*bars*sizeof(float), glBars);
glDrawArrays(GL_QUADS, 0, 4*bars);
return 0;
}
#endif
12 changes: 7 additions & 5 deletions src/output/graphical.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#ifndef H_GRAPHICAL
#define H_GRAPHICAL

#ifdef GL
#include <GL/glew.h>
int drawGLBars(int rest, int bars, double colors[8], double gradColors[24], int *f);
#endif

#ifdef GLX
#include <GL/glx.h>
GLXContext xavaGLXContext;
GLXFBConfig* xavaFBConfig;
extern int GLXmode;
#endif

#ifdef GL
#include <GL/gl.h>
int drawGLBars(int rest, int bars, double colors[8], double gradColors[24], int *f);
#endif

void calculate_win_pos(int *winX, int *winY, int winW, int winH, int scrW, int scrH, char *winPos);

void VBOGLsetup();

static const unsigned int definedColors[] = {0x000000, 0xFF0000, 0x00FF00, 0xFFFF00, 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF};

#define ARGB_A_32(x) ((x>>24)&0xff)
Expand Down
19 changes: 10 additions & 9 deletions src/output/graphical_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ int init_window_win() {
void apply_win_settings() {
clear_screen_win();
resize_framebuffer(p.w, p.h);
VBOGLsetup();
//ReleaseDC(xavaWinWindow, xavaWinFrame);

PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
Expand Down Expand Up @@ -404,15 +405,15 @@ void draw_graphical_win(int bars, int rest, int f[200]) {
if(drawGLBars(rest, bars, glColors, gradColors, f)) exit(EXIT_FAILURE);

// dumb workarounds for dumb OSes
glBegin(GL_QUADS);
glColor4d(ARGB_R_32(p.bgcol)/255.0, ARGB_G_32(p.bgcol)/255.0, ARGB_B_32(p.bgcol)/255.0, p.background_opacity);
glVertex2d(0.0, 0.0);
glVertex2d(0.0, p.h);
glVertex2d(p.w, p.h);
glVertex2d(p.w, 0.0);
glEnd();

glFlush();
//glBegin(GL_QUADS);
// glColor4d(ARGB_R_32(p.bgcol)/255.0, ARGB_G_32(p.bgcol)/255.0, ARGB_B_32(p.bgcol)/255.0, p.background_opacity);
// glVertex2d(0.0, 0.0);
// glVertex2d(0.0, p.h);
// glVertex2d(p.w, p.h);
// glVertex2d(p.w, 0.0);
//glEnd();

//glFlush();

// swap buffers
SwapBuffers(xavaWinFrame);
Expand Down
4 changes: 1 addition & 3 deletions src/output/graphical_win.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <windows.h>
#include <windowsx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#define WGL_WGLEXT_PROTOTYPES
#include <GL/glew.h>
#include <GL/wglext.h>
#include <dwmapi.h>
#include <assert.h>
Expand Down
8 changes: 5 additions & 3 deletions src/output/graphical_x.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,14 @@ int apply_window_settings_x()
glViewport(0, 0, p.w, p.h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, (double)p.w, 0, (double)p.h, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Vsync causes problems on NVIDIA GPUs, looking for possible workarounds/fixes
glXSwapIntervalEXT(xavaXDisplay, xavaXWindow, p.vsync);
VBOGLsetup();
#endif
} else clear_screen_x();

Expand Down Expand Up @@ -486,7 +487,7 @@ void draw_graphical_x(int bars, int rest, int f[200], int flastd[200])

if(GLXmode) {
#ifdef GLX
for(int i=0; i<p.gradients; i++) {
for(uint i=0; i<p.gradients; i++) {
gradColors[i*3] = xgrad[i].red/65535.0;
gradColors[i*3+1] = xgrad[i].green/65535.0;
gradColors[i*3+2] = xgrad[i].blue/65535.0;
Expand Down Expand Up @@ -529,7 +530,8 @@ void cleanup_graphical_x(void)
// make sure that all events are dead by this point
XSync(xavaXDisplay, 1);

if(gradientBox != 0) { XFreePixmap(xavaXDisplay, gradientBox); gradientBox = 0; };
if(gradientBox != 0)
XFreePixmap(xavaXDisplay, gradientBox); gradientBox = 0;

#ifdef GLX
glXMakeCurrent(xavaXDisplay, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/xava.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
if (p.fixedbars * p.bw + p.fixedbars * p.bs - p.bs > p.w) p.autobars = 1;
}

//getting orignial numbers of barss incase of resize
//getting orignial numbers of bars incase of resize
if (p.autobars == 1) {
bars = (p.w + p.bs) / (p.bw + p.bs);

Expand Down

0 comments on commit 2c7c6c1

Please sign in to comment.