src/utility.h

Wed, 25 May 2022 20:36:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 20:36:34 +0300
changeset 199
6988973515d2
parent 196
6bcb284679d4
child 200
ca23936b455b
permissions
-rw-r--r--

Fix pick() picking from weird places on the screen with high DPI scaling

glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
18
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include "basics.h"
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 namespace utility
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 // http://stackoverflow.com/a/18204188/3629665
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 template<typename T>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 inline T rotl10(T x)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 return (x << 10) | ((x >> 22) & 0x000000ff);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 template<typename T>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 inline T rotl20(T x)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 return (x << 20) | ((x >> 12) & 0x000000ff);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 inline QString format(const QString& format_string)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 return format_string;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 template<typename T, typename... Rest>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 QString format(const QString& format_string, T&& arg, Rest&&... rest)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 return format(format_string.arg(arg), std::forward<Rest>(rest)...);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 inline QString quoted(QString string)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 if (string.contains("'"))
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 string.replace("\"", "\\\"");
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 string = "\"" + string + "\"";
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 else
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 string = "'" + string + "'";
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 return string;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
62 /**
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
63 * @brief Converts the specified vertex to a simple string
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
64 * @param vertex vertex to convert
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
65 * @return "x y z"-formatted string
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
66 */
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
67 inline QString vertexToString(const glm::vec3& vertex)
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
68 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
69 return utility::format("%1 %2 %3", vertex.x, vertex.y, vertex.z);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
70 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
71
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
72 inline QString vertexToStringParens(const glm::vec3& vertex)
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
73 {
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
74 return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z);
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
75 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 }
196
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
77
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
78 template<typename T, glm::qualifier Q>
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
79 inline unsigned int qHash(const glm::vec<3, T, Q>& key)
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
80 {
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
81 return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z));
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
82 }

mercurial