-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
142 lines (116 loc) · 3.12 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
// Terrain Renderer by Mike Nichols
// UCLA Academic Technology Services
// Based on Geometry Clipmaps: Terrain Rendering Using Nested Regular
// Grids. Losasso and Hoppe, SIGGRAPH 2004
// This #define must be included before including glut.h
#define GL_GLEXT_PROTOTYPES
// This header includes all necessary gl* headers
// Must include this before other headers
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <IL/il.h>
#define ILUT_USE_OPENGL
#include <IL/ilu.h>
#include <IL/ilut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "functions.h"
#include "gl_functions.h"
#include "globals.h"
#define CONSOLE_OUTPUT
// uncomment the line below to launch the simulation in fullscreen mode
//#define FULLSCREEN
#ifndef DEBUG
#define DEBUG
#endif
void at_exit() {
// free the glut window
glutDestroyWindow(win_id);
// free vertex buffers
glDeleteBuffers(num_cmaps, cmap_vbuf_id);
glDeleteBuffers(NUM_IBUFFERS, index_id);
// free allocated memory
free (cmap_vbuf_id);
free (terrain_data);
free (cmap_center);
free (cmap_start_index);
free (index_id);
free (index_section_offset);
free (fps_array);
free (textures);
free (texbuf_id);
printf("Simulation Complete.\n");
return;
}
int main(int argc, char **argv) {
if (argc < 4) {
fprintf(stderr, "Usage: ./run <num_cmaps> <clipmap_size> \"filename regex\"\n");
exit(1);
}
num_cmaps = atoi(argv[1]);
cmap_size = atoi(argv[2]);
check_and_set_cmap_size();
#ifdef CONSOLE_OUTPUT
printf("Using %d levels with clipmap size %d\n", num_cmaps, cmap_size);
#endif
// OpenGL setup
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowSize(screen_width, screen_height);
win_id = glutCreateWindow("Terrain Renderer");
#ifdef FULLSCREEN
glutFullScreen();
#endif
// callback functions
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(key);
glutSpecialFunc(specialKey);
/* for transparency
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// */
// devIL setup
ilInit();
iluInit();
ilutInit();
ilutRenderer(ILUT_OPENGL);
// initialize global variables
y_rot = 3*PI / 4.0;
look.x = -cos(y_rot);
look.y = sin(y_rot);
rotate_step = 5.0*PI / 180.0;
lon_min = 181;
lat_min = 181;
lon_max = -181;
lat_max = -181;
SIZE = 1200;
finest_level = 0;
fps_array = (float*)calloc(fps_size, sizeof(float));
check_for_null((void*)fps_array, __FILE__, __LINE__);
fps_total = 6.0;
int i;
for (i = 0; i < fps_size; i++) {
fps_array[i] = 0.1;
}
atexit(at_exit);
// initialize all data
load_data(argv[3]);
#ifdef DEBUG2
check_for_invalid_data();
exit(1);
#endif
initialize_clipmaps();
initialize_index_buffers();
load_textures();
printf("Starting Simulation.\n");
glutMainLoop();
return 0;
}