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