-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOcean.cpp
More file actions
250 lines (214 loc) · 7.16 KB
/
Ocean.cpp
File metadata and controls
250 lines (214 loc) · 7.16 KB
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
#include "Ocean.h"
#include <iostream>
#define VALS_PER_VERT 3
#define VALS_PER_TEX 2
#define VALS_PER_TANGENT 3
#define VALS_PER_COLOUR 4
#define CUBE_NUM_TRIS 12 // number of triangles in a cube (2 per face)
#define CUBE_NUM_VERTICES 8 // number of vertices in a cube`
/*
Setup the shaders, buffers, load the textures, get everything self contained for the renderCall
*/
Ocean::Ocean( const char* posx,
const char* posy,
const char* posz,
const char* negx,
const char* negy,
const char* negz,
float pposx,
float ppoxy,
float pposz,
float initScale)
{
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return;
}
//progID= LoadShaders("", "");
progID= LoadShaders("ocean.vert", "ocean.frag");
if (progID == 0){
std::cout << "Shaders did not load for skybox" << std::endl;
return;
}
create_cube_map (
posx,
posy,
posz,
negx,
negy,
negz,
&tex_cube);
// Cube has 8 vertices at its corners
float cubeVertices[ CUBE_NUM_VERTICES*VALS_PER_VERT ] = {
-1.0f, -2.0f, 1.0f ,
1.0f, -2.0f, 1.0f ,
1.0f, 0.0f, 1.0f ,
-1.0f, 0.0f, 1.0f ,
-1.0f, -2.0f, -1.0f ,
1.0f, -2.0f, -1.0f ,
1.0f, 0.0f, -1.0f ,
-1.0f, 0.0f, -1.0f
};
// Colours for each vertex; red, green, blue and alpha
float cubeNormals[ CUBE_NUM_VERTICES*VALS_PER_VERT ] = {
-0.0f, -1.0f, 0.0f ,
0.0f, -1.0f, 0.0f ,
0.0f, 1.0f, 0.0f ,
-0.0f, 1.0f, 0.0f ,
-0.0f, 1.0f, -0.0f ,
0.0f, 1.0f, -0.0f ,
0.0f, 1.0f, -0.0f ,
-0.0f, 1.0f, -0.0f
};
// Texture coords for each vertex. 2 per vertex.
float tex_coord[CUBE_NUM_VERTICES * VALS_PER_TEX] = {
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 0.0f,
0.0f, 0.0f,
};
float tangents[CUBE_NUM_VERTICES * VALS_PER_TANGENT] = {
//incorrect but only concerned with the top face which is OK
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0
};
// Each square face is made up of two triangles
unsigned int indices[CUBE_NUM_TRIS * 3] = {
0,1,2, 2,3,0,
1,5,6, 6,2,1,
5,4,7, 7,6,5,
4,0,3, 3,7,4,
3,2,6, 6,7,3,
4,5,1, 1,0,4
};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
int vertLoc = glGetAttribLocation(progID, "a_vertex");
int normLoc = glGetAttribLocation(progID, "a_normal" );
int texLoc = glGetAttribLocation(progID, "a_tex_coord" );
int tangentLoc = glGetAttribLocation(progID, "a_tangent" );
// Buffers to store position, colour and index data
unsigned int buffer[5];
glGenBuffers(5, buffer);
// Set vertex position
glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertLoc);
glVertexAttribPointer(vertLoc, VALS_PER_VERT, GL_FLOAT, GL_FALSE, 0, 0);
// Normal attributes
glBindBuffer(GL_ARRAY_BUFFER, buffer[1]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(cubeNormals), cubeNormals, GL_STATIC_DRAW);
glEnableVertexAttribArray(normLoc);
glVertexAttribPointer(normLoc, VALS_PER_VERT, GL_FLOAT, GL_FALSE, 0, 0);
// Texture attributes
glBindBuffer(GL_ARRAY_BUFFER, buffer[2]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(tex_coord), tex_coord, GL_STATIC_DRAW);
glEnableVertexAttribArray(texLoc);
glVertexAttribPointer(texLoc, VALS_PER_TEX, GL_FLOAT, GL_FALSE, 0, 0);
// Texture attributes
glBindBuffer(GL_ARRAY_BUFFER, buffer[3]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(tangents), tangents, GL_STATIC_DRAW);
glEnableVertexAttribArray(tangentLoc);
glVertexAttribPointer(tangentLoc, VALS_PER_TEX, GL_FLOAT, GL_FALSE, 0, 0);
// Set element attributes. Notice the change to using GL_ELEMENT_ARRAY_BUFFER
// We don't attach this to a shader label, instead it controls how rendering is performed
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[4]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(indices), indices, GL_STATIC_DRAW);
// Un-bind
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
viewMatrix=glm::mat4();
projectionMatrix=glm::mat4();
boxScale=initScale;
timer=0.1;
}
void Ocean::render()
{
glUseProgram(progID);
glm::vec3 cameraTranslate = glm::vec3(viewMatrix[3]);
glm::mat4 modelMatrix = glm::scale( glm::mat4(),glm::vec3(boxScale,boxScale,boxScale));
GLint loc = glGetUniformLocation(progID, "intime");
if (loc != -1)
{
//std::cout << "update" << std::endl;
glUniform1f(loc, timer);
}
#ifdef DEBUG
std::cout << "Position Camera:" << cameraPosition.x << "," << cameraPosition.y << "," << cameraPosition.z << "," << std::endl;
#endif
GLint loc2 = glGetUniformLocation(progID, "cam_pos");
if (loc2 != -1)
{
//std::cout << "update" << std::endl;
glUniform3f(loc2, cameraPosition.x,cameraPosition.y,cameraPosition.z);
}
timer += 0.01;
//modelMatrix = glm::translate(modelMatrix,
// glm::vec3(-1.0f * cameraPosition[0],-1.0f * cameraPosition[1],-1.0f * cameraPosition[2]));
if ( setUniformMatrix( progID,
"M_matrix",
glm::value_ptr(modelMatrix) ) ) {
std::cout << "Couldnt find M_matrix in skybox shader" << std::endl;
return;
}
/*
glm::mat3 m_3x3_inv_transp = glm::transpose(glm::inverse(glm::mat3(modelMatrix)));
if ( setUniformMatrix3( progID,
"m_3x3_inv_transp",
glm::value_ptr(m_3x3_inv_transp) ) ) {
std::cout << "Couldnt find m_3x3_inv_transp in ocean shader" << std::endl;
return;
}
*/
glm::mat4 cameraRotation = glm::translate(viewMatrix,
glm::vec3(1.0f*cameraTranslate[0],-1.0f*cameraTranslate[1],-1.0f*cameraTranslate[2]));
if ( setUniformMatrix( progID,
"V_matrix",
glm::value_ptr(viewMatrix) ) ) {
std::cout << "Couldnt find V_matrix in ocean shader" << std::endl;
return;
}
if ( setUniformMatrix( progID,
"P_matrix",
glm::value_ptr(projectionMatrix) ) ) {
std::cout << "Couldnt find P_matrix in ocean shader" << std::endl;
return;
}
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex_cube);
GLuint uniform_mytexture = glGetUniformLocation(progID, "cube_texture");
glUniform1i(progID, uniform_mytexture);
glDepthMask (GL_TRUE);
glUseProgram (progID);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glDrawElements(GL_TRIANGLES, CUBE_NUM_TRIS * VALS_PER_VERT, GL_UNSIGNED_INT, 0); // New call
//glBindVertexArray (vao);
//glDrawArrays (GL_TRIANGLES, 0, 36);
glDepthMask (GL_TRUE);
glBindVertexArray(0);
//glutSwapBuffers();
//glFlush();
//std::cout << "whizz" << std::endl;
}
Ocean::~Ocean()
{
}