Skip to content

Commit

Permalink
Merge pull request #4 from assimp/kimkulling/add_missing_files_issue-3
Browse files Browse the repository at this point in the history
Add missing files
  • Loading branch information
kimkulling authored Aug 31, 2023
2 parents 0db5ea3 + badf70b commit 5c4516d
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 3 deletions.
226 changes: 226 additions & 0 deletions src/MainRenderView.cpp
Original file line number Diff line number Diff line change
@@ -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 <osre/Common/osre_common.h>
#include <osre/Animation/AnimatorBase.h>
#include <osre/RenderBackend/MaterialBuilder.h>
#include <osre/RenderBackend/Mesh.h>
#include <osre/RenderBackend/RenderPass.h>

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<f32>(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<f32>(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<f32>(size);
v[5].position.x = v[5].position.y = 0;
v[5].color0 = Colors::Blue;

cppcore::TArray<RenderBackend::ColorVert> axisData;
axisData.add(v, 6);

axis->attachVertices(&axisData[0], sizeof(ColorVert) * axisData.size());

cppcore::TArray<ui16> 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<RenderBackend::ColorVert> lineData;
cppcore::TArray<ui16> 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<ui16> 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
73 changes: 73 additions & 0 deletions src/MainRenderView.h
Original file line number Diff line number Diff line change
@@ -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 <osre/App/Entity.h>
#include <osre/RenderBackend/RenderBackendService.h>
#include <osre/RenderBackend/Mesh.h>

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
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5c4516d

Please sign in to comment.