-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview13.cpp
280 lines (213 loc) · 8.28 KB
/
view13.cpp
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
279
280
#include "view13.h"
#include "util.h"
GLuint view13_program = 0;
GLint view13_attr_coord2d = -1, view13_uniform_color = -1;
GLint view13_uniform_transform = -1;
float view13_offset_x = 0, view13_scale_x = 1;
GLuint view13_vbo[3] = {0};
const int view13_border = 10, view13_ticksize = 10;
int view13_initResources()
{
glClearColor(1.0, 1.0, 1.0, 0);
view13_program = create_program("glsl/graph.13.v.glsl",
"glsl/graph.13.f.glsl");
view13_attr_coord2d = get_attrib(view13_program, "coord2d");
view13_uniform_transform = get_uniform(view13_program, "transform");
view13_uniform_color = get_uniform(view13_program, "color");
// Create the vertex buffer object
glGenBuffers(3, view13_vbo);
glBindBuffer(GL_ARRAY_BUFFER, view13_vbo[0]);
// Create our own temporary buffer
point graph[2000];
// Fill it in just like an array
for (int i = 0; i < 2000; i++) {
float x = (i - 1000.0) / 100.0;
graph[i].x = x;
graph[i].y = sin(x * 10.0) / (1.0 + x * x);
}
// Tell OpenGL to copy our array to the buffer object
glBufferData(GL_ARRAY_BUFFER, sizeof(graph), graph, GL_STATIC_DRAW);
// Create a VBO for the border
static const point border[4] = { {-1, -1}, {1, -1}, {1, 1}, {-1, 1} };
glBindBuffer(GL_ARRAY_BUFFER, view13_vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(border), border, GL_STATIC_DRAW);
return 0;
}
// TODO: the func of view13_viewport_transform
glm::mat4 view13_viewport_transform(float x, float y, float width, float height,
float *pixel_x = 0, float *pixel_y = 0)
{
// Map OpenGL coordinates (-1,-1) to window coordinates (x,y),
// (1,1) to (x + width, y + height).
// First, we need to know the real window size:
float window_width = glutGet(GLUT_WINDOW_WIDTH);
float window_height = glutGet(GLUT_WINDOW_HEIGHT);
// Calculate how to translate the x and y coordinates:
float offset_x = (2.0 * x + (width - window_width)) / window_width;
float offset_y = (2.0 * y + (height - window_height)) / window_height;
// Calculate how to rescale the x and y coordinates:
float scale_x = width / window_width;
float scale_y = height / window_height;
// Calculate size of pixels in OpenGL coordinates
if (pixel_x) {
*pixel_x = 2.0 / width;
}
if (pixel_y) {
*pixel_y = 2.0 / height;
}
return glm::scale(glm::translate(glm::mat4(1),
glm::vec3(offset_x, offset_y, 0)),
glm::vec3(scale_x, scale_y, 1));
}
void view13Display()
{
// TODO: this is subwindow size? modify other
int window_width = glutGet(GLUT_WINDOW_WIDTH);
int window_height = glutGet(GLUT_WINDOW_HEIGHT);
glUseProgram(view13_program);
glClear(GL_COLOR_BUFFER_BIT);
/* ---------------------------------------------------------------- */
/* Draw the graph */
// Set our viewport, this will clip geometry
glViewport(view13_border + view13_ticksize, view13_border + view13_ticksize,
window_width - view13_border * 2 - view13_ticksize,
window_height - view13_border * 2 - view13_ticksize);
// Set the scissor rectangle,this will clip fragments
glScissor(view13_border + view13_ticksize, view13_border + view13_ticksize,
window_width - view13_border * 2 - view13_ticksize,
window_height - view13_border * 2 - view13_ticksize);
glEnable(GL_SCISSOR_TEST);
// Set our coordinate transformation matrix
glm::mat4 transform =
glm::translate(glm::scale(glm::mat4(1.0f),
glm::vec3(view13_scale_x, 1, 1)),
glm::vec3(view13_offset_x, 0, 0));
glUniformMatrix4fv(view13_uniform_transform, 1, GL_FALSE,
glm::value_ptr(transform));
// Set the color to red
GLfloat red[4] = { 1, 0, 0, 1 };
glUniform4fv(view13_uniform_color, 1, red);
// Draw using the vertices in our vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, view13_vbo[0]);
glEnableVertexAttribArray(view13_attr_coord2d);
glVertexAttribPointer(view13_attr_coord2d, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_STRIP, 0, 2000);
// Stop clipping
glViewport(0, 0, window_width, window_height);
glDisable(GL_SCISSOR_TEST);
/* ---------------------------------------------------------------- */
/* Draw the borders */
float pixel_x, pixel_y;
// Calculate a transformation matrix that gives us the same normalized device
// coordinates as above
transform = view13_viewport_transform(view13_border + view13_ticksize,
view13_border + view13_ticksize,
window_width - view13_border * 2 - view13_ticksize,
window_height - view13_border * 2 - view13_ticksize,
&pixel_x, &pixel_y);
// Tell our vertex shader about it
glUniformMatrix4fv(view13_uniform_transform, 1,
GL_FALSE, glm::value_ptr(transform));
// Set the color to black
GLfloat black[4] = {0, 0, 0, 1};
glUniform4fv(view13_uniform_color, 1, black);
// Draw a border around our graph
glBindBuffer(GL_ARRAY_BUFFER, view13_vbo[1]);
glVertexAttribPointer(view13_attr_coord2d, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_LOOP, 0, 4);
/* ---------------------------------------------------------------- */
/* Draw the y tick marks */
point ticks[42];
for (int i = 0; i <= 20; i++) {
float y = -1 + i * 0.1;
float tickscale = (i % 10) ? 0.5 : 1;
ticks[i * 2].x = -1;
ticks[i * 2].y = y;
ticks[i * 2 + 1].x = -1 - view13_ticksize * tickscale * pixel_x;
ticks[i * 2 + 1].y = y;
}
glBindBuffer(GL_ARRAY_BUFFER, view13_vbo[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(ticks), ticks, GL_DYNAMIC_DRAW);
glVertexAttribPointer(view13_attr_coord2d, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINES, 0, 42);
/* ---------------------------------------------------------------- */
/* Draw the x tick marks */
// desired space between ticks, in graph coordinates
float tickspacing = 0.1 * powf(10, -floor(log10(view13_scale_x)));
// left edge, in graph coordinates
float left = -1.0 / view13_scale_x - view13_offset_x;
// right edge, in graph coordinates
float right = 1.0 / view13_scale_x - view13_offset_x;
// index of left tick, counted from the origin
int left_i = ceil(left / tickspacing);
// index of right tick, counted from the origin
int right_i = floor(right / tickspacing);
// space between left edge of graph and the first tick
float rem = left_i * tickspacing - left;
// first tick in device coordinates
float firsttick = -1.0 + rem * view13_scale_x;
int nticks = right_i - left_i + 1; // number of ticks to show
if (nticks > 21)
nticks = 21; // should not happen
for (int i = 0; i < nticks; i++) {
float x = firsttick + i * tickspacing * view13_scale_x;
float tickscale = ((i + left_i) % 10) ? 0.5 : 1;
ticks[i * 2].x = x;
ticks[i * 2].y = -1;
ticks[i * 2 + 1].x = x;
ticks[i * 2 + 1].y = -1 - view13_ticksize * tickscale * pixel_y;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(ticks), ticks, GL_DYNAMIC_DRAW);
glVertexAttribPointer(view13_attr_coord2d, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINES, 0, nticks * 2);
// And we are done.
glDisableVertexAttribArray(view13_attr_coord2d);
glutSwapBuffers();
}
void view13_special(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT:
view13_offset_x -= 0.03;
break;
case GLUT_KEY_RIGHT:
view13_offset_x += 0.03;
break;
case GLUT_KEY_UP:
view13_scale_x *= 1.5;
break;
case GLUT_KEY_DOWN:
view13_scale_x /= 1.5;
break;
case GLUT_KEY_HOME:
view13_offset_x = 0.0;
view13_scale_x = 1.0;
break;
}
glutPostRedisplay();
}
void view13_freeResources()
{
glDisable(GL_SCISSOR_TEST);
glDeleteProgram(view13_program);
}
void view13_entry(Window *window)
{
window->display = view13Display;
window->entry = viewEntry;
window->init = view13_initResources;
window->free = view13_freeResources;
window->special = view13_special;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view13_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */