diff --git a/src/MainRenderView.cpp b/src/MainRenderView.cpp new file mode 100644 index 0000000..7fcf727 --- /dev/null +++ b/src/MainRenderView.cpp @@ -0,0 +1,226 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2015-2023 OSRE ( Open Source Render Engine ) by Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ +#include "MainRenderView.h" +#include +#include +#include +#include +#include + +namespace AssimpViewer { + +namespace Colors { + static const glm::vec3 Black(0, 0, 0); + static const glm::vec3 White(1, 1, 1); + static const glm::vec3 Grey(0.5, 0.5, 0.5); + static const glm::vec3 Red(1, 0, 0); + static const glm::vec3 Green(0, 1, 0); + static const glm::vec3 Blue(0, 0, 1); +} // namespace Colors + +using namespace OSRE; +using namespace OSRE::RenderBackend; +using namespace OSRE::App; +using namespace OSRE::Animation; + +MainRenderView::MainRenderView() : + mEditorElements() { + // empty +} + +MainRenderView::~MainRenderView() { + mEditorElements.clear(); +} + +Mesh *MainRenderView::createCoordAxis(uint32_t size) { + Mesh *axis = new Mesh("axis", VertexType::ColorVertex, IndexType::UnsignedShort); + ColorVert v[6]; + v[0].position.x = v[0].position.y = v[0].position.z = 0; + v[0].color0 = Colors::Red; + + v[1].position.x = static_cast(size); + v[1].position.y = v[1].position.z = 0; + v[1].color0 = Colors::Red; + + v[2].position.x = v[2].position.y = v[2].position.z = 0; + v[2].color0 = Colors::Green; + + v[3].position.y = static_cast(size); + v[3].position.x = v[3].position.z = 0; + v[3].color0 = Colors::Green; + + v[4].position.x = v[4].position.y = v[4].position.z = 0; + v[4].color0 = Colors::Blue; + + v[5].position.z = static_cast(size); + v[5].position.x = v[5].position.y = 0; + v[5].color0 = Colors::Blue; + + cppcore::TArray axisData; + axisData.add(v, 6); + + axis->attachVertices(&axisData[0], sizeof(ColorVert) * axisData.size()); + + cppcore::TArray axisIndices; + axisIndices.add(0); + axisIndices.add(1); + + axisIndices.add(2); + axisIndices.add(3); + + axisIndices.add(4); + axisIndices.add(5); + + axis->attachIndices(&axisIndices[0], sizeof(uint16_t) * axisIndices.size()); + axis->addPrimitiveGroup(axisData.size(), PrimitiveType::LineList, 0); + axis->setMaterial(MaterialBuilder::createBuildinMaterial(VertexType::ColorVertex)); + + return axis; +} + +Mesh *MainRenderView::createGrid(ui32 numLines) { + if (0 == numLines) { + return nullptr; + } + + Mesh *grid = new Mesh("grid", VertexType::ColorVertex, IndexType::UnsignedShort); + f32 currentX = -300.0f, currentY = -300.0f; + f32 diffX = 600.0f / numLines; + f32 diffY = 600.0f / numLines; + cppcore::TArray lineData; + cppcore::TArray lineIndices; + ui16 currentIndex = 0; + for (ui32 x = 0; x < numLines + 1; ++x) { + ColorVert v1, v2; + v1.position.x = v2.position.x = currentX; + currentX += diffX; + + v1.position.y = -300; + v2.position.y = 300; + + v1.position.z = v2.position.z = 0.0f; + v1.color0 = v2.color0 = Colors::Grey; + + lineData.add(v1); + lineData.add(v2); + lineIndices.add(currentIndex); + ++currentIndex; + lineIndices.add(currentIndex); + ++currentIndex; + } + for (ui32 y = 0; y < numLines + 1; ++y) { + ColorVert v1, v2; + v1.position.x = -300; + v2.position.x = 300; + v1.position.y = v2.position.y = currentY; + currentY += diffY; + v1.position.z = v2.position.z = 0.0f; + v1.color0 = v2.color0 = Colors::Grey; + lineData.add(v1); + lineData.add(v2); + lineIndices.add(currentIndex); + ++currentIndex; + lineIndices.add(currentIndex); + ++currentIndex; + } + grid->attachVertices(&lineData[0], sizeof(ColorVert) * lineData.size()); + grid->attachIndices(&lineIndices[0], sizeof(ui16) * lineIndices.size()); + grid->addPrimitiveGroup(lineData.size(), PrimitiveType::LineList, 0); + grid->setMaterial(MaterialBuilder::createBuildinMaterial(VertexType::ColorVertex)); + + return grid; +} + +void MainRenderView::createRect2D(const Rect2ui &r, Mesh *mesh2D, Style &style) { + if (nullptr == mesh2D) { + return; + } + + glm::vec2 p0(r.x1, r.y1), p1(r.getX1(), r.getY2()), p2(r.getX2(), r.getY2()), p3(r.getX2(), r.getY2()); + UIVert edges[4] = {}; + edges[0].position = p0; + edges[1].position = p1; + edges[2].position = p2; + edges[3].position = p3; + edges[0].color0 = style.BG.toVec4(); + edges[1].color0 = style.BG.toVec4(); + edges[2].color0 = style.BG.toVec4(); + edges[3].color0 = style.BG.toVec4(); + + constexpr size_t NumIndices = 6; + cppcore::TArray indices; + indices.resize(NumIndices); + indices[0] = 0; + indices[1] = 2; + indices[2] = 1; + + indices[3] = 1; + indices[4] = 2; + indices[5] = 3; + + mesh2D->attachVertices(&edges[0], sizeof(glm::vec2) * 4); + mesh2D->attachIndices(&indices[0], sizeof(ui16) * NumIndices); + mesh2D->addPrimitiveGroup(6, PrimitiveType::TriangleList, NumIndices); +} + +static void drawJoint(Bone *currentBone) { + if (currentBone == nullptr) { + return; + } + +} + +static void drawBone(Bone *currentBone) { + if (currentBone == nullptr) { + return; + } + + if (currentBone->mParent != -1) { + drawJoint(currentBone); + } +} + +void MainRenderView::createEditorElements(RenderComponent *rc) { + if (rc == nullptr) { + return; + } + + Mesh *grid = MainRenderView::createGrid(60); + rc->addStaticMesh(grid); + mEditorElements.add(grid); + Mesh *axis = MainRenderView::createCoordAxis(100); + rc->addStaticMesh(axis); + mEditorElements.add(axis); +} + +void MainRenderView::render( RenderBackendService *rbSrv, glm::mat4 model ) { + rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId)); + rbSrv->beginRenderBatch("b1"); + + rbSrv->setMatrix(MatrixType::Model, model); + + rbSrv->endRenderBatch(); + rbSrv->endPass(); +} + +} // namespace Editor diff --git a/src/MainRenderView.h b/src/MainRenderView.h new file mode 100644 index 0000000..5c673f0 --- /dev/null +++ b/src/MainRenderView.h @@ -0,0 +1,73 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2015-2023 OSRE ( Open Source Render Engine ) by Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ +#pragma once + +//#include "Gui/UIElements.h" + +#include +#include +#include + +namespace OSRE { + +namespace RenderBackend { +class Mesh; +} + +namespace App { +class RenderComponent; +} + +namespace Animation { +struct Skeleton; +} +} // namespace OSRE + +namespace AssimpViewer { + +struct Style { + OSRE::Color4 FG; + OSRE::Color4 BG; + uint32_t DefaultFontSize; +}; + +//------------------------------------------------------------------------------------------------- +/// @ingroup Editor +/// +/// @brief +//------------------------------------------------------------------------------------------------- +class MainRenderView { +public: + MainRenderView(); + ~MainRenderView(); + static OSRE::RenderBackend::Mesh *createCoordAxis(uint32_t size); + static OSRE::RenderBackend::Mesh *createGrid(uint32_t numLines); + static void createRect2D(const OSRE::Rect2ui &r, OSRE::RenderBackend::Mesh *mesh2D, Style &style); + void createEditorElements(OSRE::App::RenderComponent *rc); + void render(OSRE::RenderBackend::RenderBackendService *rbSrb, glm::mat4 model); + +private: + OSRE::RenderBackend::MeshArray mEditorElements; +}; + +} // namespace AssimpViewer diff --git a/src/main.cpp b/src/main.cpp index 3963751..e6d3d86 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -156,7 +156,7 @@ struct Logger { Logger Logger::sInstance; -errcode_t initSDL(SDLContext &ctx) { +errcode_t initSDL(SDLContext &ctx, uint32_t x, uint32_t y, uint32_t w, uint32_t h) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { printf("Error: %s\n", SDL_GetError()); return -1; @@ -193,7 +193,7 @@ errcode_t initSDL(SDLContext &ctx) { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); - ctx.window = SDL_CreateWindow("Assimp Viewer", 20, 20, 1280, 1024, window_flags); + ctx.window = SDL_CreateWindow("Assimp Viewer", x, y, w, h, window_flags); ctx.gl_context = SDL_GL_CreateContext(ctx.window); SDL_GL_MakeCurrent(ctx.window, ctx.gl_context); @@ -221,7 +221,7 @@ int main(int argc, char *argv[]) { // Setup SDL SDLContext ctx; - if (initSDL(ctx) == -1) { + if (initSDL(ctx, 20, 20, 1280, 1024) == -1) { Logger::getInstance().logError("Cannot initialize SDL."); return -1; }