1
0
Fork 0

Raw sphere tool

sphinx
May B. 2020-10-23 22:50:00 +02:00
parent 686505bded
commit 5fdff56f8f
34 changed files with 209 additions and 87 deletions

View File

@ -14,6 +14,7 @@
- [~] Authentication
- [x] Compression
- [ ] Encryption
- DTLS
- [x] Embedded
- [x] Standalone
@ -24,8 +25,10 @@
- [ ] Local prediction
- [ ] Contouring service
- [~] Edit
- [ ] More types
- [ ] Shape iterators
- Cube
- Sphere
- [ ] More types
- Anchor
- Prevent suffocation
- [ ] Local prediction

View File

@ -6,5 +6,5 @@ layout(location = 0) out vec4 color;
in vec4 Color;
void main(){
color = vec4(Color.xyz, .5);
color = Color;
}

BIN
resource/content/shaders/Color.fs.spv (Stored with Git LFS)

Binary file not shown.

View File

@ -2,14 +2,14 @@
layout(location = 0) in vec3 Position_modelspace;
layout(location = 1) in vec4 Color_model;
uniform mat4 MVP;
uniform vec4 Col;
out vec4 Color;
void main(){
gl_Position = MVP * vec4(Position_modelspace, 1);
Color = Color_model;
Color = Col;
}

BIN
resource/content/shaders/Color.vs.spv (Stored with Git LFS)

Binary file not shown.

View File

@ -6,5 +6,5 @@ layout(location = 0) in vec4 Color;
layout(location = 0) out vec4 color;
void main(){
color = vec4(Color.xyz, .5);
color = Color;
}

View File

@ -6,15 +6,15 @@ layout(binding = 0) uniform UniformBufferObject {
} UBO;
layout(push_constant) uniform PushConst {
mat4 model;
vec4 color;
} Push;
layout(location = 0) in vec3 Position_modelspace;
layout(location = 1) in vec4 Color_model;
layout(location = 0) out vec4 Color;
void main(){
gl_Position = UBO.proj * UBO.view * Push.model * vec4(Position_modelspace, 1);
Color = Color_model;
Color = Push.color;
}

View File

@ -66,6 +66,8 @@ void Client::run(server_handle* const localHandle) {
const auto ray_result = world->raycast(camera.getRay() * options.voxel_density);
if(auto target = std::get_if<world::Universe::ray_target>(&ray_result)) {
state.look_at = *target;
const auto &tool = options.editor.tool;
state.can_fill = true; //FIXME: world->canFill(target->pos, tool.shape, tool.radius);
} else {
state.look_at = {};
}
@ -75,11 +77,11 @@ void Client::run(server_handle* const localHandle) {
ZoneScopedN("Edit");
const auto &tool = options.editor.tool;
if (inputs.isPressing(Mouse::Left))
world->emit(world::action::FillCube(
state.look_at.value().pos, world::Voxel(world::materials::AIR, tool.emptyAir * world::Voxel::DENSITY_MAX), tool.radius));
world->emit(world::action::FillShape(
state.look_at.value().pos, world::Voxel(world::materials::AIR, tool.emptyAir * world::Voxel::DENSITY_MAX), tool.shape, tool.radius));
else if (inputs.isPressing(Mouse::Right))
world->emit(world::action::FillCube(
state.look_at.value().pos, world::Voxel(tool.material, world::Voxel::DENSITY_MAX), tool.radius));
world->emit(world::action::FillShape(
state.look_at.value().pos, world::Voxel(tool.material, world::Voxel::DENSITY_MAX), tool.shape, tool.radius));
}
if (inputs.isDown(Input::Throw)) {
//FIXME: register entity type world->addEntity(entity_id(0), {state.position * options.voxel_density, glm::vec3(10, 0, 0)});
@ -196,7 +198,7 @@ void Client::run(server_handle* const localHandle) {
if(state.look_at.has_value()) { // Indicator
const auto model = glm::scale(glm::translate(glm::scale(glm::mat4(1), 1.f / glm::vec3(options.voxel_density)), glm::vec3(state.look_at.value().pos.second + state.look_at.value().offset - offset * glm::llvec3(options.voxel_density)) - glm::vec3(.5 + options.editor.tool.radius)), glm::vec3(1 + options.editor.tool.radius * 2));
reports.models_count++;
reports.tris_count += pass(model);
reports.tris_count += pass(model, options.editor.tool.shape, state.can_fill ? glm::vec4(1, 1, 1, .5) : glm::vec4(1, 0, 0, .5));
}
}
pipeline->postProcess();

View File

@ -79,6 +79,7 @@ public:
}
editor.visible = config["editor"]["visible"].value_or(editor.visible);
editor.tool.shape = static_cast<world::action::Shape>(config["editor"]["tool"]["shape"].value_or(static_cast<int>(editor.tool.shape)));
editor.tool.radius = config["editor"]["tool"]["radius"].value_or(editor.tool.radius);
editor.tool.material = config["editor"]["tool"]["material"].value_or(editor.tool.material);
editor.tool.emptyAir = config["editor"]["tool"]["empty_air"].value_or(editor.tool.emptyAir);
@ -156,6 +157,7 @@ public:
config.insert_or_assign("editor", toml::table({
{"visible", editor.visible},
{"tool", toml::table({
{"shape", static_cast<int>(editor.tool.shape)},
{"radius", editor.tool.radius},
{"material", editor.tool.material},
{"empty_air", editor.tool.emptyAir}
@ -205,6 +207,7 @@ public:
struct {
bool visible = false;
struct {
world::action::Shape shape = world::action::Shape::Cube;
int radius = 2;
unsigned short material = 5;
bool emptyAir = true;

View File

@ -1,6 +1,7 @@
#pragma once
#include "../../core/flags.hpp"
#include "../../core/world/actions.hpp"
#include <cassert>
#include <memory>
#include <string>
@ -73,8 +74,8 @@ public:
virtual std::function<size_t(render::LodModel *const, glm::mat4, glm::vec4, float)> beginWorldPass(bool solid) = 0;
/// Get started entity program
virtual std::function<size_t(render::Model *const, const std::vector<glm::mat4> &)> beginEntityPass() = 0;
/// Draw cube indicator
virtual std::function<size_t(glm::mat4)> beginIndicatorPass() = 0;
/// Draw line indicator (model, shape, color)
virtual std::function<size_t(glm::mat4, world::action::Shape, glm::vec4)> beginIndicatorPass() = 0;
/// Apply postprocessing
virtual void postProcess() = 0;
/// Finalise frame

View File

@ -227,6 +227,7 @@ UI::Actions UI::draw(config::client::options &options, state::state &state, cons
ImGui::EndCombo();
}
ImGui::SliderInt("Radius", &options.editor.tool.radius, 0, 10);
ImGui::Combo("Shape", (int*)&options.editor.tool.shape, world::action::SHAPES);
ImGui::Checkbox("Empty air", &options.editor.tool.emptyAir);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Is cleaned area breathable");

View File

@ -15,10 +15,27 @@ const std::vector<glm::vec3> Shape::SKY_CUBE = {
{-1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, -1.0f},
{-1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f}
};
const std::pair<std::vector<glm::vec3>, std::vector<glm::vec4>> Indicator::CUBE = {{
const std::vector<glm::vec3> Shape::LINE_CUBE = {
{0, 0, 0}, {0, 0, 1}, {0, 0, 1}, {0, 1, 1}, {0, 1, 1}, {0, 1, 0}, {0, 1, 0}, {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {1, 0, 1}, {1, 1, 1},
{1, 1, 1}, {1, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 1}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}, {0, 1, 0}, {1, 1, 0}
}, {
};
constexpr auto SINPIOVER4 = 0.853553391f;
const std::vector<glm::vec3> Shape::LINE_SPHERE = {
{1, .5f, .5f}, {SINPIOVER4, SINPIOVER4, .5f}, {SINPIOVER4, SINPIOVER4, .5f}, {.5f, 1, .5f},
{.5f, 1, .5f}, {1-SINPIOVER4, SINPIOVER4, .5f}, {1-SINPIOVER4, SINPIOVER4, .5f}, {0, .5f, .5f},
{1, .5f, .5f}, {SINPIOVER4, 1-SINPIOVER4, .5f}, {SINPIOVER4, 1-SINPIOVER4, .5f}, {.5f, 0, .5f},
{.5f, 0, .5f}, {1-SINPIOVER4, 1-SINPIOVER4, .5f}, {1-SINPIOVER4, 1-SINPIOVER4, .5f}, {0, .5f, .5f},
{1, .5f, .5f}, {SINPIOVER4, .5f, SINPIOVER4}, {SINPIOVER4, .5f, SINPIOVER4}, {.5f, .5f, 1},
{.5f, .5f, 1}, {1-SINPIOVER4, .5f, SINPIOVER4}, {1-SINPIOVER4, .5f, SINPIOVER4}, {0, .5f, .5f},
{1, .5f, .5f}, {SINPIOVER4, .5f, 1-SINPIOVER4}, {SINPIOVER4, .5f, 1-SINPIOVER4}, {.5f, .5f, 0},
{.5f, .5f, 0}, {1-SINPIOVER4, .5f, 1-SINPIOVER4}, {1-SINPIOVER4, .5f, 1-SINPIOVER4}, {0, .5f, .5f},
{.5f, 1, .5f}, {.5f, SINPIOVER4, SINPIOVER4}, {.5f, SINPIOVER4, SINPIOVER4}, {.5f, .5f, 1},
{.5f, .5f, 1}, {.5f, 1-SINPIOVER4, SINPIOVER4}, {.5f, 1-SINPIOVER4, SINPIOVER4}, {.5f, 0, .5f},
{.5f, 1, .5f}, {.5f, SINPIOVER4, 1-SINPIOVER4}, {.5f, SINPIOVER4, 1-SINPIOVER4}, {.5f, .5f, 0},
{.5f, .5f, 0}, {.5f, 1-SINPIOVER4, 1-SINPIOVER4}, {.5f, 1-SINPIOVER4, 1-SINPIOVER4}, {.5f, 0, .5f}
};
const std::pair<std::vector<glm::vec3>, std::vector<glm::vec4>> ColoredShape::LINE_CUBE = {Shape::LINE_CUBE,
{
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1},
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
}};

View File

@ -45,14 +45,16 @@ public:
virtual ~Shape() { }
static const std::vector<glm::vec3> SKY_CUBE;
static const std::vector<glm::vec3> LINE_CUBE;
static const std::vector<glm::vec3> LINE_SPHERE;
};
/// Color lines model
class Indicator {
class ColoredShape {
public:
virtual ~Indicator() { }
virtual ~ColoredShape() { }
static const std::pair<std::vector<glm::vec3>, std::vector<glm::vec4>> CUBE;
static const std::pair<std::vector<glm::vec3>, std::vector<glm::vec4>> LINE_CUBE;
};
/// VertexData model with index

View File

@ -18,7 +18,8 @@ constexpr auto GL_MINOR = 6;
#define TEXTURES_DIR CONTENT_DIR "textures/"
Renderer::Renderer(const renderOptions& options):
IndicatorCubeBuffer(Indicator::CUBE.first, Indicator::CUBE.second) {
IndicatorCubeBuffer(Shape::LINE_CUBE), IndicatorSphereBuffer(Shape::LINE_SPHERE)
{
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
@ -122,11 +123,12 @@ std::function<size_t(render::Model *const, const std::vector<glm::mat4> &)> Rend
};
}
std::function<size_t(glm::mat4)> Renderer::beginIndicatorPass() {
std::function<size_t(glm::mat4, world::action::Shape, glm::vec4)> Renderer::beginIndicatorPass() {
IndicatorPass->useIt();
return [&](glm::mat4 model) {
IndicatorPass->setup(this, model);
return IndicatorCubeBuffer.draw();
return [&](glm::mat4 model, world::action::Shape shape, glm::vec4 color) {
IndicatorPass->setup(this, model, color);
return shape == world::action::Shape::Cube ?
IndicatorCubeBuffer.draw(GL_LINES) : IndicatorSphereBuffer.draw(GL_LINES);
};
}

View File

@ -45,7 +45,7 @@ public:
void beginFrame() override;
std::function<size_t(render::LodModel *const, glm::mat4, glm::vec4, float)> beginWorldPass(bool solid) override;
std::function<size_t(render::Model *const, const std::vector<glm::mat4>&)> beginEntityPass() override;
std::function<size_t(glm::mat4)> beginIndicatorPass() override;
std::function<size_t(glm::mat4, world::action::Shape, glm::vec4)> beginIndicatorPass() override;
void postProcess() override;
void endFrame() override;
void swapBuffer(Window&) override;
@ -72,7 +72,8 @@ private:
std::unique_ptr<pass::EntityProgram> EntityPass;
std::unique_ptr<pass::SkyProgram> SkyPass;
std::unique_ptr<pass::ColorProgram> IndicatorPass;
Indicator IndicatorCubeBuffer;
Shape IndicatorCubeBuffer;
Shape IndicatorSphereBuffer;
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;

View File

@ -11,16 +11,16 @@ Shape::Shape(const std::vector<glm::vec3>& pos) {
Shape::~Shape() {
glDeleteBuffers(1, &bufferId);
}
size_t Shape::draw() {
size_t Shape::draw(GLenum format) {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
glDrawArrays(GL_TRIANGLES, 0, size);
glDrawArrays(format, 0, size);
glDisableVertexAttribArray(0);
return size;
}
Indicator::Indicator(const std::vector<glm::vec3>& pos, const std::vector<glm::vec4>& col) {
ColoredShape::ColoredShape(const std::vector<glm::vec3>& pos, const std::vector<glm::vec4>& col) {
assert(pos.size() == col.size());
size = pos.size();
glGenBuffers(1, &vertexBufferId);
@ -30,11 +30,11 @@ Indicator::Indicator(const std::vector<glm::vec3>& pos, const std::vector<glm::v
glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
glBufferData(GL_ARRAY_BUFFER, col.size() * sizeof(glm::vec4), col.data(), GL_STATIC_DRAW);
}
Indicator::~Indicator() {
ColoredShape::~ColoredShape() {
glDeleteBuffers(1, &colorBufferId);
glDeleteBuffers(1, &vertexBufferId);
}
void Indicator::enableAttribs() {
void ColoredShape::enableAttribs() {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
@ -43,17 +43,17 @@ void Indicator::enableAttribs() {
glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *)0);
}
void Indicator::disableAttribs() {
void ColoredShape::disableAttribs() {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
size_t Indicator::draw() {
size_t ColoredShape::draw() {
enableAttribs();
glDrawArrays(GL_LINES, 0, size);
disableAttribs();
return size;
}
size_t Indicator::drawInstanced(size_t count) {
size_t ColoredShape::drawInstanced(size_t count) {
enableAttribs();
glDrawArraysInstanced(GL_LINES, 0, size, count);
disableAttribs();

View File

@ -13,17 +13,17 @@ public:
Shape(const std::vector<glm::vec3>&);
~Shape();
size_t draw();
size_t draw(GLenum format);
private:
size_t size;
GLuint bufferId;
};
class Indicator final: public render::Indicator {
class ColoredShape final: public render::ColoredShape {
public:
Indicator(const std::vector<glm::vec3>&, const std::vector<glm::vec4>&);
~Indicator();
ColoredShape(const std::vector<glm::vec3>&, const std::vector<glm::vec4>&);
~ColoredShape();
size_t draw();
size_t drawInstanced(size_t count);

View File

@ -13,6 +13,7 @@ ColorProgram::ColorProgram(): Program() {
load(shaders);
MVPMatrixID = glGetUniformLocation(ProgramID, "MVP");
ColID = glGetUniformLocation(ProgramID, "Col");
}
ColorProgram::~ColorProgram() { }
@ -20,11 +21,15 @@ ColorProgram::~ColorProgram() { }
std::string ColorProgram::getName() const {
return "Color";
}
void ColorProgram::setup(render::gl::Renderer *renderer, glm::mat4 modelMatrix) {
void ColorProgram::setup(render::gl::Renderer *renderer, glm::mat4 modelMatrix, glm::vec4 color) {
const auto mvp = renderer->getProjectionMatrix() * renderer->getViewMatrix() * modelMatrix;
setMVP(&mvp[0][0]);
setCol(&color[0]);
}
void ColorProgram::setMVP(const GLfloat *matrix) {
glUniformMatrix4fv(MVPMatrixID, 1, GL_FALSE, matrix);
}
void ColorProgram::setCol(const GLfloat *color) {
glUniform4fv(ColID, 1, color);
}

View File

@ -9,13 +9,15 @@ namespace pass {
ColorProgram();
~ColorProgram();
void setup(render::gl::Renderer *, glm::mat4 modelMatrix);
void setup(render::gl::Renderer *, glm::mat4 modelMatrix, glm::vec4 color);
protected:
std::string getName() const override;
void setMVP(const GLfloat *matrix);
void setCol(const GLfloat *color);
private:
GLuint MVPMatrixID;
GLuint ColID;
};
}

View File

@ -32,7 +32,7 @@ void SkyProgram::start(render::gl::Renderer *renderer) {
void SkyProgram::draw(render::gl::Renderer *renderer) {
useIt();
start(renderer);
CubeBuffer.draw();
CubeBuffer.draw(GL_TRIANGLES);
}
void SkyProgram::setView(const GLfloat *matrix) {

View File

@ -23,8 +23,9 @@ CommandCenter::CommandCenter(VkDevice device, const PhysicalDeviceInfo &info, co
}
}
indicCubeBuffer = Indicator::Create(Indicator::CUBE.first, Indicator::CUBE.second);
if (!indicCubeBuffer) {
indicSphereBuffer = Shape::Create(Shape::LINE_SPHERE);
indicCubeBuffer = Shape::Create(Shape::LINE_CUBE);
if (!(indicCubeBuffer && indicSphereBuffer)) {
FATAL("Failed to create vertex buffer!");
}
@ -337,15 +338,15 @@ void CommandCenter::startIndicPass(uint32_t idx, const Subpass& indicPass) {
vkCmdBindPipeline(graphicsBuffers[idx], VK_PIPELINE_BIND_POINT_GRAPHICS, indicPass.pipeline);
vkCmdBindDescriptorSets(graphicsBuffers[idx], VK_PIPELINE_BIND_POINT_GRAPHICS, indicPass.layout, 0, 1, &indicDescriptorSets[idx], 0, nullptr);
}
size_t CommandCenter::recordIndicator(uint32_t idx, const Subpass& indicPass, glm::mat4 model) {
ModelPush push{model};
size_t CommandCenter::recordIndicator(uint32_t idx, const Subpass& indicPass, const ModelColorPush& push, bool isCube) {
vkCmdPushConstants(graphicsBuffers[idx], indicPass.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(push), &push);
VkBuffer vertexBuffers[] = {indicCubeBuffer->getRef()};
const auto buffer = isCube ? indicCubeBuffer.get() : indicSphereBuffer.get();
VkBuffer vertexBuffers[] = {buffer->getRef()};
VkDeviceSize offsets[] = {0};
vkCmdBindVertexBuffers(graphicsBuffers[idx], 0, 1, vertexBuffers, offsets);
vkCmdDraw(graphicsBuffers[idx], indicCubeBuffer->size, 1, 0, 0);
return indicCubeBuffer->size;
vkCmdDraw(graphicsBuffers[idx], buffer->size, 1, 0, 0);
return buffer->size;
}
void CommandCenter::recordPostprocess(uint32_t idx, const Subpass& skyPass, bool skybox) {
vkCmdNextSubpass(graphicsBuffers[idx], VK_SUBPASS_CONTENTS_INLINE);

View File

@ -29,7 +29,7 @@ public:
void startEntityPass(uint32_t idx, const Subpass &entityPass);
size_t recordModels(uint32_t idx, const Subpass &entityPass, const std::vector<glm::mat4>&, const Model *const);
void startIndicPass(uint32_t idx, const Subpass&);
size_t recordIndicator(uint32_t idx, const Subpass&, glm::mat4 model);
size_t recordIndicator(uint32_t idx, const Subpass&, const ModelColorPush&, bool isCube);
void recordPostprocess(uint32_t idx, const Subpass&, bool skybox);
void recordUI(uint32_t idx, VkRenderPass uiPass, VkExtent2D, const std::function<void(VkCommandBuffer)> &);
void submitGraphics(uint32_t, VkSemaphore, VkSemaphore, VkFence);
@ -66,7 +66,8 @@ private:
std::unique_ptr<TextureArray> voxelHOSAtlas;
std::vector<VkDescriptorSet> indicDescriptorSets;
std::unique_ptr<Indicator> indicCubeBuffer;
std::unique_ptr<Shape> indicCubeBuffer;
std::unique_ptr<Shape> indicSphereBuffer;
std::vector<VkDescriptorSet> skyDescriptorSets;
std::unique_ptr<TextureCube> skyboxTexture;

View File

@ -464,7 +464,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render
{ // Indicator pipeline
VkPushConstantRange pushRange{};
pushRange.offset = 0;
pushRange.size = sizeof(ModelPush);
pushRange.size = sizeof(ModelColorPush);
pushRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
setLayout(indicPass, {indicDescriptorSet}, {pushRange});
auto shaderStages = setShaders(indicPass, "Color");
@ -476,12 +476,12 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
auto bindingDescription = Indicator::getBindingDescription();
auto bindingDescription = Shape::getBindingDescription();
vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
auto attributeDescriptions = Indicator::getAttributeDescription();
vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.size();
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
auto attributeDescription = Shape::getAttributeDescription();
vertexInputInfo.vertexAttributeDescriptionCount = 1;
vertexInputInfo.pVertexAttributeDescriptions = &attributeDescription;
VkGraphicsPipelineCreateInfo pipelineInfo{};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;

View File

@ -482,13 +482,16 @@ std::function<size_t(render::Model *const, const std::vector<glm::mat4> &)> Rend
return [](render::Model *const, const std::vector<glm::mat4> &) { return 0; };
}
std::function<size_t(glm::mat4)> Renderer::beginIndicatorPass() {
std::function<size_t(glm::mat4, world::action::Shape, glm::vec4)> Renderer::beginIndicatorPass() {
assert(currentImage < swapChain->getImageViews().size());
auto &pass = pipeline->getIndicPass();
commandCenter->startIndicPass(currentImage, pass);
return [&](glm::mat4 model) {
return commandCenter->recordIndicator(currentImage, pass, model);
return [&](glm::mat4 model, world::action::Shape shape, glm::vec4 color) {
ModelColorPush push{};
push.model = model;
push.color = color;
return commandCenter->recordIndicator(currentImage, pass, push, shape == world::action::Shape::Cube);
};
}

View File

@ -23,7 +23,7 @@ public:
void beginFrame() override;
std::function<size_t(render::LodModel *const, glm::mat4, glm::vec4, float)> beginWorldPass(bool solid) override;
std::function<size_t(render::Model *const, const std::vector<glm::mat4> &)> beginEntityPass() override;
std::function<size_t(glm::mat4)> beginIndicatorPass() override;
std::function<size_t(glm::mat4, world::action::Shape, glm::vec4)> beginIndicatorPass() override;
void postProcess() override;
void recordUI(std::function<void(VkCommandBuffer)>);
void endFrame() override;

View File

@ -9,7 +9,7 @@ std::unique_ptr<Shape> Shape::Create(const std::vector<glm::vec3>& vertices) {
return std::unique_ptr<Shape>(new Shape(tmp.ref, std::move(mem), vertices.size()));
}
std::unique_ptr<Indicator> Indicator::Create(const std::vector<glm::vec3>& vert, const std::vector<glm::vec4>& cols) {
std::unique_ptr<ColoredShape> ColoredShape::Create(const std::vector<glm::vec3>& vert, const std::vector<glm::vec4>& cols) {
assert(vert.size() == cols.size());
std::vector<Vertex> vertices;
vertices.reserve(vert.size());
@ -20,7 +20,7 @@ std::unique_ptr<Indicator> Indicator::Create(const std::vector<glm::vec3>& vert,
vk::Buffer::info tmp;
data_view view(vertices);
auto mem = createBuffer(view.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, view, tmp);
return std::unique_ptr<Indicator>(new Indicator(vertices.size(), tmp.ref, std::move(mem)));
return std::unique_ptr<ColoredShape>(new ColoredShape(vertices.size(), tmp.ref, std::move(mem)));
}
std::unique_ptr<Model> Model::Create(const Data& data) {

View File

@ -33,11 +33,11 @@ protected:
Buffer(ref, std::move(mem)), size(size) { }
};
class Indicator final: public render::Indicator, public Buffer {
class ColoredShape final: public render::ColoredShape, public Buffer {
public:
const size_t size;
static std::unique_ptr<Indicator> Create(const std::vector<glm::vec3>&, const std::vector<glm::vec4>&);
static std::unique_ptr<ColoredShape> Create(const std::vector<glm::vec3>&, const std::vector<glm::vec4>&);
struct Vertex {
alignas(16) glm::vec3 pos;
@ -65,7 +65,7 @@ public:
}
protected:
Indicator(size_t size, VkBuffer ref, memory::ptr mem): Buffer(ref, std::move(mem)), size(size) {}
ColoredShape(size_t size, VkBuffer ref, memory::ptr mem): Buffer(ref, std::move(mem)), size(size) {}
};
@ -162,6 +162,10 @@ struct VoxelUBO {
struct ModelPush {
alignas(16) glm::mat4 model;
};
struct ModelColorPush {
alignas(16) glm::mat4 model;
alignas(16) glm::vec4 color;
};
struct CurvaturePush {
alignas(16) glm::vec4 sphereProj;
alignas(4) float curvature;

View File

@ -9,6 +9,7 @@ struct state {
bool capture_mouse = true;
camera_pos position = camera_pos(voxel_pos(0), 1);
std::optional<world::Universe::ray_target> look_at = {};
bool can_fill = true;
contouring::Abstract* contouring;

View File

@ -258,8 +258,8 @@ void DistantUniverse::emit(const action::packet &action) {
peer.send(net::client_packet_type::MOVE, move->pos, net::channel_type::NOTIFY);
} else if(const auto message = std::get_if<action::Message>(&action)) {
peer.send(net::client_packet_type::MESSAGE, message->text.data(), message->text.size(), net::channel_type::RELIABLE);
} else if(const auto fillCube = std::get_if<action::FillCube>(&action)) {
peer.send(net::client_packet_type::FILL_CUBE, *fillCube, net::channel_type::RELIABLE);
} else if(const auto fill = std::get_if<action::FillShape>(&action)) {
peer.send(net::client_packet_type::FILL_SHAPE, *fill, net::channel_type::RELIABLE);
} else {
LOG_W("Bad action " << action.index());
}

View File

@ -66,8 +66,8 @@ enum class server_packet_type: enet_uint8 {
};
enum class client_packet_type: enet_uint8 {
/// Interact with voxels
/// actions::FillCube reliable
FILL_CUBE = 0,
/// actions::FillShape reliable
FILL_SHAPE = 0,
/// Request missing chunks
/// area_id, chunk_pos[] reliable

View File

@ -17,10 +17,28 @@ struct Fill: part::Ping {
const area_<voxel_pos> pos;
const Voxel val;
};
struct FillCube: Fill {
FillCube(const area_<voxel_pos> &pos, const Voxel &val, int radius): Fill(pos, val), radius(radius) { }
enum class Shape : uint8_t {
Cube,
Sphere,
/*SmoothSphere,
CylinderX,
CylinderY,
CylinderZ,
ConePX,
ConeNX,
ConePY,
ConeNY,
ConePZ,
ConeNZ,
*/
};
constexpr auto SHAPES = "Cube\0Sphere\0";
struct FillShape: Fill {
FillShape(const area_<voxel_pos> &pos, const Voxel &val, Shape shape, uint8_t radius):
Fill(pos, val), shape(shape), radius(radius) {}
const int radius;
const Shape shape;
const uint8_t radius;
};
struct Move: part::Ping {
Move(const voxel_pos& pos): pos(pos) { }
@ -33,5 +51,5 @@ struct Message: part::Ping {
const std::string text;
};
using packet = std::variant<Move, Message, Fill, FillCube>;
using packet = std::variant<Move, Message, Fill, FillShape>;
}

View File

@ -6,7 +6,7 @@ using namespace world::server;
SharedUniverse::SharedUniverse(const options &o, server_handle *const localHandle): Universe(o), localHandle(localHandle) {
// Local player
[[maybe_unused]]
const auto id = entities.at(PLAYER_ENTITY_ID).instances.emplace(Entity::Instance{spawnPoint});
const auto id = entities.at(PLAYER_ENTITY_ID).instances.emplace(Entity::Instance{spawnPoint, glm::vec3(0)});
assert(id == PLAYER_ENTITY_ID);
localHandle->teleport = spawnPoint;
movedPlayers.insert(id);
@ -14,10 +14,11 @@ SharedUniverse::SharedUniverse(const options &o, server_handle *const localHandl
localHandle->areas = (world::client::area_map*)(&areas); //WONT FIX: templated area
localHandle->entities = &entities;
localHandle->emit = std::function([&](const world::action::packet &packet) {
if(const auto fill = std::get_if<world::action::Fill>(&packet)) {
this->set(fill->pos, fill->val);
} else if(const auto fillCube = std::get_if<world::action::FillCube>(&packet)) {
this->setCube(fillCube->pos, fillCube->val, fillCube->radius);
if(const auto fill = std::get_if<world::action::FillShape>(&packet)) {
if (fill->shape == world::action::Shape::Cube)
this->setCube(fill->pos, fill->val, fill->radius);
else
this->setSphere(fill->pos, fill->val, fill->radius);
} else if(const auto message = std::get_if<world::action::Message>(&packet)) {
this->broadcastMessage("Player" + std::to_string(id.index) + ": " + message->text);
} else if(const auto move = std::get_if<world::action::Move>(&packet)) {

View File

@ -391,7 +391,7 @@ void Universe::pullNetwork() {
[&](peer_t *peer, salt_t salt) {
ZoneScopedN("Connect");
LOG_I("Client connect from " << peer->address);
net_client* client = new net_client(salt, entities.at(PLAYER_ENTITY_ID).instances.emplace(Entity::Instance{spawnPoint}));
net_client* client = new net_client(salt, entities.at(PLAYER_ENTITY_ID).instances.emplace(Entity::Instance{spawnPoint, glm::vec3(0)}));
peer->data = client;
const salt_t rnd = std::rand();
@ -451,12 +451,15 @@ void Universe::pullNetwork() {
}
break;
}
case client_packet_type::FILL_CUBE: {
if(const auto fill = PacketReader(packet).read<world::action::FillCube>()) {
case client_packet_type::FILL_SHAPE: {
if(const auto fill = PacketReader(packet).read<world::action::FillShape>()) {
//TODO: check ray
//TODO: check entities
//TODO: handle inventory
setCube(fill->pos, fill->val, fill->radius);
if (fill->shape == world::action::Shape::Cube)
setCube(fill->pos, fill->val, fill->radius);
else
setSphere(fill->pos, fill->val, fill->radius);
} else {
LOG_T("Bad fill");
}
@ -527,7 +530,7 @@ void Universe::broadcastMessage(const std::string& text) {
host.broadcast(net::server_packet_type::MESSAGE, text.data(), text.size(), net::channel_type::RELIABLE);
}
void Universe::updateChunk(area_map::iterator &, world::ChunkContainer::iterator &, chunk_pos, float deltaTime) {}
void Universe::updateChunk(area_map::iterator &, world::ChunkContainer::iterator &, chunk_pos, float /*deltaTime*/) {}
void Universe::loadChunk(area_<chunk_pos>, chunk_pos, const world::ChunkContainer &) {}
void Universe::setOptions(const Universe::options& options) {
@ -594,6 +597,55 @@ world::ItemList Universe::setCube(const area_<voxel_pos>& pos, const Voxel& val,
}
return list;
}
world::ItemList Universe::setSphere(const area_<voxel_pos>& pos, const Voxel& val, int radius) {
ZoneScopedN("FillSphere");
ItemList list;
if(const auto it = areas.find(pos.first); it != areas.end()) {
robin_hood::unordered_map<chunk_pos, std::vector<Chunk::Edit>> edits;
auto &chunks = it->second->setChunks();
for (int z = -radius; z <= radius; z++) {
for (int y = -radius; y <= radius; y++) {
for (int x = -radius; x <= radius; x++) {
const auto offset = voxel_pos(x, y, z);
//FIXME: refactor with voxel_pos iterator
if (glm::length2(offset) > glm::pow2(radius))
continue;
//TODO: list.pop(val)
const auto split = glm::splitIdx(pos.second + offset);
if(chunks.inRange(split.first))
if(const auto chunk = it->second->setChunks().findInRange(split.first)) {
auto ck = std::dynamic_pointer_cast<Chunk>(chunk.value());
auto prev = ck->get(split.second);
if(prev.value != val.value) {
//TODO: apply break table
//TODO: inventory
const auto delay = glm::length2(offset) / radius * .05f;
edits[split.first].push_back(Chunk::Edit{split.second, val, delay});
ck->replace(split.second, val, delay);
}
}
}}}
ZoneScopedN("Packet");
size_t size = sizeof(area_id);
for(const auto& part: edits) {
size += sizeof(chunk_pos);
size += sizeof(chunk_voxel_idx);
size += sizeof(Chunk::Edit) * part.second.size();
}
auto packet = net::Server::makePacket(net::server_packet_type::EDITS, NULL, size, 0);
packet.write(pos.first);
for(const auto& part: edits) {
packet.write(part.first);
packet.write<chunk_voxel_idx>(part.second.size());
packet.write(part.second.data(), part.second.size() * sizeof(Chunk::Edit));
}
assert(packet.isFull());
host.broadcast(packet.get(), net::channel_type::NOTIFY);
}
return list;
}
bool Universe::collide_end(const glm::ifvec3 &pos, const glm::vec3 &vel, int density, float radius) const {
return std::holds_alternative<ray_target>(raycast(geometry::Ray((pos + vel) * density, vel, radius)));

View File

@ -44,6 +44,9 @@ namespace world::server {
/// Set cube of voxel with pos as center
/// MAYBE: allow set multi area
ItemList setCube(const area_<voxel_pos> &pos, const Voxel &val, int radius);
/// Set sphere of voxel with pos as center
/// MAYBE: allow set multi area
ItemList setSphere(const area_<voxel_pos> &pos, const Voxel &val, int radius);
/// Instante entity
entity_instance_id addEntity(entity_id type, const Entity::Instance &instance);
@ -76,7 +79,6 @@ namespace world::server {
virtual std::shared_ptr<Chunk> createChunk(const chunk_pos &pos, const std::unique_ptr<generator::Abstract> &rnd) const;
virtual std::shared_ptr<Chunk> createChunk(std::istream &str) const;
virtual void updateChunk(area_map::iterator&, world::ChunkContainer::iterator&, chunk_pos, float deltaTime);
virtual void loadChunk(area_<chunk_pos>, chunk_pos, const world::ChunkContainer &);