-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
148 lines (131 loc) · 3.69 KB
/
Main.cpp
File metadata and controls
148 lines (131 loc) · 3.69 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
// Libraries
#include "Libs.h";
// Initialisations
#include "Init.h"
void processNormalKeys(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
camera->move(FORWARD);
break;
case 's':
camera->move(BACKWARD);
break;
case 'a':
camera->move(LEFT);
break;
case 'd':
camera->move(RIGHT);
break;
case '[':
camera->move(UP);
break;
case ']':
camera->move(DOWN);
break;
case 27: // Esc -> exit
glutLeaveMainLoop();
break;
}
}
void processSpecialKeys(int key, int xx, int yy)
{
switch (key)
{
case GLUT_KEY_UP:
camera->updatePitch(1.0);
break;
case GLUT_KEY_DOWN:
camera->updatePitch(-1.0);
break;
case GLUT_KEY_LEFT:
camera->updateYaw(-1.0);
break;
case GLUT_KEY_RIGHT:
camera->updateYaw(1.0);
break;
}
}
void Initialize(void)
{
// INIT GL FUNCTIONS
glClearColor(0.5f, 0.5f, 0.5f, 1.0f); // specify clear values for the color buffers
glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
//glFrontFace(GL_CCW);
glEnable(GL_BLEND); // render images with different levels of transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // factor values for blending
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // How polygons are drawn, try GL_LINE
//glutSetCursor(GLUT_CURSOR_NONE); // Don't display the mouse cursor
// INIT VARIABLES
initMatrices();
initShaders();
initTextures();
initMaterials();
initModels();
initLights();
initCamera();
initUniforms();
}
void RenderFunction(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers each frame
glEnable(GL_DEPTH_TEST); // Depth Buffer -> if z value < current z value, don't render pixel
// UPDATE UNIFORMS
glUseProgram(*shaders[SHADER_CORE_PROGRAM]); // Use Program (Shader)
updateUniforms();
// DRAW
for (auto& i : models)
{
//i->rotate(glm::vec3(0.f, 0.01f, 0.f));
i->render(*shaders[SHADER_CORE_PROGRAM]);
}
// END DRAW
glutSwapBuffers(); // One buffer is shown, one buffer is drawn
glFlush(); // Push all buffered operations to OpenGL
}
void reshapeFcn(GLint newWidth, GLint newHeight)
{
// Resize Viewport, Width, Height
glViewport(0, 0, newWidth, newHeight);
winWidth = newWidth;
winHeight = newHeight;
// To maintain aspect ratio, recalculate ProjectionMatrix and send it to the shader
float width = winWidth / 10, height = winHeight / 10;
ProjectionMatrix = glm::perspective(glm::radians(45.0f), width / height, 0.1f, 100.0f);
glUseProgram(*shaders[SHADER_CORE_PROGRAM]);
glUniformMatrix4fv(glGetUniformLocation(*shaders[SHADER_CORE_PROGRAM], "ProjectionMatrix"), 1, GL_FALSE, glm::value_ptr(ProjectionMatrix));
glUseProgram(0);
}
void Cleanup(void)
{
// Destroy Objects / Free Memory
glDeleteProgram(*shaders[SHADER_CORE_PROGRAM]);
freeMemory();
}
int main(int argc, char* argv[])
{
// INIT GLUT
glutInit(&argc, argv);
// CREATE WINDOW
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); // RGBA, Depth and double buffer
glutInitWindowPosition(0, 0); // relative to the screen
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Proiect II");
// INIT GLEW
if (glewInit() != GLEW_OK)
{
std::cout << "ERROR::MAIN.CPP::GLEW_INIT_FAILED\n";
glutLeaveMainLoop();
}
Initialize();
glutDisplayFunc(RenderFunction); // Redraw windows when change size, move window, key press, etc.
glutIdleFunc(glutPostRedisplay); // When no events are happening -> redisplay window
glutKeyboardFunc(processNormalKeys); // Key pressed -> processNormalKeys callback for the current window
glutSpecialFunc(processSpecialKeys);
glutReshapeFunc(reshapeFcn); // When resize window -> callback reshapeFcn
glutCloseFunc(Cleanup); // When window is destroyed, call Cleanup function
glutMainLoop(); // Loops main looking for events, keeps window open
}