Skip to content

Commit fb678a9

Browse files
committed
use GL 2.1 shaders for spheres
1 parent fbc4e5e commit fb678a9

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

Changes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- trigrid3d $colours is now per-vertex
55
- coordinate inputs now converted to float, so pass right type if want flow
66
- add labels3d, arrows3d
7-
- compatibility break: requires OpenGL 1.5 (from 2003)
7+
- compatibility break: requires OpenGL 2.1 (from 2006)
88
- now uses OpenGL::Modern
99
- `demo 3dgal` Mandelbrot entries now native complex
1010

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,46 @@ sub PDL::Graphics::TriD::Lines::primitive {OpenGL::Modern::GL_LINES}
369369

370370
{ package # hide from PAUSE
371371
PDL::Graphics::TriD::Spheres;
372-
use OpenGL::Modern qw(glShadeModel GL_SMOOTH);
373372
use PDL::Graphics::OpenGLQ;
373+
my $vertex_shader = <<'EOF';
374+
#version 120
375+
varying vec3 vNormal;
376+
varying vec4 vPosition;
377+
void main() {
378+
vNormal = normalize(gl_NormalMatrix * gl_Normal);
379+
vPosition = gl_ModelViewMatrix * gl_Vertex;
380+
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
381+
}
382+
EOF
383+
my $fragment_shader = <<'EOF';
384+
#version 120
385+
varying vec3 vNormal;
386+
varying vec4 vPosition;
387+
388+
/* modified from https://community.khronos.org/t/help-with-gouraud-phong-shading-in-shaders/73192/2 */
389+
void light(vec4 position, vec3 norm, out vec4 diffuse, out vec4 spec) {
390+
vec3 n = normalize(norm);
391+
vec3 s = vec3(normalize(gl_LightSource[0].position - position));
392+
vec4 v = normalize(-position);
393+
vec4 r = vec4(reflect(-s, n), 1);
394+
float sDotN = max(dot(s, n), 0.0);
395+
diffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * sDotN;
396+
spec = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
397+
}
398+
399+
void main() {
400+
vec4 diffuse, spec;
401+
if (gl_FrontFacing) {
402+
light(vPosition, vNormal, diffuse, spec);
403+
} else {
404+
light(vPosition, -vNormal, diffuse, spec);
405+
}
406+
gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
407+
}
408+
EOF
374409
my %SPHERE;
375410
my @KEYS = qw(vertices normals idx);
411+
my $SHADER_PROGRAM;
376412
sub togl_setup {
377413
my ($this,$points) = @_;
378414
print "togl_setup $this\n" if $PDL::Graphics::TriD::verbose;
@@ -383,12 +419,13 @@ sub togl_setup {
383419
$this->load_buffer(vert_buf => $this->{Impl}{vertices});
384420
$this->load_buffer(norm_buf => $this->{Impl}{normals});
385421
$this->load_idx_buffer(indx_buf => $this->{Impl}{idx});
422+
$SHADER_PROGRAM //= $this->compile_program($vertex_shader, $fragment_shader);
423+
$this->{Impl}{program_nodestroy} = $SHADER_PROGRAM;
386424
$this->togl_unbind;
387425
}
388426
sub gdraw {
389427
my($this,$points) = @_;
390428
$this->togl_bind;
391-
glShadeModel(GL_SMOOTH);
392429
PDL::gl_spheres($points, $this->{Impl}{idx}->dims);
393430
$this->togl_unbind;
394431
}

0 commit comments

Comments
 (0)