forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.cpp
154 lines (118 loc) · 4.7 KB
/
Example.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
/*
* 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::ShaderProgram* shaderProgram = nullptr;
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();
shaderProgram = LoadStandardShaderProgram(vertexFormats);
CreatePipelines();
}
std::vector<LLGL::VertexFormat> CreateBuffers()
{
// Specify vertex formats
LLGL::VertexFormat vertexFormatPositions;
vertexFormatPositions.AppendAttribute({ "position", LLGL::Format::RG32Float, 0 });
vertexFormatPositions.SetSlot(0);
LLGL::VertexFormat vertexFormatColors;
vertexFormatColors.AppendAttribute({ "color", LLGL::Format::RGB32Float, 1 });
vertexFormatColors.SetSlot(1);
LLGL::VertexFormat vertexFormatInstanceData;
vertexFormatInstanceData.AppendAttribute({ "instanceColor", LLGL::Format::RGB32Float, 2, 1 });
vertexFormatInstanceData.AppendAttribute({ "instanceOffset", LLGL::Format::RG32Float, 3, 1 });
vertexFormatInstanceData.AppendAttribute({ "instanceScale", LLGL::Format::R32Float, 4, 1 });
vertexFormatInstanceData.SetSlot(2);
// Initialize buffer data
Gs::Vector2f vertexPositions[] =
{
{ 0, 1 },
{ 1, -1 },
{ -1, -1 },
};
LLGL::ColorRGBf vertexColors[] =
{
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 },
};
struct InstanceData
{
LLGL::ColorRGBf color;
Gs::Vector2f offset;
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 },
};
// 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()
{
// Create common graphics pipeline for scene rendering
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.shaderProgram = shaderProgram;
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
}
pipeline = renderer->CreatePipelineState(pipelineDesc);
}
private:
void OnDrawFrame() override
{
commands->Begin();
{
// Set buffer array
commands->SetVertexBufferArray(*vertexBufferArray);
// Set the render context as the initial render target
commands->BeginRenderPass(*context);
{
// Clear color buffer
commands->Clear(LLGL::ClearFlags::Color);
// Set viewports
commands->SetViewport(context->GetVideoMode().resolution);
// 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
context->Present();
}
};
LLGL_IMPLEMENT_EXAMPLE(Example_BufferArray);