27 files changed, 179 insertions, 549 deletions
diff --git a/src/model/data/base.sqc b/src/model/data/base.sqc new file mode 100644 index 0000000..ad1c395 --- a/dev/null +++ b/src/model/data/base.sqc | |||
@@ -0,0 +1,185 @@ | |||
1 | /* I seem to need this for glGenBuffers as per | ||
2 | http://www.gamedev.net/community/forums/topic.asp?topic_id=422358 */ | ||
3 | #define GL_GLEXT_PROTOTYPES | ||
4 | |||
5 | #include "base.h" | ||
6 | #include "../../view/state0.h" | ||
7 | #include "../../util/check_error.h" | ||
8 | #include <GL/glut.h> | ||
9 | #include <string.h> | ||
10 | #include <stdlib.h> | ||
11 | #include "sqlca.h" | ||
12 | extern struct sqlca sqlca; | ||
13 | |||
14 | /* | ||
15 | * A simple alias to make the code more readable. | ||
16 | */ | ||
17 | #define S state0 | ||
18 | |||
19 | void | ||
20 | base (void) | ||
21 | { | ||
22 | /* | ||
23 | * This implementation can be improved by mapping the video memory | ||
24 | * directly rather than loading into system memory and then copying | ||
25 | * into video memory. | ||
26 | */ | ||
27 | |||
28 | /* | ||
29 | * db2dclgn -d exp004 -t coordinates | ||
30 | */ | ||
31 | EXEC SQL BEGIN DECLARE SECTION; | ||
32 | EXEC SQL INCLUDE 'model/data/coordinates.h'; | ||
33 | sqlint32 rows; | ||
34 | EXEC SQL END DECLARE SECTION; | ||
35 | |||
36 | /* Determine how many nodes have coordinates assigned to them. */ | ||
37 | EXEC SQL SELECT COUNT (*) INTO:rows FROM coordinates; | ||
38 | |||
39 | /* Free any existing coordinates and allocate memory to store the | ||
40 | new values. */ | ||
41 | S.rows = rows; | ||
42 | |||
43 | free (S.selection.set); | ||
44 | S.selection.set = calloc (rows, sizeof (bool)); | ||
45 | |||
46 | free (S.gi_data); | ||
47 | S.gi_data = calloc (rows, sizeof (char) * 20); | ||
48 | |||
49 | free (S.base_vertices_data); | ||
50 | S.base_vertices_data = calloc (rows, sizeof (float) * 2); | ||
51 | |||
52 | free (S.base_colors_data); | ||
53 | S.base_colors_data = calloc (rows, sizeof (float) * 4); | ||
54 | |||
55 | EXEC SQL DECLARE c2 CURSOR FOR SELECT *FROM coordinates; | ||
56 | |||
57 | EXEC SQL OPEN c2; | ||
58 | |||
59 | /* | ||
60 | * Initialize the bounding box of the points. | ||
61 | */ | ||
62 | S.bb.min_x = 0.0; | ||
63 | S.bb.max_x = 0.0; | ||
64 | S.bb.min_y = 0.0; | ||
65 | S.bb.max_y = 0.0; | ||
66 | |||
67 | EXEC SQL FETCH c2 INTO:coordinates; | ||
68 | while (sqlca.sqlcode != 100) | ||
69 | { | ||
70 | int i = coordinates.coord_id - 1; | ||
71 | |||
72 | strncpy (S.gi_data + i, coordinates.gi.data, sizeof (S.gi_data[i])); | ||
73 | |||
74 | float *v = S.base_vertices_data + (i * 2); | ||
75 | *v = coordinates.x; | ||
76 | v++; | ||
77 | *v = coordinates.y; | ||
78 | |||
79 | v = S.base_vertices_data + (i * 2); | ||
80 | if (*v < S.bb.min_x) | ||
81 | S.bb.min_x = *v; | ||
82 | if (*v > S.bb.max_x) | ||
83 | S.bb.max_x = *v; | ||
84 | |||
85 | v++; | ||
86 | if (*v < S.bb.min_y) | ||
87 | S.bb.min_y = *v; | ||
88 | if (*v > S.bb.max_y) | ||
89 | S.bb.max_y = *v; | ||
90 | |||
91 | /* | ||
92 | * Deselected by default. | ||
93 | */ | ||
94 | S.selection.set[i] = false; | ||
95 | |||
96 | float *c = S.base_colors_data + (i * 4); | ||
97 | *c = DEFAULT_COLOR_R; | ||
98 | c++; | ||
99 | *c = DEFAULT_COLOR_G; | ||
100 | c++; | ||
101 | *c = DEFAULT_COLOR_B; | ||
102 | c++; | ||
103 | *c = DEFAULT_COLOR_A; | ||
104 | |||
105 | EXEC SQL FETCH c2 INTO:coordinates; | ||
106 | } | ||
107 | |||
108 | EXEC SQL CLOSE c2; | ||
109 | |||
110 | EXEC SQL COMMIT; | ||
111 | |||
112 | /* | ||
113 | * Find the largest axis and use it to setup the projection. This | ||
114 | * is done to preserve the aspect ratio. The aspect ratio should be | ||
115 | * preserved since relative distance is a meaningful indicator in | ||
116 | * the map. | ||
117 | */ | ||
118 | |||
119 | // Min of min x or min y. | ||
120 | if (S.bb.min_x <= S.bb.min_y) | ||
121 | S.ortho_min = S.bb.min_x; | ||
122 | else | ||
123 | S.ortho_min = S.bb.min_y; | ||
124 | S.ortho_min--; | ||
125 | |||
126 | // Max of max x or max y. | ||
127 | if (S.bb.max_x >= S.bb.max_y) | ||
128 | S.ortho_max = S.bb.max_x; | ||
129 | else | ||
130 | S.ortho_max = S.bb.max_y; | ||
131 | S.ortho_max++; | ||
132 | |||
133 | // Invert the y coordinate to match up with the LGL Java viewer. | ||
134 | float *v = S.base_vertices_data; | ||
135 | for (int i = 0; i < S.rows; i++) | ||
136 | { | ||
137 | v++; | ||
138 | *v = S.ortho_max - *v; | ||
139 | v++; | ||
140 | } | ||
141 | |||
142 | // Move the origin (0,0) to the center of the data. | ||
143 | S.ortho_min = 0.0; | ||
144 | S.ortho_max = 0.0; | ||
145 | |||
146 | v = S.base_vertices_data; | ||
147 | for (int i = 0; i < S.rows; i++) | ||
148 | { | ||
149 | *v = *v - (0.5 * (S.bb.max_x - S.bb.min_x)); | ||
150 | |||
151 | if (S.ortho_min > *v) | ||
152 | S.ortho_min = *v; | ||
153 | |||
154 | if (S.ortho_max < *v) | ||
155 | S.ortho_max = *v; | ||
156 | |||
157 | v++; | ||
158 | |||
159 | *v = *v - (0.5 * (S.bb.max_y - S.bb.min_y)); | ||
160 | |||
161 | if (S.ortho_min > *v) | ||
162 | S.ortho_min = *v; | ||
163 | |||
164 | if (S.ortho_max < *v) | ||
165 | S.ortho_max = *v; | ||
166 | |||
167 | v++; | ||
168 | } | ||
169 | |||
170 | glGenBuffers (2, S.buffers); | ||
171 | |||
172 | glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_VERTICES]); | ||
173 | glVertexPointer (2, GL_FLOAT, 0, 0); | ||
174 | glBufferData (GL_ARRAY_BUFFER, | ||
175 | sizeof (S.base_vertices_data), S.base_vertices_data, | ||
176 | GL_STATIC_DRAW); | ||
177 | |||
178 | glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_COLORS]); | ||
179 | glColorPointer (4, GL_FLOAT, 0, 0); | ||
180 | glBufferData (GL_ARRAY_BUFFER, | ||
181 | sizeof (S.base_colors_data), S.base_colors_data, | ||
182 | GL_STATIC_DRAW); | ||
183 | |||
184 | return; | ||
185 | } | ||