summaryrefslogtreecommitdiffstats
path: root/src/model/exp004base.sqc (plain)
blob: 2879263a491b0eec6fc0ef0eebe918f43058b843
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
/* I seem to need this for glGenBuffers as per
   http://www.gamedev.net/community/forums/topic.asp?topic_id=422358 */
#define GL_GLEXT_PROTOTYPES

#include "exp004base.h"
#include "../view/exp004state0.h"
#include "../util/check_error.h"
#include <GL/glut.h>
#include <string.h>
#include <stdlib.h>
#include "sqlca.h"
extern struct sqlca sqlca;

/*
 * A simple alias to make the code more readable.
 */
#define S exp004state0

void
exp004base (void)
{
  /*
   * This implementation can be improved by mapping the video memory
   * directly rather than loading into system memory and then copying
   * into video memory.
   */

  /*
   * db2dclgn -d exp004 -t coordinates
   */
  EXEC SQL BEGIN DECLARE SECTION;
  EXEC SQL INCLUDE 'model/coordinates.h';
  sqlint32 rows;
  EXEC SQL END DECLARE SECTION;

  /* Determine how many nodes have coordinates assigned to them. */
  EXEC SQL SELECT COUNT(*) INTO :rows FROM coordinates;

  /* Free any existing coordinates and allocate memory to store the
     new values. */
  S.rows = rows;

  free (S.selection.set);
  S.selection.set = calloc (rows, sizeof (bool));

  free (S.gi_data);
  S.gi_data = calloc (rows * 20, sizeof (char));

  free (S.base_vertices_data);
  S.base_vertices_data = calloc (rows * 2, sizeof (float));

  free (S.base_colors_data);
  S.base_colors_data = calloc (rows * 4, sizeof (float));

  EXEC SQL DECLARE c2 CURSOR FOR SELECT *FROM coordinates;

  EXEC SQL OPEN c2;

  /*
   * Initialize the bounding box of the points.
   */
  S.bb.min_x = 0.0;
  S.bb.max_x = 0.0;
  S.bb.min_y = 0.0;
  S.bb.max_y = 0.0;

  EXEC SQL FETCH c2 INTO:coordinates;
  while (sqlca.sqlcode != 100)
    {
      int i = coordinates.coord_id - 1;

      strncpy (S.gi_data + i, coordinates.gi.data, sizeof (S.gi_data[i]));

      float* v = S.base_vertices_data + (i * 2);
      *v = coordinates.x;
      v++;
      *v = coordinates.y;

      v = S.base_vertices_data + (i * 2);
      if (*v < S.bb.min_x)
	S.bb.min_x = *v;
      if (*v > S.bb.max_x)
	S.bb.max_x = *v;

      v++;
      if (*v < S.bb.min_y)
	S.bb.min_y = *v;
      if (*v > S.bb.max_y)
	S.bb.max_y = *v;

      /*
       * Deselected by default.
       */
      S.selection.set[i] = false;

      float* c = S.base_colors_data + (i * 4);
      *c = DEFAULT_COLOR_R;
      c++;
      *c = DEFAULT_COLOR_G;
      c++;
      *c = DEFAULT_COLOR_B;
      c++;
      *c = DEFAULT_COLOR_A;

      EXEC SQL FETCH c2 INTO:coordinates;
    }

  EXEC SQL CLOSE c2;

  EXEC SQL COMMIT;

  /*
   * Find the largest axis and use it to setup the projection.  This
   * is done to preserve the aspect ratio.  The aspect ratio should be
   * preserved since relative distance is a meaningful indicator in
   * the map.
   */

  // Min of min x or min y.
  if (S.bb.min_x <= S.bb.min_y)
    S.ortho_min = S.bb.min_x;
  else
    S.ortho_min = S.bb.min_y;
  S.ortho_min--;

  // Max of max x or max y.
  if (S.bb.max_x >= S.bb.max_y)
    S.ortho_max = S.bb.max_x;
  else
    S.ortho_max = S.bb.max_y;
  S.ortho_max++;

  // Invert the y coordinate to match up with the LGL Java viewer.
  float* v = S.base_vertices_data;
  for (int i = 0; i < S.rows; i++)
    {
      v++;
      *v = S.ortho_max - *v;
      v++;
    }

  // Move the origin (0,0) to the center of the data.
  S.ortho_min = 0.0;
  S.ortho_max = 0.0;

  v = S.base_vertices_data;
  for (int i = 0; i < S.rows; i++)
    {
      *v = *v - (0.5 * (S.bb.max_x - S.bb.min_x));

      if (S.ortho_min > *v)
	S.ortho_min = *v;

      if (S.ortho_max < *v)
	S.ortho_max = *v;

      v++;

      *v = *v - (0.5 * (S.bb.max_y - S.bb.min_y));

      if (S.ortho_min > *v)
	S.ortho_min = *v;

      if (S.ortho_max < *v)
	S.ortho_max = *v;

      v++;
    }

  glGenBuffers (2, S.buffers);

  glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_VERTICES]);
  glVertexPointer (2, GL_FLOAT, 0, 0);
  glBufferData (GL_ARRAY_BUFFER,
		sizeof (S.base_vertices_data), S.base_vertices_data,
		GL_STATIC_DRAW);

  glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_COLORS]);
  glColorPointer (4, GL_FLOAT, 0, 0);
  glBufferData (GL_ARRAY_BUFFER,
		sizeof (S.base_colors_data), S.base_colors_data,
		GL_STATIC_DRAW);

  return;
}

Valid XHTML 1.0 Strict

Copyright © 2009 Don Pellegrino All Rights Reserved.