Skip to content

Commit

Permalink
finished rebasing, merged branches
Browse files Browse the repository at this point in the history
  • Loading branch information
anhduong275 committed Dec 14, 2022
1 parent 26c5fbb commit 43d98bd
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 34 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void Camera::setView(glm::vec3 pos, glm::vec3 look, glm::vec3 up)
this->pos = pos;
this->look = look;
this->up = up;
this->view = glm::lookAt(pos, look, up);
this->view = glm::lookAt(pos, pos + look, up);
this->viewProj = proj * view;
this->left = glm::cross(glm::vec3(this->up), glm::vec3(this->look));
this->right = glm::cross(glm::vec3(this->look), glm::vec3(this->up));
Expand Down
3 changes: 3 additions & 0 deletions src/cubemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ void Cubemap::initCubeMap() {
-10.0f, -10.0f, 10.0f,
10.0f, -10.0f, 10.0f
};
for (float& i : points) {
i *= 5;
}
glGenBuffers(1, &cubemapVbo);
glGenVertexArrays(1, &cubemapVao);
glBindBuffer(GL_ARRAY_BUFFER, cubemapVbo);
Expand Down
18 changes: 16 additions & 2 deletions src/physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void Physics::init(Scene* scene) {
mass.v = glm::vec3(0, 0, 0);
mass.f = glm::vec3(0, 0, 0);
mass.r = model.geometry->getVertex(i);
mass.n = model.geometry->getNormal(i);
body.masses.push_back(mass);
}

Expand Down Expand Up @@ -99,6 +100,16 @@ void Physics::applyPhysics(float dt) {
if (mass.r.z < far)
far = mass.r.z;
}
glm::vec3 center(0, 0, 0);
for (MassPoint mass : body.masses) {
center += mass.r;

}
center = center * (1 / (float)body.masses.size());
// float obj_x = (right - left) / 2 + right;
// float obj_y = (top - bottom) / 2 + top;
// float obj_z = (far - near) / 2 + far;
this->objectCenter = center;


for (int k = 0; k < scene->models[i].geometry->numIndices / 3; k++) {
Expand All @@ -116,6 +127,9 @@ void Physics::applyPhysics(float dt) {
v0.f += f * normal;
v1.f += f * normal;
v2.f += f * normal;
v0.n = normal;
v1.n = normal;
v2.n = normal;

}

Expand All @@ -133,7 +147,7 @@ void Physics::applyPhysics(float dt) {
IntersectData hit = Raycast::cubeCollide(objectR);
if (hit.collide) {
mass.r = modelMatrix * glm::vec4(hit.pos, 1);
mass.v = glm::reflect(mass.v, glm::vec3(normalMatrix * glm::vec4(hit.normal, 0)));
mass.v = 0.5f * glm::reflect(mass.v, glm::vec3(normalMatrix * glm::vec4(hit.normal, 0)));
}
}
}
Expand All @@ -145,7 +159,7 @@ void Physics::updateScene() {
for (int i = 0; i < bodies.size(); i++) {
for (int j = 0; j < bodies[i].masses.size(); j++) {
scene->models[i].geometry->setVertex(j, bodies[i].masses[j].r);
//scene->models[i].geometry->setNormal(j, glm::normalize(bodies[i].masses[j].r));
scene->models[i].geometry->setNormal(j, glm::normalize(bodies[i].masses[j].r - objectCenter)); // glm::normalize(bodies[i].masses[j].r - objectCenter)
}
scene->models[i].geometry->updateBuffers();
}
Expand Down
2 changes: 2 additions & 0 deletions src/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct MassPoint {
glm::vec3 r;
glm::vec3 v;
glm::vec3 f;
glm::vec3 n;
};

struct Spring {
Expand All @@ -27,6 +28,7 @@ class Physics {
float stiffness;
float damping;
float massOfPoint;
glm::vec3 objectCenter = glm::vec3(0.f);
private:
void applyPhysics(float dt);
void updateScene();
Expand Down
20 changes: 10 additions & 10 deletions src/raycast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ struct IntersectData Raycast::cubeCollide(glm::vec3 point) {
return IntersectData{hits % 2, glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)};
*/

int collide = point.x <= 0.5 && point.x >= -0.5 &&
point.y <= 0.5 && point.y >= -0.5 &&
point.z <= 0.5 && point.z >= -0.5;
float e = 0.0f;
int collide = point.x + e <= 0.5 && point.x - e >= -0.5 &&
point.y + e <= 0.5 && point.y - e >= -0.5 &&
point.z + e <= 0.5 && point.z - e >= -0.5;
if (collide) {
std::vector<IntersectData> tmp;
glm::vec3 top(point.x, 0.5, point.z);
glm::vec3 bottom(point.x, -0.5, point.z);
glm::vec3 left(-0.5, point.y, point.z);
glm::vec3 right(0.5, point.y, point.z);
glm::vec3 forward(point.x, point.y, 0.5);
glm::vec3 back(point.x, point.y, -0.5);
glm::vec3 top(point.x, 0.5 + e, point.z);
glm::vec3 bottom(point.x, -0.5 - e, point.z);
glm::vec3 left(-0.5 - e, point.y, point.z);
glm::vec3 right(0.5 + e, point.y, point.z);
glm::vec3 forward(point.x, point.y, 0.5 + e);
glm::vec3 back(point.x, point.y, -0.5 - e);
tmp.push_back(IntersectData{1, top, glm::vec3(0, 1, 0)});
tmp.push_back(IntersectData{1, bottom, glm::vec3(0, -1, 0)});
tmp.push_back(IntersectData{1, left, glm::vec3(-1, 0, 0)});
Expand Down
22 changes: 17 additions & 5 deletions src/realtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ void Realtime::initializeGL()
// shader->set(":/shaders/default.vert", ":/shaders/default.frag");
// scene->addModel(new CubeMesh(glm::translate(glm::mat4(1), glm::vec3(0, 6, 0))),
// shader->set(":/shaders/default.vert", ":/shaders/default.frag");
scene->addModel(new SphereMesh(glm::translate(glm::scale(glm::mat4(1), glm::vec3(0.4, 0.4, 0.4)), glm::vec3(-3, 12, 0))),
glm::vec4(0.5, 0.5, 0.5, 1),
scene->addModel(new SphereMesh(glm::translate(glm::scale(glm::mat4(1), glm::vec3(1.f)), glm::vec3(-3, 12, 0))), // x = -3 y = 12
// glm::vec3(0.4, 0.4, 0.4)
glm::vec4(0.f, 0.5, 0.f, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), true, 1);
scene->updateReflectiveObjectPosition(glm::vec3(-3, 12, 0));
scene->addModel(new CubeMesh(glm::translate(glm::scale(glm::rotate(-0.4f, glm::vec3(0, 0, 1)), glm::vec3(5, 1, 3)), glm::vec3(-0.5, 0, 0))),
glm::vec4(0.5, 0.5, 0.5, 1),
glm::vec4(1, 1, 1, 1),
Expand All @@ -111,17 +113,23 @@ void Realtime::initializeGL()
glm::vec4(1, 1, 1, 1),
false,
0);
// scene->addModel(new SphereMesh(glm::translate(glm::mat4(1), glm::vec3(0, 4, 0))),
// glm::vec4(0, 0.5, 0, 1),
// glm::vec4(0, 1, 0, 1),
// glm::vec4(0, 1, 0, 1),
// false,
// 0);
scene->setLight(glm::vec4(1, 1, 1, 1), glm::vec3(-1, -1, -1));
scene->setupCubemap();
this->scene->screenWidth = m_width;
this->scene->screenHeight = m_height;
scene->initializeSideCameras();
camera->init(m_width, m_height, glm::vec3(0, 0, 10), glm::vec3(0, 0, -3), glm::vec3(0, 1, 0), 45.f);
// camera->init(m_width, m_height, glm::vec3(0, 0, 10), glm::vec3(0, 0, -3), glm::vec3(0, 1, 0), 45.f);

// scene->setLight(glm::vec4(1, 1, 1, 1), glm::vec3(-1, -1, -1));
// scene->setupCubemap();
// camera->init(m_width, m_height, glm::vec3(0, 5, 10), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
// physics->init(scene);
camera->init(m_width, m_height, glm::vec3(0, 5, 10), glm::vec3(0, 0, 0) - glm::vec3(0, 5, 10), glm::vec3(0, 1, 0), 45.f);
physics->init(scene);
}

void Realtime::paintGL()
Expand Down Expand Up @@ -235,6 +243,10 @@ void Realtime::timerEvent(QTimerEvent *event)
tmp(0.001);
tmp(0.001);
tmp(0.001);
this->scene->updateReflectiveObjectPosition(this->physics->objectCenter);
// std::cout << glm::to_string(physics->objectCenter) << std::endl;
// reinitialize side cams
this->scene->initializeSideCameras();

// a physics->update(deltaTime);

Expand Down
54 changes: 39 additions & 15 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ void Scene::drawFboSide(Shader *shader, Camera *camera)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);
int modelIndex = 0;
for (Model model : models)
{
if (modelIndex == 0) {
modelIndex++;
continue;
}
drawModel(shader, camera, model);
break; // DRAW ONLY THE FIRST ONE!
// break; // DRAW ONLY THE FIRST ONE!
modelIndex++;
}
glBindVertexArray(0);

Expand All @@ -40,6 +46,7 @@ void Scene::drawFboSide(Shader *shader, Camera *camera)

void Scene::drawScene(Shader *shader, Camera *camera)
{
// this->drawFboSide(shader, bottomCamera);
// dynamic env map
this->cubemap->bindDynamic();
glViewport(0, 0, this->cubemap->cubemapSideLength, this->cubemap->cubemapSideLength);
Expand Down Expand Up @@ -140,15 +147,16 @@ void Scene::drawScene(Shader *shader, Camera *camera)

glActiveTexture(GL_TEXTURE0);
this->cubemap->bindCubesideTex(); // binding fbo_tex_cube, the texture of the cube
int count = 0;
// int count = 0;
for (Model model : models)
{
drawModel(shader, camera, model);
if (count == 0)
{
continue;
} // DRAW ONLY THE SECOND ONE!
count++;
// break; // DRAW ONLY THE FIRST MODEL (THE SPHERE)
// if (count == 0)
// {
// continue;
// } // DRAW ONLY THE SECOND ONE!
// count++;
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glBindVertexArray(0);
Expand All @@ -158,8 +166,8 @@ void Scene::drawScene(Shader *shader, Camera *camera)
shader->bind(ShaderType::CUBEMAP_SHADER);
// camera->viewProj
shader->addUniformMat4(ShaderType::CUBEMAP_SHADER, "viewProj", camera->viewProj); /* viewproj matrix */
// shader->addUniformInt(ShaderType::CUBEMAP_SHADER, "cubemap_texture", 0);
// this->cubemap->drawCubeMap();
shader->addUniformInt(ShaderType::CUBEMAP_SHADER, "cubemap_texture", 0);
this->cubemap->drawCubeMap();
shader->unbind();
glDepthMask(GL_TRUE);
// CUBEMAP ---------------------------------------------------------
Expand Down Expand Up @@ -231,15 +239,31 @@ void Scene::initializeSideCameras()
{
// CURRENTLY SETTING BOTH WIDTH & HEIGHT TO 1.f TO MAINTAIN 1:1 RATIO!
// init top camera:
this->topCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, 10, 0), glm::vec3(0, 0, 1), 90.f);
// this->topCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, 10, 0), glm::vec3(0, 0, 1), 90.f);
// // init bottom camera:
// this->bottomCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, -10, 0), glm::vec3(0, 0, -1), 90.f);
// // init left camera:
// this->leftCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(-10, 0, 0), glm::vec3(0, -1, 0), 90.f);
// // init right camera:
// this->rightCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(10, 0, 0), glm::vec3(0, -1, 0), 90.f);
// // init front camera:
// this->frontCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, 0, -10), glm::vec3(0, -1, 0), 90.f); // -1, 90
// // init back camera:
// this->backCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, 0, 10), glm::vec3(0, -1, 0), 90.f);
this->topCamera->init(1.f, 1.f, this->objectPosition, glm::vec3(0, 10, 0), glm::vec3(0, 0, 1), 90.f);
// init bottom camera:
this->bottomCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, -10, 0), glm::vec3(0, 0, -1), 90.f);
this->bottomCamera->init(1.f, 1.f, this->objectPosition, glm::vec3(0, -10, 0), glm::vec3(0, 0, -1), 90.f);
// init left camera:
this->leftCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(-10, 0, 0), glm::vec3(0, -1, 0), 90.f);
this->leftCamera->init(1.f, 1.f, this->objectPosition, glm::vec3(-10, 0, 0), glm::vec3(0, -1, 0), 90.f);
// init right camera:
this->rightCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(10, 0, 0), glm::vec3(0, -1, 0), 90.f);
this->rightCamera->init(1.f, 1.f, this->objectPosition, glm::vec3(10, 0, 0), glm::vec3(0, -1, 0), 90.f);
// init front camera:
this->frontCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, 0, -10), glm::vec3(0, -1, 0), 90.f); // -1, 90
this->frontCamera->init(1.f, 1.f, this->objectPosition, glm::vec3(0, 0, -10), glm::vec3(0, -1, 0), 90.f); // -1, 90
// init back camera:
this->backCamera->init(1.f, 1.f, glm::vec3(0, 0, 0), glm::vec3(0, 0, 10), glm::vec3(0, -1, 0), 90.f);
this->backCamera->init(1.f, 1.f, this->objectPosition, glm::vec3(0, 0, 10), glm::vec3(0, -1, 0), 90.f);
// glm::vec3(-3, 3, 0)
}

void Scene::updateReflectiveObjectPosition(glm::vec3 objectPos) {
this->objectPosition = objectPos;
}
2 changes: 2 additions & 0 deletions src/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Scene

// dynamic env mapping
void drawFboSide(Shader *shader, Camera *camera);
void updateReflectiveObjectPosition(glm::vec3 objectPosition);
void initializeSideCameras();

private:
Expand All @@ -54,4 +55,5 @@ class Scene
Camera *rightCamera = new Camera();
Camera *frontCamera = new Camera();
Camera *backCamera = new Camera();
glm::vec3 objectPosition = glm::vec3(0.f);
};
1 change: 0 additions & 1 deletion src/spheremesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class SphereMesh : public Mesh
SphereMesh(glm::mat4 model) {
createIcosahedron();
subdivide();

subdivide();

buildGeometry(model);
Expand Down

0 comments on commit 43d98bd

Please sign in to comment.