Can now generate and render meshes of sch_hull's!

master
Martin Felis 2021-11-05 14:04:05 +01:00
parent 890ea713db
commit 4448059ce8
1 changed files with 263 additions and 69 deletions

View File

@ -11,20 +11,25 @@
#include <cstdio>
#include <iostream>
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "imgui.h"
#include "ImGuizmo.h"
// clang-format off
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "ImGuizmo.h"
#include "imgui_internal.h"
#include "imgui.h"
// clang-format on
#include "render_utils.h"
#include "simulator.h"
#include "srender.h"
#include "utils.h"
#define SCONVCOL_IMPLEMENTATION
#include "sconvcol.h"
GLFWwindow* gWindow = nullptr;
GuiInputState* gGuiInputState = nullptr;
CameraState* gCameraState = nullptr;
@ -87,38 +92,74 @@ static void opengl_error_callback(
const GLchar* message,
const void* userParam) {
const char* source_str;
switch (source)
{
case GL_DEBUG_SOURCE_API: source_str = "API"; break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: source_str = "Window System"; break;
case GL_DEBUG_SOURCE_SHADER_COMPILER: source_str = "Shader Compiler"; break;
case GL_DEBUG_SOURCE_THIRD_PARTY: source_str = "3rd Party"; break;
case GL_DEBUG_SOURCE_APPLICATION: source_str = "Application"; break;
default: source_str = "Other"; break;
switch (source) {
case GL_DEBUG_SOURCE_API:
source_str = "API";
break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
source_str = "Window System";
break;
case GL_DEBUG_SOURCE_SHADER_COMPILER:
source_str = "Shader Compiler";
break;
case GL_DEBUG_SOURCE_THIRD_PARTY:
source_str = "3rd Party";
break;
case GL_DEBUG_SOURCE_APPLICATION:
source_str = "Application";
break;
default:
source_str = "Other";
break;
}
const char* type_str;
switch (type)
{
case GL_DEBUG_TYPE_ERROR: type_str = "Error"; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: type_str = "Deprecated Behaviour"; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: type_str = "Undefined Behaviour"; break;
case GL_DEBUG_TYPE_PORTABILITY: type_str = "Portability"; break;
case GL_DEBUG_TYPE_PERFORMANCE: type_str = "Performance"; break;
case GL_DEBUG_TYPE_MARKER: type_str = "Marker"; break;
case GL_DEBUG_TYPE_PUSH_GROUP: type_str = "Push Group"; break;
case GL_DEBUG_TYPE_POP_GROUP: type_str = "Pop Group"; break;
default: type_str = "Other"; break;
switch (type) {
case GL_DEBUG_TYPE_ERROR:
type_str = "Error";
break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
type_str = "Deprecated Behaviour";
break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
type_str = "Undefined Behaviour";
break;
case GL_DEBUG_TYPE_PORTABILITY:
type_str = "Portability";
break;
case GL_DEBUG_TYPE_PERFORMANCE:
type_str = "Performance";
break;
case GL_DEBUG_TYPE_MARKER:
type_str = "Marker";
break;
case GL_DEBUG_TYPE_PUSH_GROUP:
type_str = "Push Group";
break;
case GL_DEBUG_TYPE_POP_GROUP:
type_str = "Pop Group";
break;
default:
type_str = "Other";
break;
}
const char* severity_str;
switch (severity)
{
case GL_DEBUG_SEVERITY_HIGH: severity_str = "high"; break;
case GL_DEBUG_SEVERITY_MEDIUM: severity_str = "medium"; break;
case GL_DEBUG_SEVERITY_LOW: severity_str = "low"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION: severity_str = "notification"; break;
default: severity_str = "unknown";
switch (severity) {
case GL_DEBUG_SEVERITY_HIGH:
severity_str = "high";
break;
case GL_DEBUG_SEVERITY_MEDIUM:
severity_str = "medium";
break;
case GL_DEBUG_SEVERITY_LOW:
severity_str = "low";
break;
case GL_DEBUG_SEVERITY_NOTIFICATION:
severity_str = "notification";
break;
default:
severity_str = "unknown";
}
gLog(
@ -357,7 +398,8 @@ void ShowTransformManip(
case ImGuizmo::SCALE:
ImGui::InputFloat("Scale Snap", &snap[0]);
break;
default: break;
default:
break;
}
ImGui::Checkbox("Bound Sizing", &boundSizing);
if (boundSizing) {
@ -410,7 +452,170 @@ void ShowTransformManip(
}
}
void DoRender() {
void SimulatorSceneRender(srcmdbuf* render_commands) {
// Populate render commands
srcmdbuf_clear(render_commands);
srcmd rcmd;
srcmd_clear(&rcmd);
rcmd.type = SRndrCmdTypeFrame;
srcmdbuf_add(render_commands, &rcmd);
simulator_draw(render_commands);
if (gDialogs->mSimulator) {
ImGui::Begin("Simulator", &gDialogs->mSimulator);
simulator_gui();
ImGui::End();
}
}
sch_hull box_hull;
srmeshdata* box_hull_mesh;
void SconvHUllSceneInit() {
sch_create_unitbox(&box_hull);
// create mesh for hull
static srvrtxdata mesh_buffer_data[1024];
static GLuint index_buffer_data[1024];
int vertex_index = -1;
int index_count = -1;
int triangle_count = 0;
sch_hull* hull = &box_hull;
int face_vert0_idx = 0;
for (int fi = 0; fi < hull->num_faces; fi++) {
sch_face* face = &hull->faces[fi];
sch_edge* edge0 = face->edge;
sch_vert* face_vert0 = edge0->vert;
sch_edge* cur_edge = edge0->next;
sch_vert* cur_vert = nullptr;
sch_edge* next_edge = nullptr;
sch_vert* next_vert = nullptr;
int tri_vert_0_idx = 0;
int tri_vert_1_idx = 0;
int tri_vert_2_idx = 0;
sch_plane plane;
sch_hull_calc_plane(hull, fi, &plane);
// add face_vert0 (later used as tri_vert_0)
vertex_index++;
face_vert0_idx = vertex_index;
tri_vert_0_idx = face_vert0_idx;
srvrtxdata* mesh_vert = &mesh_buffer_data[tri_vert_0_idx];
mesh_vert->x = simd4f_get_x(face_vert0->p);
mesh_vert->y = simd4f_get_y(face_vert0->p);
mesh_vert->z = simd4f_get_z(face_vert0->p);
mesh_vert->w = 1.f;
mesh_vert->nx = simd4f_get_x(plane.n);
mesh_vert->ny = simd4f_get_y(plane.n);
mesh_vert->nz = simd4f_get_z(plane.n);
mesh_vert->color[0] = fabs(mesh_vert->nx) * 255;
mesh_vert->color[1] = fabs(mesh_vert->ny) * 255;
mesh_vert->color[2] = fabs(mesh_vert->nz) * 255;
mesh_vert->color[3] = 255;
// add tri_vert_1
cur_vert = cur_edge->vert;
tri_vert_1_idx = ++vertex_index;
mesh_vert = &mesh_buffer_data[tri_vert_1_idx];
mesh_vert->x = simd4f_get_x(cur_vert->p);
mesh_vert->y = simd4f_get_y(cur_vert->p);
mesh_vert->z = simd4f_get_z(cur_vert->p);
mesh_vert->w = 1.f;
mesh_vert->nx = simd4f_get_x(plane.n);
mesh_vert->ny = simd4f_get_y(plane.n);
mesh_vert->nz = simd4f_get_z(plane.n);
mesh_vert->color[0] = fabs(mesh_vert->nx) * 255;
mesh_vert->color[1] = fabs(mesh_vert->ny) * 255;
mesh_vert->color[2] = fabs(mesh_vert->nz) * 255;
mesh_vert->color[3] = 255;
do {
// add face_vert0
index_buffer_data[++index_count] = tri_vert_0_idx;
// add face_vert1
index_buffer_data[++index_count] = tri_vert_1_idx;
next_edge = cur_edge->next;
next_vert = next_edge->vert;
tri_vert_2_idx = ++vertex_index;
mesh_vert = &mesh_buffer_data[tri_vert_2_idx];
mesh_vert->x = simd4f_get_x(next_vert->p);
mesh_vert->y = simd4f_get_y(next_vert->p);
mesh_vert->z = simd4f_get_z(next_vert->p);
mesh_vert->w = 1.f;
mesh_vert->nx = simd4f_get_x(plane.n);
mesh_vert->ny = simd4f_get_y(plane.n);
mesh_vert->nz = simd4f_get_z(plane.n);
mesh_vert->color[0] = fabs(mesh_vert->nx) * 255;
mesh_vert->color[1] = fabs(mesh_vert->ny) * 255;
mesh_vert->color[2] = fabs(mesh_vert->nz) * 255;
mesh_vert->color[3] = 255;
index_buffer_data[++index_count] = tri_vert_2_idx;
triangle_count++;
cur_edge = next_edge;
tri_vert_1_idx = tri_vert_2_idx;
} while (next_edge->next != edge0);
}
box_hull_mesh = srmesh_create(
mesh_buffer_data,
vertex_index + 1,
index_buffer_data,
index_count + 1,
GL_TRIANGLES,
index_count + 1);
}
void SconvHUllSceneCleanup() { srmesh_destroy(box_hull_mesh); }
void SconvHUllSceneUpdate() {}
void SconvHullSceneRender(srcmdbuf* render_commands) {
// Populate render commands
srcmdbuf_clear(render_commands);
srcmd rcmd;
srcmd_clear(&rcmd);
rcmd.type = SRndrCmdTypeFrame;
srcmdbuf_add(render_commands, &rcmd);
rcmd.type = SRndrCmdTypeGrid;
float origin[3] = {0.f, 0.f, 0.f};
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
simd4x4f_translation(
&rcmd.mat,
origin[0] + i * 10.0f,
origin[1],
origin[2] + j * 10.f);
srcmdbuf_add(render_commands, &rcmd);
}
}
// Render hull
srcmd cmd;
cmd.type = SrndrCmdTypeMesh;
cmd.color = simd4f_create(1.f, 1.f, 1.f, 1.f);
simd4x4f_identity(&cmd.mat);
cmd.meshdata = box_hull_mesh;
srcmdbuf_add(render_commands, &cmd);
}
void ImSRenderWidget(srndr* renderer, srview* view) {
ZoneScoped;
const ImGuiStyle& imgui_style = ImGui::GetStyle();
@ -420,7 +625,7 @@ void DoRender() {
// Update the gCameraState->mtxView
const ImVec2 content_avail = ImGui::GetContentRegionAvail();
srview_set_size(gView, int(content_avail.x), int(content_avail.y));
srview_set_size(view, int(content_avail.x), int(content_avail.y));
float view_width = content_avail.x / 50.f;
float view_height = content_avail.y / 50.f;
@ -439,34 +644,12 @@ void DoRender() {
view_width / view_height,
0.1f,
50.f);
srview_set_view(gView, gCameraState->mtxView);
srview_set_proj(gView, gProjMatrix);
// Populate render commands
srcmdbuf_clear(gRndrCmds);
srcmd rcmd;
srcmd_clear(&rcmd);
rcmd.type = SRndrCmdTypeFrame;
srcmdbuf_add(gRndrCmds, &rcmd);
if (gViewMode == ViewModeSimulation) {
simulator_draw(gRndrCmds);
if (gDialogs->mSimulator) {
ImGui::Begin("Simulator", &gDialogs->mSimulator);
simulator_gui();
ImGui::End();
}
} else {
RenderGuizmoTest(gRndrCmds);
}
srview_set_view(view, gCameraState->mtxView);
srview_set_proj(view, gProjMatrix);
// Perform the actual render
srndr_render(gRndr, gView, gRndrCmds);
GLuint view_texture;
srview_get_output_texture(gView, &view_texture);
srview_get_output_texture(view, &view_texture);
ImGui::Image(
(void*)view_texture,
@ -483,7 +666,7 @@ void DoRender() {
(float*)(&gTransformMatrix));
ImGui::End();
}
};
int main() {
gTimerInit();
@ -574,7 +757,12 @@ int main() {
gRndr = srndr_create();
gView = srview_create();
gRndrCmds = srcmdbuf_create(1024);
//
// Scene init
//
simulator_init();
SconvHUllSceneInit();
simd4x4f_translation(&gCameraState->mtxView, 0.1f, 0.1f, -0.5f);
simd4f eye = simd4f_create(0.f, 0.f, 10.f, 1.f);
@ -607,6 +795,7 @@ int main() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ShowDockspace(true);
ImGuizmo::BeginFrame();
ImGuizmo::Enable(gGizmoEnabled);
}
@ -710,19 +899,22 @@ int main() {
if (changed) {
CameraState_CalcToMatrix(gCameraState, &gCameraState->mtxView);
}
ImGui::End();
}
if (gDialogs->mImGuiDemo) {
ImGui::ShowDemoWindow(&gDialogs->mImGuiDemo);
}
ShowDockspace(true);
ImGui::End();
}
DoRender();
SconvHullSceneRender(gRndrCmds);
// SimulatorSceneRender(gRndrCmds);
srndr_render(gRndr, gView, gRndrCmds);
ImSRenderWidget(gRndr, gView);
}
simulator_update(gTimer->mDeltaTime);
SconvHUllSceneUpdate();
{
ZoneNamedN(render_submit, "RenderSubmit", true);
@ -772,6 +964,8 @@ int main() {
gLog("Exiting application");
SconvHUllSceneCleanup();
srview_destroy(gView);
srndr_destroy(gRndr);