9 AppMain::~AppMain(
void) {
12 ShaderMap::const_iterator itr;
13 for(itr = _shaders.begin(); itr != _shaders.end(); ++itr) {
15 _shaders[itr->first] = NULL;
18 if (_molecule != NULL)
26 if (_occlusionDirections != NULL)
27 delete _occlusionDirections;
29 glDeleteTextures(1,&_shadowTex);
30 glDeleteTextures(1,&_ambientOcclusionTex);
31 glDeleteFramebuffers(1,&_FBO);
37 void AppMain::GenerateShaders() {
41 _shaders[
"main"] =
new Shader(
"../Shaders/main");
45 _shaders[
"shadow"] =
new Shader(
"../Shaders/shadow");
48 _shaders[
"atlas"] =
new Shader(
"../Shaders/atlas");
50 if (!_shaders[
"main"] || !_shaders[
"shadow"] || !_shaders[
"atlas"] ) {
51 throw "Could not compile minimal shader program.";
54 _shaders[
"atlas"]->bind_frag_data_location(
"out_color");
55 _shaders[
"main"]->bind_frag_data_location(
"out_color");
66 _camera =
new Camera(GetWindowWidth(),GetWindowHeight());
71 _aoTextureSize = 2048;
73 _shadowTextureSize = _aoTextureSize;
77 glGenFramebuffers(1, &_FBO);
78 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _FBO);
79 glGenTextures(1, &_shadowTex);
80 glBindTexture(GL_TEXTURE_2D, _shadowTex);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
83 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
84 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
85 glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,_shadowTextureSize, _shadowTextureSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
86 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowTex, 0);
87 glReadBuffer(GL_NONE);
88 glBindTexture(GL_TEXTURE_2D, 0);
89 glGenTextures(1, &_ambientOcclusionTex);
90 glBindTexture(GL_TEXTURE_2D, _ambientOcclusionTex);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
93 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
94 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
97 if (GetOcclusionQuality()>2)
98 glTexImage2D (GL_TEXTURE_2D, 0, GL_R16, _aoTextureSize, _aoTextureSize, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
100 glTexImage2D (GL_TEXTURE_2D, 0, GL_RED, _aoTextureSize, _aoTextureSize, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
102 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _ambientOcclusionTex, 0);
103 glBindTexture(GL_TEXTURE_2D, 0);
104 glBindFramebuffer(GL_FRAMEBUFFER, 0);
116 glClearColor(0.3,0.3,0.3,1);
117 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 glBindVertexArray(_vao);
125 if (_shadowsActive) {
130 glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
131 glDrawBuffer(GL_NONE);
132 glViewport(0,0,_aoTextureSize,_aoTextureSize);
133 glClearColor(0,0,0,1);
134 glClear(GL_DEPTH_BUFFER_BIT);
136 _shaders[
"shadow"]->bind();
138 glUniformMatrix4fv(_shaders[
"shadow"]->GetAddress(Shader::ShadowPMatrix), 1, GL_FALSE, glm::value_ptr(*(_light->
GetProjectionMatrix())));
140 glDrawArrays(GL_POINTS, 0, _numberOfSpheres);
142 _shaders[
"shadow"]->unbind();
144 glDrawBuffer(GL_COLOR_ATTACHMENT0);
152 glBindFramebuffer(GL_FRAMEBUFFER, 0);
153 glViewport(0, 0, GetWindowWidth(),GetWindowHeight());
154 glClearColor(0.3,0.3,0.3,1);
155 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
158 _shaders[
"main"]->bind();
159 glBindTexture(GL_TEXTURE_2D, _shadowTex);
160 glUniform1i(_shaders[
"main"]->GetAddress(Shader::ShadowTex), 0);
161 glActiveTexture(GL_TEXTURE0 + 1);
162 glBindTexture(GL_TEXTURE_2D, _ambientOcclusionTex);
165 glUniform1i(_shaders[
"main"]->GetAddress(Shader::OcclusionTexture), 1);
166 glUniformMatrix4fv(_shaders[
"main"]->GetAddress(Shader::ProjectionMatrix), 1, GL_FALSE, glm::value_ptr(_camera->
GetProjectionMatrix()));
167 glUniform4fv(_shaders[
"main"]->GetAddress(Shader::LightPosition), 1, glm::value_ptr(_camera->
GetViewMatrix()*glm::vec4(_light->
GetPosition(),1)));
168 glUniformMatrix4fv(_shaders[
"main"]->GetAddress(Shader::ModelViewMatrix), 1, GL_FALSE, glm::value_ptr(_camera->
GetViewMatrix()* _molecule->
GetModelMatrix()));
170 glUniformMatrix4fv(_shaders[
"main"]->GetAddress(Shader::ShadowPMatrix), 1, GL_FALSE, glm::value_ptr(*(_light->
GetProjectionMatrix())));
174 glDrawArrays(GL_POINTS, 0, _numberOfSpheres);
177 glBindTexture(GL_TEXTURE_2D, 0);
178 glActiveTexture(GL_TEXTURE0 + 0);
179 glBindTexture(GL_TEXTURE_2D, 0);
180 glBindVertexArray(0);
181 _shaders[
"main"]->unbind();
186 void AppMain::ComputeAmbientOcclusion() {
188 glClearColor(0,0,0,1);
190 glViewport(0,0,_aoTextureSize,_aoTextureSize);
191 glBindVertexArray(_vao);
193 glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
194 glClear(GL_COLOR_BUFFER_BIT);
199 glEnable(GL_DEPTH_TEST);
200 glDrawBuffer(GL_NONE);
201 glClear(GL_DEPTH_BUFFER_BIT);
203 _shaders[
"shadow"]->bind();
205 glUniformMatrix4fv(_shaders[
"shadow"]->GetAddress(Shader::ShadowPMatrix), 1, GL_FALSE, glm::value_ptr(_orthoMatrix));
206 glUniformMatrix4fv(_shaders[
"shadow"]->GetAddress(Shader::ShadowMVMatrix), 1, GL_FALSE, glm::value_ptr(_occlusionDirections->
GetViewMatrix(i)));
208 glDrawArrays(GL_POINTS, 0, _numberOfSpheres);
210 _shaders[
"shadow"]->unbind();
215 glDrawBuffer(GL_COLOR_ATTACHMENT0);
217 glBindTexture(GL_TEXTURE_2D, _shadowTex);
218 glDisable(GL_DEPTH_TEST);
220 _shaders[
"atlas"]->bind();
222 glUniform1i(_shaders[
"atlas"]->GetAddress(Shader::OcclusionTexture), 0);
223 glUniform1f(_shaders[
"atlas"]->GetAddress(Shader::OcclusionDirections),_occlusionDirections->
GetDirectionNumber());
224 glUniformMatrix4fv(_shaders[
"atlas"]->GetAddress(Shader::ShadowPMatrix), 1, GL_FALSE, glm::value_ptr(_orthoMatrix));
225 glUniformMatrix4fv(_shaders[
"atlas"]->GetAddress(Shader::ShadowMVMatrix), 1, GL_FALSE, glm::value_ptr(_occlusionDirections->
GetViewMatrix(i)));
227 glDrawArrays(GL_POINTS, 0, _numberOfSpheres);
231 _shaders[
"atlas"]->unbind();
232 glBindTexture(GL_TEXTURE_2D, 0);
237 glEnable(GL_DEPTH_TEST);
239 glBindVertexArray(0);
240 glBindFramebuffer(GL_FRAMEBUFFER, 0);
252 _light->
RotateXY(y,x,4*_loader ->GetBoundingSphere());
261 if (x<0 && pos.x> -_loader ->GetBoundingSphere())
262 _camera->
Translate(0.01 * x * pos.z, 0.0f, 0.0f);
264 _camera->
Translate(0.01 * x * pos.z, 0.0f, 0.0f);
266 if (y<0 && pos.y> -_loader ->GetBoundingSphere())
267 _camera->
Translate(0.0f, 0.01 * y * pos.z, 0.0f);
269 _camera->
Translate(0.0f, 0.01 * y * pos.z, 0.0f);
280 if (z<0 && pos.z <= 0)
293 _camera->
UpdateProjectionMatrix(GetWindowWidth(),GetWindowHeight(), 5.5f * _loader ->GetBoundingSphere());
308 _light->
Reset(2.5f * _loader ->GetBoundingSphere(), 4.8f * _loader ->GetBoundingSphere());
314 _camera->
Translate(0,0,2*_loader ->GetBoundingSphere());
315 _camera->
UpdateProjectionMatrix(GetWindowWidth(),GetWindowHeight(), 5.5f * _loader ->GetBoundingSphere());
316 _shaders[
"main"]->bind();
317 glUniform1f(_shaders[
"main"]->GetAddress(Shader::FarPlane),5.5f * _loader ->GetBoundingSphere());
318 _shaders[
"main"]->unbind();
325 _orthoMatrix = glm::ortho(left,right,bottom,top,n,f);
332 ComputeAmbientOcclusion();
351 SetOcclusionQuality(occlusionQuality);
353 delete _occlusionDirections;
356 glBindTexture(GL_TEXTURE_2D, _ambientOcclusionTex);
359 if (GetOcclusionQuality()>2)
360 glTexImage2D (GL_TEXTURE_2D, 0, GL_R16, _aoTextureSize, _aoTextureSize, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
362 glTexImage2D (GL_TEXTURE_2D, 0, GL_RED, _aoTextureSize, _aoTextureSize, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
363 glBindTexture(GL_TEXTURE_2D, 0);
367 ComputeAmbientOcclusion();
373 _shaders[
"main"]->bind();
374 glUniform1f(_shaders[
"main"]->GetAddress(Shader::DirectIntensity),(
float)pointLightIntensity*(4.0f/255.0f));
375 _shaders[
"main"]->unbind();
378 _shaders[
"main"]->bind();
379 glUniform1f(_shaders[
"main"]->GetAddress(Shader::AmbientIntensity),(
float)ambientIntensity*(4.0f/255.0f));
380 _shaders[
"main"]->unbind();
383 _shaders[
"main"]->bind();
384 glUniform1f(_shaders[
"main"]->GetAddress(Shader::Glossiness),10 + glossiness/3.0f);
385 _shaders[
"main"]->unbind();
388 _shaders[
"main"]->bind();
389 glUniform1f(_shaders[
"main"]->GetAddress(Shader::ShadowActive),shadows);
390 _shaders[
"main"]->unbind();
391 _shadowsActive = shadows;
394 _shaders[
"main"]->bind();
395 glUniform2f(_shaders[
"main"]->GetAddress(Shader::ContourData),(
float)borderWidth*(3.0f/255.0f),(
float)borderVariance*(20.0f/255.0f));
396 _shaders[
"main"]->unbind();
400 _shaders[
"main"]->bind();
401 glUniform1f(_shaders[
"main"]->GetAddress(Shader::OcclusionActive),ambientOcclusionActive);
402 _shaders[
"main"]->unbind();
408 const string& AppMain::GetErrorMessage()
const {
409 return _errorMessage;