From 35b940a0fe2bd67b2ff1c02b2ca748ba9040f0df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A4=E9=BE=99?= <68932014+12gch138@users.noreply.github.com> Date: Mon, 19 Dec 2022 20:31:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=B0=E5=BD=A2=E5=9B=BEhittest=E8=B6=8A?= =?UTF-8?q?=E7=95=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FinalProject/sceneviewer.cpp | 4 ++-- FinalProject/terrain.cpp | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index 5af9f2b..ab24aa9 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -158,8 +158,8 @@ void SceneViewer::initializeGL() { terrainFragmentShader.dispose(); // Test Code Start - _sky = new SkyBox("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\SkyBoxes"); - _terrain = new Terrain("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\Terrains"); + _sky = new SkyBox("D:\\ProgrammingFile\\SceneEditor\\SkyBoxes"); + _terrain = new Terrain("D:\\ProgrammingFile\\SceneEditor\\Terrains"); _dirLight = new DirLight(); diff --git a/FinalProject/terrain.cpp b/FinalProject/terrain.cpp index 370f8fb..6801e1b 100644 --- a/FinalProject/terrain.cpp +++ b/FinalProject/terrain.cpp @@ -147,6 +147,8 @@ float Terrain::GetHeight(float px, float pz) { float ans = (x - fx) * (Point[gx][gz] - Point[x][z]) + Point[x][z]; + //float ans = Point[x][z]; + return ans; } @@ -166,9 +168,12 @@ glm::vec3 Terrain::GetNormal(glm::vec3 pos) { HitRecord Terrain::hit(const Ray& ray) { glm::vec3 orig = ray.origin(); glm::vec3 dir = ray.direction(); + if (orig.x >= width / 2 || orig.x <= -width / 2 || orig.z >= height / 2 || orig.z <= -height / 2) { + return HitRecord(); + } // A good ray step is half of the blockScale - glm::vec3 rayStep = dir * (float)width * 0.25f; + glm::vec3 rayStep = dir; glm::vec3 rayStartPosition = orig; // Linear search - Loop until find a point inside and outside the terrain Vector3 @@ -179,6 +184,9 @@ HitRecord Terrain::hit(const Ray& ray) { { lastRayPosition = orig; orig += rayStep; + if (orig.x >= width / 2 || orig.x <= -width / 2 || orig.z >= height / 2 || orig.z <= -height / 2) { + return HitRecord(); + } map_height = GetHeight(orig.x, orig.z); }