This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
forked from FallDownT/CS452-LAB4
/
main.cpp
284 lines (236 loc) · 10.4 KB
/
main.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
// by Jeffrey Chastine
// shamelessly adapted by Mark Elsinger
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include "MathHelper.h"
#include "cube.h" // NEW! - look at this file if you haven't already
#define USING_INDEX_BUFFER 1
#ifdef USING_INDEX_BUFFER
#define NUM_VERTICES num_vertices // NEW! Note, these values are in the .h file, too
#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 light1[] = { -5.0f, 1.0f, 0.0f, 1.0f }; // NEW
GLfloat light2[] = { 5.0f, 1.0f, 0.0f, 1.0f }; // NEW
GLuint shaderProgramID;
GLuint vao = 0;
GLuint vbo;
GLuint positionID, normalID; // NEW
GLuint indexBufferID;
// ==================== BEGIN NEW STUFF ============================
GLuint perspectiveMatrixID, viewMatrixID, modelMatrixID; // IDs of variables mP, mV and mM in the shader
GLuint allRotsMatrixID; // NEW
GLuint lightID1; // NEW
GLuint lightID2;
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* allRotsMatrix; // NEW - we keep all of the model's rotations in this matrix (for rotating normals)
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
GLfloat camX, camY, camZ; // NEW! A first (purposely bad) attempt at camera movement
GLfloat yaw, pitch, roll; // NEW! Store this in a matrix instead!
void initMatrices() {
theta = 0.0f;
scaleAmount = 1.0f;
// Allocate memory for the matrices and initialize them to the Identity matrix
rotXMatrix = new GLfloat[16]; MathHelper::makeIdentity(rotXMatrix);
rotYMatrix = new GLfloat[16]; MathHelper::makeIdentity(rotYMatrix);
rotZMatrix = new GLfloat[16]; MathHelper::makeIdentity(rotZMatrix);
allRotsMatrix = new GLfloat[16]; MathHelper::makeIdentity(allRotsMatrix); // NEW
transMatrix = new GLfloat[16]; MathHelper::makeIdentity(transMatrix);
scaleMatrix = new GLfloat[16]; MathHelper::makeIdentity(scaleMatrix);
tempMatrix1 = new GLfloat[16]; MathHelper::makeIdentity(tempMatrix1);
M = new GLfloat[16]; MathHelper::makeIdentity(M);
V = new GLfloat[16]; MathHelper::makeIdentity(V);
P = new GLfloat[16]; MathHelper::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
MathHelper::makePerspectiveMatrix(P, 60.0f, 1.77f, 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);
}
void keyboardFunc(unsigned char key, int x, int y) {
switch (key) {
case 033:
case 'q':
exit(1);
break;
case 'w': camY -= 0.1f; break;
case 's': camY += 0.1f; break;
case 'a': camX += 0.1f; break;
case 'd': camX -= 0.1f; break;
case 'e': yaw += 0.05f; break;
case 'r': yaw -= 0.05f; break;
}
}
// 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);
glUseProgram(shaderProgramID);
theta += 0.001f;
scaleAmount = 0.5f; //sin(theta);
// Set the (M)odel matrix
MathHelper::makeScale(scaleMatrix, scaleAmount, scaleAmount, scaleAmount); // Fill the scaleMatrix variable
MathHelper::makeRotateY(rotYMatrix, theta); // Fill the rotYMatrix variable
MathHelper::makeTranslate(transMatrix, 0.0f, 0.0f, -2.0f); // Fill the transMatrix to push the model back 1 "unit" into the scene
// Multiply them together
MathHelper::matrixMult4x4(tempMatrix1, rotYMatrix, scaleMatrix); // Scale, then rotate...
MathHelper::matrixMult4x4(M, transMatrix, tempMatrix1); // ... then multiply THAT by the translate
// Copy the rotations into the allRotsMatrix
MathHelper::copyMatrix(rotYMatrix, allRotsMatrix);
// NEW! Change the (V)iew matrix if you want to "move" around the scene
MathHelper::makeRotateY(rotYMatrix, yaw);
MathHelper::makeTranslate(transMatrix, camX, camY, camZ);
MathHelper::matrixMult4x4(V, rotYMatrix, transMatrix);
// 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);
glUniformMatrix4fv(allRotsMatrixID, 1, GL_TRUE, allRotsMatrix);
glUniform4fv(lightID1, 1, light1);
glUniform4fv(lightID2, 1, light2);
//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);
#endif
glutSwapBuffers();
glutPostRedisplay(); // NEW! This calls "render" again, allowing for animation!
}
int main(int argc, char** argv) {
// Standard stuff...
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1080, 600);
glutCreateWindow("Lighted Cube");
glutReshapeFunc(changeViewport);
glutKeyboardFunc(keyboardFunc);
glutDisplayFunc(render);
glewInit();
initMatrices(); // New <========================================
// Make a shader
char* vertexShaderSourceCode = readFile("vertexShader.vsh");
char* fragmentShaderSourceCode = readFile("fragmentShader.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); // NEW! - we're only loading vertices and normals (6 elements, not 7)
// 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
positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
normalID = glGetAttribLocation(shaderProgramID, "s_vNormal"); // NEW
lightID1 = glGetUniformLocation(shaderProgramID, "vLight1"); // NEW
lightID2 = glGetUniformLocation(shaderProgramID, "vLight2"); // NEW
// ============ New! glUniformLocation is how you pull IDs for uniform variables===============
perspectiveMatrixID = glGetUniformLocation(shaderProgramID, "mP");
viewMatrixID = glGetUniformLocation(shaderProgramID, "mV");
modelMatrixID = glGetUniformLocation(shaderProgramID, "mM");
allRotsMatrixID = glGetUniformLocation(shaderProgramID, "mRotations"); // NEW
//=============================================================================================
glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(normalID, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
glUseProgram(shaderProgramID);
glEnableVertexAttribArray(positionID);
glEnableVertexAttribArray(normalID); // NEW
glEnable(GL_CULL_FACE); // NEW! - we're doing real 3D now
glCullFace(GL_BACK); // Other options? GL_FRONT and GL_FRONT_AND_BACK
glEnable(GL_DEPTH_TEST); // Make sure the depth buffer is on. As you draw a pixel, update the screen only if it's closer than previous ones
glutMainLoop();
return 0;
}