Skip to content

Commit 436b3f1

Browse files
committed
Added based class for shadow map
1 parent 87455fc commit 436b3f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+486
-154
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
*.opensdf
12
*.sdf
23
/astria/Debug

Release/astria.exe

544 KB
Binary file not shown.

Release/astria.pdb

0 Bytes
Binary file not shown.

Release/gfx/img/Thumbs.db

0 Bytes
Binary file not shown.

Release/gfx/img/sprites/Thumbs.db

0 Bytes
Binary file not shown.

Release/shaders/main_shader.frag

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ uniform Material mat;
4646
uniform FogParameters fogParams;
4747

4848
uniform int bSkybox;
49-
uniform int bFog;
49+
uniform bool bFog;
5050
uniform bool bEnableNormalMap;
5151

5252
smooth in vec2 vTexCoord;
@@ -64,7 +64,7 @@ void main()
6464
{
6565
vec3 vTexColor = sunLight.vColor * vec3(texture2D(mat.diffuse, vTexCoord));
6666
outputColor = vec4(vTexColor * sunLight.fBrightness, 1.0f);
67-
if(bFog == 1)
67+
if(bFog)
6868
{
6969
float fFogCoord = 500 *250*fogParams.fDensity;
7070
outputColor = mix(outputColor, fogParams.vFogColor, getFogFactor(fogParams, fFogCoord));
@@ -98,7 +98,7 @@ void main()
9898

9999
outputColor = vec4(vAmbientColor + vDiffuseColor + vSpecularColor, 1.0f);
100100

101-
if (bFog == 1)
101+
if (bFog)
102102
{
103103
float fFogCoord = distance(vEyePosition, vWorldPos);
104104
outputColor = mix(outputColor, fogParams.vFogColor, getFogFactor(fogParams, fFogCoord));

astria.v12.suo

512 Bytes
Binary file not shown.

astria/AppStateMain.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "AppStateManager.h"
22
#include "AppStateMain.h"
33
#include "Main.h"
4-
#include "utils.h"
54
#include "Stringify.h"
65

76
CAppStateMain CAppStateMain::Instance;
@@ -168,7 +167,7 @@ void CAppStateMain::OnUpdate()
168167
ParticleSnow.ChangePosition(glm::vec3(Position.x, Position.y + 200, Position.z));
169168
ParticleSnow.SetMatrices(&ProjectionMatrix, &ViewMatrix, Direction);
170169

171-
float dist = Distance(FirePosition, Position);
170+
float dist = glm::distance(FirePosition, Position);
172171
int vol = 1/(dist - 50) * MIX_MAX_VOLUME;
173172
SoundFire.SetVolume(vol);
174173
}
@@ -257,19 +256,12 @@ void CAppStateMain::OnRender()
257256
//Render particles
258257
float fps = CFPS::FPSControl.GetFPS();
259258
float FrameInterval = 1 / fps;
260-
TextureParticleEruption.Bind();
261259
ParticleEruption.Update(FrameInterval);
262260
ParticleEruption.Render();
263-
264-
TextureParticleSmoke.Bind();
265261
ParticleSmoke.Update(FrameInterval);
266262
ParticleSmoke.Render();
267-
268-
TextureParticleFire.Bind();
269263
ParticleFire.Update(FrameInterval);
270264
ParticleFire.Render();
271-
272-
TextureParticleSnow.Bind();
273265
ParticleSnow.Update(FrameInterval);
274266
ParticleSnow.Render();
275267

@@ -288,6 +280,11 @@ void CAppStateMain::OnRender()
288280
SDL_GL_SwapWindow(CMain::GetInstance()->GetWindow());
289281
}
290282

283+
void CAppStateMain::RenderScene(CShaderProgram* modelProgram, CShaderProgram* instanceProgram, CShaderProgram* terrainProgram)
284+
{
285+
286+
}
287+
291288
int CAppStateMain::OnLoad()
292289
{
293290
//Initialize GLEW
@@ -375,9 +372,18 @@ int CAppStateMain::OnLoad()
375372

376373
ModelMatrices.clear(); ModelMatrices.shrink_to_fit();
377374

375+
std::string ParticleShaders[] =
376+
{
377+
"shaders/particles_update.vert",
378+
"shaders/particles_update.geom",
379+
"shaders/particles_render.vert",
380+
"shaders/particles_render.geom",
381+
"shaders/particles_render.frag"
382+
};
383+
378384
//Load particles
379-
TextureParticleEruption.Load_2D("gfx/particle.bmp", true);
380-
ParticleEruption.Init();
385+
ParticleEruption.Init(ParticleShaders);
386+
ParticleEruption.SetTexture("gfx/particle.bmp");
381387
FirePosition = glm::vec3(64, 0, 193);
382388
FirePosition.y = Map.GetHeight(FirePosition);
383389
ParticleEruption.Set(
@@ -392,8 +398,8 @@ int CAppStateMain::OnLoad()
392398
0.02, //Spawn interval
393399
1000); //Count i.e. number generated per frame
394400

395-
TextureParticleFire.Load_2D("gfx/img/FireParticle.jpg", true);
396-
ParticleFire.Init();
401+
ParticleFire.Init(ParticleShaders);
402+
ParticleFire.SetTexture("gfx/img/FireParticle.jpg");
397403
ParticleFire.Set(
398404
FirePosition,
399405
glm::vec3(-7, 8, -7),
@@ -407,8 +413,8 @@ int CAppStateMain::OnLoad()
407413
50);
408414

409415
FirePosition.y += 3;
410-
TextureParticleSmoke.Load_2D("gfx/img/SmokeParticle.jpg", true);
411-
ParticleSmoke.Init();
416+
ParticleSmoke.Init(ParticleShaders);
417+
ParticleSmoke.SetTexture("gfx/img/SmokeParticle.jpg");
412418
ParticleSmoke.Set(
413419
FirePosition,
414420
glm::vec3(-5, 8, -5),
@@ -421,8 +427,8 @@ int CAppStateMain::OnLoad()
421427
0.2,
422428
10);
423429

424-
TextureParticleSnow.Load_2D("gfx/img/SnowParticle.png", true);
425-
ParticleSnow.Init();
430+
ParticleSnow.Init(ParticleShaders);
431+
ParticleSnow.SetTexture("gfx/img/SnowParticle.png");
426432
ParticleSnow.Set(
427433
glm::vec3(0, 500, 0),
428434
glm::vec3(-30, -20, -30),
@@ -460,7 +466,7 @@ int CAppStateMain::OnLoad()
460466

461467
SoundFire.Load("sound/fireplace.wav", 1);
462468
//This is need so we dont hear the sound effect for a split second at the very beginning
463-
float dist = Distance(FirePosition, Position);
469+
float dist = glm::distance(FirePosition, Position);
464470
int vol = 1 / (dist - 50) * MIX_MAX_VOLUME;
465471
SoundFire.SetVolume(vol);
466472
SoundFire.Play(-1);

astria/AppStateMain.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#define _APP_STATE_GL_H_
33

44
#include "AppState.h"
5+
#include "Common.h"
56
#include "Shader.h"
67
#include "Light.h"
78
#include "Skybox.h"
89
#include "HeightMap.h"
9-
#include "Params.h"
1010
#include "Model.h"
1111
#include "Water.h"
1212
#include "ParticleSystem.h"
@@ -32,10 +32,18 @@ class CAppStateMain : public CAppState
3232

3333
SDL_Texture* GetSnapshot();
3434

35+
private:
36+
//Private methods
37+
int OnLoad();
38+
void RenderScene(CShaderProgram* modelProgram, CShaderProgram* instanceProgram, CShaderProgram* terrainProgram);
39+
//Event functions
40+
void OnKeyDown(SDL_Keycode sym, Uint16 mod, SDL_Scancode scancode);
41+
void OnKeyUp(SDL_Keycode sym, Uint16 mod, SDL_Scancode scancode);
42+
void OnLoseFocus();
43+
3544
private:
3645
static CAppStateMain Instance;
3746
int Loaded;
38-
int OnLoad();
3947
SDL_GLContext glContext;
4048

4149
const int OPENGL_MAJOR_VERSION = 3;
@@ -49,11 +57,6 @@ class CAppStateMain : public CAppState
4957

5058
bool MoveLeft, MoveRight, MoveUp, MoveDown;
5159

52-
//Event functions
53-
void OnKeyDown(SDL_Keycode sym, Uint16 mod, SDL_Scancode scancode);
54-
void OnKeyUp(SDL_Keycode sym, Uint16 mod, SDL_Scancode scancode);
55-
void OnLoseFocus();
56-
5760
//Shaders & Shader programs
5861
CShader ShaderVertex;
5962
CShader ShaderFragment;

astria/Common.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,40 @@
2121
#include <glm\glm.hpp>
2222
#include <glm\gtc\matrix_transform.hpp>
2323

24+
#include "Params.h"
25+
2426
#define NDEBUG
2527

2628
extern std::string HelpText;
2729
extern std::string CreditsText;
2830

31+
//=====================
32+
// HELPER FUNCTIONS =
33+
//=====================
34+
35+
//returns a random integer between x and y
36+
inline int RandInt(int x, int y) { return rand() % (y - x + 1) + x; }
37+
//returns a random float between zero and 1
38+
inline double RandFloat() { return (rand()) / (RAND_MAX + 1.0); }
39+
inline double RandFloatInterval(float min, float max) { float interval = max - min; return min + interval*RandFloat(); }
40+
//returns a random bool
41+
inline bool RandBool(){ if (RandInt(0, 1)) return true; else return false; }
42+
//returns a random float in the range -1 < n < 1
43+
inline double RandNormal() { return RandFloat() - RandFloat(); }
44+
//Clamp first argument between 2nd and 3rd arguments
45+
inline void Clamp(double &arg, double min, double max) { arg = arg <= min ? min : (arg >= max ? max : arg); }
46+
inline void Clamp(float &arg, float min, float max) { arg = arg <= min ? min : (arg >= max ? max : arg); }
47+
inline void Clamp(int &arg, int min, int max) { arg = arg <= min ? min : (arg >= max ? max : arg); }
48+
//Radian to degree
49+
inline double ToDeg(double radian) { return radian * 180 / CParams::Pi; }
50+
51+
//Log functions
52+
inline void Error(const char* title, const char* text) { MessageBox(NULL, text, title, MB_ICONERROR); }
53+
inline void Error(const char* title, std::string text) { MessageBox(NULL, text.c_str(), title, MB_ICONERROR); }
54+
inline void Error(std::string title, std::string text) { MessageBox(NULL, text.c_str(), title.c_str(), MB_ICONERROR); }
55+
inline void Warning(const char* title, const char* text) { MessageBox(NULL, text, title, MB_ICONWARNING); }
56+
inline void Warning(const char* title, std::string text) { MessageBox(NULL, text.c_str(), title, MB_ICONWARNING); }
57+
inline void Warning(std::string title, std::string text) { MessageBox(NULL, text.c_str(), title.c_str(), MB_ICONWARNING); }
58+
59+
2960
#endif

astria/FBO.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "FBO.h"
2+
3+
CFBO::CFBO()
4+
{
5+
FrameBuffer = NULL;
6+
DepthBuffer = NULL;
7+
}
8+
9+
bool CFBO::CreateDepthShadowBuffer()
10+
{
11+
const char* LOG_TAG = "FBO::CreateDepthShadowBuffer";
12+
glGenFramebuffers(1, &FrameBuffer);
13+
14+
Texture.CreateEmpty(1024, 1024, 0);
15+
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL_DEPTH_COMPONENT16, GL_FLOAT, 0);
16+
Texture.SetFiltering(TEXTURE_FILTER_MAG_NEAREST, TEXTURE_FILTER_MIN_NEAREST);
17+
18+
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, Texture.GetID(), 0);
19+
glDrawBuffer(GL_NONE);
20+
21+
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
22+
{
23+
Error(LOG_TAG, "glCheckFramebufferStatus failed");
24+
return false;
25+
}
26+
}
27+
28+
bool CFBO::CreateWithTexture(int width, int height)
29+
{
30+
const char* LOG_TAG = "FBO::CreateWithTexture";
31+
if (FrameBuffer != 0)
32+
{
33+
Error(LOG_TAG, "Trying to create into an existing buffer");
34+
return false;
35+
}
36+
37+
glGenFramebuffers(1, &FrameBuffer);
38+
glBindFramebuffer(GL_FRAMEBUFFER, FrameBuffer);
39+
40+
Texture.CreateEmpty(width, height, GL_RGB);
41+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Texture.GetID(), 0);
42+
43+
Width = width;
44+
Height = height;
45+
46+
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
47+
{
48+
Error(LOG_TAG, "glCheckFramebufferStatus failed");
49+
return false;
50+
}
51+
52+
return true;
53+
}
54+
55+
bool CFBO::AddDepthBuffer()
56+
{
57+
if (FrameBuffer == 0) return false;
58+
59+
glBindFramebuffer(GL_FRAMEBUFFER, FrameBuffer);
60+
61+
glGenRenderbuffers(1, &DepthBuffer);
62+
glBindRenderbuffer(GL_RENDERBUFFER, DepthBuffer);
63+
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, Width, Height);
64+
65+
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthBuffer);
66+
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
67+
}
68+
69+
void CFBO::Bind(bool fullViewPort)
70+
{
71+
glBindFramebuffer(GL_FRAMEBUFFER, FrameBuffer);
72+
if (fullViewPort) glViewport(0, 0, 1024, 1024);
73+
}
74+
75+
void CFBO::BindShadowMap()
76+
{
77+
glBindFramebuffer(GL_FRAMEBUFFER, FrameBuffer);
78+
glViewport(0, 0, 1024, 1024);
79+
}
80+
81+
void CFBO::SetTextureFiltering(int mag, int min)
82+
{
83+
Texture.SetFiltering(mag, min);
84+
Texture.SetWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
85+
}
86+
87+
void CFBO::BindTexture(int textureUnit, bool mipmaps)
88+
{
89+
Texture.Bind(textureUnit);
90+
if (mipmaps) glGenerateMipmap(GL_TEXTURE_2D);
91+
}
92+
93+
void CFBO::Release()
94+
{
95+
if (FrameBuffer)
96+
{
97+
glDeleteFramebuffers(1, &FrameBuffer);
98+
FrameBuffer = 0;
99+
}
100+
if (DepthBuffer)
101+
{
102+
glDeleteRenderbuffers(1, &DepthBuffer);
103+
DepthBuffer = 0;
104+
}
105+
Texture.Release();
106+
}
107+
108+
glm::mat4 CFBO::GetProjectionMatrix(float a_fov, float a_near, float a_far)
109+
{
110+
return glm::perspective(a_fov, float(Width) / float(Height), a_near, a_far);
111+
}
112+
113+
glm::mat4 CFBO::GetOrthogonalMatrix()
114+
{
115+
return glm::ortho(0.0f, float(Width), 0.0f, float(Height));
116+
}
117+
118+
int CFBO::GetWidth()
119+
{
120+
return Width;
121+
}
122+
123+
int CFBO::GetHeight()
124+
{
125+
return Height;
126+
}

astria/FBO.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef _FBO_H_
2+
#define _FBO_H_
3+
4+
#include "Common.h"
5+
#include "Texture.h"
6+
7+
class CFBO
8+
{
9+
public:
10+
CFBO::CFBO();
11+
12+
bool Create();
13+
14+
void Bind(bool fullViewPort = true);
15+
void BindShadowMap();
16+
17+
void SetTextureFiltering(int mag, int min);
18+
void BindTexture(int textureUnit = 0, bool mipmaps = false);
19+
20+
void Release();
21+
22+
bool CreateDepthShadowBuffer();
23+
bool CreateWithTexture(int width, int height);
24+
25+
bool AddDepthBuffer();
26+
27+
glm::mat4 GetProjectionMatrix(float a_fov, float a_near, float a_far);
28+
glm::mat4 GetOrthogonalMatrix();
29+
int GetWidth();
30+
int GetHeight();
31+
private:
32+
GLuint FrameBuffer;
33+
GLuint DepthBuffer;
34+
CTexture Texture;
35+
int Width, Height;
36+
};
37+
38+
#endif

0 commit comments

Comments
 (0)