[CORE][ADD] Bounding box display

- Add render API in bounding box
- Add getter API in renderer to get bounding box
This commit is contained in:
Linloir 2022-12-18 20:05:08 +08:00
parent 9d95220073
commit 7321455581
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
8 changed files with 73 additions and 1 deletions

View File

@ -172,6 +172,8 @@
<ClInclude Include="shader.h" />
</ItemGroup>
<ItemGroup>
<None Include="boundfragmentshader.glsl" />
<None Include="boundvertexshader.glsl" />
<None Include="fragmentshader.glsl">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
</ExcludedFromBuild>

View File

@ -294,5 +294,11 @@
<None Include="terrainvertexshader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="boundvertexshader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="boundfragmentshader.glsl">
<Filter>Resource Files</Filter>
</None>
</ItemGroup>
</Project>

View File

@ -1,6 +1,8 @@
#include <algorithm>
#include <vector>
#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;
}
}
void Boundary::render() const {
// Generate vertices
std::vector<Vertex> 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<unsigned int> 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();
}

View File

@ -3,6 +3,7 @@
#include <GLM/glm.hpp>
#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 {

View File

@ -0,0 +1,7 @@
#version 430 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

View File

@ -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);
}

View File

@ -6,6 +6,8 @@
<file>terrainfragmentshader.glsl</file>
<file>terrainvertexshader.glsl</file>
<file>skyboxfragmentshader.glsl</file>
<file>boundfragmentshader.glsl</file>
<file>boundvertexshader.glsl</file>
</qresource>
<qresource prefix="/fonts">
<file>font_awesome_6_regular_free.otf</file>

View File

@ -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;