From 73214555811d0fe28bd283e3a0775ea2a336c6be Mon Sep 17 00:00:00 2001
From: Linloir <3145078758@qq.com>
Date: Sun, 18 Dec 2022 20:05:08 +0800
Subject: [PATCH] [CORE][ADD] Bounding box display - Add render API in bounding
box - Add getter API in renderer to get bounding box
---
FinalProject/FinalProject.vcxproj | 2 ++
FinalProject/FinalProject.vcxproj.filters | 6 ++++
FinalProject/boundary.cpp | 42 ++++++++++++++++++++++-
FinalProject/boundary.h | 3 ++
FinalProject/boundfragmentshader.glsl | 7 ++++
FinalProject/boundvertexshader.glsl | 10 ++++++
FinalProject/mainwindow.qrc | 2 ++
FinalProject/renderable.h | 2 ++
8 files changed, 73 insertions(+), 1 deletion(-)
create mode 100644 FinalProject/boundfragmentshader.glsl
create mode 100644 FinalProject/boundvertexshader.glsl
diff --git a/FinalProject/FinalProject.vcxproj b/FinalProject/FinalProject.vcxproj
index d0126d9..38577c8 100644
--- a/FinalProject/FinalProject.vcxproj
+++ b/FinalProject/FinalProject.vcxproj
@@ -172,6 +172,8 @@
+
+
diff --git a/FinalProject/FinalProject.vcxproj.filters b/FinalProject/FinalProject.vcxproj.filters
index 77c6290..b9fc51e 100644
--- a/FinalProject/FinalProject.vcxproj.filters
+++ b/FinalProject/FinalProject.vcxproj.filters
@@ -294,5 +294,11 @@
Resource Files
+
+ Resource Files
+
+
+ Resource Files
+
\ No newline at end of file
diff --git a/FinalProject/boundary.cpp b/FinalProject/boundary.cpp
index 49abfc5..5ff405e 100644
--- a/FinalProject/boundary.cpp
+++ b/FinalProject/boundary.cpp
@@ -1,6 +1,8 @@
#include
+#include
#include "boundary.h"
+#include "vao.h"
void Boundary::updateControlPoints(const glm::vec3 point) {
_bottomControlPoint.x = std::min(_bottomControlPoint.x, point.x);
@@ -40,4 +42,42 @@ bool Boundary::hit(const Ray& ray) const {
if ((tmin > tzmax) || (tzmin > tmax)) return false;
return true;
-}
\ No newline at end of file
+}
+
+void Boundary::render() const {
+ // Generate vertices
+ std::vector vertices;
+ vertices.push_back(Vertex(glm::vec3(_bottomControlPoint.x, _bottomControlPoint.y, _bottomControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_topControlPoint.x, _bottomControlPoint.y, _bottomControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_topControlPoint.x, _topControlPoint.y, _bottomControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_bottomControlPoint.x, _topControlPoint.y, _bottomControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_bottomControlPoint.x, _bottomControlPoint.y, _topControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_topControlPoint.x, _bottomControlPoint.y, _topControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_topControlPoint.x, _topControlPoint.y, _topControlPoint.z)));
+ vertices.push_back(Vertex(glm::vec3(_bottomControlPoint.x, _topControlPoint.y, _topControlPoint.z)));
+ // Generate indices
+ std::vector indices;
+ indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(3); // bottom
+ indices.push_back(4); indices.push_back(5); indices.push_back(6); indices.push_back(7); // top
+ indices.push_back(0); indices.push_back(1); indices.push_back(5); indices.push_back(4); // left
+ indices.push_back(3); indices.push_back(2); indices.push_back(6); indices.push_back(7); // right
+ indices.push_back(0); indices.push_back(3); indices.push_back(7); indices.push_back(4); // back
+ indices.push_back(1); indices.push_back(2); indices.push_back(6); indices.push_back(5); // front
+ // Generate VBO
+ VertexBufferObject vbo(vertices);
+ // Generate EBO
+ ElementBufferObject ebo(indices);
+ // Generate VAO
+ VertexArrayObject vao(vbo, ebo);
+ vao.setVertexAttributePointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
+ // Draw
+ vao.bind();
+ OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, (void*)(0 * sizeof(unsigned int)));
+ OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, (void*)(4 * sizeof(unsigned int)));
+ OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, (void*)(8 * sizeof(unsigned int)));
+ OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, (void*)(12 * sizeof(unsigned int)));
+ vao.unbind();
+ // Delete VBO and VAO
+ vbo.dispose();
+ vao.dispose();
+}
diff --git a/FinalProject/boundary.h b/FinalProject/boundary.h
index 15f5856..7be63f3 100644
--- a/FinalProject/boundary.h
+++ b/FinalProject/boundary.h
@@ -3,6 +3,7 @@
#include
#include "ray.h"
+#include "utils.h"
class Boundary {
private:
@@ -21,6 +22,8 @@ public:
inline glm::vec3 bottomCenterPoint() const;
bool hit(const Ray& ray) const;
+
+ void render() const;
};
inline glm::vec3 Boundary::bottomCenterPoint() const {
diff --git a/FinalProject/boundfragmentshader.glsl b/FinalProject/boundfragmentshader.glsl
new file mode 100644
index 0000000..386e49f
--- /dev/null
+++ b/FinalProject/boundfragmentshader.glsl
@@ -0,0 +1,7 @@
+#version 430 core
+
+out vec4 FragColor;
+
+void main() {
+ FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
+}
\ No newline at end of file
diff --git a/FinalProject/boundvertexshader.glsl b/FinalProject/boundvertexshader.glsl
new file mode 100644
index 0000000..d98f5a8
--- /dev/null
+++ b/FinalProject/boundvertexshader.glsl
@@ -0,0 +1,10 @@
+#version 430 core
+
+layout(location = 0) in vec3 position;
+
+uniform mat4 view;
+uniform mat4 projection;
+
+void main() {
+ gl_Position = projection * view * vec4(position, 1.0);
+}
\ No newline at end of file
diff --git a/FinalProject/mainwindow.qrc b/FinalProject/mainwindow.qrc
index b2b86e7..47f7923 100644
--- a/FinalProject/mainwindow.qrc
+++ b/FinalProject/mainwindow.qrc
@@ -6,6 +6,8 @@
terrainfragmentshader.glsl
terrainvertexshader.glsl
skyboxfragmentshader.glsl
+ boundfragmentshader.glsl
+ boundvertexshader.glsl
font_awesome_6_regular_free.otf
diff --git a/FinalProject/renderable.h b/FinalProject/renderable.h
index e5684a7..e8b268b 100644
--- a/FinalProject/renderable.h
+++ b/FinalProject/renderable.h
@@ -44,6 +44,8 @@ public:
ScopedLight* originalLight() const; // pass out the light object to scene manager to set light attributes
bool hasLight() const { return _light != nullptr; }
void makeLight(); // create a light source in the object
+
+ const Boundary& boundary() const { return _boundary; }
inline glm::mat4 modelMatrix() const;