From 43d98bd108346aad6d13bc6fca0f5728322653d5 Mon Sep 17 00:00:00 2001 From: anhduong275 <84537455+anhduong275@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:10:43 -0500 Subject: [PATCH] finished rebasing, merged branches --- .DS_Store | Bin 8196 -> 8196 bytes src/camera.cpp | 2 +- src/cubemap.cpp | 3 +++ src/physics.cpp | 18 ++++++++++++++-- src/physics.h | 2 ++ src/raycast.cpp | 20 +++++++++--------- src/realtime.cpp | 22 ++++++++++++++----- src/scene.cpp | 54 ++++++++++++++++++++++++++++++++++------------- src/scene.h | 2 ++ src/spheremesh.h | 1 - 10 files changed, 90 insertions(+), 34 deletions(-) diff --git a/.DS_Store b/.DS_Store index 6b8feefcd316b9a523470832e1e9151804a28b85..12a297551533e1ae3837ce28cb27bc9576dbc198 100644 GIT binary patch delta 38 ucmZp1XmQxERA_R$U>UoiiH?G?rO9MN5&O*v!pm4TvrBwq+5AU@of!c6V+?5k delta 73 zcmZp1XmQxERETl=CSF&tollacE`JV_oGXO!b7I^>w diff --git a/src/camera.cpp b/src/camera.cpp index bbe8771..2d896a6 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -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)); diff --git a/src/cubemap.cpp b/src/cubemap.cpp index f2635de..995fa48 100644 --- a/src/cubemap.cpp +++ b/src/cubemap.cpp @@ -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); diff --git a/src/physics.cpp b/src/physics.cpp index 622ee21..6b18d87 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -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); } @@ -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++) { @@ -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; } @@ -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))); } } } @@ -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(); } diff --git a/src/physics.h b/src/physics.h index a6cb2d3..9815adc 100644 --- a/src/physics.h +++ b/src/physics.h @@ -5,6 +5,7 @@ struct MassPoint { glm::vec3 r; glm::vec3 v; glm::vec3 f; + glm::vec3 n; }; struct Spring { @@ -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(); diff --git a/src/raycast.cpp b/src/raycast.cpp index 866b170..d4616f4 100644 --- a/src/raycast.cpp +++ b/src/raycast.cpp @@ -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 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)}); diff --git a/src/realtime.cpp b/src/realtime.cpp index 6a61769..16dc4aa 100644 --- a/src/realtime.cpp +++ b/src/realtime.cpp @@ -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), @@ -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() @@ -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); diff --git a/src/scene.cpp b/src/scene.cpp index b686060..1bb8698 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -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); @@ -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); @@ -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); @@ -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 --------------------------------------------------------- @@ -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; } diff --git a/src/scene.h b/src/scene.h index bc59551..93d48c3 100644 --- a/src/scene.h +++ b/src/scene.h @@ -40,6 +40,7 @@ class Scene // dynamic env mapping void drawFboSide(Shader *shader, Camera *camera); + void updateReflectiveObjectPosition(glm::vec3 objectPosition); void initializeSideCameras(); private: @@ -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); }; diff --git a/src/spheremesh.h b/src/spheremesh.h index 33e15c1..30d97a0 100644 --- a/src/spheremesh.h +++ b/src/spheremesh.h @@ -14,7 +14,6 @@ class SphereMesh : public Mesh SphereMesh(glm::mat4 model) { createIcosahedron(); subdivide(); - subdivide(); buildGeometry(model);