Skip to content

Commit e490e72

Browse files
author
Ravbug
committed
Add TextureViewer sample
1 parent a72f92e commit e490e72

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,6 @@ add_sample("Rendering")
205205
add_sample("SoundDynamics")
206206
add_sample("SceneViewer")
207207
add_sample("Rats")
208+
add_sample("TextureViewer")
208209

209210
target_link_libraries(RavEngineSceneViewer PUBLIC rve_importlib assimp)

RavEngine

Samples/TextureViewer/main.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <RavEngine/App.hpp>
2+
#include <RavEngine/StaticMesh.hpp>
3+
#include <RavEngine/World.hpp>
4+
#include <RavEngine/CameraComponent.hpp>
5+
#include <RavEngine/GameObject.hpp>
6+
#include <RavEngine/Dialogs.hpp>
7+
#include <RavEngine/StartApp.hpp>
8+
#include <RavEngine/MeshCollection.hpp>
9+
#include <SDL3/SDL_dialog.h>
10+
#include <RavEngine/Window.hpp>
11+
12+
using namespace RavEngine;
13+
using namespace std;
14+
15+
16+
17+
struct TextureViewerWorld : public RavEngine::World {
18+
19+
Ref<PBRMaterialInstance> mat;
20+
ComponentHandle<Transform> planeTransform;
21+
TextureViewerWorld() {
22+
23+
auto cubeEntity = Instantiate<GameObject>();
24+
25+
mat = RavEngine::New<PBRMaterialInstance>(Material::Manager::Get<PBRMaterial>());
26+
auto mesh = MeshCollectionStaticManager::Get("quad");
27+
cubeEntity.EmplaceComponent<StaticMesh>(mesh, mat);
28+
cubeEntity.GetTransform().LocalRotateDelta(vector3{deg_to_rad(90),0.f,0.f});
29+
planeTransform = cubeEntity;
30+
31+
auto cameraEntity = Instantiate<GameObject>();
32+
33+
auto& cameraComponent = cameraEntity.EmplaceComponent<CameraComponent>();
34+
35+
cameraComponent.SetActive(true);
36+
37+
cubeEntity.GetTransform().LocalTranslateDelta(vector3(0, 0, -5));
38+
39+
auto lightsEntity = Instantiate<GameObject>();
40+
lightsEntity.EmplaceComponent<DirectionalLight>().SetIntensity(4);
41+
lightsEntity.EmplaceComponent<AmbientLight>().SetIntensity(0.2);
42+
43+
lightsEntity.GetTransform().LocalRotateDelta(vector3{ deg_to_rad(45), deg_to_rad(45),0 });
44+
}
45+
46+
void LoadTexture(const Filesystem::Path& path) {
47+
auto tx = New<Texture>(path);
48+
mat->SetAlbedoTexture(tx);
49+
auto dim = tx->GetTextureSize();;
50+
auto aspect = (float)dim.width / dim.height;
51+
planeTransform->SetLocalScale({ aspect,1,1 });
52+
}
53+
};
54+
55+
struct TextureViewerApp : public RavEngine::App {
56+
constexpr static SDL_DialogFileFilter filters[] = { {"Any file", "*"} };
57+
58+
Ref<TextureViewerWorld> world;
59+
60+
void OnStartup(int argc, char** argv) final {
61+
62+
SetWindowTitle("TextureViewer");
63+
64+
world = RavEngine::New<TextureViewerWorld>();
65+
66+
AddWorld(world);
67+
LoadNewTexture();
68+
}
69+
70+
void OnFatal(const std::string_view msg) final {
71+
RavEngine::Dialog::ShowBasic("Fatal Error", msg, Dialog::MessageBoxType::Error);
72+
}
73+
74+
void LoadNewTexture() {
75+
SDL_ShowOpenFileDialog([](void* userdata, const char* const* filelist, int filter) {
76+
77+
auto app = static_cast<TextureViewerApp*>(userdata);
78+
if (filelist == nullptr) {
79+
Debug::Fatal("An error occured: {}", SDL_GetError());
80+
app->Quit();
81+
}
82+
if (filelist && *filelist) {
83+
Filesystem::Path chosenFile(*filelist);
84+
app->DispatchMainThread([app,chosenFile] {
85+
app->world->LoadTexture(chosenFile);
86+
});
87+
}
88+
else {
89+
app->Quit();
90+
}
91+
92+
93+
}, this, GetMainWindow()->window, filters, std::size(filters), NULL, false);
94+
}
95+
};
96+
97+
START_APP(TextureViewerApp)

0 commit comments

Comments
 (0)