texture heightmapping test
This commit is contained in:
parent
9ff485588e
commit
60a3e4a038
|
@ -26,11 +26,10 @@ void Application::Quit()
|
|||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
for (Shape* shape : shapes)
|
||||
delete shape;
|
||||
|
||||
if (window != nullptr)
|
||||
{
|
||||
delete topology;
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
window = nullptr;
|
||||
}
|
||||
|
@ -129,38 +128,25 @@ void Application::Init(int width, int height, const std::string& title)
|
|||
float aspectRatio = (float)windowWidth / (float)windowHeight;
|
||||
camera = OrbitingCamera(glm::vec3(0.0f, 0.0f, 0.0f), 6.0f);
|
||||
camera.Update(100.0f, aspectRatio, 0.01f, 100.0f);
|
||||
pitch = camera.GetAngles().x;
|
||||
yaw = camera.GetAngles().y;
|
||||
|
||||
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(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(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(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);
|
||||
|
||||
cubePosition = glm::vec3(0.0f);
|
||||
cubeOrientation = glm::vec3(0.0f);
|
||||
cubeScale = glm::vec3(1.0f);
|
||||
pitch = -50.0f;
|
||||
yaw = 0.0f;
|
||||
|
||||
data.camera = &camera;
|
||||
|
||||
topology = new Topology(glm::vec2(7.5f), glm::uvec2(100));
|
||||
glm::uvec2 size = topology->GetSize();
|
||||
|
||||
float* pixels = topology->GetTopology();
|
||||
for (unsigned int y = 0; y < size.y; y++)
|
||||
{
|
||||
for (unsigned int x = 0; x < size.x; x++)
|
||||
{
|
||||
pixels[y * size.x + x] = 0.5f + (cos(x * 0.1f) + cos(y * 0.1f)) * 0.25f;
|
||||
}
|
||||
}
|
||||
|
||||
topology->MakeTexture();
|
||||
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
|
||||
// glEnable(GL_CULL_FACE);
|
||||
|
@ -172,43 +158,26 @@ void Application::Launch()
|
|||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
// plot->SetPosition(cubePosition);
|
||||
// plot->SetRotation(cubeOrientation);
|
||||
// plot->SetScale(cubeScale);
|
||||
|
||||
|
||||
camera.SetPosition(pitch, yaw);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
camera.Draw(*topology);
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
for (Shape* shape : shapes)
|
||||
camera.Draw(*shape);
|
||||
|
||||
ImGui::Begin("Debug");
|
||||
|
||||
/*if (ImGui::CollapsingHeader("Plot"))
|
||||
{
|
||||
ImGui::SliderFloat3("Position", &(cubePosition[0]), -2.0f, 2.0f);
|
||||
ImGui::SliderFloat3("Orientation", &(cubeOrientation[0]), -glm::pi<float>(), glm::pi<float>());
|
||||
ImGui::SliderFloat3("Scale", &(cubeScale[0]), 0.0f, 2.0f);
|
||||
}*/
|
||||
|
||||
if (ImGui::CollapsingHeader("Camera"))
|
||||
{
|
||||
ImGui::SliderFloat("Yaw", &yaw, 0.0f, 360.0f);
|
||||
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();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <string>
|
||||
#include "OrbitingCamera.hpp"
|
||||
#include "Shapes.hpp"
|
||||
#include "Topology.hpp"
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
|
@ -45,7 +45,5 @@ private:
|
|||
OrbitingCamera camera;
|
||||
float pitch, yaw;
|
||||
|
||||
glm::vec3 cubeOrientation, cubePosition, cubeScale;
|
||||
std::vector<Shape*> shapes;
|
||||
std::shared_ptr<lol::Texture> texture;
|
||||
Topology* topology;
|
||||
};
|
|
@ -1,7 +1,7 @@
|
|||
add_executable(visualizer
|
||||
"main.cpp" "Application.cpp"
|
||||
"OrbitingCamera.cpp" "Shape.cpp"
|
||||
)
|
||||
"OrbitingCamera.cpp"
|
||||
"Topology.cpp")
|
||||
|
||||
target_sources(visualizer PUBLIC
|
||||
${VENDOR_DIR}/imgui/backends/imgui_impl_opengl3.cpp
|
||||
|
@ -22,8 +22,4 @@ 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
|
||||
)
|
152
src/Shape.cpp
152
src/Shape.cpp
|
@ -1,152 +0,0 @@
|
|||
#include "Shapes.hpp"
|
||||
|
||||
#include "Util.hpp"
|
||||
|
||||
Shape::Shape(const std::shared_ptr<lol::Texture>& texture)
|
||||
{
|
||||
shader = lol::ShaderManager::GetInstance().Get(SHAPE_ID);
|
||||
if (shader == nullptr)
|
||||
{
|
||||
shader = std::make_shared<lol::Shader>(
|
||||
R"(
|
||||
#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);
|
||||
}
|
||||
)",
|
||||
R"(
|
||||
#version 460 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec2 UVcoord;
|
||||
|
||||
uniform sampler2D shapeTexture;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = texture(shapeTexture, UVcoord);
|
||||
}
|
||||
)"
|
||||
);
|
||||
|
||||
lol::ShaderManager::GetInstance().Register(SHAPE_ID, shader);
|
||||
}
|
||||
|
||||
this->texture = texture;
|
||||
}
|
||||
|
||||
Shape::~Shape()
|
||||
{
|
||||
lol::ShaderManager::GetInstance().Return(SHAPE_ID);
|
||||
}
|
||||
|
||||
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(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 + 2),
|
||||
std::vector<float> {
|
||||
-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}
|
||||
}
|
||||
);
|
||||
|
||||
std::shared_ptr<lol::ElementBuffer> ebo = std::make_shared<lol::ElementBuffer>(6 * 3 * 2,
|
||||
std::vector<unsigned int> {
|
||||
0, 1, 3, 3, 1, 2,
|
||||
1, 5, 2, 2, 5, 6,
|
||||
5, 4, 6, 6, 4, 7,
|
||||
4, 0, 7, 7, 0, 3,
|
||||
3, 2, 7, 7, 2, 6,
|
||||
4, 5, 0, 0, 5, 1
|
||||
}
|
||||
);
|
||||
|
||||
vao = std::make_shared<lol::VertexArray>(vbo, ebo);
|
||||
lol::VAOManager::GetInstance().Register(CUBE_ID, vao);
|
||||
}
|
||||
}
|
||||
|
||||
Cube::~Cube()
|
||||
{
|
||||
lol::VAOManager::GetInstance().Return(CUBE_ID);
|
||||
}
|
||||
|
||||
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 + 2),
|
||||
std::vector<float> {
|
||||
-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},
|
||||
{lol::Type::Float, 2, false}
|
||||
}
|
||||
);
|
||||
|
||||
std::shared_ptr<lol::ElementBuffer> ebo = std::make_shared<lol::ElementBuffer>(18,
|
||||
std::vector<unsigned int> {
|
||||
0, 2, 1,
|
||||
1, 2, 3,
|
||||
0, 1, 4,
|
||||
1, 3, 4,
|
||||
3, 2, 4,
|
||||
2, 0, 4,
|
||||
}
|
||||
);
|
||||
|
||||
vao = std::make_shared<lol::VertexArray>(vbo, ebo);
|
||||
lol::VAOManager::GetInstance().Register(PYRAMID_ID, vao);
|
||||
}
|
||||
}
|
||||
|
||||
Pyramid::~Pyramid()
|
||||
{
|
||||
lol::VAOManager::GetInstance().Return(PYRAMID_ID);
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <lol/lol.hpp>
|
||||
|
||||
class Shape : public lol::Drawable, public lol::Transformable
|
||||
{
|
||||
public:
|
||||
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(const std::shared_ptr<lol::Texture>& texture);
|
||||
~Cube();
|
||||
};
|
||||
|
||||
class Pyramid : public Shape
|
||||
{
|
||||
public:
|
||||
Pyramid(const std::shared_ptr<lol::Texture>& texture);
|
||||
~Pyramid();
|
||||
};
|
119
src/Topology.cpp
Normal file
119
src/Topology.cpp
Normal file
|
@ -0,0 +1,119 @@
|
|||
#include "Topology.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Util.hpp"
|
||||
|
||||
Topology::Topology(const glm::vec2& size, const glm::uvec2& subdivisions) :
|
||||
texture(nullptr)
|
||||
{
|
||||
// Create VAO
|
||||
vao = std::make_shared<lol::VertexArray>();
|
||||
|
||||
std::vector<float> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
for (unsigned int y = 0; y < subdivisions.y; y++)
|
||||
{
|
||||
float yCoord = Map(glm::vec2(0, subdivisions.y), 0.5f * size.y * glm::vec2(-1.0f, 1.0f), y);
|
||||
for (unsigned int x = 0; x < subdivisions.x; x++)
|
||||
{
|
||||
float xCoord = Map(glm::vec2(0, subdivisions.x), 0.5f * size.x * glm::vec2(-1.0f, 1.0f), x);
|
||||
|
||||
vertices.push_back(xCoord);
|
||||
vertices.push_back(yCoord);
|
||||
vertices.push_back(Map(glm::vec2(0, subdivisions.x), glm::vec2(0.0f, 1.0f), x));
|
||||
vertices.push_back(Map(glm::vec2(0, subdivisions.y), glm::vec2(0.0f, 1.0f), y));
|
||||
|
||||
if (y > 0 && x > 0)
|
||||
{
|
||||
indices.push_back(y * subdivisions.x + x);
|
||||
indices.push_back(y * subdivisions.x + (x - 1));
|
||||
indices.push_back((y - 1) * subdivisions.x + (x - 1));
|
||||
|
||||
indices.push_back(y * subdivisions.x + x);
|
||||
indices.push_back((y - 1)* subdivisions.x + (x - 1));
|
||||
indices.push_back((y - 1)* subdivisions.x + x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<lol::VertexBuffer> vertexBuffer = std::make_shared<lol::VertexBuffer>(vertices.size(), vertices);
|
||||
vertexBuffer->SetLayout(
|
||||
{
|
||||
{ lol::Type::Float, 2, false },
|
||||
{ lol::Type::Float, 2, false }
|
||||
}
|
||||
);
|
||||
|
||||
std::shared_ptr<lol::ElementBuffer> elementBuffer = std::make_shared<lol::ElementBuffer>(indices.size(), indices);
|
||||
|
||||
vao->SetVertexBuffer(vertexBuffer);
|
||||
vao->SetElementBuffer(elementBuffer);
|
||||
|
||||
// Set up shader
|
||||
shader = lol::ShaderManager::GetInstance().Get(TOPOLOGY_ID);
|
||||
if (shader == nullptr)
|
||||
{
|
||||
shader = std::make_shared<lol::Shader>(
|
||||
R"(
|
||||
#version 460 core
|
||||
|
||||
layout (location = 0) in vec2 position;
|
||||
layout (location = 1) in vec2 texCoord;
|
||||
|
||||
out float height;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
uniform sampler2DShadow heightmap;
|
||||
|
||||
void main()
|
||||
{
|
||||
height = texture(heightmap, vec3(texCoord, 0.0f));
|
||||
gl_Position = projection * view * vec4(position.x, 2.0f * height, position.y, 1.0f);
|
||||
}
|
||||
)",
|
||||
R"(
|
||||
#version 460 core
|
||||
|
||||
in float height;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f - height, 0.0f, height, 1.0f);
|
||||
}
|
||||
)"
|
||||
);
|
||||
|
||||
lol::ShaderManager::GetInstance().Register(TOPOLOGY_ID, shader);
|
||||
}
|
||||
|
||||
// Generate image
|
||||
image = lol::Image(subdivisions.x, subdivisions.y, lol::PixelFormat::DepthComponent, lol::PixelType::Float);
|
||||
}
|
||||
|
||||
Topology::~Topology()
|
||||
{
|
||||
if (texture != nullptr)
|
||||
delete texture;
|
||||
}
|
||||
|
||||
void Topology::PreRender(const lol::CameraBase& camera) const
|
||||
{
|
||||
if(texture != nullptr)
|
||||
texture->Bind();
|
||||
|
||||
shader->SetUniform("view", camera.GetView());
|
||||
shader->SetUniform("projection", camera.GetProjection());
|
||||
}
|
||||
|
||||
void Topology::MakeTexture()
|
||||
{
|
||||
if (texture != nullptr)
|
||||
delete texture;
|
||||
|
||||
texture = new lol::Texture(image, lol::TextureFormat::DepthComponent);
|
||||
}
|
25
src/Topology.hpp
Normal file
25
src/Topology.hpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <lol/lol.hpp>
|
||||
|
||||
inline float Map(const glm::vec2& from, const glm::vec2& to, float val)
|
||||
{
|
||||
return (val - from.x) * (to.y - to.x) / (from.y - from.x) + to.x;
|
||||
}
|
||||
|
||||
class Topology : public lol::Drawable
|
||||
{
|
||||
public:
|
||||
Topology(const glm::vec2& size, const glm::uvec2& subdivision);
|
||||
~Topology();
|
||||
|
||||
void PreRender(const lol::CameraBase& camera) const override;
|
||||
|
||||
inline float* GetTopology() const { return (float*)image.GetPixels(); };
|
||||
inline const glm::uvec2& GetSize() const { return image.GetDimensions(); };
|
||||
void MakeTexture();
|
||||
|
||||
private:
|
||||
lol::Image image;
|
||||
lol::Texture* texture;
|
||||
};
|
|
@ -1,5 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
#define SHAPE_ID 0x0
|
||||
#define CUBE_ID 0x1
|
||||
#define PYRAMID_ID 0x2
|
||||
#define TOPOLOGY_ID 0x1
|
Binary file not shown.
Before Width: | Height: | Size: 52 KiB |
Binary file not shown.
Before Width: | Height: | Size: 45 KiB |
Loading…
Reference in a new issue