7. Modèles chargés depuis des fichiers
Cet exemple montre des modèles lus depuis des fichiers : un chameau (un seul fichier OBJ, sans texture) et l'atrium de Sponza (un fichier OBJ, ses matériaux et 12 textures). Les boutons de la slide choisissent le modèle, et la slide affiche son nombre de triangles. L'intérêt de l'exemple tient aux fichiers : comment une démo pour le web les lit, et ce que cela coûte.
Télécharger : c4-cgp-load-obj.zip. Le zip contient le programme C++, sa slide en reveal.js (slides.html) et en lhtml-site (lhtml-site/), la compilation web de la démo (prête à montrer), le pont et le script de compilation. Le projet CGP n'est nécessaire que pour recompiler.
L'essayer : décompressez-le, et dans le dossier décompressé lancez python3 -m http.server 8080 ; puis ouvrez http://localhost:8080/examples/c4-cgp-load-obj/slides.html, la slide avec sa démo. Pour modifier le programme : compiler et lancer.
Ce qui est nouveau
- Des fichiers lus comme sur le bureau : le programme lit
project::path + "assets/camel.obj". Pour le web, leCMakeLists.txtde CGP empaquette le dossierassets/dansproject.data(--preload-file), un fichier à côté deproject.jsque la page télécharge avant le démarrage du programme. - La taille de la démo :
project.datafait ici environ 3 Mo, téléchargés au premier affichage de la slide. Les fichiers de Sponza ont été allégés pour le web (coordonnées arrondies, images de 512 pixels au plus). La lecture de son fichier OBJ prend quelques secondes : le programme ne lit Sponza qu'à son premier affichage, pour que le chameau apparaisse sans attendre. - Les noms des fichiers : le système de fichiers du navigateur distingue les majuscules des minuscules, comme
sous Linux. Les noms écrits dans
sponza.mtldoivent être exactement ceux des fichiers images. - Le modèle choisi par la slide : un choix
slide::param("model", ...)dont leon_changeplace la caméra pour chaque modèle ; le nombre de triangles est une valeur en lecture seule (comme dans l'exemple 6).
Compiler et lancer
Comme l'exemple 3, avec c4-cgp-load-obj.
Les fichiers
src/scene.cpp
Afficher le fichier complet : scene.cpp (108 lignes)
#include "scene.hpp" #include "slide_bridge.hpp" using namespace cgp; void scene_structure::initialize() { camera_control.initialize(inputs, window); camera_control.set_rotation_axis_z(); // The files are read from assets/, as on the desktop. For the web, the CMakeLists.txt of CGP packs // this folder into project.data (--preload-file), downloaded with the demo: keep it small. // A single OBJ file, without texture (CGP How-to: load an OBJ file) mesh camel_mesh = mesh_load_file_obj(project::path + "assets/camel.obj"); camel.initialize_data_on_gpu(camel_mesh); camel.material.color = {0.85f, 0.7f, 0.5f}; camel_triangles = float(camel_mesh.connectivity.size()); // The parameters of the demo: the buttons of the slide choose the model, and the slide shows the // number of triangles of the model shown slide::param("model", parameters.model, {{"camel", "Camel"}, {"sponza", "Sponza"}}) .on_change([this] { load_sponza(); place_camera(); slide::notify(); }); // notify: for the ImGui buttons (slide::set from C++) slide::param("turn", parameters.turn).with_label("Turn"); slide::param("gui", parameters.gui).with_label("ImGui"); slide::readout("triangles", [this] { return parameters.model == "sponza" ? sponza_triangles : camel_triangles; }) .with_label("triangles"); load_sponza(); // only if the address asks for it at once (?model=sponza) place_camera(); } // Sponza is read the first time it is shown: parsing its 2.6 MB OBJ file takes a few seconds, the camel // is shown without waiting for it void scene_structure::load_sponza() { if (parameters.model != "sponza" || !sponza.empty()) return; // An OBJ file with its materials (sponza.mtl) and its textures (*.JPG): one mesh per material // (CGP How-to: load an OBJ with materials). On the web, the names of the files are case-sensitive. std::vector<mesh_obj_advanced_loader::shape_element_node> parts = mesh_load_file_obj_advanced(project::path + "assets/sponza/", "sponza.obj"); for (auto const& part : parts) sponza_triangles += float(part.mesh_element.connectivity.size()); sponza = mesh_obj_advanced_loader::convert_to_mesh_drawable(parts); for (mesh_drawable& part : sponza) { // this file is y-up and 35 units wide part.model.rotation = rotation_transform::from_axis_angle({1, 0, 0}, Pi / 2); part.model.scaling = 0.1f; } } // A view adapted to each model void scene_structure::place_camera() { if (parameters.model == "sponza") camera_control.look_at({1.5f, -0.35f, 0.25f}, {-0.6f, 0.1f, 0.55f}, {0, 0, 1}); else camera_control.look_at({5.2f, -3.5f, 2.6f}, {0, 0, 0}, {0, 0, 1}); // the camel is about 4 units long } void scene_structure::display_frame() { environment.camera_view = camera_control.camera().matrix_view(); environment.light = camera_control.camera_model.position(); if (parameters.model == "sponza") { for (mesh_drawable const& part : sponza) draw(part, environment); } else { if (parameters.turn) angle += 0.5f * inputs.time_interval; camel.model.rotation = rotation_transform::from_axis_angle({0, 0, 1}, angle); draw(camel, environment); } } void scene_structure::display_gui() { // The template always draws its ImGui window: in a slide, the buttons of the slide replace it, // so the window is hidden unless gui=1 (see example c3) ImGui::GetStyle().Alpha = parameters.gui ? 1.0f : 0.0f; if (!parameters.gui) { ImGui::SetWindowCollapsed(true); ImGui::SetWindowPos(ImVec2(-10000, -10000)); return; } ImGui::SetWindowCollapsed(false); ImGui::SetWindowPos(ImVec2(10, 10)); if (ImGui::Button("Camel")) slide::set("model", "camel"); ImGui::SameLine(); if (ImGui::Button("Sponza")) slide::set("model", "sponza"); ImGui::Checkbox("turn", ¶meters.turn); } void scene_structure::mouse_move_event() { if (!inputs.keyboard.shift) camera_control.action_mouse_move(); } void scene_structure::mouse_click_event() { camera_control.action_mouse_click(); } void scene_structure::keyboard_event() { camera_control.action_keyboard(); } void scene_structure::idle_frame() { camera_control.idle_frame(); }
src/scene.hpp
Afficher le fichier complet : scene.hpp (46 lignes)
#pragma once #include "cgp/cgp.hpp" #include "environment.hpp" #include <string> #include <vector> using cgp::mesh_drawable; // The parameters of the demo, declared to the bridge in initialize() struct model_parameters { std::string model = "camel"; // the model shown: camel (one OBJ file) or sponza (OBJ, MTL and textures) bool turn = true; // the camel turns by itself bool gui = false; // the ImGui window of the template (hidden in a slide) }; struct scene_structure : cgp::scene_inputs_generic { void initialize(); void display_frame(); void display_gui(); environment_structure environment; window_structure window; input_devices inputs; camera_controller_orbit_euler camera_control; camera_projection_perspective camera_projection; mesh_drawable camel; // a single mesh, without texture std::vector<mesh_drawable> sponza; // one mesh per material, each with its texture float camel_triangles = 0; // the number of triangles of each model (readout) float sponza_triangles = 0; // known once Sponza is read model_parameters parameters; float angle = 0; void place_camera(); void load_sponza(); void mouse_move_event(); void mouse_click_event(); void keyboard_event(); void idle_frame(); };
slides.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Models from files</title> <!-- A slide of reveal.js with the demo of this example. Open it through a web server, from the root of the repository: python3 -m http.server 8080, then http://localhost:8080/examples/c4-cgp-load-obj/slides.html reveal.js comes from a CDN: for a talk without network, download it next to this file. --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@5.1.0/dist/reveal.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@5.1.0/dist/theme/white.css"> <style> .reveal iframe.demo { width: 100%; height: 540px; border: 0; } .reveal .row { font-size: 0.6em; margin-top: 12px; } .reveal .row button { font: inherit; padding: 4px 14px; margin: 0 4px; border-radius: 8px; border: 2px solid #999; background: white; cursor: pointer; } .reveal .row button.on { background: #333; color: white; border-color: #333; } </style> </head> <body> <div class="reveal"><div class="slides"> <section> <h3>Models from files</h3> <iframe class="demo" id="demo" data-src="web/index.html?controls=turn&panel=bl"></iframe> <div class="row"> <button data-demo-set="model=camel">Camel</button> <button data-demo-set="model=sponza">Sponza</button> Triangles: <span data-demo-value="triangles"></span> </div> </section> </div></div> <script src="https://cdn.jsdelivr.net/npm/reveal.js@5.1.0/dist/reveal.js"></script> <script src="../../bridge/slide-controls.js"></script> <script>Reveal.initialize({hash: true});</script> </body> </html>
lhtml-site/
La même slide en lhtml-site (l'ouvrir), organisée comme dans
l'exemple 1 ; seule la slide change, src/01_models/index.html.j2 :
{% set layout = 'side' %}
= Models from files
* An OBJ file, and an OBJ file with its materials and textures
* Packed with the demo in `project.data`
<div class="controls">
<p>
<button data-demo-set="model=camel">Camel</button>
<button data-demo-set="model=sponza">Sponza</button>
</p>
<p>Triangles: <span class="value" data-demo-value="triangles"></span></p>
</div>
media::
demo::../_demo/index.html?controls=turn&panel=bl(.live)
img::../_demo/poster.png(.still)
::
Rem. Le chameau provient de csc43043ep-lab-code ; Sponza a été modélisé par Marko Dabrovic (son copyright).
Ensuite : 8. Dessiner à la souris.