shader-test.pl untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 109 SCAR 34.0
CC
5
Cov
0%
go
12.0
cb_keyboard
6.0
toggle_shader
6.0
CRAP 30.0
1 #!/usr/bin/perl
2  
3 # Purpose: Simple demo of per-pixel lighting with GLSL and OpenGL::Shader
4  
5 # Copyright (c) 2007, Geoff Broadwell; this script is released
6 # as open source and may be distributed and modified under the terms
7 # of either the Artistic License or the GNU General Public License,
8 # in the same manner as Perl itself. These licenses should have been
9 # distributed to you as part of your Perl distribution, and can be
10 # read using `perldoc perlartistic` and `perldoc perlgpl` respectively.
11  
12 use strict;
13 use warnings;
14 use OpenGL ':all';
15 use OpenGL::Shader;
16 use Time::HiRes 'time';
17  
18 our $VERSION = '0.1.0';
19  
20 my $width = 1000;
21 my $height = 1000;
22 my ($frames, $start);
23 my ($window, $teapot);
24 my ($shader, $shader_enabled);
25  
26 go();
27  
28 sub go {
29 # Simple usage
30 print "Press 'Q' or 'Esc' to exit, or any other key to toggle shader.\n";
31  
32 # GLUT setup
33 glutInit;
34 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
35 glutInitWindowSize($width, $height);
36  
37 $window = glutCreateWindow('Shader Test');
38  
39 glutIdleFunc (\&cb_draw);
40 glutDisplayFunc (\&cb_draw);
41 glutKeyboardFunc(\&cb_keyboard);
42  
43 # Shader program
44 $shader = new OpenGL::Shader('GLSL');
45 die "This program requires support for GLSL shaders.\n" unless $shader;
46  
47 my $fragment = fragment_shader();
48 my $vertex = vertex_shader();
49 my $info = $shader->Load($fragment, $vertex);
50 print $info if $info;
51 toggle_shader();
52  
53 # Display list for teapot
54 $teapot = glGenLists(1);
55 glNewList($teapot, GL_COMPILE);
56 glutSolidTeapot(1);
57 glEndList;
58  
59 # Unchanging GL config
60 glViewport(0, 0, $width, $height);
61  
62 glEnable(GL_DEPTH_TEST);
63  
64 glMatrixMode(GL_PROJECTION);
65 glLoadIdentity;
66 gluPerspective(90, $width/$height, 1, 10);
67 glMatrixMode(GL_MODELVIEW);
68  
69 glShadeModel(GL_SMOOTH);
70 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
71 glEnable(GL_LIGHTING);
72 glEnable(GL_LIGHT0);
73 glLightfv_p(GL_LIGHT0, GL_POSITION, 4, 4, 4, 1);
74  
75 glMaterialfv_p(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 1, .7, .7, 1);
76 glMaterialfv_p(GL_FRONT_AND_BACK, GL_SPECULAR, 1, 1, 1, 1);
77 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 50 );
78  
79 # Actually start the test
80 $start = time;
81 glutMainLoop;
82 }
83  
84 sub cb_draw {
85 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
86  
87 glLoadIdentity;
88 glTranslatef(0, 0, -3);
89  
90 my $slow_time = time / 5;
91 my $frac_time = $slow_time - int $slow_time;
92 my $angle = $frac_time * 360;
93 glRotatef($angle, 0, 1, 0);
94 glRotatef(30, 1, 0, 0);
95  
96 glCallList($teapot);
97  
98 glutSwapBuffers;
99  
100 $frames++;
101 }
102  
103 sub cb_keyboard {
104 my $key = shift;
105 my $chr = lc chr $key;
106  
107 if ($key == 27 or $chr eq 'q') {
108 my $time = time - $start;
109 my $fps = $frames / $time;
110 printf "%.3f FPS\n", $fps;
111  
112 glutDestroyWindow($window);
113 exit(0);
114 }
115 else {
116 toggle_shader();
117 }
118 }
119  
120 sub toggle_shader {
121 $shader_enabled = !$shader_enabled;
122 $shader_enabled ? $shader->Enable : $shader->Disable;
123 }
124  
125 sub vertex_shader {
126 return <<'VERTEX';
127  
128 varying vec3 Normal;
129 varying vec3 Position;
130  
131 void main(void) {
132 gl_Position = ftransform();
133 Position = vec3(gl_ModelViewMatrix * gl_Vertex);
134 Normal = gl_NormalMatrix * gl_Normal;
135 }
136  
137 VERTEX
138 }
139  
140 sub fragment_shader {
141 return <<'FRAGMENT';
142  
143 varying vec3 Position;
144 varying vec3 Normal;
145  
146 void main(void) {
147 vec3 normal = normalize(Normal);
148 vec3 reflected = normalize(reflect(Position, normal));
149 vec3 light_dir = normalize(vec3(gl_LightSource[0].position) - Position);
150  
151 float diffuse = max (dot(light_dir, normal ), 0.0);
152 float spec = clamp(dot(light_dir, reflected), 0.0, 1.0);
153 spec = pow (spec, gl_FrontMaterial.shininess);
154  
155 gl_FragColor = gl_FrontLightModelProduct.sceneColor
156 + gl_FrontLightProduct[0].ambient
157 + diffuse * gl_FrontLightProduct[0].diffuse
158 + spec * gl_FrontLightProduct[0].specular;
159 }
160  
161 FRAGMENT
162 }