-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColoredQuadTest.cpp
191 lines (142 loc) · 6.19 KB
/
ColoredQuadTest.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
#define _IRR_STATIC_LIB_
#include <irrlicht.h>
#include "../source/Irrlicht/COpenGLExtensionHandler.h"
const char *vS = R"(
#version 430 core
layout(location = 0) in vec4 pos;
layout(location = 1) in vec4 color;
out vec4 vColor;
void main(){
gl_Position = pos;
vColor = color;
}
)";
const char *fS = R"(
#version 330 core
in vec4 vColor;
layout(location = 0) out vec4 color;
void main(){
color = vColor;
}
)";
using namespace irr;
class TestReceiver : public IEventReceiver{
public:
TestReceiver() {}
bool OnEvent(const SEvent &e) {
switch (e.KeyInput.Key) {
case KEY_KEY_Q:
exit(0);
return true;
}
return false;
}
};
#include "irr/irrunpack.h"
struct VertexLayout{
float pos[3];
float color[3];
} PACK_STRUCT;
#include "irr/irrpack.h"
video::IGPUMeshBuffer *createTestQuad(video::IVideoDriver *driver) {
VertexLayout vertexData[4];
vertexData[0] = { 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f };
vertexData[1] = { 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f };
vertexData[2] = { -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f };
vertexData[3] = { -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f };
uint16_t indexData[] = { 0,1,2, 0,2,3 };
auto upBuf = driver->getDefaultUpStreamingBuffer();
const void *dataToPlace[] = { vertexData, indexData };
uint32_t offsets[2] = { video::StreamingTransientDataBufferMT<>::invalid_address,
video::StreamingTransientDataBufferMT<>::invalid_address };
uint32_t alignments[2] = { sizeof(decltype(vertexData[0u])),
sizeof(decltype(indexData[0u]))};
uint32_t sizes[2] = { sizeof(vertexData), sizeof(indexData) };
upBuf->multi_place(2u, (const void* const*)dataToPlace, (uint32_t*)offsets, (uint32_t*)sizes, (uint32_t*)alignments);
if (upBuf->needsManualFlushOrInvalidate())
{
auto upStreamMem = upBuf->getBuffer()->getBoundMemory();
driver->flushMappedMemoryRanges({ video::IDriverMemoryAllocation::MappedMemoryRange(upStreamMem,offsets[0],sizes[0]),video::IDriverMemoryAllocation::MappedMemoryRange(upStreamMem,offsets[1],sizes[1]) });
}
auto buf = upBuf->getBuffer();
video::IGPUMeshDataFormatDesc* desc = driver->createGPUMeshDataFormatDesc();
desc->setVertexAttrBuffer(buf, asset::EVAI_ATTR0, asset::EF_R32G32B32_SFLOAT, sizeof(VertexLayout), offsetof(VertexLayout, pos[0]) + offsets[0]);
desc->setVertexAttrBuffer(buf, asset::EVAI_ATTR1, asset::EF_R32G32B32_SFLOAT, sizeof(VertexLayout), offsetof(VertexLayout, color[0]) + offsets[0]);
desc->setIndexBuffer(buf);
video::IGPUMeshBuffer *mb = new video::IGPUMeshBuffer();
mb->setMeshDataAndFormat(desc);
mb->setIndexBufferOffset(offsets[1]);
mb->setIndexType(asset::EIT_16BIT);
mb->setIndexCount(6);
desc->drop();
return mb;
}
int main(){
// create device with full flexibility over creation parameters
// you can add more parameters if desired, check irr::SIrrlichtCreationParameters
irr::SIrrlichtCreationParameters params;
params.Bits = 24; //may have to set to 32bit for some platforms
params.ZBufferBits = 24; //we'd like 32bit here
params.DriverType = video::EDT_OPENGL; //! Only Well functioning driver, software renderer left for sake of 2D image drawing
params.WindowSize = core::dimension2d<uint32_t>(1280, 720);
params.Fullscreen = false;
params.Vsync = true; //! If supported by target platform
params.Doublebuffer = true;
params.Stencilbuffer = false; //! This will not even be a choice soon
params.AuxGLContexts = 16;
IrrlichtDevice* device = createDeviceEx(params);
if (device == 0)
return 1; // could not create selected driver.
TestReceiver eventMgr;
device->setEventReceiver(&eventMgr);
video::IVideoDriver* driver = device->getVideoDriver();
video::SGPUMaterial material;
material.MaterialType = (video::E_MATERIAL_TYPE)driver->getGPUProgrammingServices()->addHighLevelShaderMaterial(
vS, nullptr, nullptr, nullptr, fS, 3, video::EMT_SOLID, 0, 0);
material.BackfaceCulling = false;
video::IGPUMeshBuffer *mb = createTestQuad(driver);
scene::ISceneManager* smgr = device->getSceneManager();
uint64_t lastFPSTime = 0;
while(device->run())
if (device->isWindowActive()){
driver->beginScene(true, true, video::SColor(0,0,0,0) ); //this gets 11k FPS
driver->setTransform(video::E4X3TS_WORLD, core::matrix4x3());
driver->setMaterial(material);
driver->drawMeshBuffer(mb);
driver->endScene();
// display frames per second in window title
uint64_t time = device->getTimer()->getRealTime();
if (time-lastFPSTime > 1000){
std::wostringstream str(L"Hello World - Irrlicht Engine [");
str.seekp(0,std::ios_base::end);
str << driver->getName() << "] FPS:" << driver->getFPS() << " PrimitivesDrawn: " << driver->getPrimitiveCountDrawn();
device->setWindowCaption(str.str());
lastFPSTime = time;
}
}
//create a screenshot
video::IImage* screenshot = driver->createImage(asset::EF_B8G8R8A8_UNORM,params.WindowSize);
glReadPixels(0,0, params.WindowSize.Width,params.WindowSize.Height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, screenshot->getData());{
// images are horizontally flipped, so we have to fix that here.
uint8_t* pixels = (uint8_t*)screenshot->getData();
const int32_t pitch=screenshot->getPitch();
uint8_t* p2 = pixels + (params.WindowSize.Height - 1) * pitch;
uint8_t* tmpBuffer = new uint8_t[pitch];
for (uint32_t i=0; i < params.WindowSize.Height; i += 2){
memcpy(tmpBuffer, pixels, pitch);
memcpy(pixels, p2, pitch);
memcpy(p2, tmpBuffer, pitch);
pixels += pitch;
p2 -= pitch;
}
delete [] tmpBuffer;
}
asset::CImageData* img = new asset::CImageData(screenshot);
asset::IAssetWriter::SAssetWriteParams wparams(img);
device->getAssetManager().writeAsset("screenshot.png", wparams);
img->drop();
screenshot->drop();
mb->drop();
device->drop();
return 0;
}