CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ObjWriter.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "MeshVertex.hpp"
11#include "Triangle.hpp"
12
13#include <sstream>
14
16
18 class ObjWriter {
19 public:
21
22 template<typename Mesh> void writeMesh(const Mesh& mesh) {
23 int newBase = mBase;
24
25 for(const MeshVertex& vertex : mesh.vertices()) {
26 ++newBase;
27
28 ss_ << "v " << vertex.position[0] << " " << vertex.position[1] << " " << vertex.position[2] << "\n";
29
30 ss_ << "vn " << vertex.normal[0] << " " << vertex.normal[1] << " " << vertex.normal[2] << "\n";
31
32 ss_ << "vt " << vertex.texCoord[0] << " " << vertex.texCoord[1] << "\n";
33 }
34
35 for(const Triangle& triangle : mesh.triangles()) {
36 auto t = triangle.vertices + mBase;
37 ss_ << "f " << t[0] << "/" << t[0] << "/" << t[0] << " " << t[1] << "/" << t[1] << "/" << t[1] << " " << t[2] << "/" << t[2] << "/" << t[2]
38 << "\n";
39 }
40
41 mBase = newBase;
42 }
43
44 std::string str() { return ss_.str(); }
45
46 private:
47 int mBase;
48
49 std::stringstream ss_;
50 };
51
52} // namespace CeresEngine::MeshGenerator
Definition MeshVertex.hpp:14
A class for generating obj files for preview and debug purposes.
Definition ObjWriter.hpp:18
std::stringstream ss_
Definition ObjWriter.hpp:49
int mBase
Definition ObjWriter.hpp:47
void writeMesh(const Mesh &mesh)
Definition ObjWriter.hpp:22
std::string str()
Definition ObjWriter.hpp:44
Definition Triangle.hpp:14
A base class for all mesh implementations.
Definition Mesh.hpp:112
Definition AnyGenerator.hpp:12
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25