Debian Codegraph @

src/screen.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2017 Computer Graphics Group, University of Siegen
5 * Written by Martin Lambers <[email protected]>
6 *
7 * Copyright (C) 2022
8 * Martin Lambers <[email protected]>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <string>
30 #include <utility>
31 #include <map>
32
33 #include <QDataStream>
34
35 #include "screen.hpp"
36 #include "log.hpp"
37
38 #define TINYOBJLOADER_IMPLEMENTATION
39 #include "tiny_obj_loader.h"
40
41
42 Screen::Screen()
43 {
44 positions = {
45 -1.0f, +1.0f, 0.0f,
46 +1.0f, +1.0f, 0.0f,
47 +1.0f, -1.0f, 0.0f,
48 -1.0f, -1.0f, 0.0f
49 };
50 texcoords = {
51 0.0f, 1.0f,
52 1.0f, 1.0f,
53 1.0f, 0.0f,
54 0.0f, 0.0f
55 };
56 indices = { 0, 3, 1, 1, 3, 2 };
57 aspectRatio = 0.0f;
58 }
59
60 Screen::Screen(const QVector3D& bottomLeftCorner,
61 const QVector3D& bottomRightCorner,
62 const QVector3D& topLeftCorner)
63 {
64 QVector3D topRightCorner = bottomRightCorner + (topLeftCorner - bottomLeftCorner);
65 positions = {
66 topLeftCorner.x(), topLeftCorner.y(), topLeftCorner.z(),
67 topRightCorner.x(), topRightCorner.y(), topRightCorner.z(),
68 bottomRightCorner.x(), bottomRightCorner.y(), bottomRightCorner.z(),
69 bottomLeftCorner.x(), bottomLeftCorner.y(), bottomLeftCorner.z()
70 };
71 texcoords = {
72 0.0f, 1.0f,
73 1.0f, 1.0f,
74 1.0f, 0.0f,
75 0.0f, 0.0f
76 };
77 indices = { 0, 3, 1, 1, 3, 2 };
78 float width = (bottomRightCorner - bottomLeftCorner).length();
79 float height = (topLeftCorner - bottomLeftCorner).length();
80 aspectRatio = width / height;
81 }
82
83 /* Helper function to split a multiline TinyObjLoader message */
84 static std::vector<std::string> tinyObjMsgToLines(const std::string& s)
85 {
86 std::vector<std::string> lines;
87 size_t i = 0;
88 for (;;) {
89 size_t j = s.find_first_of('\n', i);
90 if (j < std::string::npos) {
91 lines.push_back(s.substr(i, j - i));
92 i = j + 1;
93 } else {
94 break;
95 }
96 }
97 return lines;
98 }
99
100 Screen::Screen(const QString& objFileName, const QString& shapeName, float aspectRatio)
101 {
102 LOG_INFO("%s", qPrintable(tr("Loading screen from %1").arg(objFileName)));
103 tinyobj::ObjReaderConfig conf;
104 conf.triangulate = true;
105 conf.vertex_color = false;
106 tinyobj::ObjReader reader;
107 reader.ParseFromFile(qPrintable(objFileName), conf);
108 if (reader.Warning().size() > 0) {
109 std::vector<std::string> lines = tinyObjMsgToLines(reader.Warning());
110 for (size_t i = 0; i < lines.size(); i++)
111 LOG_WARNING(" %s", qPrintable(tr("Warning: %1").arg(lines[i].c_str())));
112 }
113 if (!reader.Valid()) {
114 if (reader.Error().size() > 0) {
115 std::vector<std::string> lines = tinyObjMsgToLines(reader.Error());
116 for (size_t i = 0; i < lines.size(); i++)
117 LOG_FATAL(" %s", qPrintable(tr("Error: %1").arg(lines[i].c_str())));
118 } else {
119 LOG_FATAL(" %s", qPrintable(tr("Unknown error")));
120 }
121 return;
122 }
123
124 // Read all geometry (or at least the shape with the given name)
125 // and ignore all materials.
126 const tinyobj::attrib_t& attrib = reader.GetAttrib();
127 const std::vector<tinyobj::shape_t>& shapes = reader.GetShapes();
128 std::map<std::tuple<int, int>, unsigned int> indexTupleMap;
129 positions.clear();
130 texcoords.clear();
131 indices.clear();
132 bool haveTexcoords = true;
133 for (size_t s = 0; s < shapes.size(); s++) {
134 if (!shapeName.isEmpty() && shapeName != shapes[s].name.c_str())
135 continue;
136 const tinyobj::mesh_t& mesh = shapes[s].mesh;
137 for (size_t i = 0; i < mesh.indices.size(); i++) {
138 const tinyobj::index_t& index = mesh.indices[i];
139 int vi = index.vertex_index;
140 int ti = index.texcoord_index;
141 std::tuple<int, int> indexTuple = std::make_tuple(vi, ti);
142 auto it = indexTupleMap.find(indexTuple);
143 if (it == indexTupleMap.end()) {
144 unsigned int newIndex = indexTupleMap.size();
145 assert(vi >= 0);
146 positions.append(attrib.vertices[3 * vi + 0]);
147 positions.append(attrib.vertices[3 * vi + 1]);
148 positions.append(attrib.vertices[3 * vi + 2]);
149 if (ti < 0)
150 haveTexcoords = false;
151 if (haveTexcoords) {
152 texcoords.append(attrib.texcoords[2 * ti + 0]);
153 texcoords.append(attrib.texcoords[2 * ti + 1]);
154 }
155 indices.push_back(newIndex);
156 indexTupleMap.insert(std::make_pair(indexTuple, newIndex));
157 } else {
158 indices.push_back(it->second);
159 }
160 }
161 }
162 if (!haveTexcoords) {
163 texcoords.clear();
164 }
165
166 this->aspectRatio = aspectRatio;
167 }
168
169 QDataStream &operator<<(QDataStream& ds, const Screen& s)
170 {
171 ds << s.positions << s.texcoords << s.indices << s.aspectRatio;
172 return ds;
173 }
174
175 QDataStream &operator>>(QDataStream& ds, Screen& s)
176 {
177 ds >> s.positions >> s.texcoords >> s.indices >> s.aspectRatio;
178 return ds;
179 }