Rename t/rectpm.t -> t/core_rect.t
[sdlgit/SDL_perl.git] / test / OpenGL / test2.pl
CommitLineData
8fde61e3 1#!/usr/bin/env perl
2
3use SDL;
4use SDL::App;
5use SDL::Surface;
6use SDL::Event;
7use SDL::OpenGL;
8
9package SDL::OpenGL::Cube;
10use SDL;
11use SDL::OpenGL;
12
13my $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
16
17my $indicies = pack "C24",
18 4,5,6,7, # front
19 1,2,6,5, # right
20 0,1,5,4, # bottom
21 0,3,2,1, # back
22 0,4,7,3, # left
23 2,3,7,6; # top
24
25sub new {
26 my $proto = shift;
27 my $class = ref($proto) || $proto;
28 my $self = {};
29 bless $self,$class;
30 $self;
31}
32
33sub draw {
34 my ($self) = @_;
35 $self->color();
36 glEnableClientState(GL_VERTEX_ARRAY());
37 glVertexPointer(3,GL_DOUBLE(),0,$vertex_array);
38 glDrawElements(GL_QUADS(), 24, GL_UNSIGNED_BYTE(), $indicies);
39}
40
41sub color {
42 my ($self,@colors) = @_;
43
44 if (@colors) {
45 $$self{colored} = 1;
46 die "SDL::OpenGL::Cube::color requires 24 floating point color values\n"
47 unless (scalar(@colors) == 24);
48 $$self{-colors} = pack "f24",@colors;
49 }
50
51 if ($$self{colored}) {
52 glEnableClientState(GL_COLOR_ARRAY);
53 glColorPointer(3,GL_FLOAT,0,$$self{-colors});
54 } else {
55 glDisableClientState(GL_COLOR_ARRAY);
56 }
57}
58
59
601;
61
62
63die "Usage: $0 delay\n Hold the space key for a white cube\n" unless (defined $ARGV[0]);
64$delay = $ARGV[0];
65
66print "Starting $0\n";
67
68my $app = new SDL::App -w => 800, -h => 600, -d => 16, -gl =>1;
69
70print "Initializing OpenGL settings\n";
71printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL_GL_RED_SIZE() );
72printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute( SDL_GL_GREEN_SIZE());
73printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL_GL_BLUE_SIZE() );
74printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL_GL_DEPTH_SIZE() );
75printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute( SDL_GL_DOUBLEBUFFER() );
76
77$angle = 0;
78$other = 0;
79
80my @colors = (
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
83
84
85$cube = new SDL::OpenGL::Cube;
86$cube->color(@colors);
87
88$white = new SDL::OpenGL::Cube;
89
90$toggle = 1;
91
92glEnable(GL_CULL_FACE);
93glFrontFace(GL_CCW);
94glCullFace(GL_BACK);
95
96sub DrawScene {
97
98 glClear( GL_DEPTH_BUFFER_BIT()
99 | GL_COLOR_BUFFER_BIT());
100
101 glLoadIdentity();
102
103 glTranslate(0,0,-6.0);
104 glRotate($angle % 360,1,1,0);
105 glRotate($other % 360,0,1,1);
106
107 $angle += 6;
108 $other += $angle % 5;
109
110 glColor(1,1,1);
111 $toggle ? $cube->draw() : $white->draw();
112
113 $app->sync();
114
115}
116
117sub InitView {
118 glViewport(0,0,800,600);
119
120 glMatrixMode(GL_PROJECTION());
121 glLoadIdentity();
122
123 if ( @_ ) {
124 gluPerspective(45.0,4/3,0.1,100.0);
125 } else {
126 glFrustum(-0.1,0.1,-0.075,0.075,0.3,100.0);
127 }
128
129 glMatrixMode(GL_MODELVIEW());
130 glLoadIdentity();
131}
132
133InitView();
134
135DrawScene();
136$app->sync();
137
138my $event = new SDL::Event;
139
140for (;;) {
141 for (0 .. 5) {
142 $event->pump();
143 $event->poll();
144 exit(0) if ($event->type() == SDL_QUIT());
145 if (SDL::GetKeyState(SDLK_SPACE()) == SDL_PRESSED()) {
146 $toggle = 0;
147 } else {
148 $toggle = 1;
149 }
150 $app->delay($delay);
151 }
152 DrawScene();
153}
154