-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
278 lines (220 loc) · 7.74 KB
/
main.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright (c) 2010 Jan Miderbaeck and Karim Benchemsi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "sdl.h"
#include "sdl_opengl.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
extern int jklist_test( void );
#define WIDTH 640
#define HEIGHT 480
GLfloat yaw;
GLfloat pitch;
int level;
static void subdivide
(
GLfloat point0[3],
GLfloat point1[3],
GLfloat point2[3],
int level
)
{
int coord;
GLfloat midpoint[3][3];
/* Don't subdivide any further; just draw the triangle */
if (level==0) {
glColor3fv(point0);
glVertex3fv(point0);
glColor3fv(point1);
glVertex3fv(point1);
glColor3fv(point2);
glVertex3fv(point2);
return;
}
/* Calculate a midpoint on each edge of the triangle */
for(coord = 0; coord<3; coord++) {
midpoint[0][coord] = (point0[coord] + point1[coord])*0.5;
midpoint[1][coord] = (point1[coord] + point2[coord])*0.5;
midpoint[2][coord] = (point2[coord] + point0[coord])*0.5;
}
/* Subdivide each triangle into three more */ /* . */
level--; /* /X\ */
subdivide(point0,midpoint[0],midpoint[2],level); /* /xxx\ */
subdivide(point1,midpoint[1],midpoint[0],level); /* /X\ /X\ */
subdivide(point2,midpoint[2],midpoint[1],level); /* /XXXVXXX\ */
}
static void repaint()
{
int i;
/* Coordinates of the 6 vertices of the octahedron */
static GLfloat point[6][3] = {
{1.0f,0.0f,0.0f},{-1.0f,0.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,-1.0f,0.0f},
{0.0f,0.0f,1.0f},{0.0f,0.0f,-1.0f}
};
/* indices of the vertices of the triangles which make up each of
* the 8 faces of the octahedron */
static int triangle[8][3] = {
{2,4,0},{2,0,5},{2,5,1},{2,1,4},{3,0,4},{3,5,0},{3,1,5},{3,4,1}
};
/* Clear the color plane and the z-buffer */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Move the object 2 units away from the camera */
glTranslatef(0.0f, 0.0f, -2.0f);
/* Rotate the object */
glRotatef(pitch, 1.0f, 0.0f, 0.0f);
glRotatef(yaw, 0.0f, 1.0f, 0.0f);
/* Draw the triangles which make up the object */
glBegin(GL_TRIANGLES);
for (i=0; i<8; i++) {
subdivide(point[triangle[i][0]],point[triangle[i][1]],point[triangle[i][2]],level);
}
glEnd();
/* increment the rotation every frame */
yaw = yaw + 0.05;
/* finally, swap the back and front buffers */
SDL_GL_SwapBuffers();
}
static void setup_sdl()
{
const SDL_VideoInfo *video;
if( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* Quit SDL properly on exit */
atexit(SDL_Quit);
/* Get the current video information */
video = SDL_GetVideoInfo();
if( video == NULL )
{
fprintf(stderr, "Couldn't get video information: %s\n", SDL_GetError());
exit(1);
}
/* Set the minimum requirements for the OpenGL window */
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
/* Note the SDL_DOUBLEBUF flag is not required to enable double
* buffering when setting an OpenGL video mode.
* Double buffering is enabled or disabled using the
* SDL_GL_DOUBLEBUFFER attribute.
*/
if( SDL_SetVideoMode( WIDTH, HEIGHT, video->vfmt->BitsPerPixel, SDL_OPENGL ) == 0 )
{
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
}
static void setup_opengl()
{
float aspect = (float)WIDTH / (float)HEIGHT;
/* Make the viewport cover the whole window */
glViewport(0, 0, WIDTH, HEIGHT);
/* Set the camera projection matrix:
* field of view: 90 degrees
* near clipping plane at 0.1
* far clipping plane at 100.0
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, aspect, 0.1, 100.0);
/* We're done with the camera, now matrix operations
* will affect the modelview matrix
* */
glMatrixMode(GL_MODELVIEW);
/* set the clear color to gray */
glClearColor(0.5, 0.5 ,0.5, 0);
/* We want z-buffer tests enabled*/
glEnable(GL_DEPTH_TEST);
/* Do draw back-facing polygons*/
glDisable(GL_CULL_FACE);
}
static void main_loop()
{
SDL_Event event;
while( 1 )
{
/* process pending events */
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYDOWN:
switch ( event.key.keysym.sym )
{
case SDLK_ESCAPE:
exit(0);
break;
case SDLK_KP_PLUS:
level++;
if (level > 5) level=5;
break;
case SDLK_KP_MINUS:
level--;
if (level < 0) level=0;
break;
default:
//no default key processing
//(stops compiler warnings for unhandled SDL keydefs
break;
}
break;
case SDL_MOUSEMOTION:
pitch += event.motion.yrel;
if (pitch < -70) pitch = -70;
if (pitch > 70) pitch = 70;
break;
case SDL_QUIT:
exit (0);
break;
}
}
/* update the screen */
repaint();
/* Wait 50ms to avoid using up all the CPU time */
SDL_Delay( 50 );
}
}
int main( void )
{
#ifdef UNIT_TEST
#define RUN_UNIT_TEST( testFunc ) \
do { \
if( testFunc() ) printf( #testFunc " OK\n"); \
else printf( #testFunc " FAILED!\n"); \
} while( 0 )
printf( "Runnning unit tests...\n" );
RUN_UNIT_TEST( jklist_test );
RUN_UNIT_TEST( jkstack_test );
return 0;
#else
setup_sdl();
setup_opengl();
yaw = 45;
pitch = 0;
level = 2;
main_loop();
return 0;
#endif
}