Skip to content

Commit

Permalink
implement variable major grid
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhuntley committed Jul 30, 2021
1 parent e8b5a0e commit e698480
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
27 changes: 21 additions & 6 deletions plots/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ def do_activate(self):

self.refresh_history_buttons()

@staticmethod
def major_grid(pixel_extent):
min_extent = 100*pixel_extent
exponent = math.floor(math.log10(abs(min_extent)))
mantissa = min_extent/10**exponent
major = 1.0
for m in (2.0, 5.0, 10.0):
if mantissa < m:
major = m * 10**exponent
return major

def gl_render(self, area, context):
area.make_current()
w = area.get_allocated_width() * area.get_scale_factor()
Expand All @@ -200,21 +211,25 @@ def gl_render(self, area, context):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

shaders.glUseProgram(self.shader)
glUniform2f(glGetUniformLocation(self.shader, "viewport"), *self.viewport)
glUniform2f(glGetUniformLocation(self.shader, "translation"), *self.translation)
glUniform2f(glGetUniformLocation(self.shader, "pixel_extent"), *pixel_extent)
glUniform1f(glGetUniformLocation(self.shader, "scale"), self.scale)
glUniform2f(self.uniform("viewport"), *self.viewport)
glUniform2f(self.uniform("translation"), *self.translation)
glUniform2f(self.uniform("pixel_extent"), *pixel_extent)
glUniform1f(self.uniform("scale"), self.scale)
glUniform1f(self.uniform("major_grid"), self.major_grid(pixel_extent[0]))
for slider in self.slider_rows:
glUniform1f(glGetUniformLocation(self.shader, slider.name), slider.value)
glUniform1f(self.uniform(slider.name), slider.value)
glBindVertexArray(self.vao)
glDrawArrays(GL_TRIANGLES, 0, 18)
glBindVertexArray(0)
self.text_renderer.render(w, h)
return True

def uniform(self, name):
return glGetUniformLocation(self.shader, name)

def gl_realize(self, area):
area.make_current()

Expand Down
5 changes: 3 additions & 2 deletions plots/shaders/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ out vec4 rgba;

uniform vec2 pixel_extent;
uniform float scale;
uniform float major_grid;

#define pi 3.141592653589793
#define e 2.718281828459045
Expand Down Expand Up @@ -143,7 +144,7 @@ void main() {
float axis_width = pixel_extent.x;
color -= (1.0-vec3(0.2,0.2,1.0))*(1.0-smoothstep(axis_width, axis_width*1.05, abs(graph_pos.x)));
color -= (1.0-vec3(0.2,0.2,1.0))*(1.0-smoothstep(axis_width, axis_width*1.05, abs(graph_pos.y)));
color -= (1.0-vec3(0.8,0.8,1.0))*(1.0-smoothstep(axis_width, axis_width*1.05, abs(mod(graph_pos.x, 1.0))));
color -= (1.0-vec3(0.8,0.8,1.0))*(1.0-smoothstep(axis_width, axis_width*1.05, abs(mod(graph_pos.y, 1.0))));
color -= (1.0-vec3(0.8,0.8,1.0))*(1.0-smoothstep(axis_width, axis_width*1.05, abs(mod(graph_pos.x, major_grid))));
color -= (1.0-vec3(0.8,0.8,1.0))*(1.0-smoothstep(axis_width, axis_width*1.05, abs(mod(graph_pos.y, major_grid))));
rgba = vec4(color, 1);
}

0 comments on commit e698480

Please sign in to comment.