-
Notifications
You must be signed in to change notification settings - Fork 3
/
sprite3dExample.cpp
256 lines (227 loc) · 9.89 KB
/
sprite3dExample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Sprite3d EXAMPLE
//
// by Hapax (http://github.com/Hapaxia)
//
//
// Key controls:
//
// Escape Quit
// Space Pause/unpause
// Backspace Reset clock
// - = Adjust depth
// [ ] Adjust mesh density
// , . Adjust subdivision level
// Tab Toggle textures
// Return Toggle Dynamic Subdivision (default range is 1 to 3)
// B Toggle back face correction (flip)
// F8 Resets mesh to minimal (two triangles) and turns off Dynamic Subdivision
// F9 Toggle visibility of Sprite3d's bounds
// F10 Toggle visibility of standard sprite's bounds
// F11 Toggle visibility of Sprite3d
// F12 Toggle visibility of standard sprite
//
// Please note that this example makes use of C++11 features
//
//////////////////////////////////////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <iostream> // for std::cerr
#include "Sprite3d.hpp"
int main()
{
// set up window
sf::RenderWindow window(sf::VideoMode(800, 600), "Sprite3d", sf::Style::Default);
//window.setVerticalSyncEnabled(true);
// load resources (textures and font)
sf::Texture texture, frontTexture, backTexture;
if (!texture.loadFromFile("resources/uv map.jpg") ||
!frontTexture.loadFromFile("resources/uv map front.jpg") ||
!backTexture.loadFromFile("resources/uv map back.jpg"))
/*
if (!texture.loadFromFile("resources/Card Face - SFML.png") ||
!frontTexture.loadFromFile("resources/Card Face - SFML.png") ||
!backTexture.loadFromFile("resources/Card Back - SFML.png"))
*/
{
std::cerr << "Error loading textures." << std::endl;
return EXIT_FAILURE; // textures are required for this example. it's pointless carrying on without them
}
sf::Font font;
if (!font.loadFromFile("resources/arial.ttf"))
std::cerr << "Error loading font." << std::endl; // no need to exit if font fails; it will just not show any text
// text headers
sf::Text sprite3dText("Sprite3d", font, 48);
sf::Text spriteText("Sprite", font, 48);
sprite3dText.setOrigin(sprite3dText.getLocalBounds().left + sprite3dText.getLocalBounds().width / 2.f, 0.f);
spriteText.setOrigin(spriteText.getLocalBounds().left + spriteText.getLocalBounds().width / 2.f, 0.f);
sprite3dText.setPosition(sf::Vector2f(window.getSize().x * 0.25f, 0.f));
spriteText.setPosition(sf::Vector2f(window.getSize().x * 0.75f, 0.f));
// text feedback
sf::Text feedbackText("FPS:\nVertices:\nSubdivision Level:\nSubdivided Mesh Density:\nMesh Density:\nDynamic Subdivision enabled:\nMost Extreme Angle:\nDepth:", font, 16);
feedbackText.setOrigin(feedbackText.getLocalBounds().left, feedbackText.getLocalBounds().top + feedbackText.getLocalBounds().height);
feedbackText.setPosition(sf::Vector2f(2.f, window.getSize().y - 1.f));
// sprite3d
sf::Sprite3d sprite3d(frontTexture, backTexture);
sprite3d.setOrigin(sf::Vector2f(sprite3d.getLocalBounds().width, sprite3d.getLocalBounds().height) / 2.f);
sprite3d.setPosition(sf::Vector2f(window.getSize().x * 0.25f, window.getSize().y / 2.f));
//sprite3d.setBackFlipEnabled();
//sprite3d.setMeshDensity(3); // = 5x5 points = 4x4 quads
//sprite3d.setNumberOfPoints(25); // 5x5
//sprite3d.setNumberOfQuads(16); // 4x4
//sprite3d.setDynamicSubdivisionRange(3, 0); // max: 32x32 quads (4 subdivided 3 times is 32), min: 4x4 quads (zero subdivisions)
// sprite
//sf::Sprite sprite(sprite3d.getSprite());
sf::Sprite sprite(texture);
sprite.setOrigin(sf::Vector2f(sprite.getLocalBounds().width, sprite.getLocalBounds().height) / 2.f);
sprite.setPosition(sf::Vector2f(window.getSize().x * 0.75f, window.getSize().y / 2.f));
// bounds rectangles
sf::RectangleShape boundsSprite3d;
boundsSprite3d.setFillColor(sf::Color::Transparent);
boundsSprite3d.setOutlineColor(sf::Color::Red);
boundsSprite3d.setOutlineThickness(1.f);
sf::RectangleShape boundsSprite = boundsSprite3d;
// clock and pause
sf::Clock clock;
bool isPaused{ true };
float time{ 0.f };
// visibility flags
struct
{
bool sprite3d, sprite, sprite3dBounds, spriteBounds;
} isVisible{ true, true, false, false };
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::KeyPressed)
{
// pause/unpause
if (event.key.code == sf::Keyboard::Space)
{
isPaused = !isPaused;
clock.restart();
}
// reset time
else if (event.key.code == sf::Keyboard::BackSpace)
{
time = 0.f;
clock.restart();
}
// control depth
else if (event.key.code == sf::Keyboard::Equal)
sprite3d.setDepth(sprite3d.getDepth() + 1.f);
else if (event.key.code == sf::Keyboard::Dash)
sprite3d.setDepth(sprite3d.getDepth() - 1.f);
// control mesh density
else if (event.key.code == sf::Keyboard::RBracket)
sprite3d.setMeshDensity(sprite3d.getMeshDensity() + 1);
else if (event.key.code == sf::Keyboard::LBracket)
{
if (sprite3d.getMeshDensity() > 0)
sprite3d.setMeshDensity(sprite3d.getMeshDensity() - 1);
}
// control subdivision level
else if (event.key.code == sf::Keyboard::Period)
sprite3d.setSubdivision(sprite3d.getSubdivision() + 1);
else if (event.key.code == sf::Keyboard::Comma)
{
if (sprite3d.getSubdivision() > 0)
sprite3d.setSubdivision(sprite3d.getSubdivision() - 1);
}
// toggle dynamic subdivision
else if (event.key.code == sf::Keyboard::Return)
sprite3d.setDynamicSubdivisionEnabled(!sprite3d.getDynamicSubdivisionEnabled());
// toggle back face is flipped state
else if (event.key.code == sf::Keyboard::B)
sprite3d.setBackFlipEnabled(!sprite3d.getBackFlipEnabled());
// toggle bounds rectangles
else if (event.key.code == sf::Keyboard::F9)
isVisible.sprite3dBounds = !isVisible.sprite3dBounds;
else if (event.key.code == sf::Keyboard::F10)
isVisible.spriteBounds = !isVisible.spriteBounds;
// toggle whether or not to draw the sprites
else if (event.key.code == sf::Keyboard::F11)
isVisible.sprite3d = !isVisible.sprite3d;
else if (event.key.code == sf::Keyboard::F12)
isVisible.sprite = !isVisible.sprite;
// reset the mesh to minimal (also turns off dynamic subdivision)
else if (event.key.code == sf::Keyboard::F8)
{
sprite3d.setDynamicSubdivisionEnabled(false);
sprite3d.minimalMesh();
}
// toggle texture usage
else if (event.key.code == sf::Keyboard::Tab)
{
if (sprite3d.getTexture() != &frontTexture)
{
sprite3d.setTexture(frontTexture);
sprite3d.setBackTexture(backTexture);
}
else
{
sprite3d.setTexture(sf::Texture());
sprite3d.setBackTexture(sf::Texture());
}
}
}
}
// update time
float frameTime = 1.f;
if (!isPaused)
{
frameTime = clock.restart().asSeconds();
time += frameTime;
}
// update sprites
sprite3d.setRotation({ time * 97.f, time * 42.f, time * 51.f });
//sprite.setScale({ cos(time * 97.f * 3.141592653f / 180.f), cos(time * 42.f * 3.141592653f / 180.f) });
sprite.setScale({ cos(time * 42 * 3.141592653f / 180.f), cos(time * 97 * 3.141592653f / 180.f) });
sprite.setRotation(time * 51.f);
// other sprite3d rotations for testing
//sprite3d.setRotation(time * 60.f);
/*
sprite3d.setPitch(time * 60.f);
sprite3d.setYaw(time * 40.f);
*/
//sprite3d.setYaw(time * 180.f);
// update bounds rectangle
boundsSprite3d.setPosition(sf::Vector2f(sprite3d.getGlobalBounds().left, sprite3d.getGlobalBounds().top));
boundsSprite3d.setSize(sf::Vector2f(sprite3d.getGlobalBounds().width, sprite3d.getGlobalBounds().height));
boundsSprite.setPosition(sf::Vector2f(sprite.getGlobalBounds().left, sprite.getGlobalBounds().top));
boundsSprite.setSize(sf::Vector2f(sprite.getGlobalBounds().width, sprite.getGlobalBounds().height));
// update depth text
unsigned int subdividedMeshDensity = sprite3d.getMeshDensity();
for (unsigned int i = 0; i < sprite3d.getSubdivision(); ++i)
subdividedMeshDensity = subdividedMeshDensity * 2 + 1;
unsigned int numberOfVerticesInSubdividedMesh = (subdividedMeshDensity * 2 + 5) * subdividedMeshDensity + 4;
feedbackText.setString(
"FPS: " + std::to_string(static_cast<unsigned int>(1.f / frameTime)) +
"\nVertices: " + std::to_string(numberOfVerticesInSubdividedMesh) + " (" + std::to_string(numberOfVerticesInSubdividedMesh - 2) + " triangles)" +
"\nSubdivision Level: " + std::to_string(sprite3d.getSubdivision()) +
"\nSubdivided Mesh Density: " + std::to_string(sprite3d.getSubdividedMeshDensity()) + " (" + std::to_string(sprite3d.getSubdividedMeshDensity() + 2) + "x" + std::to_string(sprite3d.getSubdividedMeshDensity() + 2) + " = " + std::to_string((sprite3d.getSubdividedMeshDensity() + 2) * (sprite3d.getSubdividedMeshDensity() + 2)) + " points)" +
"\nMesh Density: " + std::to_string(sprite3d.getMeshDensity()) + " (" + std::to_string(sprite3d.getMeshDensity() + 2) + "x" + std::to_string(sprite3d.getMeshDensity() + 2) + " = " + std::to_string((sprite3d.getMeshDensity() + 2) * (sprite3d.getMeshDensity() + 2)) + " points)" +
"\nDynamic Subdivision enabled: " + (sprite3d.getDynamicSubdivisionEnabled() ? "true" : "false") +
"\nMost Extreme Angle: " + std::to_string(sprite3d.getMostExtremeAngle()) +
"\nDepth: " + std::to_string(sprite3d.getDepth()));
// update display
window.clear(sf::Color(64, 64, 64));
if (isVisible.sprite)
window.draw(sprite);
if (isVisible.sprite3d)
window.draw(sprite3d);
if (isVisible.spriteBounds)
window.draw(boundsSprite);
if (isVisible.sprite3dBounds)
window.draw(boundsSprite3d);
window.draw(spriteText);
window.draw(sprite3dText);
window.draw(feedbackText);
window.display();
}
return EXIT_SUCCESS;
}