-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview9_sphere.cpp
319 lines (253 loc) · 10 KB
/
view9_sphere.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "view9_sphere.h"
#include "lib/Mesh.h"
#include "util.h"
#include "SOIL/SOIL.h"
#include "global.h"
#include <iostream>
using namespace std;
GLuint view9_sphere_program = 0;
GLuint view9_mytexture_id = 0, view9_mytexture_sunlit_id = 0;
GLuint view9_sphere_vbo = -1;
GLint view9_attr_v_coord = -1, view9_attr_v_normal = 1;
GLint view9_uniform_m = -1, view9_uniform_v = -1, view9_uniform_p = -1,
view9_uniform_m_3x3_inv_transp = -1, view9_uniform_v_inv = -1,
view9_uniform_mytexture = -1, view9_uniform_mytexture_sunlit = -1,
view9_uniform_mytexture_ST = -1;
struct demo view9_demos[] = {
// Textures Spheres
{"data/Earthmap720x360_grid.jpg",
"glsl/sphere.9.v.glsl", "glsl/sphere.9.f.glsl"},
{"data/Earthmap720x360_grid.jpg",
"glsl/sphere.9.v.glsl", "glsl/sphere_ST.9.f.glsl"},
// Lighting Textured Surfaces
{"data/Earthmap720x360_grid.jpg", "glsl/sphere-gouraud.9.v.glsl",
"glsl/sphere-gouraud.9.f.glsl"},
// Glossy Textures
{"data/Land_shallow_topo_alpha_2048.png", "glsl/sphere-gouraud.9.v.glsl",
"glsl/sphere-gouraud-glossy.9.f.glsl"},
{"data/Land_shallow_topo_alpha_2048.png", "glsl/sphere-phong.9.v.glsl",
"glsl/sphere-phong.9.f.glsl"},
// Transparent Textures
{"data/Land_shallow_topo_alpha_2048.png", "glsl/sphere.9.v.glsl",
"glsl/sphere_discard.9.f.glsl"},
{"data/Land_shallow_topo_alpha_2048.png",
"glsl/sphere.9.v.glsl", "glsl/sphere.9.f.glsl"},
{"data/Land_shallow_topo_alpha_2048.png", "glsl/sphere.9.v.glsl",
"glsl/sphere_oceans.9.f.glsl"},
// Layers of Textures
{"data/Earth_lights_lrg.jpg",
"glsl/sphere-sunlit.9.v.glsl", "glsl/sphere-sunlit.9.f.glsl"},
};
int view9_cur_demo = 0;
int view9_Sphere_initResources();
void view9SphereLogic()
{
float angle = glutGet(GLUT_ELAPSED_TIME) / 1000.0 * 30; // 30° per second
glm::mat4 anim = glm::rotate(glm::mat4(1.0f),
glm::radians(angle),
glm::vec3(0, 1, 0));
// Fix for Blender- or GLUT-style orientation (Z-is-up).
// Not necessary since switching to our own sphere code, but require
// fixing 'latitudeLongitude' in the shaders.
glm::mat4 fix_orientation = glm::rotate(glm::mat4(1.0f),
glm::radians(-90.f),
glm::vec3(1, 0, 0));
glm::mat4 model = glm::translate(glm::mat4(1.0f),
glm::vec3(0.0, 0.0, -2.0));
glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0),
glm::vec3(0.0, 0.0, -2.0),
glm::vec3(0.0, 1.0, 0.0));
glm::mat4 projection = glm::perspective(45.0f,
1.0f*128/128,
0.1f, 10.0f);
//glm::mat4 mvp = projection * view * model * anim * fix_orientation;
//glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
glUseProgram(view9_sphere_program);
glm::mat4 m = model * anim * fix_orientation;
glUniformMatrix4fv(view9_uniform_m, 1, GL_FALSE, glm::value_ptr(m));
/* Transform normal vectors with transpose of inverse of upper left
3x3 model matrix (ex-gl_NormalMatrix): */
glm::mat3 view9_m_3x3_inv_transp = glm::transpose(glm::inverse(glm::mat3(m)));
glUniformMatrix3fv(view9_uniform_m_3x3_inv_transp, 1, GL_FALSE,
glm::value_ptr(view9_m_3x3_inv_transp));
glUniformMatrix4fv(view9_uniform_v, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 v_inv = glm::inverse(view);
glUniformMatrix4fv(view9_uniform_v_inv, 1, GL_FALSE, glm::value_ptr(v_inv));
glUniformMatrix4fv(view9_uniform_p, 1, GL_FALSE, glm::value_ptr(projection));
// tiling and offset
if (view9_uniform_mytexture_ST >= 0) {
glUniform4f(view9_uniform_mytexture_ST, 2, 1, 0, -.05);
}
}
void view9SphereDisplay()
{
view9SphereLogic();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(view9_sphere_program);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, view9_mytexture_id);
glUniform1i(view9_uniform_mytexture, /*GL_TEXTURE*/0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, view9_mytexture_sunlit_id);
glUniform1i(view9_uniform_mytexture_sunlit, /*GL_TEXTURE*/1);
// To be used when FreeGLUT 3.0.0 is out :)
// glutSetVertexAttribCoord3(attribute_v_coord);
// glutSetVertexAttribNormal(attribute_v_normal);
// glutSolidSphere(1.0,30,30);
glEnableVertexAttribArray(view9_attr_v_coord);
glEnableVertexAttribArray(view9_attr_v_normal);
glBindBuffer(GL_ARRAY_BUFFER, view9_sphere_vbo);
glVertexAttribPointer(view9_attr_v_coord, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(view9_attr_v_normal, 3, GL_FLOAT, GL_FALSE, 0, 0);
// TODO: what's this?
glCullFace(GL_FRONT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2 * 31 * 30);
glCullFace(GL_BACK);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2 * 31 * 30);
glutSwapBuffers();
}
// TODO: something wrong with 3d texture, no back cube
void view9SphereKeyboard(unsigned char key, int x, int y)
{
switch(key)
{
case '1':
{
view9_cur_demo = ++view9_cur_demo %
(sizeof(view9_demos)/sizeof(struct demo));
// printf ("view9_demos = %d\n", sizeof(view9_demos));
break;
}
case '2':
{
// view7_shaders_index = ++view7_shaders_index % view7_shaders_num;
break;
}
default:
return ;
}
if (view9_sphere_program != 0){
glDeleteProgram(view9_sphere_program);
}
if (view9_mytexture_id != 0){
glDeleteTextures(1, &view9_mytexture_id);
}
if (view9_mytexture_sunlit_id != 0){
glDeleteTextures(1, &view9_mytexture_sunlit_id);
}
view9_Sphere_initResources();
}
// TODO: add shadow to subwindow when selected
int view9_Sphere_initResources()
{
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 0);
// printf("init_resources: %s %s %s\n",
// view9_demos[view9_cur_demo].texture_filename,
// view9_demos[view9_cur_demo].vshader_filename,
// view9_demos[view9_cur_demo].fshader_filename);
view9_mytexture_id = SOIL_load_OGL_texture(
view9_demos[view9_cur_demo].texture_filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y | SOIL_FLAG_TEXTURE_REPEATS);
if (view9_mytexture_id == 0) {
cerr << "SOIL loading error: '"
<< SOIL_last_result()
<< "' (" << view9_demos[view9_cur_demo].texture_filename
<< ")" << endl;
}
// day-time texture:
view9_mytexture_sunlit_id = SOIL_load_OGL_texture(
"data/Land_shallow_topo_2048.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y | SOIL_FLAG_TEXTURE_REPEATS);
if (view9_mytexture_sunlit_id == 0) {
cerr << "SOIL loading error: '"
<< SOIL_last_result()
<< "' ("
<< "Land_shallow_topo_2048.jpg"
<< ")" << endl;
}
view9_sphere_program = create_program(
view9_demos[view9_cur_demo].vshader_filename,
view9_demos[view9_cur_demo].fshader_filename);
const char* attr_name;
attr_name = "v_coord";
view9_attr_v_coord = glGetAttribLocation(view9_sphere_program, attr_name);
if (view9_attr_v_coord == -1) {
fprintf(stderr, "Could not bind attr %s\n", attr_name);
return 0;
}
attr_name = "v_normal";
view9_attr_v_normal = glGetAttribLocation(view9_sphere_program, attr_name);
if (view9_attr_v_normal == -1) {
// fprintf(stderr, "Warning: Could not bind attr %s\n", attr_name);
}
const char* uniform_name;
uniform_name = "m";
view9_uniform_m = get_uniform(view9_sphere_program, uniform_name);
if (view9_uniform_m == -1) {
fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
return 0;
}
uniform_name = "v";
view9_uniform_v = get_uniform(view9_sphere_program, uniform_name);
if (view9_uniform_v == -1) {
fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
return 0;
}
uniform_name = "p";
view9_uniform_p = get_uniform(view9_sphere_program, uniform_name);
if (view9_uniform_p == -1) {
fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
return 0;
}
uniform_name = "m_3x3_inv_transp";
view9_uniform_m_3x3_inv_transp = get_uniform(view9_sphere_program,
uniform_name);
uniform_name = "v_inv";
view9_uniform_v_inv = get_uniform(view9_sphere_program,
uniform_name);
uniform_name = "mytexture";
view9_uniform_mytexture = get_uniform(view9_sphere_program,
uniform_name);
uniform_name = "mytexture_sunlit";
view9_uniform_mytexture_sunlit = get_uniform(view9_sphere_program,
uniform_name);
if (strstr(view9_demos[view9_cur_demo].fshader_filename, "_ST")) {
uniform_name = "mytexture_ST";
view9_uniform_mytexture_ST = get_uniform(view9_sphere_program,
uniform_name);
}
view9_sphere_vbo = sphere(1, 30, 30);
return 0;
}
void view9_Sphere_freeResources()
{
glDeleteProgram(view9_sphere_program);
glDeleteTextures(1, &view9_mytexture_id);
glDeleteTextures(1, &view9_mytexture_sunlit_id);
}
void view9_entry(Window *window)
{
window->display = view9SphereDisplay;
window->entry = viewEntry;
window->init = view9_Sphere_initResources;
window->free = view9_Sphere_freeResources;
window->keyboard = view9SphereKeyboard;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view9_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */