forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cpp
More file actions
353 lines (297 loc) · 12.1 KB
/
Example.cpp
File metadata and controls
353 lines (297 loc) · 12.1 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Example.cpp (Example_Animation)
*
* This file is part of the "LLGL" project (Copyright (c) 2015-2019 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include <ExampleBase.h>
class Example_Animation : public ExampleBase
{
LLGL::PipelineLayout* pipelineLayout = nullptr;
LLGL::ResourceHeap* resourceHeap = nullptr;
LLGL::PipelineState* pipelineScene = nullptr;
LLGL::Buffer* vertexBuffer = nullptr;
LLGL::Buffer* constantBuffer = nullptr;
LLGL::Texture* colorMap = nullptr;
LLGL::Sampler* linearSampler = nullptr;
TriangleMesh meshStairsTop;
TriangleMesh meshStairsBottom;
TriangleMesh meshBall;
const Gs::Vector2f viewRotationOrigin = { -33.4f, 45.0f };
const float viewDistanceToCenter = 15.0f;
const float ballJumpHeight = 0.5f;
float viewRotationAnim = 0.0f;
Gs::Vector2f viewRotation = viewRotationOrigin;
Gs::Vector2f viewRotationPrev;
struct Settings
{
Gs::Matrix4f wMatrix;
Gs::Matrix4f vpMatrix;
Gs::Vector3f lightDir = Gs::Vector3f(-0.25f, -0.7f, 1.25f).Normalized();
float shininess = 90.0f; // Blinn-phong specular power factor
Gs::Vector3f viewPos; // World-space camera position
float pad1;
LLGL::ColorRGBAf albedo = { 1.0f, 1.0f, 1.0f, 1.0f }; // Albedo material color
}
settings;
struct Ball
{
Gs::Vector3f position;
Gs::Vector3f scale;
LLGL::ColorRGBf color;
std::size_t frame = 0u;
float frameInterpolator = 0.0f;
};
std::vector<Ball> balls;
const std::array<Gs::Vector2f, 15> gridPosFrames
{{
Gs::Vector2f{ 0.0f, -3.0f },
Gs::Vector2f{ +1.0f, -3.0f },
Gs::Vector2f{ +2.0f, -3.0f },
Gs::Vector2f{ +2.0f, -2.0f },
Gs::Vector2f{ +2.0f, -1.0f },
Gs::Vector2f{ +2.0f, 0.0f },
Gs::Vector2f{ +2.0f, +1.0f },
Gs::Vector2f{ +2.0f, +2.0f },
Gs::Vector2f{ +1.0f, +2.0f },
Gs::Vector2f{ 0.0f, +2.0f },
Gs::Vector2f{ -1.0f, +2.0f },
Gs::Vector2f{ -2.0f, +2.0f },
Gs::Vector2f{ -3.0f, +2.0f },
Gs::Vector2f{ -3.0f, +1.0f },
Gs::Vector2f{ -3.0f, 0.0f }
}};
public:
Example_Animation() :
ExampleBase { L"LLGL Example: Animation" }
{
// Create all graphics objects
auto vertexFormat = CreateBuffers();
CreateTextures();
CreateSamplers();
CreatePipelineLayouts();
CreatePipelines(vertexFormat);
CreateResourceHeaps();
// Add balls to scene
AddBall(LLGL::ColorRGBf{ 1, 0, 0 });
AddBall(LLGL::ColorRGBf{ 0, 1, 0 }, 5, 0.33f);
AddBall(LLGL::ColorRGBf{ 0, 0, 1 }, 10, 0.66f);
}
private:
LLGL::VertexFormat CreateBuffers()
{
// Specify vertex format
LLGL::VertexFormat vertexFormat;
vertexFormat.AppendAttribute({ "position", LLGL::Format::RGB32Float });
vertexFormat.AppendAttribute({ "normal", LLGL::Format::RGB32Float });
vertexFormat.AppendAttribute({ "texCoord", LLGL::Format::RG32Float });
vertexFormat.SetStride(sizeof(TexturedVertex));
// Load 3D models
std::vector<TexturedVertex> vertices;
meshStairsTop = LoadObjModel(vertices, "../../Media/Models/PenroseStairs-Top.obj");
meshStairsBottom = LoadObjModel(vertices, "../../Media/Models/PenroseStairs-Bottom.obj");
meshBall = LoadObjModel(vertices, "../../Media/Models/IcoSphere.obj");
meshStairsTop.color = { 1.0f, 1.0f, 1.0f, 1.0f };
meshStairsBottom.color = { 1.0f, 1.0f, 0.0f, 0.0f };
// Create vertex, index, and constant buffer
vertexBuffer = CreateVertexBuffer(vertices, vertexFormat);
constantBuffer = CreateConstantBuffer(settings);
return vertexFormat;
}
void CreateTextures()
{
// Load color map texture
colorMap = LoadTexture("../../Media/Textures/TilesGray512.jpg");
}
void CreateSamplers()
{
// Create default sampler state
linearSampler = renderer->CreateSampler(LLGL::SamplerDescriptor{});
}
void CreatePipelineLayouts()
{
// Create pipeline layouts for shadow-map and scene rendering
if (IsOpenGL())
{
pipelineLayout = renderer->CreatePipelineLayout(
LLGL::PipelineLayoutDesc("cbuffer(Settings@1):frag:vert, texture(colorMap@2):frag, sampler(2):frag")
);
}
else
{
pipelineLayout = renderer->CreatePipelineLayout(
LLGL::PipelineLayoutDesc("cbuffer(Settings@1):frag:vert, texture(colorMap@2):frag, sampler(linearSampler@3):frag")
);
}
}
void CreatePipelines(const LLGL::VertexFormat& vertexFormat)
{
// Create graphics pipeline for scene rendering
{
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.vertexShader = LoadStandardVertexShader("VS", { vertexFormat });
pipelineDesc.fragmentShader = LoadStandardFragmentShader("PS");
pipelineDesc.renderPass = swapChain->GetRenderPass();
pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.depth.testEnabled = true;
pipelineDesc.depth.writeEnabled = true;
pipelineDesc.rasterizer.cullMode = LLGL::CullMode::Back;
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
}
pipelineScene = renderer->CreatePipelineState(pipelineDesc);
ThrowIfFailed(pipelineScene);
}
}
void CreateResourceHeaps()
{
// Create resource heap for scene rendering
LLGL::ResourceHeapDescriptor resourceHeapDesc;
{
resourceHeapDesc.pipelineLayout = pipelineLayout;
resourceHeapDesc.resourceViews = { constantBuffer, colorMap, linearSampler };
}
resourceHeap = renderer->CreateResourceHeap(resourceHeapDesc);
}
Gs::Vector3f GetGridPos(std::size_t frame) const
{
return Gs::Vector3f
{
gridPosFrames[frame].x + 0.5f,
3.3f - static_cast<float>(frame) * 0.2f,
gridPosFrames[frame].y + 0.5f
};
}
void AddBall(const LLGL::ColorRGBf& color, std::size_t initialFrame = 0, float t = 0.0f)
{
Ball ball;
{
ball.position = GetGridPos(initialFrame);
ball.color = color;
ball.frame = initialFrame;
ball.frameInterpolator = t;
}
balls.push_back(ball);
}
void UpdateBallAnimation(Ball& ball, float dt)
{
// Update frame interpolation
ball.frameInterpolator += dt * 2.0f;
while (ball.frameInterpolator > 1.0f)
{
ball.frameInterpolator -= 1.0f;
ball.frame++;
if (ball.frame + 1 >= gridPosFrames.size())
{
ball.position = GetGridPos(0);
ball.frame = 0;
}
}
float t = ball.frameInterpolator;
float ts = Gs::SmoothStep(t);
// Add scaling to animate bouncing
float s = 0.0f;
if (ts <= 0.1f)
s = std::cos((ts * 5.0f) * Gs::pi);
else if (ts >= 0.9f)
s = std::sin(((ts - 0.9f) * 5.0f) * Gs::pi);
// Interpolate transformation between current and next frame
ball.position = Gs::Lerp(GetGridPos(ball.frame), GetGridPos(ball.frame + 1), t);
ball.position.y += std::sin(t * Gs::pi) * ballJumpHeight;
ball.scale = Gs::Vector3f{ 1.0f + s*0.1f, 1.0f - s*0.3f, 1.0f + s*0.1f };
}
void UpdateScene(float dt)
{
// Update animation
if (input.KeyPressed(LLGL::Key::LButton))
{
auto motion = input.GetMouseMotion();
viewRotation.x += static_cast<float>(motion.y) * 0.25f;
viewRotation.x = Gs::Clamp(viewRotation.x, -90.0f, 90.0f);
viewRotation.y += static_cast<float>(motion.x) * 0.25f;
}
else if (input.KeyUp(LLGL::Key::LButton))
{
// Reset animation
viewRotationAnim = 1.0f;
viewRotationPrev = viewRotation;
}
else if (viewRotationAnim > 0.0f)
{
viewRotationAnim = viewRotationAnim - dt * 3.0f;
viewRotation = Gs::Lerp(viewRotationOrigin, viewRotationPrev, std::max(0.0f, viewRotationAnim*viewRotationAnim));
}
else
{
viewRotationAnim = 0.0f;
viewRotation = viewRotationOrigin;
}
// Initialize camera matrices for orthogonal projection
const float winSize = 8.0f;
projection = OrthogonalProjection(winSize * GetAspectRatio(), winSize, 0.1f, 100.0f);
// Update view transformation
settings.vpMatrix.LoadIdentity();
Gs::RotateFree(settings.vpMatrix, { 0, 1, 0 }, Gs::Deg2Rad(viewRotation.y));
Gs::RotateFree(settings.vpMatrix, { 1, 0, 0 }, Gs::Deg2Rad(viewRotation.x));
Gs::Translate(settings.vpMatrix, { 0, 0, -viewDistanceToCenter });
settings.viewPos = Gs::TransformVector(settings.vpMatrix, Gs::Vector3f{ 0, 0, 0 });
settings.vpMatrix.MakeInverse();
settings.vpMatrix = projection * settings.vpMatrix;
// Update ball animations
for (auto& ball : balls)
UpdateBallAnimation(ball, dt);
}
void RenderMesh(const TriangleMesh& mesh)
{
settings.wMatrix = mesh.transform;
settings.albedo = mesh.color;
commands->UpdateBuffer(*constantBuffer, 0, &settings, sizeof(settings));
commands->Draw(mesh.numVertices, mesh.firstVertex);
}
void RenderBall(const Ball& ball)
{
// Set ball color
settings.albedo = LLGL::ColorRGBAf{ ball.color.r, ball.color.g, ball.color.b, 0.0f };
// Update model-to-world transformation matrix
settings.wMatrix.LoadIdentity();
Gs::Translate(settings.wMatrix, ball.position);
Gs::Scale(settings.wMatrix, ball.scale*0.3f);
// Submit data to constant buffer
commands->UpdateBuffer(*constantBuffer, 0, &settings, sizeof(settings));
// Draw ball
commands->Draw(meshBall.numVertices, meshBall.firstVertex);
}
void RenderScene()
{
commands->SetPipelineState(*pipelineScene);
commands->SetResourceHeap(*resourceHeap);
RenderMesh(meshStairsBottom);
RenderMesh(meshStairsTop);
for (const auto& ball : balls)
RenderBall(ball);
}
void OnDrawFrame() override
{
// Update scene by user input
timer.MeasureTime();
UpdateScene(static_cast<float>(timer.GetDeltaTime()));
commands->Begin();
{
// Bind common input assembly
commands->SetVertexBuffer(*vertexBuffer);
// Render everything directly into the swap-chain
commands->BeginRenderPass(*swapChain);
{
commands->Clear(LLGL::ClearFlags::All, { backgroundColor });
commands->SetViewport(swapChain->GetResolution());
RenderScene();
}
commands->EndRenderPass();
}
commands->End();
commandQueue->Submit(*commands);
// Present result on the screen
swapChain->Present();
}
};
LLGL_IMPLEMENT_EXAMPLE(Example_Animation);