Skip to content

Commit

Permalink
Merge pull request #196 from kimkulling/kimkulling/prepare_terrain_re…
Browse files Browse the repository at this point in the history
…ndering

Kimkulling/prepare terrain rendering
  • Loading branch information
kimkulling authored Oct 4, 2023
2 parents d825726 + b06b2af commit 5cb17c8
Show file tree
Hide file tree
Showing 22 changed files with 360 additions and 935 deletions.
Binary file added assets/HeightMaps/world_heightmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion contrib/assimp
Submodule assimp updated 996 files
18 changes: 9 additions & 9 deletions include/osre/App/Stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ class OSRE_EXPORT Stage : public Common::Object {
public:
using WorldArray = cppcore::TArray<World*>;

/// @brief
/// @param stageName
/// @brief The class constructor.
/// @param stageName The stage name.
Stage(const String &stageName);
/// @brief

/// @brief The class destructor.
~Stage() override;
/// @brief
/// @param name
/// @return

/// @brief Will create a new world instance within the stage.
/// @param[in] name The name of the stage.
/// @return The new created stage.
World *createWorld(const String &name);

/// @brief
/// @param name
/// @return
Expand Down
4 changes: 2 additions & 2 deletions include/osre/IO/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace IO {

class OSRE_EXPORT File {
public:
File();
~File();
File() = default;
~File() = default;
static bool exists(const String &filename);
};

Expand Down
9 changes: 3 additions & 6 deletions include/osre/RenderBackend/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ class TextureResource;
struct Texture;
struct UniformVar;


/// @brief This enum describes the kind of build-in material.
enum class MaterialType {
ShaderMaterial = 0, ///< Material using a build-in shader assigned to its type of vertex.
NumMaterialTypes, ///< Number of enums.

InvalidMaterialType ///< Enum for invalid enum.
InvalidMaterialType = -1, ///< Enum for invalid enum.
ShaderMaterial = 0, ///< Material using a build-in shader assigned to its type of vertex.
NumMaterialTypes ///< Number of enums.
};

/// @brief This enum describes the color type for the material.
Expand All @@ -38,7 +36,6 @@ using TextureResourceArray = cppcore::TArray<RenderBackend::TextureResource *>;

static constexpr ui32 MaxMatColorType = static_cast<ui32>(MaterialColorType::NumMaterialColorTypes);


//-------------------------------------------------------------------------------------------------
/// @ingroup Engine
///
Expand Down
2 changes: 1 addition & 1 deletion include/osre/RenderBackend/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class OSRE_EXPORT Mesh {
void setModelMatrix(bool islocal, const glm::mat4 &model);
bool isLocal() const;
const glm::mat4 &getLocalMatrix() const;

template <class T>
void attachVertices(T *vertices, size_t size) {
if (mVertexBuffer == nullptr) {
Expand Down
5 changes: 0 additions & 5 deletions samples/02_Demo2D/Demo2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ class Demo2DApp : public App::AppBase {
return false;
}

AssetRegistry::registerAssetPathInBinFolder("assets", "assets");




return true;
}
};
Expand Down
149 changes: 149 additions & 0 deletions samples/04_terrain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
## Terrain renderer
![A simple model](../../assets/Images/01_box.png)
This sample shows how to import a model using Assimp.

```cpp
#include <osre/App/AppBase.h>
#include <osre/App/Entity.h>
#include <osre/Properties/Settings.h>
#include <osre/Scene/Stage.h>
#include <osre/Scene/Node.h>
#include <osre/Scene/View.h>
#include <osre/App/World.h>
#include <osre/App/AssetRegistry.h>
#include <osre/App/AssimpWrapper.h>
#include <osre/App/Component.h>
#include <osre/IO/Uri.h>
#include <osre/Platform/AbstractWindow.h>
#include <osre/RenderBackend/RenderCommon.h>
#include <osre/RenderBackend/RenderBackendService.h>
#include <osre/Scene/GeometryBuilder.h>
#include <osre/Scene/DbgRenderer.h>
#include <osre/Math/BaseMath.h>

using namespace ::OSRE;
using namespace ::OSRE::App;
using namespace ::OSRE::Common;
using namespace ::OSRE::RenderBackend;
using namespace ::OSRE::Scene;

// To identify local log entries
static const c8 *Tag = "ModelLoadingApp";

// The file to load
static const char *ModelPath = "file://assets/Models/Obj/spider.obj";

static const char *AssetFolderArg = "asset_folder";

static const char* ModelArg = "model";

/// The example application, will create the render environment and render a simple triangle onto it
class ModelLoadingApp : public App::AppBase {
String m_assetFolder;
Scene::Stage *m_stage;
Scene::View *m_view;
f32 m_angle;
glm::mat4 m_model;
TransformMatrixBlock m_transformMatrix;
Node::NodePtr m_modelNode;

public:
ModelLoadingApp( int argc, char *argv[] )
: AppBase( argc, (const char**) argv, "api:model", "The render API:The model to load")
, m_assetFolder("")
, m_stage( nullptr )
, m_view( nullptr )
, m_angle( 0.0f )
, m_model()
, m_transformMatrix()
, m_modelNode() {
// empty
}

virtual ~ModelLoadingApp() {
// empty
}

protected:
bool onCreate() override {
if ( !AppBase::onCreate() ) {
return false;
}
AppBase::setWindowsTitle("ModelLoader sample!");

const Common::ArgumentParser &parser = AppBase::getArgumentParser();
if ( parser.hasArgument( AssetFolderArg ) ) {
m_assetFolder = parser.getArgument( AssetFolderArg );
}

IO::Uri modelLoc(ModelPath);
if (parser.hasArgument(ModelArg)) {
String modelArg = parser.getArgument(ModelArg);
String model = "file://assets/" + modelArg;
modelLoc.setPath(model);
}

#ifdef OSRE_WINDOWS
AssetRegistry::registerAssetPath( "assets", "../../assets" );
#else
AssetRegistry::registerAssetPath( "assets", "../assets" );
#endif
AssimpWrapper assimpWrapper(*getIdContainer());
if ( assimpWrapper.importAsset( modelLoc, 0 ) ) {
RenderBackendService *rbSrv( getRenderBackendService() );
if (nullptr == rbSrv) {
return false;
}
Platform::AbstractWindow *rootWindow(getRootWindow());
if (nullptr == rootWindow) {
return false;
}

m_stage = AppBase::createStage("ModelLoading");
AppBase::setActiveStage(m_stage);
Scene::View *view = m_stage->addView("default_view", nullptr);
AppBase::setActiveView(view);

const Rect2ui &windowsRect = rootWindow->getWindowsRect();
view->setProjectionParameters( 60.f, (f32) windowsRect.m_width, (f32) windowsRect.m_height, 0.01f, 1000.f );
Entity *entity = assimpWrapper.getEntity();
World *world = getActiveWorld();
world->addEntity( entity );
view->observeBoundingBox( entity->getAABB() );
m_modelNode = entity->getNode();
}

return true;
}

void onUpdate() override {
// Rotate the model
glm::mat4 rot( 1.0 );
m_transformMatrix.m_model = glm::rotate( rot, m_angle, glm::vec3( 0, 1, 1 ) );

m_angle += 0.01f;
RenderBackendService *rbSrv( getRenderBackendService() );

rbSrv->beginPass(PipelinePass::getPassNameById(RenderPassId));
rbSrv->beginRenderBatch("b1");

rbSrv->setMatrix( MatrixType::Model, m_transformMatrix.m_model);

rbSrv->endRenderBatch();
rbSrv->endPass();

Scene::DbgRenderer::getInstance()->renderDbgText(-1, -1, 2U, "XXX");

AppBase::onUpdate();
}
};

OSRE_MAIN( ModelLoadingApp )
```
At first we are generating our render window as usual. In the onCreate-Method we will generate the
assimp-wrapper and import a model with it.
In the onUpdate-callback the model will be rotated.
119 changes: 119 additions & 0 deletions samples/04_terrain/TerrainRendering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <osre/App/AppBase.h>
#include <osre/App/CameraComponent.h>
#include <osre/App/Entity.h>
#include <osre/App/World.h>
#include <osre/App/Stage.h>
#include <osre/App/ServiceProvider.h>
#include <osre/App/TransformController.h>
#include <osre/RenderBackend/RenderBackendService.h>
#include <osre/RenderBackend/MeshBuilder.h>
#include <osre/RenderBackend/TransformMatrixBlock.h>

using namespace ::OSRE;
using namespace ::OSRE::RenderBackend;
using namespace ::OSRE::App;

// To identify local log entries we will define this tag.
static constexpr c8 Tag[] = "TerrainRenderingApp";

class TerrainRenderingApp : public App::AppBase {
/// The transform block, contains the model-, view- and projection-matrix
TransformMatrixBlock mTransformMatrix;
/// The entity to render
Entity *mEntity;
/// The keyboard controller instance.
Animation::AnimationControllerBase *mKeyboardTransCtrl;

public:
TerrainRenderingApp(int argc, char *argv[]) :
AppBase(argc, (const char **)argv),
mTransformMatrix(),
mEntity(nullptr),
mKeyboardTransCtrl(nullptr) {
// empty
}

~TerrainRenderingApp() override {
delete mEntity;
}

protected:
bool loadHeightMap(const String &filename) {
if (filename.empty()) {
return false;
}

return true;
}
Camera *setupCamera(World *world) {
Entity *camEntity = new Entity("camera", *getIdContainer(), world);
world->addEntity(camEntity);
Camera *camera =(Camera*) camEntity->createComponent(ComponentType::CameraComponentType);
world->setActiveCamera(camera);
ui32 w, h;
AppBase::getResolution(w, h);
camera->setProjectionParameters(60.f, (f32)w, (f32)h, 0.001f, 1000.f);

return camera;
}

bool onCreate() override {
if (!AppBase::onCreate()) {
return false;
}

AppBase::setWindowsTitle("Terrain sample! Rotate with keyboard: w, a, s, d, scroll with q, e");
World *world = getStage()->addActiveWorld("hello_world");
mEntity = new Entity("entity", *AppBase::getIdContainer(), world);
Camera *camera = setupCamera(world);


MeshBuilder meshBuilder;
RenderBackend::Mesh *mesh = meshBuilder.createCube(VertexType::ColorVertex, .5,.5,.5,BufferAccessType::ReadOnly).getMesh();
if (nullptr != mesh) {
RenderComponent *rc = (RenderComponent*) mEntity->getComponent(ComponentType::RenderComponentType);
rc->addStaticMesh(mesh);

Time dt;
world->update(dt);
camera->observeBoundingBox(mEntity->getAABB());
}
mKeyboardTransCtrl = AppBase::getTransformController(mTransformMatrix);

osre_info(Tag, "Creation finished.");

return true;
}

void onUpdate() override {
Platform::Key key = AppBase::getKeyboardEventListener()->getLastKey();
mKeyboardTransCtrl->update(TransformController::getKeyBinding(key));

RenderBackendService *rbSrv = ServiceProvider::getService<RenderBackendService>(ServiceType::RenderService);
rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId));
rbSrv->beginRenderBatch("b1");

rbSrv->setMatrix(MatrixType::Model, mTransformMatrix.m_model);

rbSrv->endRenderBatch();
rbSrv->endPass();

AppBase::onUpdate();
}
};

int main(int argc, char *argv[]) {
TerrainRenderingApp myApp(argc, argv);
if (!myApp.initWindow(10, 10, 1024, 768, "Instancing-Sample", false, false, RenderBackendType::OpenGLRenderBackend)) {
return 1;
}

while (myApp.handleEvents()) {
myApp.update();
myApp.requestNextFrame();
}

myApp.destroy();

return 0;
}
Loading

0 comments on commit 5cb17c8

Please sign in to comment.