9 package SDL::OpenGL::Cube;
13 my $vertex_array = pack "d24",
14 -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
15 -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
17 my $indicies = pack "C24",
27 my $class = ref($proto) || $proto;
36 glEnableClientState(GL_VERTEX_ARRAY());
37 glVertexPointer(3,GL_DOUBLE(),0,$vertex_array);
38 glDrawElements(GL_QUADS(), 24, GL_UNSIGNED_BYTE(), $indicies);
42 my ($self,@colors) = @_;
46 die "SDL::OpenGL::Cube::color requires 24 floating point color values\n"
47 unless (scalar(@colors) == 24);
48 $$self{-colors} = pack "f24",@colors;
51 if ($$self{colored}) {
52 glEnableClientState(GL_COLOR_ARRAY);
53 glColorPointer(3,GL_FLOAT,0,$$self{-colors});
55 glDisableClientState(GL_COLOR_ARRAY);
63 die "Usage: $0 delay\n Hold the space key for a white cube\n" unless (defined $ARGV[0]);
66 print "Starting $0\n";
68 my $app = new SDL::App -w => 800, -h => 600, -d => 16, -gl =>1;
70 print "Initializing OpenGL settings\n";
71 printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL_GL_RED_SIZE() );
72 printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute( SDL_GL_GREEN_SIZE());
73 printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL_GL_BLUE_SIZE() );
74 printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL_GL_DEPTH_SIZE() );
75 printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute( SDL_GL_DOUBLEBUFFER() );
81 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
82 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
85 $cube = new SDL::OpenGL::Cube;
86 $cube->color(@colors);
88 $white = new SDL::OpenGL::Cube;
92 glEnable(GL_CULL_FACE);
98 glClear( GL_DEPTH_BUFFER_BIT()
99 | GL_COLOR_BUFFER_BIT());
103 glTranslate(0,0,-6.0);
104 glRotate($angle % 360,1,1,0);
105 glRotate($other % 360,0,1,1);
108 $other += $angle % 5;
111 $toggle ? $cube->draw() : $white->draw();
118 glViewport(0,0,800,600);
120 glMatrixMode(GL_PROJECTION());
124 gluPerspective(45.0,4/3,0.1,100.0);
126 glFrustum(-0.1,0.1,-0.075,0.075,0.3,100.0);
129 glMatrixMode(GL_MODELVIEW());
138 my $event = new SDL::Event;
144 exit(0) if ($event->type() == SDL_QUIT());
145 if (SDL::GetKeyState(SDLK_SPACE()) == SDL_PRESSED()) {