Interactive 3D slides

7. Models from files

This example shows models read from files: a camel (a single OBJ file, without texture) and the atrium of Sponza (an OBJ file, its materials and 12 textures). The buttons of the slide choose the model, and the slide displays its number of triangles. The point of the example is the files: how a demo for the web reads them, and what it costs.

Get this example

Download: c4-cgp-load-obj.zip. The zip contains the C++ program, its slide in reveal.js (slides.html) and in lhtml-site (lhtml-site/), the web build of the demo (ready to show), the bridge and the build script. The CGP project is needed only to recompile.

Try it: unzip it, and in the unzipped folder run python3 -m http.server 8080; then open http://localhost:8080/examples/c4-cgp-load-obj/slides.html, the slide with its demo. To change the program: build and run.

Graphics: meshes and textures read from filesInterface: buttons in the slide; a value sent to the slide
Triangles:
Live: choose a model, and drag in the frame to turn the camera. Turn, in the panel: the camel turns by itself.

What is new

  • Files read as on the desktop: the program reads project::path + "assets/camel.obj". For the web, the CMakeLists.txt of CGP packs the folder assets/ into project.data (--preload-file), a file next to project.js that the page downloads before the program starts.
  • The size of the demo: project.data is about 3 MB here, downloaded at the first display of the slide. The files of Sponza were reduced for the web (coordinates rounded, images of 512 pixels at most). Reading its OBJ file takes a few seconds: the program reads Sponza only the first time it is shown, so that the camel appears without waiting.
  • The names of the files: the file system of the browser is case-sensitive, as on Linux. The names written in sponza.mtl must be exactly those of the image files.
  • The model chosen by the slide: a choice slide::param("model", ...) whose on_change places the camera for each model; the number of triangles is a readout (as in example 6).

Build and run

As example 3, with c4-cgp-load-obj.

The files

src/scene.cpp

scene.cpp

Show the full file: scene.cpp (108 lines)
#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", &parameters.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

scene.hpp

Show the full file: scene.hpp (46 lines)
#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

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/

The same slide in lhtml-site (open it), organized as in example 1; only the slide changes, 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. The camel comes from csc43043ep-lab-code; Sponza was modeled by Marko Dabrovic (its copyright).

Next: 8. Sketch with the mouse.