Skip to content

Commit

Permalink
First attemping to add instancing
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Oct 20, 2023
1 parent c79c4f6 commit 3586b86
Show file tree
Hide file tree
Showing 6 changed files with 618 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/core/core-wrappers/include/Gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,5 @@ class Gl

static void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
static void drawArrays(GLenum mode, GLint first, GLsizei count);
static void drawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount);
};
8 changes: 8 additions & 0 deletions lib/core/core-wrappers/source/Gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ Gl::State Gl::getState()
return state;
}

void Gl::drawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount)
{
glDrawArraysInstanced(mode, first, count, instanceCount);
#ifdef OPENGL_DEBUG
Gl::debugTraces();
#endif
}

std::string Gl::Texture::channelToString(Channel channel)
{
if (channel == Channel::SRGB)
Expand Down
54 changes: 54 additions & 0 deletions lib/core/shapes/include/InstancedDrawAble.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// MIT License
//
// Copyright (c) 2023 Valerii Koniushenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "ShaderPack.h"
#include "glm/glm.hpp"

#include <cstdlib>

class Camera;

class InstancedDrawAble
{
public:
virtual void draw(ShaderPack& shaderProgram, Camera* camera = nullptr);
[[nodiscard]] virtual std::size_t getVerticesCount() const;

void setPosition(const glm::vec2& newPosition);
void move(const glm::vec2& offset);
[[nodiscard]] const glm::vec2& getPosition() const;

void setRotation(float newRotation);
void rotate(float offset);
[[nodiscard]] float getRotation() const;
virtual void update() = 0;

[[nodiscard]] std::vector<glm::vec2>& getTransforms();
[[nodiscard]] const std::vector<glm::vec2>& getTransforms() const;

protected:
glm::vec2 position_{};
float rotation_{};
std::vector<glm::vec2> transforms_;
};
119 changes: 119 additions & 0 deletions lib/core/shapes/include/InstancedWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// MIT License
//
// Copyright (c) 2023 Valerii Koniushenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "Delegate.h"
#include "InstancedDrawAble.h"
#include "JsonPrintable.h"
#include "Keyboard.h"
#include "NotCopyableButMovable.h"
#include "Rect.h"
#include "Size.h"
#include "Updateable.h"
#include "Vao.h"
#include "Vbo.h"

#include <vector>

class Texture;

class InstancedWidget : public InstancedDrawAble, public JsonPrintable, public Utils::CopyableAndMoveable
{
public:
inline static constexpr glm::vec4 borderColor = {1.f, 1.f, 0.f, 1.f};
inline static constexpr float borderWidth = 0.05f;
inline static constexpr const char* componentName = "widget";

InstancedWidget() = default;

InstancedWidget(InstancedWidget&& other) noexcept;

InstancedWidget& operator=(InstancedWidget&& other) noexcept;

void draw(ShaderPack& shaderProgram, Camera* camera = nullptr) override;

[[nodiscard]] std::size_t getVerticesCount() const override;

void setTexture(Texture& texture);

[[nodiscard]] Texture& getTexture();

[[nodiscard]] const Texture& getTexture() const;

void setSize(Utils::FSize2D newSize);

[[nodiscard]] Utils::FSize2D getSize() const;

void setScale(Utils::FSize2D newScale);

[[nodiscard]] Utils::FSize2D getScale() const;

[[nodiscard]] Utils::FRect getRect() const;

void setIsDrawBorder(bool isDraw);

[[nodiscard]] bool isDrawBorder() const;

virtual void prepare(ShaderPack& shader);

void update() override;

[[nodiscard]] virtual std::string getComponentName() const;

LambdaMulticastDelegate<void()> onMouseHover;
LambdaMulticastDelegate<void()> onMouseUnHover;
LambdaMulticastDelegate<void()> onMouseLeftClick;
LambdaMulticastDelegate<void()> onMouseRightClick;
LambdaMulticastDelegate<void()> onMouseMiddleClick;
LambdaMulticastDelegate<void(double)> onMouseWheel;
LambdaMulticastDelegate<void(unsigned int)> onTextInput;

[[nodiscard]] boost::property_tree::ptree toJson() const override;

void setTextureRect(const Utils::Rect<int>& rect);

void calculateFitTextureSize();

private:
void recalculateVerticiesData();

private:
// clang-format off
inline static const std::vector<float> templateVertices_ = {
0.f, 0.f, 0.f, 1.f, 0, 0,
0.f, -1.f, 0.f, 0.f, 0, 0,
1.f, 0.f, 1.f, 1.f, 0, 0,
1.f, -1.f, 1.f, 0.f, 0, 0,
};
// clang-format on
Utils::IRect textureRect_;
Texture* texture_{};
Vbo vbo_;
Vao vao_;
Vbo vboInstancing_;
Utils::FSize2D size_ = {.width = 100.f, .height = 100.f};
Utils::FSize2D scale_ = {.width = 1.f, .height = 1.f};
bool isDrawBorder_ = false;
bool wasHover_ = false;
bool isPrepared = false;
};
76 changes: 76 additions & 0 deletions lib/core/shapes/source/InstancedDrawAble.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// MIT License
//
// Copyright (c) 2023 Valerii Koniushenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "InstancedDrawAble.h"
#include "Camera.h"

#include "Gl.h"

void InstancedDrawAble::draw(ShaderPack& shaderPack, Camera* camera/* = nullptr*/)
{
Gl::drawArraysInstanced(GL_TRIANGLE_STRIP, 0, getVerticesCount(), transforms_.size());
}

std::size_t InstancedDrawAble::getVerticesCount() const
{
return 0;
}

void InstancedDrawAble::setPosition(const glm::vec2& newPosition)
{
position_ = newPosition;
}

void InstancedDrawAble::move(const glm::vec2& offset)
{
position_ += offset;
}

const glm::vec2& InstancedDrawAble::getPosition() const
{
return position_;
}

void InstancedDrawAble::setRotation(float newRotation)
{
rotation_ = newRotation;
}

void InstancedDrawAble::rotate(float offset)
{
rotation_ += offset;
}

float InstancedDrawAble::getRotation() const
{
return rotation_;
}

std::vector<glm::vec2>& InstancedDrawAble::getTransforms()
{
return transforms_;
}

const std::vector<glm::vec2>& InstancedDrawAble::getTransforms() const
{
return transforms_;
}
Loading

0 comments on commit 3586b86

Please sign in to comment.