1
0
mirror of synced 2024-11-24 07:40:17 +01:00

patterns: Fixed various issues with the 3d visualizer

This commit is contained in:
WerWolv 2023-01-23 16:25:23 +01:00
parent a51e4afb05
commit 915106f360
5 changed files with 56 additions and 31 deletions

@ -1 +1 @@
Subproject commit 69a3d4c05eb6bb1efe7fd926fc99f2982e22a59a
Subproject commit 1fe5aa0db6277b852e60d3b622886550d901eab8

View File

@ -154,6 +154,7 @@ namespace hex::gl {
glEnableVertexAttribArray(index);
buffer.bind();
glVertexAttribPointer(index, 3, getType<T>(), GL_FALSE, 3 * sizeof(T), nullptr);
buffer.unbind();
}
void bind() const;

View File

@ -98,6 +98,7 @@ namespace hex::gl {
glGenBuffers(1, &this->m_buffer);
glBindBuffer(this->m_type, this->m_buffer);
glBufferData(this->m_type, data.size_bytes(), data.data(), GL_STATIC_DRAW);
glBindBuffer(this->m_type, 0);
}
template<typename T>
@ -110,7 +111,7 @@ namespace hex::gl {
this->m_buffer = other.m_buffer;
this->m_size = other.m_size;
this->m_type = other.m_type;
other.m_buffer = 0;
other.m_buffer = -1;
}
template<typename T>
@ -118,7 +119,7 @@ namespace hex::gl {
this->m_buffer = other.m_buffer;
this->m_size = other.m_size;
this->m_type = other.m_type;
other.m_buffer = 0;
other.m_buffer = -1;
return *this;
}
@ -163,12 +164,12 @@ namespace hex::gl {
VertexArray::VertexArray(VertexArray &&other) noexcept {
this->m_array = other.m_array;
other.m_array = 0;
other.m_array = -1;
}
VertexArray& VertexArray::operator=(VertexArray &&other) noexcept {
this->m_array = other.m_array;
other.m_array = 0;
other.m_array = -1;
return *this;
}
@ -200,12 +201,12 @@ namespace hex::gl {
Texture::Texture(Texture &&other) noexcept {
this->m_texture = other.m_texture;
other.m_texture = 0;
other.m_texture = -1;
}
Texture& Texture::operator=(Texture &&other) noexcept {
this->m_texture = other.m_texture;
other.m_texture = 0;
other.m_texture = -1;
return *this;
}
@ -231,7 +232,7 @@ namespace hex::gl {
GLuint Texture::release() {
auto copy = this->m_texture;
this->m_texture = 0;
this->m_texture = -1;
return copy;
}
@ -257,16 +258,16 @@ namespace hex::gl {
FrameBuffer::FrameBuffer(FrameBuffer &&other) noexcept {
this->m_frameBuffer = other.m_frameBuffer;
other.m_frameBuffer = 0;
other.m_frameBuffer = -1;
this->m_renderBuffer = other.m_renderBuffer;
other.m_renderBuffer = 0;
other.m_renderBuffer = -1;
}
FrameBuffer& FrameBuffer::operator=(FrameBuffer &&other) noexcept {
this->m_frameBuffer = other.m_frameBuffer;
other.m_frameBuffer = 0;
other.m_frameBuffer = -1;
this->m_renderBuffer = other.m_renderBuffer;
other.m_renderBuffer = 0;
other.m_renderBuffer = -1;
return *this;
}

View File

@ -5,7 +5,7 @@ layout (location = 1) in vec3 in_Normal;
/*uniform float time;*/
uniform float scale;
uniform vec3 rotation;
uniform vec3 translation;
out vec3 normal;
@ -32,5 +32,5 @@ mat4 viewMatrix(vec3 rotation) {
void main() {
mat4 view = viewMatrix(rotation);
normal = (vec4(in_Normal, 1.0) * view).xyz;
gl_Position = vec4(in_Position * scale, 1.0) * view;
gl_Position = vec4((in_Position + translation) * scale, 1.0) * view;
}

View File

@ -9,8 +9,10 @@
#include <implot.h>
#include <imgui_impl_opengl3_loader.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <hex/helpers/logger.hpp>
#include <pl/patterns/pattern.hpp>
#include <pl/patterns/pattern_padding.hpp>
#include <romfs/romfs.hpp>
@ -125,14 +127,17 @@ namespace hex::plugin::builtin {
static auto readValues(pl::ptrn::Pattern *pattern){
std::vector<T> result;
if (pattern->getSize() == 0)
if (dynamic_cast<pl::ptrn::PatternPadding*>(pattern) && pattern->getSize() == 0)
return result;
if (auto iteratable = dynamic_cast<pl::ptrn::Iteratable*>(pattern); iteratable != nullptr) {
result.reserve(iteratable->getEntryCount());
iteratable->forEachEntry(0, iteratable->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) {
for (auto [offset, child] : entry->getChildren()) {
auto startOffset = child->getOffset();
child->setOffset(offset);
ON_SCOPE_EXIT { child->setOffset(startOffset); };
T value;
if (std::floating_point<T>)
@ -144,7 +149,7 @@ namespace hex::plugin::builtin {
}
});
} else {
result.reserve(pattern->getSize() / sizeof(float));
result.resize(pattern->getSize() / sizeof(float));
pattern->getEvaluator()->readData(pattern->getOffset(), result.data(), result.size() * sizeof(float), pattern->getSection());
}
@ -158,6 +163,7 @@ namespace hex::plugin::builtin {
static ImGui::Texture texture;
static float scaling = 0.5F;
static gl::Vector<float, 3> rotation = { { 1.0F, -1.0F, 0.0F } };
static gl::Vector<float, 3> translation;
static std::vector<float> vertices, normals;
static std::vector<u32> indices;
static gl::Shader shader;
@ -171,6 +177,11 @@ namespace hex::plugin::builtin {
rotation[1] += -dragDelta.x * 0.0075F;
ImGui::ResetMouseDragDelta(ImGuiMouseButton_Middle);
dragDelta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
translation[0] += -dragDelta.x * 0.1F;
translation[1] += -dragDelta.y * 0.1F;
ImGui::ResetMouseDragDelta(ImGuiMouseButton_Right);
auto scrollDelta = ImGui::GetIO().MouseWheel;
scaling += scrollDelta * 0.01F;
@ -184,31 +195,36 @@ namespace hex::plugin::builtin {
normals.clear();
normals.resize(vertices.size());
for (u32 i = 0; i < normals.size() && normals.size() >= 9; i += 9) {
auto v1 = gl::Vector<float, 3>({ vertices[i + 0], vertices[i + 1], vertices[i + 2] });
auto v2 = gl::Vector<float, 3>({ vertices[i + 3], vertices[i + 4], vertices[i + 5] });
auto v3 = gl::Vector<float, 3>({ vertices[i + 6], vertices[i + 7], vertices[i + 8] });
for (u32 i = 9; i < normals.size(); i += 9) {
auto v1 = gl::Vector<float, 3>({ vertices[i - 9], vertices[i - 8], vertices[i - 7] });
auto v2 = gl::Vector<float, 3>({ vertices[i - 6], vertices[i - 5], vertices[i - 4] });
auto v3 = gl::Vector<float, 3>({ vertices[i - 3], vertices[i - 2], vertices[i - 1] });
auto normal = ((v2 - v1).cross(v3 - v1)).normalize();
normals[i + 0] = normal[0];
normals[i + 1] = normal[1];
normals[i + 2] = normal[2];
normals[i + 3] = normal[0];
normals[i + 4] = normal[1];
normals[i + 5] = normal[2];
normals[i + 6] = normal[0];
normals[i + 7] = normal[1];
normals[i + 8] = normal[2];
normals[i - 9] = normal[0];
normals[i - 8] = normal[1];
normals[i - 7] = normal[2];
normals[i - 6] = normal[0];
normals[i - 5] = normal[1];
normals[i - 4] = normal[2];
normals[i - 3] = normal[0];
normals[i - 2] = normal[1];
normals[i - 1] = normal[2];
}
shader = gl::Shader(romfs::get("shaders/default/vertex.glsl").string(), romfs::get("shaders/default/fragment.glsl").string());
vertexArray = gl::VertexArray();
vertexBuffer = {};
normalBuffer = {};
indexBuffer = {};
vertexArray.bind();
vertexBuffer = gl::Buffer<float> (gl::BufferType::Vertex, vertices);
vertexBuffer = gl::Buffer<float>(gl::BufferType::Vertex, vertices);
normalBuffer = gl::Buffer<float>(gl::BufferType::Vertex, normals);
indexBuffer = gl::Buffer<u32>(gl::BufferType::Index, indices);
indexBuffer = gl::Buffer<u32>(gl::BufferType::Index, indices);
vertexArray.addBuffer(0, vertexBuffer);
vertexArray.addBuffer(1, normalBuffer);
@ -217,6 +233,8 @@ namespace hex::plugin::builtin {
vertexArray.addBuffer(2, indexBuffer);
vertexBuffer.unbind();
normalBuffer.unbind();
indexBuffer.unbind();
vertexArray.unbind();
}
@ -229,16 +247,21 @@ namespace hex::plugin::builtin {
frameBuffer.bind();
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_CLAMP);
shader.bind();
shader.setUniform("scale", scaling);
shader.setUniform("rotation", rotation);
shader.setUniform("translation", translation);
vertexArray.bind();
glViewport(0, 0, renderTexture.getWidth(), renderTexture.getHeight());
glClearColor(0.00F, 0.00F, 0.00F, 0.00f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0F, 1.0F, -1.0F, 1.0F, 0.0000001F, 10000000.0F);
if (indices.empty())
vertexBuffer.draw();