-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//-------------------------------------------------- | ||
// Atta Graphics Module | ||
// gridPipeline.cpp | ||
// Date: 2021-11-16 | ||
// By Breno Cunha Queiroz | ||
//-------------------------------------------------- | ||
#include <atta/graphics/interface.h> | ||
#include <atta/graphics/renderers/common/gridPipeline.h> | ||
#include <atta/resource/interface.h> | ||
|
||
namespace atta::graphics { | ||
|
||
size_t GridPipeline::_gridId = 0; | ||
|
||
GridPipeline::GridPipeline(std::shared_ptr<RenderPass> renderPass) : _numLines(0) { | ||
Pipeline::CreateInfo pipelineInfo{}; | ||
pipelineInfo.shader = gfx::create<Shader>("shaders/grid/grid.asl"); | ||
pipelineInfo.renderPass = renderPass; | ||
pipelineInfo.primitive = Pipeline::Primitive::LINE; | ||
pipelineInfo.debugName = StringId("Grid Pipeline"); | ||
_pipeline = gfx::create<Pipeline>(pipelineInfo); | ||
|
||
// Create line mesh | ||
_gridMeshName = "atta::gfx::GridPipeline[" + std::to_string(_gridId++) + "]"; | ||
LOG_DEBUG("GridPipeline", "Mesh id $0", _gridMeshName); | ||
res::Mesh::CreateInfo meshInfo{}; | ||
uint8_t* data = (uint8_t*)_lines.data(); | ||
size_t size = _lines.size() * sizeof(Line); | ||
meshInfo.vertices = std::vector<uint8_t>(data, data + size); | ||
meshInfo.vertexLayout.push_back({resource::Mesh::VertexElement::VEC3, "iPos"}); | ||
meshInfo.vertexLayout.push_back({resource::Mesh::VertexElement::VEC4, "iColor"}); | ||
meshInfo.vertexLayout.push_back({resource::Mesh::VertexElement::FLOAT, "iWidth"}); | ||
res::create<res::Mesh>(_gridMeshName, meshInfo); | ||
} | ||
|
||
GridPipeline::~GridPipeline() { | ||
// TODO Delete grid | ||
} | ||
|
||
void GridPipeline::update(std::shared_ptr<Camera> camera) { | ||
// Update grid | ||
{ | ||
int i = 0; | ||
int size = 100; | ||
for (int j = -size; j <= size; j++) { | ||
_lines[i++].pos = {float(j), -float(size), 0.0f}; | ||
_lines[i++].pos = {float(j), float(size), 0.0f}; | ||
_lines[i++].pos = {-float(size), float(j), 0.0f}; | ||
_lines[i++].pos = {float(size), float(j), 0.0f}; | ||
} | ||
|
||
for (int j = 0; j < i; j++) { | ||
float width = 1.0f; | ||
vec4 color = {1, 1, 1, 0.2}; | ||
if ((j / 4) % 10 == 0) | ||
color = {1, 1, 1, 0.4}; | ||
|
||
if (_lines[j].pos.y == 0.0f) | ||
_lines[j].color = {0.6, 0.2, 0.2, 1.0f}; | ||
else if (_lines[j].pos.x == 0.0f) | ||
_lines[j].color = {0.2, 0.6, 0.2, 1.0f}; | ||
else | ||
_lines[j].color = color; | ||
_lines[j].width = width; | ||
} | ||
_numLines = i; | ||
} | ||
|
||
// Update mesh with grid | ||
uint8_t* data = (uint8_t*)_lines.data(); | ||
size_t size = _lines.size() * sizeof(Line); | ||
res::get<res::Mesh>(_gridMeshName)->updateVertices(std::vector<uint8_t>(data, data + size)); | ||
} | ||
|
||
void GridPipeline::render(std::shared_ptr<Camera> camera) { | ||
if (_numLines == 0) | ||
return; | ||
|
||
_pipeline->begin(); | ||
{ | ||
_pipeline->setMat4("uProjection", camera->getProj()); | ||
_pipeline->setMat4("uView", camera->getView()); | ||
_pipeline->renderMesh(_gridMeshName, _numLines); | ||
} | ||
_pipeline->end(); | ||
} | ||
|
||
} // namespace atta::graphics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//-------------------------------------------------- | ||
// Atta Graphics Module | ||
// gridPipeline.h | ||
// Date: 2021-11-16 | ||
// By Breno Cunha Queiroz | ||
//-------------------------------------------------- | ||
#ifndef ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H | ||
#define ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H | ||
#include <atta/graphics/cameras/camera.h> | ||
#include <atta/graphics/pipeline.h> | ||
#include <atta/graphics/renderQueue.h> | ||
|
||
namespace atta::graphics { | ||
|
||
class GridPipeline final { | ||
public: | ||
GridPipeline(std::shared_ptr<RenderPass> renderPass); | ||
~GridPipeline(); | ||
|
||
/// Update GPU line grid | ||
void update(std::shared_ptr<Camera> camera); | ||
|
||
/** | ||
* @brief Render lines from grid | ||
* | ||
* @note GridPipeline::update should be called before rendering to make sure that the GPU data is up to date | ||
*/ | ||
void render(std::shared_ptr<Camera> camera); | ||
|
||
private: | ||
std::shared_ptr<Pipeline> _pipeline; | ||
|
||
struct Line { | ||
vec3 pos; | ||
vec4 color; | ||
float width; | ||
}; | ||
static constexpr size_t MAX_LINES = 1000; | ||
std::array<Line, MAX_LINES> _lines; | ||
size_t _numLines; | ||
|
||
StringId _gridMeshName; | ||
static size_t _gridId; | ||
}; | ||
|
||
} // namespace atta::graphics | ||
|
||
#endif // ATTA_GRAPHICS_RENDERERS_COMMON_GRID_PIPELINE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters