Skip to content

Commit d201629

Browse files
committed
fix memory leak
1 parent a85217c commit d201629

File tree

7 files changed

+102
-45
lines changed

7 files changed

+102
-45
lines changed

include/happy_bird/world.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class World{
4444
public:
4545
// shared
4646
static bool keys_pressed[1024];
47+
static bool exit;
4748
static int height, width;
4849
static Camera* camera;
4950
static glm::vec3 global_ambient;

shader/common.frag

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,6 @@
22

33
const float zero = 0.00000001;
44

5-
#define UnfoldParallelSingle(n) if(i == n){ \
6-
color += CalculateParallelLight(eye, lightCollection.parallel[n], material);\
7-
}
8-
#define UnfoldParallel UnfoldParallelSingle(0)\
9-
UnfoldParallelSingle(1)\
10-
UnfoldParallelSingle(2)\
11-
UnfoldParallelSingle(3)\
12-
UnfoldParallelSingle(4)
13-
#define UnfoldPointSingle(n) if(i == n){ \
14-
color += CalculatePointLight(eye, lightCollection.point[n], material);\
15-
}
16-
#define UnfoldPoint UnfoldPointSingle(0)\
17-
UnfoldPointSingle(1)\
18-
UnfoldPointSingle(2)\
19-
UnfoldPointSingle(3)\
20-
UnfoldPointSingle(4)
21-
#define UnfoldSpotSingle(n) if(i == n){ \
22-
color += CalculateSpotLight(eye, lightCollection.spot[n], material);\
23-
}
24-
#define UnfoldSpot UnfoldSpotSingle(0)\
25-
UnfoldSpotSingle(1)\
26-
UnfoldSpotSingle(2)\
27-
UnfoldSpotSingle(3)\
28-
UnfoldSpotSingle(4)
29-
30-
315
struct Attenuation {
326
float range;
337
float constant;
@@ -66,9 +40,9 @@ struct LightCollectionTotal {
6640
struct LightCollection {
6741
vec3 ambient;
6842
LightCollectionTotal total;
69-
ParallelLight parallel[1];
70-
PointLight point[1];
71-
SpotLight spot[1];
43+
ParallelLight parallel[4];
44+
PointLight point[4];
45+
SpotLight spot[4];
7246
};
7347

7448
struct PureColorMaterial {
@@ -90,7 +64,7 @@ in vec3 vNormal;
9064

9165
out vec4 fragColor;
9266

93-
vec3 CalculatePointlLight(Eye eye, PointLight light, PureColorMaterial material) {
67+
vec3 CalculatePointLight(Eye eye, PointLight light, PureColorMaterial material) {
9468
vec3 lightDirection = normalize(light.position - vPosition);
9569
float diffuseFactor = max(dot(vNormal, lightDirection), 0.0) * light.intensity;
9670

@@ -146,16 +120,46 @@ vec3 CalculateSpotLight(Eye eye, SpotLight light, PureColorMaterial material) {
146120
vec3 CalculateFragmentColor(Eye eye, LightCollection lightCollection, PureColorMaterial material) {
147121
vec3 color = lightCollection.ambient;
148122
for (int i = 0; i < lightCollection.total.parallel; i++) {
149-
// UnfoldParallel
150-
color += CalculateParallelLight(eye, lightCollection.parallel[i], material);
123+
if(i == 0){
124+
color += CalculateParallelLight(eye, lightCollection.parallel[0], material);
125+
}
126+
if(i == 1){
127+
color += CalculateParallelLight(eye, lightCollection.parallel[1], material);
128+
}
129+
if(i == 2){
130+
color += CalculateParallelLight(eye, lightCollection.parallel[2], material);
131+
}
132+
if(i == 3){
133+
color += CalculateParallelLight(eye, lightCollection.parallel[3], material);
134+
}
151135
}
152136
for (int i = 0; i < lightCollection.total.point; i++) {
153-
// UnfoldPoint
154-
color += CalculatePointlLight(eye, lightCollection.point[i], material);
137+
if(i == 0){
138+
color += CalculatePointLight(eye, lightCollection.point[0], material);
139+
}
140+
if(i == 1){
141+
color += CalculatePointLight(eye, lightCollection.point[1], material);
142+
}
143+
if(i == 2){
144+
color += CalculatePointLight(eye, lightCollection.point[2], material);
145+
}
146+
if(i == 3){
147+
color += CalculatePointLight(eye, lightCollection.point[3], material);
148+
}
155149
}
156150
for (int i = 0; i < lightCollection.total.spot; i++) {
157-
// UnfoldSpot
158-
color += CalculateSpotLight(eye, lightCollection.spot[i], material);
151+
if(i == 0){
152+
color += CalculateSpotLight(eye, lightCollection.spot[0], material);
153+
}
154+
if(i == 1){
155+
color += CalculateSpotLight(eye, lightCollection.spot[1], material);
156+
}
157+
if(i == 2){
158+
color += CalculateSpotLight(eye, lightCollection.spot[2], material);
159+
}
160+
if(i == 3){
161+
color += CalculateSpotLight(eye, lightCollection.spot[3], material);
162+
}
159163
}
160164
return color;
161165
}

shader/hero.frag

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ struct LightCollectionTotal {
4040
struct LightCollection {
4141
vec3 ambient;
4242
LightCollectionTotal total;
43-
ParallelLight parallel[1];
44-
PointLight point[1];
45-
SpotLight spot[1];
43+
ParallelLight parallel[4];
44+
PointLight point[4];
45+
SpotLight spot[4];
4646
};
4747

4848
struct TextureMaterial {
@@ -65,7 +65,7 @@ in vec2 vTexCoord;
6565

6666
out vec4 fragColor;
6767

68-
vec3 CalculatePointlLight(Eye eye, PointLight light, TextureMaterial material) {
68+
vec3 CalculatePointLight(Eye eye, PointLight light, TextureMaterial material) {
6969
vec3 lightDirection = normalize(light.position - vPosition);
7070
float diffuseFactor = max(dot(vNormal, lightDirection), 0.0) * light.intensity;
7171

@@ -121,13 +121,46 @@ vec3 CalculateSpotLight(Eye eye, SpotLight light, TextureMaterial material) {
121121
vec3 CalculateFragmentColor(Eye eye, LightCollection lightCollection, TextureMaterial material) {
122122
vec3 color = lightCollection.ambient;
123123
for (int i = 0; i < lightCollection.total.parallel; i++) {
124-
color += CalculateParallelLight(eye, lightCollection.parallel[i], material);
124+
if(i == 0){
125+
color += CalculateParallelLight(eye, lightCollection.parallel[0], material);
126+
}
127+
if(i == 1){
128+
color += CalculateParallelLight(eye, lightCollection.parallel[1], material);
129+
}
130+
if(i == 2){
131+
color += CalculateParallelLight(eye, lightCollection.parallel[2], material);
132+
}
133+
if(i == 3){
134+
color += CalculateParallelLight(eye, lightCollection.parallel[3], material);
135+
}
125136
}
126137
for (int i = 0; i < lightCollection.total.point; i++) {
127-
color += CalculatePointlLight(eye, lightCollection.point[i], material);
138+
if(i == 0){
139+
color += CalculatePointLight(eye, lightCollection.point[0], material);
140+
}
141+
if(i == 1){
142+
color += CalculatePointLight(eye, lightCollection.point[1], material);
143+
}
144+
if(i == 2){
145+
color += CalculatePointLight(eye, lightCollection.point[2], material);
146+
}
147+
if(i == 3){
148+
color += CalculatePointLight(eye, lightCollection.point[3], material);
149+
}
128150
}
129151
for (int i = 0; i < lightCollection.total.spot; i++) {
130-
color += CalculateSpotLight(eye, lightCollection.spot[i], material);
152+
if(i == 0){
153+
color += CalculateSpotLight(eye, lightCollection.spot[0], material);
154+
}
155+
if(i == 1){
156+
color += CalculateSpotLight(eye, lightCollection.spot[1], material);
157+
}
158+
if(i == 2){
159+
color += CalculateSpotLight(eye, lightCollection.spot[2], material);
160+
}
161+
if(i == 3){
162+
color += CalculateSpotLight(eye, lightCollection.spot[3], material);
163+
}
131164
}
132165
return color;
133166
}

src/debug_utility/cg_exception.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#include <iostream>
2+
13
#include "debug_utility/cg_exception.h"
24

35
FileNotExistsError::FileNotExistsError(const std::string &path) {
46
error_message = "[file error] File not exists at " + path;
7+
std::cout << error_message << std::endl;
58
}
69

710
const char *FileNotExistsError::what() const noexcept {
@@ -10,6 +13,7 @@ const char *FileNotExistsError::what() const noexcept {
1013

1114
ShaderCompileError::ShaderCompileError(const std::string &title, const std::string &log) {
1215
error_message = "[shader compile error on " + title + "] " + log;
16+
std::cout << error_message << std::endl;
1317
}
1418

1519
const char *ShaderCompileError::what() const noexcept {
@@ -18,6 +22,7 @@ const char *ShaderCompileError::what() const noexcept {
1822

1923
ShaderLinkError::ShaderLinkError(const std::string &log) {
2024
error_message = "[shader link error] " + log;
25+
std::cout << error_message << std::endl;
2126
}
2227

2328
const char *ShaderLinkError::what() const noexcept {
@@ -26,6 +31,7 @@ const char *ShaderLinkError::what() const noexcept {
2631

2732
LoadPictureError::LoadPictureError(const std::string &path) {
2833
error_message = "[picture format error] Fail to load picture at " + path;
34+
std::cout << error_message << std::endl;
2935
}
3036

3137
const char *LoadPictureError::what() const noexcept {
@@ -34,6 +40,7 @@ const char *LoadPictureError::what() const noexcept {
3440

3541
AssimpError::AssimpError(const std::string &error_string) {
3642
error_message = "[assimp error] " + error_string;
43+
std::cout << error_message << std::endl;
3744
}
3845

3946
const char *AssimpError::what() const noexcept {
@@ -42,6 +49,7 @@ const char *AssimpError::what() const noexcept {
4249

4350
ShaderSettingError::ShaderSettingError(const std::string &name) {
4451
error_message = "[shader setting error] Fail to set uniform variable " + name;
52+
std::cout << error_message << std::endl;
4553
}
4654

4755
const char *ShaderSettingError::what() const noexcept {

src/object.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Object::Object(World* w, Shader* shader, Material* material, uint32_t stride, bo
2626
}
2727

2828
Object::~Object(){
29-
cout << __func__ << endl;
29+
// cout << __func__ << endl;
3030
// delete from Graphics
3131
glDeleteVertexArrays(1, &vao_);
3232
glDeleteBuffers(1, &vbo_);
3333
glDeleteBuffers(1, &ebo_);
3434
// delete from bullet
35-
if(bt_object_ && world_ && world_->bt_world_)world_->bt_world_->removeCollisionObject(bt_object_);
35+
if(bt_object_ && world_ && world_->bt_world_ && !World::exit)world_->bt_world_->removeCollisionObject(bt_object_);
3636
// DeleteFromPhysics();
3737
delete shader_;
3838
delete bt_object_;

src/stage.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ void Stage::InitStage(StageFlag flag){
2121
}
2222
}
2323
void Stage::InitDungeonStage(void){
24+
// Safe bound
25+
btTransform bounding;
26+
bounding.setIdentity();
27+
bounding.setOrigin(btVector3(0, 0, -10));
28+
world_->CreateRigidBody(
29+
0,
30+
bounding,
31+
new btBoxShape(btVector3(500, 500, 1))
32+
);
2433
// Ground aka Wall //
2534
btScalar thickness = 5;
2635
btScalar depth = 40;

src/world.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using namespace std;
1010
int World::height = 600;
1111
int World::width = 800;
1212
bool World::keys_pressed[1024];
13+
bool World::exit = false;
1314

1415
Camera* World::camera = new Camera(glm::vec3(25, 51, 25), (double) World::width / (double) World::height);
1516
LightCollection* World::light_collection = new LightCollection(glm::vec3(0, 0, 0));
@@ -249,6 +250,7 @@ void World::CursorPosCallback(GLFWwindow *window, double x, double y) {
249250

250251
void World::KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mode) {
251252
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
253+
World::exit = true;
252254
glfwSetWindowShouldClose(window, GL_TRUE);
253255
} else {
254256
keys_pressed[key] = (action != GLFW_RELEASE);

0 commit comments

Comments
 (0)