-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderScene.cpp
More file actions
executable file
·251 lines (184 loc) · 8 KB
/
renderScene.cpp
File metadata and controls
executable file
·251 lines (184 loc) · 8 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
#include "common_header.h"
#include "glfw_OpenGLApp.h"
#include "shaders.h"
#include "texture.h"
#include "vertexBufferObject.h"
#include "flyingCamera.h"
#include "skybox.h"
#include "dirLight.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#define NUMTEXTURES 5
/* One VBO, where all static data are stored now,
in this tutorial vertex is stored as 3 floats for
position, 2 floats for texture coordinate and
3 floats for normal vector. */
CVertexBufferObject vboSceneObjects, vboCubeInd, vboCube;
uint uiVAOs[2]; // Only one VAO now
CTexture tTextures[NUMTEXTURES];
CFlyingCamera cCamera;
CSkybox sbMainSkybox;
CDirectionalLight dlSun;
#include "static_geometry.h"
// Initializes OpenGL features that will be used.
// lpParam - Pointer to anything you want.
void InitScene(void * param)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Prepare all scene objects
vboSceneObjects.CreateVBO();
glGenVertexArrays(2, uiVAOs); // Create one VAO
glBindVertexArray(uiVAOs[0]);
vboSceneObjects.BindVBO();
AddSceneObjects(vboSceneObjects);
vboSceneObjects.UploadDataToGPU(GL_STATIC_DRAW);
// Vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2*sizeof(glm::vec3)+sizeof(glm::vec2), 0);
// Texture coordinates
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(glm::vec3)+sizeof(glm::vec2), (void*)sizeof(glm::vec3));
// Normal vectors
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 2*sizeof(glm::vec3)+sizeof(glm::vec2), (void*)(sizeof(glm::vec3)+sizeof(glm::vec2)));
glBindVertexArray(uiVAOs[1]);
vboCube.CreateVBO();
vboCube.BindVBO();
AddCube(vboCube);
vboCube.UploadDataToGPU(GL_STATIC_DRAW);
vboCubeInd.CreateVBO();
// Bind indices
vboCubeInd.BindVBO(GL_ELEMENT_ARRAY_BUFFER);
vboCubeInd.AddData(&iCubeindices, sizeof(iCubeindices));
vboCubeInd.UploadDataToGPU(GL_STATIC_DRAW);
// Vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3) + sizeof(glm::vec2), 0);
// Texture coordinates
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3) + sizeof(glm::vec2), (void*)sizeof(glm::vec3));
// Normal vectors
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3) + sizeof(glm::vec2), (void*)(sizeof(glm::vec3) + sizeof(glm::vec2)));
if(!PrepareShaderPrograms())
{
appMain.Shutdown();
return;
}
// Load textures
string sTextureNames[] = {"grass.png", "met_wall01a.jpg", "tower.jpg", "box.jpg", "ground.jpg"};
FOR(i, NUMTEXTURES)
{
if(tTextures[i].LoadTexture2D("data/textures/"+sTextureNames[i], true)) {
std::cout << "Successfully loaded texture: " << sTextureNames[i] << std::endl;
} else {
std::cout << "Failed to load texture" << std::endl;
}
tTextures[i].SetFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR_MIPMAP);
}
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
cCamera = CFlyingCamera(glm::vec3(0.0f, 10.0f, 120.0f), glm::vec3(0.0f, 10.0f, 119.0f), glm::vec3(0.0f, 1.0f, 0.0f), 25.0f, 0.001f);
cCamera.SetMovingKeys('W', 'S', 'A', 'D');
sbMainSkybox.LoadSkybox("data/skyboxes/jajlands1/", "jajlands1_ft.jpg", "jajlands1_bk.jpg", "jajlands1_lf.jpg", "jajlands1_rt.jpg", "jajlands1_up.jpg", "jajlands1_dn.jpg");
dlSun = CDirectionalLight(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(sqrt(2.0f) / 2, -sqrt(2.0f) / 2, 0), 1.0f);
}
float fGlobalAngle;
float fTextureContribution = 0.5f;
// Renders whole scene.
// lpParam - Pointer to anything you want.
void RenderScene(void * param)
{
// Typecast lpParam to COpenGLControl pointer
COpenGLControl* oglControl = (COpenGLControl*)param;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
spMain.UseProgram();
glm::mat4 mModelMatrix, mView;
glm::vec3 vCameraDir = glm::normalize(cCamera.vView-cCamera.vEye);
oglControl->ResizeOpenGLViewportFull();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
spMain.UseProgram();
spMain.SetUniform("matrices.projMatrix", oglControl->GetProjectionMatrix());
spMain.SetUniform("gSamplers[0]", 0);
spMain.SetUniform("gSamplers[1]", 1);
spMain.SetUniform("fTextureContributions[0]", 1.0f);
spMain.SetUniform("fTextureContributions[1]", fTextureContribution);
spMain.SetUniform("numTextures", 1);
mView = cCamera.Look();
spMain.SetUniform("matrices.viewMatrix", &mView);
mModelMatrix = glm::translate(glm::mat4(1.0f), cCamera.vEye);
spMain.SetUniform("matrices.modelMatrix", &mModelMatrix);
spMain.SetUniform("matrices.normalMatrix", glm::transpose(glm::inverse(mView*mModelMatrix)));
CDirectionalLight dlSun2 = dlSun;
// We set full ambient for skybox, so that its color isn't affected by directional light
dlSun2.fAmbient = 1.0f;
dlSun2.vColor = glm::vec3(1.0f, 1.0f, 1.0f);
dlSun2.SetUniformData(&spMain, "sunLight");
sbMainSkybox.RenderSkybox();
glBindVertexArray(uiVAOs[1]);
// Render cubes
glm::mat4 mModelToCamera;
tTextures[3].BindTexture();
tTextures[1].BindTexture(1);
spMain.SetUniform("fTextureContributions[0]", 1.0f - fTextureContribution);
spMain.SetUniform("numTextures", 2);
float PI = float(atan(1.0)*4.0);
glEnable(GL_CULL_FACE);
//glFrontFace(GL_CCW); //Done by default
glm::vec3 vPos2 = glm::vec3(30.0f, 8.0f, 0.0f);
mModelMatrix = glm::mat4(1.0f);
mModelMatrix = glm::translate(mModelMatrix, vPos2);
mModelMatrix = glm::scale(mModelMatrix, glm::vec3(16.0f, 16.0f, 16.0f));
// We need to transform normals properly, it's done by transpose of inverse matrix of rotations and scales
spMain.SetUniform("matrices.normalMatrix", glm::transpose(glm::inverse(mModelMatrix)));
spMain.SetUniform("matrices.modelMatrix", mModelMatrix);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void*)0);
glDisable(GL_CULL_FACE);
spMain.SetUniform("fTextureContributions[0]", 1.0f);
spMain.SetUniform("numTextures", 1);
glBindVertexArray(uiVAOs[0]);
dlSun.SetUniformData(&spMain, "sunLight");
spMain.SetUniform("vColor", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
spMain.SetUniform("matrices.modelMatrix", glm::mat4(1.0f));
spMain.SetUniform("matrices.normalMatrix", glm::mat4(1.0f));
// Render ground
tTextures[0].BindTexture();
glDrawArrays(GL_TRIANGLES, 0, 6);
// render torus
tTextures[1].BindTexture();
// Now it's gonna float in the air
glm::vec3 vPos = glm::vec3(0.0f, 10.0, 0.0f);
mModelMatrix = glm::translate(glm::mat4(1.0), vPos);
mModelMatrix = glm::rotate(mModelMatrix, fGlobalAngle, glm::vec3(0.0f, 1.0f, 0.0f));
spMain.SetUniform("matrices.normalMatrix", glm::transpose(glm::inverse(mModelMatrix)));
spMain.SetUniform("matrices.modelMatrix", &mModelMatrix);
glDrawArrays(GL_TRIANGLES, 6, iTorusFaces * 3);
tTextures[2].BindTexture();
mModelMatrix = glm::translate(glm::mat4(1.0), vPos);
mModelMatrix = glm::rotate(mModelMatrix, fGlobalAngle, glm::vec3(1.0f, 0.0f, 0.0f));
spMain.SetUniform("matrices.normalMatrix", glm::transpose(glm::inverse(mModelMatrix)));
spMain.SetUniform("matrices.modelMatrix", &mModelMatrix);
glDrawArrays(GL_TRIANGLES, 6 + iTorusFaces * 3, iTorusFaces2 * 3);
cCamera.Update();
glEnable(GL_DEPTH_TEST);
if (Keys::Key('Q')) fTextureContribution += appMain.sof(-0.2f);
if (Keys::Key('E')) fTextureContribution += appMain.sof(0.2f);
fTextureContribution = min(max(0.0f, fTextureContribution), 1.0f);
fGlobalAngle += appMain.sof(1.0f);
oglControl->SwapBuffers();
}
// Releases OpenGL scene.
// lpParam - Pointer to anything you want.
void ReleaseScene(void * param)
{
FOR(i, NUMTEXTURES)tTextures[i].DeleteTexture();
sbMainSkybox.DeleteSkybox();
spMain.DeleteProgram();
FOR(i, NUMSHADERS)shShaders[i].DeleteShader();
glDeleteVertexArrays(2, uiVAOs);
vboSceneObjects.DeleteVBO();
vboCubeInd.DeleteVBO();
vboCube.DeleteVBO();
}