Skip to content

Commit

Permalink
Added an abilty to pose a Drawable units without NDC coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Jul 10, 2023
1 parent 9a49a34 commit 5d26463
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
4 changes: 3 additions & 1 deletion game/assets/shaders/main-vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ layout (location = 1) in vec2 aCv;
out vec2 ioCv;

uniform mat4 uTransform;
uniform vec2 uResolution;

void main()
{
ioCv = aCv;
gl_Position = uTransform * vec4(aPos.x, aPos.y, 0.0, 1.0);
vec2 newPosition = vec2(aPos.x / uResolution.x, aPos.y / uResolution.y);
gl_Position = uTransform * vec4(newPosition.x, newPosition.y, 0.0, 1.0);
}
7 changes: 0 additions & 7 deletions game/source/VaKon2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,14 @@ void VaKon2D::start()

class Rectangle rect;
rect.setTexture(texture);
rect.setPosition({-0.66f, 0.f});
rect.prepare();

class Rectangle rect1;
rect1.setTexture(texture);
rect1.setPosition({0.f, 0.f});
rect1.prepare();

while (!GetWindow().shouldClose())
{
GetWindow().clearColor(0.2f, 0.3f, 0.3f, 1.0f); // TODO: create class Color
GetWindow().clear(GL_COLOR_BUFFER_BIT); // TODO: change to enum class

rect.draw(program);
rect1.draw(program);

GetWindow().swapBuffers();
GetWindow().pollEvent();
Expand Down
1 change: 1 addition & 0 deletions lib/core/shapes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ target_link_libraries(
Utils
Core-Wrappers
Glm
Window
)
8 changes: 4 additions & 4 deletions lib/core/shapes/include/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class Rectangle : public DrawAble
private:
// clang-format off
const std::vector<float> vertices = {
0.f, 0.f, 0.f, 0.f,
1.f, 0.f, 1.f, 0.f,
0.f, 1.f, 0.f, 1.f,
1.f, 1.f, 1.f, 1.f,
0.f, 0.f, 0.f, 0.f,
500.f, 0.f, 1.f, 0.f,
0.f, 500.f, 0.f, 1.f,
500.f, 500.f, 1.f, 1.f,
};
// clang-format on

Expand Down
3 changes: 3 additions & 0 deletions lib/core/shapes/source/Rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "ShaderProgram.h"
#include "Texture.h"
#include "Window.h"

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
Expand All @@ -43,6 +44,8 @@ void Rectangle::draw(ShaderProgram& shaderProgram)

shaderProgram.use();
shaderProgram.uniform("uTransform", false, trans);
shaderProgram.uniform(
"uResolution", static_cast<float>(GetWindow().getSize().width), static_cast<float>(GetWindow().getSize().height));

DrawAble::draw(shaderProgram);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/core/window/include/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ class Window : public Singleton<Window>
void clear(int code);
void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
_NODISCARD HWND getHwnd();
Utils::ISize2D getSize() const;

LambdaDelegate<void(int, int, int, int)> onKeyPressed;
LambdaDelegate<void(unsigned int)> onTextInput;
LambdaDelegate<void(int)> onCursorEntered;

protected:
GLFWwindow* window{};
Utils::ISize2D size_{};
std::string title_;
};

Window& GetWindow();
7 changes: 7 additions & 0 deletions lib/core/window/source/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ void Window::create(Utils::ISize2D size, const std::string& title)
glfwTerminate();
throw std::runtime_error("Failed to create GLFW window");
}
size_ = size;
title_ = title;

glfwMakeContextCurrent(window);

Expand Down Expand Up @@ -94,6 +96,11 @@ HWND Window::getHwnd()
return glfwGetWin32Window(window);
}

Utils::ISize2D Window::getSize() const
{
return size_;
}

Window& GetWindow()
{
return Window::instance();
Expand Down

0 comments on commit 5d26463

Please sign in to comment.