-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstub.cpp
More file actions
349 lines (286 loc) · 11.5 KB
/
stub.cpp
File metadata and controls
349 lines (286 loc) · 11.5 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
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
/******************************************************************************|
| CPSC 4050/6050 Computer Garphics Assignment 5, Daljit Singh Dhillon, 2020 |
| Reference: |
| |
| Some OpenGL setup code here including math_funcs, and gl_utils |
| are from Angton Gerdelan and "Anton's OpenGL 4 Tutorials." |
| http://antongerdelan.net/opengl/ |
| Email: anton at antongerdelan dot net |
| Copyright Dr Anton Gerdelan, Trinity College Dublin, Ireland. |
|******************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
#include "maths_funcs.h" // Anton's maths functions.
#include "gl_utils.h" // Anton's opengl functions and small utilities like logs
#include "stb_image.h" // Sean Barrett's image loader with Anton's load_texture()
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define _USE_MATH_DEFINES
#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
mat4 view_mat;
mat4 proj_mat;
mat4 model_mat;
int pointCount;
int basePointCount;
void loadSurfaceOfRevolution()
{
/*------------------------------CREATE GEOMETRY-------------------------------*/
int stepsY = 0;
int stepsT = 0;
std::cout<<"Enter how many steps along the Y axis: ";
std::cin>>stepsY;
while(stepsY < 3){
std::cout<<"Must be at least 3 steps. Please Re-enter: ";
std::cin>>stepsY;
}
std::cout<<"Enter how many steps around the y axis: ";
std::cin>>stepsT;
while(stepsT < 3){
std::cout<<"Must be at least 3 steps. Please Re-enter: ";
std::cin>>stepsT;
}
//calcluate points along the base curve (circular arc with equation x^2 + y^2 = 1)
std::vector<float> basePoints;
for(int i=0; i<=stepsY; i++){
float y = 1.0 - (i*(2.0/stepsY));
float x = sqrt(1.0-pow(y, 2));
//float x = -1*pow(y, 2) + 1;
//float x = sqrt(y+1);
basePoints.push_back(x);
basePoints.push_back(y);
basePoints.push_back(2);
//std::cout<< x << ", " << y << "\n";
}
basePointCount = basePoints.size();
//calculate all verticies based on the base curve at each step of theta
pointCount = stepsY*stepsT;
GLfloat vp[pointCount*18]; //array of vertex points
GLfloat normals[pointCount*18]; //array of normals
GLfloat vt[pointCount*12]; //array of texture coordinates
//for each level of Y, starting from the top, iterate through each vertex
for(int i = 0; i < stepsY; i++){
for(int j = 0; j < stepsT; j++){
int vIndex = (i*stepsY*18)+(j*18);
int nIndex = (i*stepsY*18)+(j*18);
int tIndex = (i*stepsY*12)+(j*12);
GLfloat Pij[3] = {basePoints[i*3]*cos(j*(2*M_PI/stepsT)),
basePoints[i*3+1],
basePoints[i*3]*sin(j*(2*M_PI/stepsT))};
GLfloat Piij[3] = {basePoints[(i+1)*3]*cos(j*(2*M_PI/stepsT)),
basePoints[(i+1)*3+1],
basePoints[(i+1)*3]*sin(j*(2*M_PI/stepsT))};
GLfloat Pijj[3] = {basePoints[i*3]*cos((j+1)*(2*M_PI/stepsT)),
basePoints[i*3+1],
basePoints[i*3]*sin((j+1)*(2*M_PI/stepsT))};
GLfloat Piijj[3] = {basePoints[(i+1)*3]*cos((j+1)*(2*M_PI/stepsT)),
basePoints[(i+1)*3+1],
basePoints[(i+1)*3]*sin((j+1)*(2*M_PI/stepsT))};
//load the points of the two triangles with their top left corner at P i,j
//P i,j
vp[vIndex+0] = Pij[0]; //x
vp[vIndex+1] = Pij[1]; //y
vp[vIndex+2] = Pij[2]; //z
//P i+1,j+1
vp[vIndex+3] = Piijj[0]; //x
vp[vIndex+4] = Piijj[1]; //y
vp[vIndex+5] = Piijj[2]; //z
//P i,j+1
vp[vIndex+6] = Pijj[0]; //x
vp[vIndex+7] = Pijj[1]; //y
vp[vIndex+8] = Pijj[2]; //z
//P i,j
vp[vIndex+9] = Pij[0]; //x
vp[vIndex+10] = Pij[1]; //y
vp[vIndex+11] = Pij[2]; //z
//P i+1,j
vp[vIndex+12] = Piij[0]; //x
vp[vIndex+13] = Piij[1]; //y
vp[vIndex+14] = Piij[2]; //z
//P i+1,j+1
vp[vIndex+15] = Piijj[0]; //x
vp[vIndex+16] = Piijj[1]; //y
vp[vIndex+17] = Piijj[2]; //z
//calculate and load normals
glm::vec3 edgeA1 = glm::vec3(Piijj[0] - Pij[0], Piijj[1] - Pij[1], Piijj[2] - Pij[2]);
glm::vec3 edgeA2 = glm::vec3(Pijj[0] - Pij[0], Pijj[1] - Pij[1], Pijj[2] - Pij[2]);
glm::vec3 normalA = glm::normalize(glm::cross(edgeA1, edgeA2));
for(int i=0; i<3; i++){
normals[nIndex+(i*3)+0] = normalA.x;
normals[nIndex+(i*3)+1] = normalA.y;
normals[nIndex+(i*3)+2] = normalA.z;
}
glm::vec3 edgeB1 = glm::vec3(Piij[0] - Pij[0], Piij[1] - Pij[1], Piij[2] - Pij[2]);
glm::vec3 edgeB2 = glm::vec3(Piijj[0] - Pij[0], Piijj[1] - Pij[1], Piijj[2] - Pij[2]);
glm::vec3 normalB = glm::normalize(glm::cross(edgeB1, edgeB2));
for(int i=0; i<3; i++){
normals[nIndex+(i*3)+9] = normalB.x;
normals[nIndex+(i*3)+10] = normalB.y;
normals[nIndex+(i*3)+11] = normalB.z;
}
//calculate and load texture coordinates
vt[tIndex+0] = (j*(2*M_PI/stepsT))/(2*M_PI); //u value (theta)
vt[tIndex+1] = (Pij[1]+1)/2; //v value
vt[tIndex+2] = (j+1)*(2*M_PI/stepsT)/(2*M_PI); //u value (theta)
vt[tIndex+3] = (Piijj[1]+1)/2; //v value
vt[tIndex+4] = (j+1)*(2*M_PI/stepsT)/(2*M_PI); //u value (theta)
vt[tIndex+5] = (Pijj[1]+1)/2; //v value
vt[tIndex+6] = j*(2*M_PI/stepsT)/(2*M_PI); //u value (theta)
vt[tIndex+7] = (Pij[1]+1)/2; //v value
vt[tIndex+8] = j*(2*M_PI/stepsT)/(2*M_PI); //u value (theta)
vt[tIndex+9] = (Piij[1]+1)/2; //v value
vt[tIndex+10] = (j+1)*(2*M_PI/stepsT)/(2*M_PI); //u value (theta)
vt[tIndex+11] = (Piijj[1]+1)/2; //v value
}
}
// VAO -- vertex attribute objects bundle the various things associated with vertices
GLuint vao;
glGenVertexArrays (1, &vao); // generating and binding is common pattern in OpenGL
glBindVertexArray (vao); // basically setting up memory and associating it
// VBO -- vertex buffer object to contain coordinates
// MODIFY THE FOLLOWING BLOCK OF CODE APPRORIATELY FOR YOUR SURFACE OF REVOLUTION
GLuint points_vbo;
glGenBuffers(1, &points_vbo);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glBufferData(GL_ARRAY_BUFFER, pointCount*18 * sizeof (GLfloat), vp, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// VBO -- normals -- needed for shading calcuations
// ADD CODE TO POPULATE AND LOAD PER-VERTEX SURFACE NORMALS
// [HINT] Vertex normals are organized in same order as that for vertex coordinates
GLuint normals_vbo;
glGenBuffers(1, &normals_vbo);
glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
glBufferData(GL_ARRAY_BUFFER, pointCount*18 * sizeof (GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
// VBO -- vt -- texture coordinates
// ADD CODE TO POPULATE AND LOAD PER-VERTEX TEXTURE COORDINATES
// [HINT] texture coordinates are organized in same order as that for vertex coordinates
// [HINT] there are two texture coordinates instead of three vertex coordinates for each vertex
GLuint vt_vbo;
glGenBuffers(1, &vt_vbo);
glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
glBufferData(GL_ARRAY_BUFFER, pointCount*12 * sizeof (GLfloat), vt, GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(2);
}
GLfloat lightX = 0.0;
GLfloat lightY = 0.0;
GLfloat lightZ = 10.0;
GLfloat shine = 32.0;
bool useTexture = true;
bool useSpecular = true;
bool useDiffuse = true;
void loadUniforms(GLuint shader_programme)
{
/*---------------------------SET RENDERING DEFAULTS---------------------------*/
// Choose vertex and fragment shaders to use as well as view and proj matrices.
int model_mat_location = glGetUniformLocation (shader_programme, "model_mat");
int view_mat_location = glGetUniformLocation (shader_programme, "view_mat");
int proj_mat_location = glGetUniformLocation (shader_programme, "proj_mat");
int light_pos_location = glGetUniformLocation (shader_programme, "light_position");
int light_col_location = glGetUniformLocation (shader_programme, "light_color");
int shininess_location = glGetUniformLocation (shader_programme, "shininess");
int useT_location = glGetUniformLocation (shader_programme, "useText");
int useS_location = glGetUniformLocation (shader_programme, "useSpec");
int useD_location = glGetUniformLocation (shader_programme, "useDiff");
glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, proj_mat.m);
glUniformMatrix4fv (model_mat_location, 1, GL_FALSE, model_mat.m);
glUniform3f(light_pos_location, lightX, lightY, lightZ);
glUniform3f(light_col_location, 1.0, 1.0, 1.0);
glUniform1f(shininess_location, shine);
glUniform1f(useT_location, useTexture);
glUniform1f(useS_location, useSpecular);
glUniform1f(useD_location, useDiffuse);
// WRITE CODE TO LOAD OTHER UNIFORM VARIABLES LIKE FLAGS FOR ENABLING OR DISABLING CERTAIN FUNCTIONALITIES
}
void drawSurfaceOfRevolution()
{
// MODIFY THIS LINE OF CODE APPRORIATELY FOR YOUR SURFACE OF REVOLUTION
glDrawArrays(GL_TRIANGLES, 0, pointCount*18);
}
void keyboardFunction(GLFWwindow* window, int key, int scancode, int action, int mods)
{
// MODIFY THIS FUNCTION FOR KEYBOARD INTERACTIVITY
//GLFW Reference Links:
// Callback Example: https://www.glfw.org/docs/3.3/input_guide.html#input_key
// List of Keys: https://www.glfw.org/docs/3.3/group__keys.html
if (key == GLFW_KEY_W && action == GLFW_PRESS)
{
printf("\nLight Position Moved Up\n");
lightY -= 1.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_S && action == GLFW_PRESS)
{
printf("\nLight Position Moved Down\n");
lightY += 1.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_D && action == GLFW_PRESS)
{
printf("\nLight Position Moved Right\n");
lightX -= 1.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_A && action == GLFW_PRESS)
{
printf("\nLight Position Moved Left\n");
lightX += 1.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_Q && action == GLFW_PRESS)
{
printf("\nLight Position Moved Closer\n");
lightZ += 1.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_E && action == GLFW_PRESS)
{
printf("\nLight Position Moved Further\n");
lightZ -= 1.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_Z && action == GLFW_PRESS)
{
printf("\nShine Increased\n");
shine *= 2.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_X && action == GLFW_PRESS)
{
printf("\nShine Decreased\n");
shine /= 2.0;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_R && action == GLFW_PRESS)
{
printf("\nTexture Mapping Toggled\n");
useTexture = !useTexture;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_T && action == GLFW_PRESS)
{
printf("Specular Lighting Toggled\n");
useSpecular = !useSpecular;
// Example case. Key 'E' pressed. Doing nothing
}
if (key == GLFW_KEY_Y && action == GLFW_PRESS)
{
printf("\nDiffuse Lighting Toggled\n");
useDiffuse = !useDiffuse;
// Example case. Key 'E' pressed. Doing nothing
}
if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) {
// Close window when esacape is pressed
glfwSetWindowShouldClose (g_window, 1);
}
}