added texture to shapes

This commit is contained in:
Lauchmelder 2021-12-24 23:34:01 +01:00
parent 38a49c504a
commit 9ff485588e
8 changed files with 70 additions and 30 deletions

View file

@ -132,22 +132,25 @@ void Application::Init(int width, int height, const std::string& title)
pitch = camera.GetAngles().x;
yaw = camera.GetAngles().y;
Shape* shape = new Cube();
lol::Image img("assets/puh.jpg");
texture = std::make_shared<lol::Texture>(img);
Shape* shape = new Cube(texture);
shape->Move(glm::vec3(0.0f, -2.0f, 0.0f));
shape->Rotate(glm::vec3(1.0f, 1.0f, 1.0f), 60);
shapes.push_back(shape);
shape = new Cube();
shape = new Cube(texture);
shape->Move(glm::vec3(0.0f, 2.0f, 0.0f));
shape->Rotate(glm::vec3(0.5f, 1.0f, 1.2f), 60);
shapes.push_back(shape);
shape = new Pyramid();
shape = new Pyramid(texture);
shape->Move(glm::vec3(0.0f, 0.0f, 3.0f));
shape->Rotate(glm::vec3(1.0f, 0.0f, 0.0f), -90);
shapes.push_back(shape);
shape = new Pyramid();
shape = new Pyramid(texture);
shape->Move(glm::vec3(0.0f, 0.0f, -3.0f));
shape->Rotate(glm::vec3(1.0f, 0.3f, 1.2f), 120);
shapes.push_back(shape);
@ -201,6 +204,11 @@ void Application::Launch()
ImGui::SliderFloat("Pitch", &pitch, 1.0f, 179.0f);
}
if (ImGui::CollapsingHeader("Texture"))
{
ImGui::Image((void*)texture->GetID(), ImVec2(67 * 3, 72 * 3));
}
ImGui::End();
ImGui::Render();

View file

@ -47,4 +47,5 @@ private:
glm::vec3 cubeOrientation, cubePosition, cubeScale;
std::vector<Shape*> shapes;
std::shared_ptr<lol::Texture> texture;
};

View file

@ -22,4 +22,8 @@ target_include_directories(visualizer PRIVATE
target_link_libraries(visualizer PRIVATE
${GLFW3_LIBRARIES}
lol
)
add_custom_command(TARGET visualizer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:visualizer>/assets
)

View file

@ -2,7 +2,7 @@
#include "Util.hpp"
Shape::Shape()
Shape::Shape(const std::shared_ptr<lol::Texture>& texture)
{
shader = lol::ShaderManager::GetInstance().Get(SHAPE_ID);
if (shader == nullptr)
@ -12,13 +12,17 @@ Shape::Shape()
#version 460 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 uv;
out vec2 UVcoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
UVcoord = uv;
gl_Position = projection * view * model * vec4(pos, 1.0f);
}
)",
@ -26,16 +30,22 @@ Shape::Shape()
#version 460 core
out vec4 FragColor;
in vec2 UVcoord;
uniform sampler2D shapeTexture;
void main()
{
FragColor = vec4(1.0f);
FragColor = texture(shapeTexture, UVcoord);
}
)"
);
lol::ShaderManager::GetInstance().Register(SHAPE_ID, shader);
}
this->texture = texture;
}
Shape::~Shape()
@ -45,29 +55,37 @@ Shape::~Shape()
void Shape::PreRender(const lol::CameraBase& camera) const
{
texture->Bind();
shader->SetUniform("model", transformation);
shader->SetUniform("view", camera.GetView());
shader->SetUniform("projection", camera.GetProjection());
}
Cube::Cube()
Cube::Cube(const std::shared_ptr<lol::Texture>& texture) :
Shape(texture)
{
vao = lol::VAOManager::GetInstance().Get(CUBE_ID);
if (vao == nullptr)
{
std::shared_ptr<lol::VertexBuffer> vbo = std::make_shared<lol::VertexBuffer>(8 * 3,
std::shared_ptr<lol::VertexBuffer> vbo = std::make_shared<lol::VertexBuffer>(8 * (3 + 2),
std::vector<float> {
-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, 0.0f, 0.0f,
1.0f, -1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
}
);
vbo->SetLayout(
{
{lol::Type::Float, 3, false},
{lol::Type::Float, 2, false}
}
);
vbo->SetLayout({{lol::Type::Float, 3, false}});
std::shared_ptr<lol::ElementBuffer> ebo = std::make_shared<lol::ElementBuffer>(6 * 3 * 2,
std::vector<unsigned int> {
@ -90,21 +108,27 @@ Cube::~Cube()
lol::VAOManager::GetInstance().Return(CUBE_ID);
}
Pyramid::Pyramid()
Pyramid::Pyramid(const std::shared_ptr<lol::Texture>& texture) :
Shape(texture)
{
vao = lol::VAOManager::GetInstance().Get(PYRAMID_ID);
if (vao == nullptr)
{
std::shared_ptr<lol::VertexBuffer> vbo = std::make_shared<lol::VertexBuffer>(5 * 3,
std::shared_ptr<lol::VertexBuffer> vbo = std::make_shared<lol::VertexBuffer>(5 * (3 + 2),
std::vector<float> {
-1.0f, -0.86f, 1.0f,
1.0f, -0.86f, 1.0f,
-1.0f, -0.86f, -1.0f,
1.0f, -0.86f, -1.0f,
0.0f, 0.86f, 0.0f,
-1.0f, -0.86f, 1.0f, 0.0f, 0.0f,
1.0f, -0.86f, 1.0f, 1.0f, 0.0f,
-1.0f, -0.86f, -1.0f, 0.0f, 1.0f,
1.0f, -0.86f, -1.0f, 1.0f, 0.0f,
0.0f, 0.86f, 0.0f, 0.5f, 1.0f
}
);
vbo->SetLayout({ {lol::Type::Float, 3, false} });
vbo->SetLayout(
{
{lol::Type::Float, 3, false},
{lol::Type::Float, 2, false}
}
);
std::shared_ptr<lol::ElementBuffer> ebo = std::make_shared<lol::ElementBuffer>(18,
std::vector<unsigned int> {

View file

@ -5,22 +5,25 @@
class Shape : public lol::Drawable, public lol::Transformable
{
public:
Shape();
Shape(const std::shared_ptr<lol::Texture>& texture);
virtual ~Shape();
void PreRender(const lol::CameraBase& camera) const override;
protected:
std::shared_ptr<lol::Texture> texture;
};
class Cube : public Shape
{
public:
Cube();
Cube(const std::shared_ptr<lol::Texture>& texture);
~Cube();
};
class Pyramid : public Shape
{
public:
Pyramid();
Pyramid(const std::shared_ptr<lol::Texture>& texture);
~Pyramid();
};

BIN
src/assets/puh.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
src/assets/texture.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

2
vendor/lol vendored

@ -1 +1 @@
Subproject commit 1610813bbe5e0f970cdf5bc18c30c1dc51cd6d07
Subproject commit 1e44994bb409a647930593723ee950c1a85879cc