-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview14.cpp
238 lines (195 loc) · 6.97 KB
/
view14.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
#include "view14.h"
#include "util.h"
GLuint view14_program = 0;
GLint view14_attr_coord2d = -1, view14_uniform_vertex_transform = -1;
GLint view14_uniform_texture_transform = 1, view14_uniform_mytexture = -1;
GLuint view14_texture_id = 0;
float view14_offset_x = 0.0, view14_offset_y = 0.0, view14_scale = 1.0;
bool view14_interpolate = false, view14_clamp = false, view14_rotate = false;
GLuint view14_vbo[2];
int view14_initResources()
{
glClearColor(1.0, 1.0, 1.0, 0);
view14_program =
create_program("glsl/graph.14.v.glsl", "glsl/graph.14.f.glsl");
view14_attr_coord2d = get_attrib(view14_program, "coord2d");
view14_uniform_vertex_transform = get_uniform(view14_program,
"vertex_transform");
view14_uniform_texture_transform = get_uniform(view14_program,
"texture_transform");
view14_uniform_mytexture = get_uniform(view14_program, "mytexture");
if (view14_attr_coord2d == -1 || view14_uniform_vertex_transform == -1
|| view14_uniform_texture_transform == -1
|| view14_uniform_mytexture == -1) {
return 1;
}
// Create our datapoints, store it as bytes
#define N 256
GLbyte graph[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
float x = (i - N / 2) / (N / 2.0);
float y = (j - N / 2) / (N / 2.0);
float d = hypotf(x, y) * 4.0;
float z = (1 - d * d) * expf(d * d / -2.0);
graph[i][j] = roundf(z * 127 + 128);
}
}
/* Upload the texture with our datapoints */
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &view14_texture_id);
glBindTexture(GL_TEXTURE_2D, view14_texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, N, N, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, graph);
// Create two vertex buffer objects
glGenBuffers(2, view14_vbo);
// Create an array for 101 * 101 vertices
glm::vec2 vertices[101][101];
for (int i = 0; i < 101; i++) {
for (int j = 0; j < 101; j++) {
vertices[i][j].x = (j - 50) / 50.0;
vertices[i][j].y = (i - 50) / 50.0;
}
}
// Tell OpenGL to copy our array to the buffer objects
glBindBuffer(GL_ARRAY_BUFFER, view14_vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Create an array of indices into the vertex array that
// traces both horizontal and vertical lines
GLushort indices[100 * 101 * 4];
int i = 0;
for (int y = 0; y < 101; y++) {
for (int x = 0; x < 100; x++) {
indices[i++] = y * 101 + x;
indices[i++] = y * 101 + x + 1;
}
}
for (int x = 0; x < 101; x++) {
for (int y = 0; y < 100; y++) {
indices[i++] = y * 101 + x;
indices[i++] = (y + 1) * 101 + x;
}
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, view14_vbo[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
indices, GL_STATIC_DRAW);
return 0;
}
void view14Display()
{
glUseProgram(view14_program);
glUniform1i(view14_uniform_mytexture, 0);
glm::mat4 model;
if (view14_rotate) {
model = glm::rotate(glm::mat4(1.0f),
glm::radians(glutGet(GLUT_ELAPSED_TIME) / 100.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
} else {
model = glm::mat4(1.0f);
}
glm::mat4 view = glm::lookAt(glm::vec3(0.0, -2.0, 2.0),
glm::vec3(0.0, 0.0, 0.0),
glm::vec3(0.0, 0.0, 1.0));
glm::mat4 projection = glm::perspective(45.0f, 1.0f * 640 / 480, 0.1f, 10.0f);
glm::mat4 vertex_transform = projection * view * model;
glm::mat4 texture_transform =
glm::translate(glm::scale(glm::mat4(1.0f),
glm::vec3(view14_scale, view14_scale, 1)),
glm::vec3(view14_offset_x, view14_offset_y, 0));
glUniformMatrix4fv(view14_uniform_vertex_transform, 1, GL_FALSE,
glm::value_ptr(vertex_transform));
glUniformMatrix4fv(view14_uniform_texture_transform, 1, GL_FALSE,
glm::value_ptr(texture_transform));
glClear(GL_COLOR_BUFFER_BIT);
/* Set texture wrapping mode */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
view14_clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
view14_clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
/* Set texture interpolation mode */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
view14_interpolate ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
view14_interpolate ? GL_LINEAR : GL_NEAREST);
/* Draw the grid using the indices to our
* vertices using our vertex buffer objects */
glEnableVertexAttribArray(view14_attr_coord2d);
glBindBuffer(GL_ARRAY_BUFFER, view14_vbo[0]);
glVertexAttribPointer(view14_attr_coord2d, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, view14_vbo[1]);
glDrawElements(GL_LINES, 100 * 101 * 4, GL_UNSIGNED_SHORT, 0);
/* Stop using the vertex buffer object */
glDisableVertexAttribArray(view14_attr_coord2d);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glutSwapBuffers();
}
// TODO: merge pengium warrior to this
void view14_special(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_F1:
view14_interpolate = !view14_interpolate;
// printf("Interpolation is now %s\n",
// view14_interpolate ? "on" : "off");
break;
case GLUT_KEY_F2:
view14_clamp = !view14_clamp;
// printf("Clamping is now %s\n", view14_clamp ? "on" : "off");
break;
case GLUT_KEY_F3:
view14_rotate = !view14_rotate;
// printf("Rotation is now %s\n", view14_rotate ? "on" : "off");
break;
case GLUT_KEY_LEFT:
view14_offset_x -= 0.03;
break;
case GLUT_KEY_RIGHT:
view14_offset_x += 0.03;
break;
case GLUT_KEY_UP:
view14_offset_y += 0.03;
break;
case GLUT_KEY_DOWN:
view14_offset_y -= 0.03;
break;
case GLUT_KEY_PAGE_UP:
view14_scale *= 1.5;
break;
case GLUT_KEY_PAGE_DOWN:
view14_scale /= 1.5;
break;
case GLUT_KEY_HOME:
view14_offset_x = 0.0;
view14_offset_y = 0.0;
view14_scale = 1.0;
break;
}
glutPostRedisplay();
}
void view14_freeResources()
{
glDeleteProgram(view14_program);
glDeleteBuffers(2, view14_vbo);
glDeleteTextures(1, &view14_texture_id);
}
void view14_entry(Window *window)
{
window->display = view14Display;
window->entry = viewEntry;
window->init = view14_initResources;
window->free = view14_freeResources;
window->special = view14_special;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view14_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */