Skip to content

Commit 30e4edd

Browse files
committed
pass light as explicit uniforms
1 parent 907a485 commit 30e4edd

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

  • lib/PDL/Graphics/TriD

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ vs_out => " gl_Position = gl_ModelViewProjectionMatrix * vec4(the_position, 1);
133133
vs_out_light => <<'EOF',
134134
vNormal = normalize(gl_NormalMatrix * normal);
135135
vPosition = vec3(gl_ModelViewMatrix * vec4(the_position, 1));
136-
vLightpos = gl_ModelViewMatrix * gl_LightSource[lightind].position;
136+
vLightpos = gl_ModelViewMatrix * uLightposition;
137137
EOF
138138
fs_diffuse_material => " vec4 in_diffuse = uMatdiffuse;\n",
139139
fs_out_light => <<'EOF',
140140
vec4 ambient, diffuse, spec;
141141
lightfunc(
142-
vLightpos, gl_LightSource[lightind].ambient, gl_LightSource[lightind].diffuse, gl_LightSource[lightind].specular,
142+
vLightpos, uLightambient, uLightdiffuse, uLightspecular,
143143
uMatambient, uMatspecular, uMatshininess,
144144
vPosition, gl_FrontFacing ? vNormal : -vNormal, in_diffuse,
145145
ambient, diffuse, spec
@@ -149,12 +149,12 @@ EOF
149149
vs_out_lightgouraudstart => " vec3 vNormal, vPosition;\n vec4 vLightpos;\n",
150150
vs_out_lightgouraud => <<'EOF',
151151
vFrontcolour = lightfuncgouraud(
152-
vLightpos, gl_LightSource[lightind].ambient, gl_LightSource[lightind].diffuse, gl_LightSource[lightind].specular,
152+
vLightpos, uLightambient, uLightdiffuse, uLightspecular,
153153
uMatambient, uMatspecular, uMatshininess,
154154
vPosition, vNormal
155155
) + uMatemission;
156156
vBackcolour = lightfuncgouraud(
157-
vLightpos, gl_LightSource[lightind].ambient, gl_LightSource[lightind].diffuse, gl_LightSource[lightind].specular,
157+
vLightpos, uLightambient, uLightdiffuse, uLightspecular,
158158
uMatambient, uMatspecular, uMatshininess,
159159
vPosition, -vNormal
160160
) + uMatemission;
@@ -163,7 +163,10 @@ fs_out_lightgouraud => <<'EOF',
163163
gl_FragColor = (gl_FrontFacing ? vFrontcolour : vBackcolour) * in_diffuse;
164164
EOF
165165
fs_light_decl => <<'EOF',
166-
uniform int lightind;
166+
uniform vec4 uLightposition;
167+
uniform vec4 uLightambient;
168+
uniform vec4 uLightdiffuse;
169+
uniform vec4 uLightspecular;
167170
uniform float uMatshininess;
168171
uniform vec4 uMatspecular;
169172
uniform vec4 uMatambient;
@@ -179,7 +182,6 @@ fs_in_lightgouraud_decl => "$FS_IN vec4 vFrontcolour;\n$FS_IN vec4 vBackcolour;\
179182
use OpenGL::Modern qw(
180183
glPushAttrib glPopAttrib
181184
glLineWidth glPointSize
182-
glLightfv_p glLightModeli
183185
glEnable glDisable
184186
glGetIntegerv_p
185187
glGenBuffers_p glBindBuffer glDeleteBuffers_p glBufferData_c
@@ -372,25 +374,13 @@ sub program_poscol {
372374
$this->load_attrib(position => $points);
373375
$this->load_attrib(colour => $colours);
374376
}
375-
sub lighting {
376-
my ($this, $bool) = @_;
377-
if ($bool) {
378-
glEnable(GL_LIGHTING);
379-
glEnable(GL_LIGHT0);
380-
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
381-
glLightfv_p(GL_LIGHT0,GL_POSITION,1.0,1.0,1.0,0.0);
382-
} else {
383-
glDisable(GL_LIGHTING);
384-
}
385-
}
386377
sub togl {
387378
my ($this, $points) = @_;
388379
print "togl $this\n" if $PDL::Graphics::TriD::verbose;
389380
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
390381
glLineWidth($this->{Options}{LineWidth} || 1);
391382
glPointSize($this->{Options}{PointSize} || 1);
392383
glEnable(GL_DEPTH_TEST);
393-
$this->lighting($this->{Options}{Lighting});
394384
eval { $this->gdraw($points // $this->{Points}) };
395385
{ local $@; glPopAttrib(); }
396386
die if $@;
@@ -888,7 +878,13 @@ sub display {
888878
print "VALID $this=$this->{IsValid}\n" if $PDL::Graphics::TriD::verbose;
889879
if (!$vp->{IsValid}) {
890880
glpSetAutoCheckErrors(1);
891-
$vp->togl_setup(undef, { lightind => ['1i' => [0]], %{$vp->{DefMaterial}->to_uniforms} });
881+
$vp->togl_setup(undef, {
882+
uLightposition => ['4f' => [1,1,1,0]],
883+
uLightambient => ['4f' => [0,0,0,1]],
884+
uLightdiffuse => ['4f' => [1,1,1,1]],
885+
uLightspecular => ['4f' => [1,1,1,1]],
886+
%{$vp->{DefMaterial}->to_uniforms},
887+
});
892888
print "VALID1 $vp\n" if $PDL::Graphics::TriD::verbose;
893889
$vp->{IsValid} = 1;
894890
}

0 commit comments

Comments
 (0)