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
158 lines (123 loc) · 4.86 KB
/
Example.cpp
File metadata and controls
158 lines (123 loc) · 4.86 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
/*
* Example.cpp (Example_BufferArray)
*
* 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_BufferArray : public ExampleBase
{
LLGL::PipelineState* pipeline = nullptr;
LLGL::Buffer* vertexBuffers[3] = { nullptr };
LLGL::BufferArray* vertexBufferArray = nullptr;
public:
Example_BufferArray() :
ExampleBase { L"LLGL Example: BufferArray" }
{
// Create all graphics objects
auto vertexFormats = CreateBuffers();
CreatePipelines(vertexFormats);
}
std::vector<LLGL::VertexFormat> CreateBuffers()
{
// Initialize buffer data
float vertexPositions[][2] =
{
{ 0, 1 },
{ 1, -1 },
{ -1, -1 },
};
float vertexColors[][3] =
{
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 },
};
struct InstanceData
{
float color[3];
float offset[2];
float scale;
}
instanceData[] =
{
{ { 1 , 1 , 1 }, { -0.5f, 0.5f }, 0.4f },
{ { 1 , 2 , 3 }, { 0.5f, 0.5f }, -0.4f },
{ { 1 , 0.2f, 0.2f }, { 0.5f, -0.5f }, 0.2f },
{ { 0.2f, 1 , 0.2f }, { -0.5f, -0.5f }, 0.3f },
};
// Specify vertex formats
LLGL::VertexFormat vertexFormatPositions;
vertexFormatPositions.attributes =
{
LLGL::VertexAttribute{ "position", LLGL::Format::RG32Float, 0, 0, sizeof(float[2]), 0 }
};
LLGL::VertexFormat vertexFormatColors;
vertexFormatColors.attributes =
{
LLGL::VertexAttribute{ "color", LLGL::Format::RGB32Float, 1, 0, sizeof(float[3]), 1 }
};
LLGL::VertexFormat vertexFormatInstanceData;
vertexFormatInstanceData.attributes =
{
LLGL::VertexAttribute{ "instanceColor", LLGL::Format::RGB32Float, 2, 0, sizeof(InstanceData), 2, 1 },
LLGL::VertexAttribute{ "instanceOffset", LLGL::Format::RG32Float, 3, 12, sizeof(InstanceData), 2, 1 },
LLGL::VertexAttribute{ "instanceScale", LLGL::Format::R32Float, 4, 20, sizeof(InstanceData), 2, 1 }
};
// Create buffer for vertex positions
LLGL::BufferDescriptor desc;
desc.size = sizeof(vertexPositions);
desc.bindFlags = LLGL::BindFlags::VertexBuffer;
desc.vertexAttribs = vertexFormatPositions.attributes;
vertexBuffers[0] = renderer->CreateBuffer(desc, vertexPositions);
// Create buffer for vertex colors
desc.size = sizeof(vertexColors);
desc.vertexAttribs = vertexFormatColors.attributes;
vertexBuffers[1] = renderer->CreateBuffer(desc, vertexColors);
// Create buffer for instance data
desc.size = sizeof(instanceData);
desc.vertexAttribs = vertexFormatInstanceData.attributes;
vertexBuffers[2] = renderer->CreateBuffer(desc, instanceData);
// Create vertex buffer array
vertexBufferArray = renderer->CreateBufferArray(3, vertexBuffers);
return { vertexFormatPositions, vertexFormatColors, vertexFormatInstanceData };
}
void CreatePipelines(const std::vector<LLGL::VertexFormat>& vertexFormats)
{
// Create common graphics pipeline for scene rendering
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.vertexShader = LoadStandardVertexShader("VS", vertexFormats);
pipelineDesc.fragmentShader = LoadStandardFragmentShader("PS");
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
}
pipeline = renderer->CreatePipelineState(pipelineDesc);
}
private:
void OnDrawFrame() override
{
commands->Begin();
{
// Set buffer array
commands->SetVertexBufferArray(*vertexBufferArray);
// Set the swap-chain as the initial render target
commands->BeginRenderPass(*swapChain);
{
// Clear color buffer
commands->Clear(LLGL::ClearFlags::Color, backgroundColor);
// Set viewports
commands->SetViewport(swapChain->GetResolution());
// Set graphics pipeline state
commands->SetPipelineState(*pipeline);
// Draw 4 instances of the triangle with 3 vertices each
commands->DrawInstanced(3, 0, 4);
}
commands->EndRenderPass();
}
commands->End();
commandQueue->Submit(*commands);
// Present result on the screen
swapChain->Present();
}
};
LLGL_IMPLEMENT_EXAMPLE(Example_BufferArray);