forked from FallDownT/CS452-PROJECT1
/
project.cpp
370 lines (295 loc) · 12.5 KB
/
project.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* by Lakshitha T. Premathilake
The Following code is to simulate an oil jet release from deep water.
Still this is under development. At the moment this is just working as a fountain,
like a primivitve particle system. Development is in process.
The code will be only working on openGL 4.1, since layout qualifiers have been
used in vertex shader*/
//===================================================================================================================
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <time.h>
#include "Matrix.h" // NEW!
//#include "cube.h"
//#define USING_INDEX_BUFFER 1
#define PARTICLE_COUNT 1000 // New definition for particle count
#ifdef USING_INDEX_BUFFER
#define NUM_VERTICES num_vertices
#define NUM_INDICES num_indices
#else
#define NUM_VERTICES num_vertices
#endif
// From http://www.opengl.org/registry/specs/EXT/pixel_buffer_object.txt
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
GLfloat emitter_pos[] = {0.0f, -0.5f, 0.0f}; // NEW!
GLuint shaderProgramID;
GLuint vao = 0;
GLuint vbo;
GLuint emitterID, timeID;
GLuint indexBufferID;
float current=0;
float current_seconds=0;
clock_t l;
float Timer(void);
// ==================== BEGIN NEW STUFF ============================
GLuint perspectiveMatrixID, viewMatrixID, modelMatrixID; // IDs of variables mP, mV and mM in the shader
GLfloat* rotXMatrix; // Matrix for rotations about the X axis
GLfloat* rotYMatrix; // Matrix for rotations about the Y axis
GLfloat* rotZMatrix; // Matrix for rotations about the Z axis
GLfloat* transMatrix; // Matrix for changing the position of the object
GLfloat* scaleMatrix; // Duh..
GLfloat* tempMatrix1; // A temporary matrix for holding intermediate multiplications
GLfloat* M; // The final model matrix M to change into world coordinates
GLfloat* V; // The camera matrix (for position/rotation) to change into camera coordinates
GLfloat* P; // The perspective matrix for the camera (to give the scene depth); initialize this ONLY ONCE!
GLfloat theta; // An amount of rotation along one axis
GLfloat scaleAmount; // In case the object is too big or small
void initMatrices() {
theta = 0.0f;
scaleAmount = 1.0f;
// Allocate memory for the matrices and initialize them to the Identity matrix
rotXMatrix = new GLfloat[16]; makeIdentity(rotXMatrix);
rotYMatrix = new GLfloat[16]; makeIdentity(rotYMatrix);
rotZMatrix = new GLfloat[16]; makeIdentity(rotZMatrix);
transMatrix = new GLfloat[16]; makeIdentity(transMatrix);
scaleMatrix = new GLfloat[16]; makeIdentity(scaleMatrix);
tempMatrix1 = new GLfloat[16]; makeIdentity(tempMatrix1);
M = new GLfloat[16]; makeIdentity(M);
V = new GLfloat[16]; makeIdentity(V);
P = new GLfloat[16]; makeIdentity(P);
// Set up the (P)erspective matrix only once! Arguments are 1) the resulting matrix, 2) FoV, 3) aspect ratio, 4) near plane 5) far plane
makePerspectiveMatrix(P, 60.0f, 1.0f, 1.0f, 1000.0f);
}
//==================== END NEW STUFF ==========================================
#pragma region SHADER_FUNCTIONS
static char* readFile(const char* filename) {
// Open the file
FILE* fp = fopen (filename, "r");
// Move the file pointer to the end of the file and determing the length
fseek(fp, 0, SEEK_END);
long file_length = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* contents = new char[file_length+1];
// zero out memory
for (int i = 0; i < file_length+1; i++) {
contents[i] = 0;
}
// Here's the actual read
fread (contents, 1, file_length, fp);
// This is how you denote the end of a string in C
contents[file_length+1] = '\0';
fclose(fp);
return contents;
}
bool compiledStatus(GLint shaderID){
GLint compiled = 0;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
if (compiled) {
return true;
}
else {
GLint logLength;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
char* msgBuffer = new char[logLength];
glGetShaderInfoLog(shaderID, logLength, NULL, msgBuffer);
printf ("%s\n", msgBuffer);
delete (msgBuffer);
return false;
}
}
GLuint makeVertexShader(const char* shaderSource) {
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource (vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
glCompileShader(vertexShaderID);
bool compiledCorrectly = compiledStatus(vertexShaderID);
if (compiledCorrectly) {
return vertexShaderID;
}
return -1;
}
GLuint makeFragmentShader(const char* shaderSource) {
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
glCompileShader(fragmentShaderID);
bool compiledCorrectly = compiledStatus(fragmentShaderID);
if (compiledCorrectly) {
return fragmentShaderID;
}
return -1;
}
GLuint makeShaderProgram (GLuint vertexShaderID, GLuint fragmentShaderID) {
GLuint shaderID = glCreateProgram();
glAttachShader(shaderID, vertexShaderID);
glAttachShader(shaderID, fragmentShaderID);
glLinkProgram(shaderID);
return shaderID;
}
#pragma endregion SHADER_FUNCTIONS
//===============================================================================
// Any time the window is resized, this function gets called. It's setup to the
// "glutReshapeFunc" in main.
void changeViewport(int w, int h){
glViewport(0, 0, w, h);
}
// Here is the function that gets called each time the window needs to be redrawn.
// It is the "paint" method for our program, and is set up from the glutDisplayFunc in main
void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// New!
glUseProgram(shaderProgramID);
//theta = 0.01f;
scaleAmount = sin(theta);
// Fill the matrices with valid data
makeScale(scaleMatrix, 1.0f, 1.0f, 1.0f); // Fill the scaleMatrix variable
makeRotateY(rotYMatrix, theta); // Fill the rotYMatrix variable
//makeTranslate(transMatrix, -0.25f, -0.25f, -0.25f); // Fill the transMatrix to push the model back 1 "unit" into the scene
makeTranslate(V, 0.0f, 0.0f, -1.0f); // Fill the (V)iew matrix to push the world up 1 unit
// Multiply them together
//matrixMult4x4(M, transMatrix, rotYMatrix); // Order is important!
//matrixMult4x4(M, rotYMatrix, transMatrix); // This is backwards, unless you want orbiting!
matrixMult4x4(tempMatrix1, transMatrix, scaleMatrix); // Scale, then rotate...
matrixMult4x4(M, rotYMatrix, tempMatrix1); // ... then multiply THAT by the translate
// Important! Pass that data to the shader variables
//glUniformMatrix4fv(modelMatrixID, 1, GL_TRUE, M);
glUniformMatrix4fv(viewMatrixID, 1, GL_TRUE, V);
glUniformMatrix4fv(perspectiveMatrixID, 1, GL_TRUE, P);
glUniform3fv(emitterID, 1, emitter_pos);
/* Render Particles. Enabling point re-sizing in vertex shader */
glEnable (GL_PROGRAM_POINT_SIZE);
glEnable (GL_BLEND);
//glActiveTexture (GL_TEXTURE0);
//glBindTexture (GL_TEXTURE_2D, tex);
glUseProgram (shaderProgramID);
current_seconds=Timer();
/* update time in shaders */
glUniform1f (timeID, (GLfloat)current_seconds);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#ifdef USING_INDEX_BUFFER
glDrawElements (GL_TRIANGLES, NUM_INDICES, GL_UNSIGNED_INT, NULL);
#else
//glDrawArrays(GL_TRIANGLES, 0, NUM_VERTICES);
glBindVertexArray (vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays (GL_POINTS, 0, PARTICLE_COUNT);
glDisable (GL_BLEND);
glDisable (GL_PROGRAM_POINT_SIZE);
l=clock();
#endif
glutSwapBuffers();
glutPostRedisplay(); // NEW! This calls "render" again, allowing for animation!
}
void keyboard(unsigned char key,int x,int y){
switch(key){
case'S':
theta-=0.01f; break;
case'D':
theta+=0.01f; break;
}
}
//======================PARTICLE GENERATION=================================================
/* create initial attribute values for particles. return a VAO */
GLuint gen_particles () {
float vv[PARTICLE_COUNT * 3]; // start velocities vec3
float vt[PARTICLE_COUNT]; // start times
float t_accum = 0.0f; // start time
int j = 0;
for (int i = 0; i < PARTICLE_COUNT; i++) {
// start times
vt[i] = t_accum;
t_accum += 0.01f; // spacing for start times is 0.01 seconds
// start velocities. randomly vary x and z components
float randx = ((float)rand() / (float)RAND_MAX) * 1.0f - 0.5f;
float randz = ((float)rand() / (float)RAND_MAX) * 1.0f - 0.5f;
vv[j] = randx; // x
vv[j + 1] = 2.0f; // y
vv[j + 2] = randz; // z
j+= 3;
}
GLuint velocity_vbo;
glGenBuffers (1, &velocity_vbo);
glBindBuffer (GL_ARRAY_BUFFER, velocity_vbo);
glBufferData (GL_ARRAY_BUFFER, sizeof (vv), vv, GL_STATIC_DRAW);
GLuint time_vbo;
glGenBuffers (1, &time_vbo);
glBindBuffer (GL_ARRAY_BUFFER, time_vbo);
glBufferData (GL_ARRAY_BUFFER, sizeof (vt), vt, GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glBindBuffer (GL_ARRAY_BUFFER, velocity_vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer (GL_ARRAY_BUFFER, time_vbo);
glVertexAttribPointer (1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray (0);
glEnableVertexAttribArray (1);
return vao;
}
//============================================================================================
float Timer(){
static clock_t t1=clock();
clock_t t2 = l;
float time_elapsed = (1.005*(float)t2-(float)t1)/CLOCKS_PER_SEC;
current = current + time_elapsed;
t1=t2;
return current;
}
int main (int argc, char** argv) {
// Standard stuff...
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Index Buffers");
glutReshapeFunc(changeViewport);
glewInit();
initMatrices(); // New <========================================
vao=gen_particles();
/*#ifdef USING_INDEX_BUFFER
#endif*/
// Make a shader
char* vertexShaderSourceCode = readFile("vertexShader1.vsh");
char* fragmentShaderSourceCode = readFile("fragmentShader1.fsh");
GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);
/*/ / Create the "remember all"
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Create the buffer, but don't load anything yet
//glBufferData(GL_ARRAY_BUFFER, 7*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, 6*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
// Load the vertex points
glBufferSubData(GL_ARRAY_BUFFER, 0, 3*NUM_VERTICES*sizeof(GLfloat), vertices);
// Load the colors right after that
//glBufferSubData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat),4*NUM_VERTICES*sizeof(GLfloat), colors);
glBufferSubData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat),3*NUM_VERTICES*sizeof(GLfloat), normals);*/
/*#ifdef USING_INDEX_BUFFER
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES*sizeof(GLuint), indices, GL_STATIC_DRAW);
#endif*/
// Find the position of the variables in the shader
//ositionID = glGetAttribLocation(shaderProgramID, "v_i");
//colorID = glGetAttribLocation(shaderProgramID, "elapsed_system_time");
// ============ New! glUniformLocation is how you pull IDs for uniform variables===============
perspectiveMatrixID = glGetUniformLocation(shaderProgramID, "P");
viewMatrixID = glGetUniformLocation(shaderProgramID, "V");
emitterID=glGetUniformLocation(shaderProgramID, "emitter_pos_wor");
timeID=glGetUniformLocation(shaderProgramID, "elapsed_system_time");
//=============================================================================================
/*glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
//glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
glVertexAttribPointer(colorID, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
printf ("BUFFER_OFFSET is: %d\n", sizeof(vertices));*/
glUseProgram(shaderProgramID);
//glEnableVertexAttribArray(positionID);
//glEnableVertexAttribArray(colorID);
//glEnable(GL_DEPTH_TEST);
//glClearColor( 0.0, 0.0, 0.0, 0.2 );
//current_seconds=Timer();
glutDisplayFunc(render);
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}