Skip to content

Commit

Permalink
Merge pull request #197 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 Nov 14, 2023
2 parents 2ee6f65 + faf38ef commit 02a8465
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 16 deletions.
2 changes: 1 addition & 1 deletion contrib/assimp
Submodule assimp updated 996 files
2 changes: 1 addition & 1 deletion include/osre/App/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class OSRE_EXPORT Entity : public Common::Object {
Entity( const String &name, Common::Ids &ids, World *world );
~Entity() override;
void setBehaviourControl(AbstractBehaviour *behaviour );
void setNode( TransformComponent *node );
void setNode(TransformComponent *node);
TransformComponent *getNode() const;
bool update( Time dt );
bool render( RenderBackend::RenderBackendService *rbSrv );
Expand Down
6 changes: 3 additions & 3 deletions include/osre/RenderBackend/MeshBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class OSRE_EXPORT MeshBuilder {
/// @param type [in] The vertex type.
/// @param access [in] The data access type.
/// @return The created mesh.
MeshBuilder& allocQuads( RenderBackend::VertexType type, RenderBackend::BufferAccessType access );
MeshBuilder& allocQuads(RenderBackend::VertexType type, RenderBackend::BufferAccessType access );

/// @brief
/// @param dim
/// @param vc
/// @param ic
/// @return
MeshBuilder& allocUiQuad( const Rect2ui &dim, RenderBackend::UiVertexCache &vc, RenderBackend::UiIndexCache &ic );
MeshBuilder& allocUiQuad(const Rect2ui &dim, RenderBackend::UiVertexCache &vc, RenderBackend::UiIndexCache &ic );

/// @brief
/// @param type
Expand All @@ -76,7 +76,7 @@ class OSRE_EXPORT MeshBuilder {
/// @param d
/// @param access
/// @return
MeshBuilder& createCube( RenderBackend::VertexType type, f32 w, f32 h, f32 d, RenderBackend::BufferAccessType access );
MeshBuilder& createCube(RenderBackend::VertexType type, f32 w, f32 h, f32 d, RenderBackend::BufferAccessType access );

/// @brief Will allocate vertices for a list of lines.
/// @param type [in] The vertex type.
Expand Down
4 changes: 2 additions & 2 deletions samples/00_HelloWorld/HelloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ class HelloWorldApp : public App::AppBase {

AppBase::setWindowsTitle("Hello-World 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);

mEntity = new Entity("entity", *AppBase::getIdContainer(), 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);

Camera *camera = setupCamera(world);
Time dt;
world->update(dt);
camera->observeBoundingBox(mEntity->getAABB());
Expand Down
77 changes: 70 additions & 7 deletions samples/04_terrain/TerrainRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
#include <osre/App/ServiceProvider.h>
#include <osre/App/TransformController.h>
#include <osre/RenderBackend/RenderBackendService.h>
#include <osre/RenderBackend/MaterialBuilder.h>
#include <osre/RenderBackend/MeshBuilder.h>
#include <osre/RenderBackend/Mesh.h>
#include <osre/RenderBackend/TransformMatrixBlock.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#include <vector>
#include <iostream>

using namespace ::OSRE;
using namespace ::OSRE::RenderBackend;
using namespace ::OSRE::App;
Expand Down Expand Up @@ -38,13 +46,69 @@ class TerrainRenderingApp : public App::AppBase {
}

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

int width = 0, height = 0, nChannels = 0;
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nChannels, 0);
if (data == nullptr) {
return nullptr;
}

// vertex generation
const int numVertices = width * height;
size_t index = 0;
RenderVert *v = new RenderVert[numVertices];
std::vector<float> vertices;
float yScale = 64.0f / 256.0f, yShift = 16.0f; // apply a scale+shift to the height data
for(unsigned int i = 0; i < height; i++) {
for(unsigned int j = 0; j < width; j++) {
// retrieve texel for (i,j) tex coord
unsigned char* texel = data + (j + width * i) * nChannels;
// raw height at coordinate
unsigned char y = texel[0];

// vertex position
v[index].position.x = -height/2.0f + i;
v[index].position.y = (int) y * yScale - yShift;
v[index].position.z = -width/2.0f + j;
++index;
}
}
std::cout << "Number of vertices = " << numVertices << "\n";
stbi_image_free(data);

// Index generation
std::vector<unsigned int> indices;
for (int i = 0; i < height-1; i++) { // for each row a.k.a. each strip
for (int j = 0; j < width; j++) { // for each column
for (int k = 0; k < 2; k++) { // for each side of the strip
indices.push_back(j + width * (i + k));
}
}
}
std::cout << "Number of indices = " << indices.size() << "\n";

MeshBuilder meshBuilder;
Mesh *mesh = meshBuilder.allocEmptyMesh("terrain", VertexType::RenderVertex).getMesh();
if (mesh == nullptr) {
return nullptr;
}

return true;
mesh->createVertexBuffer(v, (size_t) numVertices * sizeof(RenderVert), BufferAccessType::ReadWrite);
mesh->createIndexBuffer(&indices[0], indices.size(), IndexType::UnsignedInt, BufferAccessType::ReadWrite);

// setup primitives
mesh->addPrimitiveGroup(indices.size(), PrimitiveType::TriangleList, 0);


mesh->setMaterial(MaterialBuilder::createBuildinMaterial(VertexType::RenderVertex));

return mesh;
}

Camera *setupCamera(World *world) {
Entity *camEntity = new Entity("camera", *getIdContainer(), world);
world->addEntity(camEntity);
Expand All @@ -67,9 +131,8 @@ class TerrainRenderingApp : public App::AppBase {
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();
String filename = "world_heightmap.png";
RenderBackend::Mesh *mesh = loadHeightMap(filename);
if (nullptr != mesh) {
RenderComponent *rc = (RenderComponent*) mEntity->getComponent(ComponentType::RenderComponentType);
rc->addStaticMesh(mesh);
Expand All @@ -91,7 +154,7 @@ class TerrainRenderingApp : public App::AppBase {

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Engine/RenderBackend/DbgRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void DbgRenderer::renderAABB(const glm::mat4 &transform, const AABB &aabb) {

// setup primitives
mesh->setModelMatrix(false, transform);

mesh->addPrimitiveGroup(NumIndices, PrimitiveType::LineList, 0);

// setup material
Expand Down

0 comments on commit 02a8465

Please sign in to comment.