5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
7 # ------------------------------------------------------------------------------
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # ------------------------------------------------------------------------------
25 # Please feel free to send questions, suggestions or improvements to:
37 package SDL::OpenGL::Cube;
41 my $vertex_array = pack "d24",
42 -0.5,-0.5,-0.5, 0.5,-0.5,-0.5, 0.5,0.5,-0.5, -0.5,0.5,-0.5, # back
43 -0.5,-0.5,0.5, 0.5,-0.5,0.5, 0.5,0.5,0.5, -0.5,0.5,0.5 ; # front
45 my $indicies = pack "C24",
55 my $class = ref($proto) || $proto;
64 glEnableClientState(SDL::GL_VERTEX_ARRAY());
65 glVertexPointer(3,SDL::GL_DOUBLE(),0,$vertex_array);
66 glDrawElements(SDL::GL_QUADS(), 24, SDL::GL_UNSIGNED_BYTE(), $indicies);
70 my ($self,@colors) = @_;
74 die "SDL::OpenGL::Cube::color requires 24 floating point color values\n"
75 unless (scalar(@colors) == 24);
76 $$self{-colors} = pack "f24",@colors;
79 if ($$self{colored}) {
80 glEnableClientState(SDL::GL_COLOR_ARRAY);
81 glColorPointer(3,SDL::GL_FLOAT,0,$$self{-colors});
83 glDisableClientState(SDL::GL_COLOR_ARRAY);
91 $delay = $ARGV[0] || 5;
93 print "Starting $0\n";
95 my $app = new SDL::App -w => 800, -h => 600, -d => 16, -gl =>1;
97 print "Initializing OpenGL settings\n";
98 printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL::SDL_GL_RED_SIZE() );
99 printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute( SDL::SDL_GL_GREEN_SIZE());
100 printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL::SDL_GL_BLUE_SIZE() );
101 printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL::SDL_GL_DEPTH_SIZE() );
102 printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute( SDL::SDL_GL_DOUBLEBUFFER() );
108 1.0,1.0,0.0, 1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0, #back
109 0.4,0.4,0.4, 0.3,0.3,0.3, 0.2,0.2,0.2, 0.1,0.1,0.1 ); #front
112 $cube = new SDL::OpenGL::Cube;
113 $cube->color(@colors);
115 $white = new SDL::OpenGL::Cube;
119 glEnable(GL_CULL_FACE);
125 glClear( SDL::GL_DEPTH_BUFFER_BIT()
126 | SDL::GL_COLOR_BUFFER_BIT());
130 glTranslate(0,0,-6.0);
131 glRotate($angle % 360,1,1,0);
132 glRotate($other % 360,0,1,1);
135 $other += $angle % 5;
138 $toggle ? $cube->draw() : $white->draw();
145 glViewport(0,0,800,600);
147 glMatrixMode(SDL::GL_PROJECTION());
151 gluPerspective(45.0,4/3,0.1,100.0);
153 glFrustum(-0.1,0.1,-0.075,0.075,0.3,100.0);
156 glMatrixMode(SDL::GL_MODELVIEW());
165 my $event = new SDL::Event;
171 exit(0) if ($event->type() == SDL::SDL_QUIT());
172 if (SDL::GetKeyState(SDL::SDLK_SPACE()) == SDL::SDL_PRESSED()) {