Skip to content

Commit

Permalink
Feat: Infinite world grid
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Jan 5, 2024
1 parent 40a0d8e commit e6813b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
13 changes: 6 additions & 7 deletions src/atta/graphics/apis/openGL/framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,15 @@ void Framebuffer::bind(bool clear) {
clearColor[i] = (GLint)std::round(_clearColor[i]);
glClearBufferiv(GL_COLOR, 0, clearColor);
}

if (_depthAttachmentIndex != -1 && _stencilAttachmentIndex != -1) {
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glStencilMask(0x00);
} else if (_depthAttachmentIndex != -1) {
if (_depthAttachmentIndex != -1) {
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
}
if (_stencilAttachmentIndex != -1) {
glClear(GL_STENCIL_BUFFER_BIT);
glStencilMask(0x00);
glEnable(GL_STENCIL_TEST);
}
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/atta/graphics/apis/vulkan/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,22 @@ Pipeline::Pipeline(const gfx::Pipeline::CreateInfo& info) : gfx::Pipeline(info),
// Color blend
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE; // XXX blend is disabled for now
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.blendEnable = VK_FALSE;
if (gfx::Image::getNumChannels(_framebuffer->getImage(0)->getFormat()) == 4) {
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
}

VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.logicOp = VK_LOGIC_OP_COPY;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
colorBlending.blendConstants[0] = 0.0f;
colorBlending.blendConstants[1] = 0.0f;
colorBlending.blendConstants[2] = 0.0f;
colorBlending.blendConstants[3] = 0.0f;

// Dynamic state
std::vector<VkDynamicState> dynamicStates = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
Expand Down
41 changes: 25 additions & 16 deletions src/atta/graphics/renderers/common/gridPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,35 @@ void GridPipeline::update(std::shared_ptr<Camera> camera) {
// Update grid
{
int i = 0;
int size = 100;
int size = 50;
vec2i camPos = camera->getPosition();
// Create grid
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};
_lines[i++].pos = {float(j + camPos.x), float(camPos.y - size), 0.0f};
_lines[i++].pos = {float(j + camPos.x), float(size + camPos.y), 0.0f};
_lines[i++].pos = {float(camPos.x - size), float(j + camPos.y), 0.0f};
_lines[i++].pos = {float(size + camPos.x), float(j + camPos.y), 0.0f};
}

for (int j = 0; j < i; j++) {
for (int j = 0; j < i; j += 2) {
// Default line style
float width = 1.0f;
vec4 color = {1, 1, 1, 0.2};
if ((j / 4) % 10 == 0)
color = {1, 1, 1, 0.4};
vec4 color = {1, 1, 1, 0.1};

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;
// Line style every 10 meters
if ((int(_lines[j].pos.y) % 10 == 0 && _lines[j].pos.y == _lines[j + 1].pos.y) ||
(int(_lines[j].pos.x) % 10 == 0 && _lines[j].pos.x == _lines[j + 1].pos.x))
color = {1, 1, 1, 0.2};

// Line style of X and Y axes
if (_lines[j].pos.y == 0.0f && _lines[j + 1].pos.y == 0.0f)
color = {0.6, 0.2, 0.2, 1.0f};
else if (_lines[j].pos.x == 0.0f && _lines[j + 1].pos.x == 0.0f)
color = {0.2, 0.6, 0.2, 1.0f};

// Update line style
_lines[j].color = _lines[j + 1].color = color;
_lines[j].width = _lines[j + 1].width = width;
}
_numLines = i;
}
Expand All @@ -80,6 +88,7 @@ void GridPipeline::render(std::shared_ptr<Camera> camera) {
{
_pipeline->setMat4("uProjection", camera->getProj());
_pipeline->setMat4("uView", camera->getView());
_pipeline->setVec3("uCamPos", camera->getPosition());
_pipeline->renderMesh(_gridMeshName, _numLines);
}
_pipeline->end();
Expand Down

0 comments on commit e6813b5

Please sign in to comment.