Covellite++  Version: 2.3.0 Revision: ??? Platform: x64 Build: 23:13 04.01.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
OpenGLCommonShader.cpp
1
2#include "stdafx.h"
3#include "OpenGLCommonShader.hpp"
4#include <glm/glm.force.hpp>
5#include <alicorn/std/vector.hpp>
6#include <alicorn/std.fast/unordered-map.hpp>
7#include <Covellite/Api/Vertex.hpp>
8#include <Covellite/Api/Defines.hpp>
9#include "OpenGLCommon.Texture.hpp"
10#include "Shaders/Shaders.hpp"
11#include "GraphicApi.Constants.hpp"
12
13namespace covellite
14{
15
16namespace api
17{
18
19namespace renderer
20{
21
22inline GLuint glGetProgramId(void)
23{
24 GLint iProgramId = 0;
25 glGetIntegerv(GL_CURRENT_PROGRAM, &iProgramId);
26 return static_cast<GLuint>(iProgramId);
27}
28
29inline void CheckError(const int _Line)
30{
31 const auto Error = glGetError();
32 if (Error != GL_NO_ERROR)
33 {
34 throw EXCEPTION_NO_FILE_LINE(::std::runtime_error) <<
35 _Line << ": error " << Error;
36 }
37}
38
39#define GL_CHECK \
40 CheckError(__LINE__)
41
42template<class>
43class Support; // Not implement!!!
44
45template<>
46class Support<::Camera>
47{
48public:
49 static const GLuint Index = COVELLITE_BUFFER_INDEX_CAMERA;
50
51private:
52 static void Update(
53 const GLuint _ProgramId,
54 const ::Camera & _Matrices,
55 const ::std::string & _BufferName)
56 {
57 const auto MatrixProjectionId =
58 glGetUniformLocation(_ProgramId, (_BufferName + ".Projection").c_str());
59 const auto MatrixViewId =
60 glGetUniformLocation(_ProgramId, (_BufferName + ".View").c_str());
61 const auto MatrixViewInverseId =
62 glGetUniformLocation(_ProgramId, (_BufferName + ".ViewInverse").c_str());
63
64 glUniformMatrix4fv(MatrixProjectionId, 1, GL_FALSE,
65 ::glm::value_ptr(_Matrices.Projection));
66 glUniformMatrix4fv(MatrixViewId, 1, GL_FALSE,
67 ::glm::value_ptr(_Matrices.View));
68 glUniformMatrix4fv(MatrixViewInverseId, 1, GL_FALSE,
69 ::glm::value_ptr(_Matrices.ViewInverse));
70 }
71
72public:
73 static void Update(const GLuint _ProgramId, const ::Camera & _Matrices)
74 {
75 Update(_ProgramId, _Matrices, "MatricesData");
76 Update(_ProgramId, _Matrices, "CameraData");
77 }
78};
79
80template<>
81class Support<::Object>
82{
83public:
84 static const GLuint Index = COVELLITE_BUFFER_INDEX_OBJECT;
85
86public:
87 static void Update(const GLuint _ProgramId, const ::Object & _Object)
88 {
89 const auto MatrixWorldId =
90 glGetUniformLocation(_ProgramId, "ObjectData.World");
91 glUniformMatrix4fv(MatrixWorldId, 1, GL_FALSE,
92 ::glm::value_ptr(_Object.World));
93 }
94};
95
96template<>
97class Support<uint8_t>
98{
99public:
100 static const GLuint Index = COVELLITE_BUFFER_INDEX_USER;
101};
102
103class OpenGLCommonShader::Buffer
104{
105public:
106 inline void Bind(const bool _IsActivate = true) const
107 {
108 glBindBuffer(m_Type, _IsActivate ? m_BufferId : 0);
109 }
110
111 inline void UpdateData(const void * _pData, const size_t _Size) const
112 {
113 glBufferSubData(m_Type, 0, static_cast<GLsizeiptr>(_Size), _pData);
114 }
115
116 inline void UpdateData(const ::std::string & _UniformBufferName,
117 const void * _pData, const size_t _Size) const
118 {
119 const auto ProgramId = glGetProgramId();
120
121 const auto BlockIndex = glGetUniformBlockIndex(
122 ProgramId, _UniformBufferName.c_str());
123
124 // Вероятно, шейдерная программа выкидывает неиспользуемые uniform-буфера,
125 // в результате чего Index становится некорректным и glUniformBlockBinding()
126 // завершается с ошибкой.
127 // При игнорирование ошибки здесь визуально все работает корректно.
128 if (BlockIndex == GL_INVALID_INDEX) return;
129
130 Bind();
131 UpdateData(_pData, _Size);
132 Bind(false);
133
134 glBindBufferBase(m_Type, m_Index, m_BufferId);
135 glUniformBlockBinding(ProgramId, BlockIndex, m_Index);
136 }
137
138 void SetVertexInputData(void) const
139 {
140 // Нужно обязательно проверять валидность идентификаторов, т.к. некоторые
141 // реализации драйверов (не все!) удаляют при компиляции шейдера
142 // объявленные, но не используемые переменные.
143
144 // Явный вызов glVertexAttribDivisor() требуется по той же причине -
145 // из-за удаления в шейдере не используемой переменной вызов этой функции
146 // при использовании инстансинга привяжет смещение к идентификатору,
147 // который в дальнейшем может быть получен здесь.
148
149 const auto ProgramId = glGetProgramId();
150
151 const auto hPosition = static_cast<GLuint>(
152 glGetAttribLocation(ProgramId, "Covellite_VertexPosition"));
153 if (hPosition != -1)
154 {
155 glEnableVertexAttribArray(hPosition);
156 glVertexAttribPointer(hPosition, 4, GL_FLOAT, GL_FALSE,
157 sizeof(::covellite::api::Vertex), (void *)0);
158 glVertexAttribDivisor(hPosition, 0);
159 }
160
161 const auto hTexCoord = static_cast<GLuint>(
162 glGetAttribLocation(ProgramId, "Covellite_VertexTexCoord"));
163 if (hTexCoord != -1)
164 {
165 glEnableVertexAttribArray(hTexCoord);
166 glVertexAttribPointer(hTexCoord, 2, GL_FLOAT, GL_FALSE,
167 sizeof(::covellite::api::Vertex), (void *)(sizeof(float) * 4));
168 glVertexAttribDivisor(hTexCoord, 0);
169 }
170
171 const auto hExtra = static_cast<GLuint>(
172 glGetAttribLocation(ProgramId, "Covellite_VertexExtra"));
173 if (hExtra != -1)
174 {
175 glEnableVertexAttribArray(hExtra);
176 glVertexAttribPointer(hExtra, 4, GL_FLOAT, GL_FALSE,
177 sizeof(::covellite::api::Vertex), (void *)(sizeof(float) * 6));
178 glVertexAttribDivisor(hExtra, 0);
179 }
180 }
181
182 void SetInstanceData(
183 const void * _pData,
184 const size_t _Size,
185 const GLsizei _Stride)
186 {
187 using AttributeTypes_t = ::alicorn::extension::std::fast::unordered_map<
188 ::std::string, ::std::pair<GLint, GLenum>>;
189
190 constexpr auto BlockSize = sizeof(float) * 4;
191 const auto BlockCount = _Stride / BlockSize;
192
193 Bind(true);
194 UpdateData(_pData, _Size);
195
196 const auto ProgramId = glGetProgramId();
197
198 const auto GetAttributeTypes = [&](void) -> AttributeTypes_t
199 {
200 // С одной стороны, список атрибутов достаточно формировать один раз
201 // для каждой шейдерной программы, с другой стороны - индексы могут быть
202 // изменены при помощи функции glBindAttribLocation().
203
204 constexpr GLsizei NameBufferSize = 255;
205 GLsizei NameLength = 0;
206 GLint ValueSize = 0;
207 GLchar ValueName[NameBufferSize] = { 0 };
208
209 GLint AttributeCount = 0;
210 glGetProgramiv(ProgramId, GL_ACTIVE_ATTRIBUTES, &AttributeCount);
211
212 AttributeTypes_t Result;
213
214 for (GLuint i = 0; i < static_cast<GLuint>(AttributeCount); i++)
215 {
216 auto iType = static_cast<GLenum>(-1);
217 glGetActiveAttrib(ProgramId, i, NameBufferSize, &NameLength,
218 &ValueSize, &iType, ValueName);
219
220 Result[ValueName] = { i, // Индексы - для отладочных целей
221 (iType == GL_FLOAT_VEC4) ? GL_FLOAT :
222 (iType == GL_INT_VEC4) ? GL_INT : iType };
223 }
224
225 return Result;
226 };
227
228 // Работа через список всех атрибутов понадобилась из-за того, что функция
229 // glGetActiveAttrib() в Windows версии ожидает hInstance, а в Android
230 // версии - индекс атрибута (в Android версии при компиляции шейдера
231 // удаляются все неиспользуемые переменные и hInstance и индекс не совпадают).
232 auto AttributeTypes = GetAttributeTypes();
233
234 for (::std::size_t i = 0; i < BlockCount; i++)
235 {
236 const auto Name = "iValue" + ::std::to_string(i + 1);
237
238 const auto hInstance = static_cast<GLuint>(
239 glGetAttribLocation(ProgramId, Name.c_str()));
240 if (hInstance == -1) continue;
241
242 glEnableVertexAttribArray(hInstance);
243 glVertexAttribPointer(hInstance, 4, AttributeTypes[Name].second,
244 GL_FALSE, _Stride, (void*)(BlockSize * i));
245 glVertexAttribDivisor(hInstance, 1);
246 }
247
248 Bind(false);
249 }
250
251private:
252 const GLenum m_Type;
253 const GLuint m_Index;
254 GLuint m_BufferId;
255
256public:
257 Buffer(
258 const GLenum _Type,
259 const void * _pData,
260 const ::std::size_t _Size,
261 const GLenum _Usage,
262 const GLuint _Index = static_cast<GLuint>(-1)) :
263 m_Type(_Type),
264 m_Index(_Index)
265 {
266 glGenBuffers(1, &m_BufferId);
267 Bind();
268 glBufferData(m_Type, static_cast<GLsizeiptr>(_Size), _pData, _Usage);
269 Bind(false);
270
271 const auto Error = glGetError();
272 if (Error != GL_NO_ERROR)
273 {
274 throw STD_EXCEPTION << "Create buffer error: " << Error;
275 }
276 }
277
278 template<class T>
279 Buffer(const T * _pData, const ::std::size_t _Size) :
280 Buffer(GL_UNIFORM_BUFFER, _pData, _Size, GL_DYNAMIC_DRAW, Support<T>::Index)
281 {
282
283 }
284
285 Buffer(const Buffer &) = delete;
286 Buffer(Buffer &&) = delete;
287 Buffer & operator= (const Buffer &) = delete;
288 Buffer & operator= (Buffer &&) = delete;
289
290 ~Buffer(void) noexcept
291 {
292 glDeleteBuffers(1, &m_BufferId);
293 }
294};
295
296template<class T>
297class OpenGLCommonShader::ConstantBuffer :
298 public GraphicApi::Constants::Data<T>
299{
300public:
301 void Update(void) const override
302 {
303 const auto ProgramId = glGetProgramId();
304
305 Support<T>::Update(ProgramId, Constants::Data<T>::m_Data);
306
307 // 21 Сентябрь 2019 11:58 (unicornum.verum@gmail.com)
308 TODO("Использование uniform-буфера");
309
310 //static const char * Name[] =
311 //{
312 // "cbCameraData",
313 // "cbFogData",
314 // "cbObjectData",
315 // "cbUserData",
316 // "cbMatricesData",
317 // "cbLightsData",
318 //};
319
320 //const auto & Data = Constants::Data<T>::m_Data;
321
322 //if (m_pBuffer == nullptr)
323 //{
324 // m_pBuffer = ::std::make_shared<OpenGLCommonShader::Buffer>(
325 // &Data, sizeof(T));
326 //}
327
328 //m_pBuffer->Update(Name[Support<T>::Index], &Data, sizeof(T));
329 }
330
331private:
332 //mutable ::std::shared_ptr<OpenGLCommonShader::Buffer> m_pBuffer;
333};
334
335OpenGLCommonShader::OpenGLCommonShader(
336 const Data_t & _Data,
337 const String_t & _PreVersion,
338 const ::std::string & _ShaderHeader) :
339 OpenGLCommon{ _Data, _PreVersion },
340 m_pPrograms{ ::std::make_shared<Programs>(_ShaderHeader) }
341{
342 MakeConstants<ConstantBuffer>();
343}
344
345auto OpenGLCommonShader::GetUsingApi(void) const -> String_t /*override*/
346{
347 const auto UsingApi = OpenGLCommon::GetUsingApi();
348
349 LOGGER(Info) << "GL_VENDOR: " <<
350 (const char *)glGetString(GL_VENDOR);
351 LOGGER(Info) << "GL_RENDERER: " <<
352 (const char *)glGetString(GL_RENDERER);
353 LOGGER(Info) << "GL_SHADING_LANGUAGE_VERSION: " <<
354 (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
355
356 GLint ExtensionCount = 0;
357 glGetIntegerv(GL_NUM_EXTENSIONS, &ExtensionCount);
358
359 for (GLint i = 0; i < ExtensionCount; i++)
360 {
361 LOGGER(Info) << "GL_EXTENSIONS: " <<
362 (const char *)glGetStringi(GL_EXTENSIONS, static_cast<GLuint>(i));
363 }
364
365 return UsingApi;
366}
367
368void OpenGLCommonShader::PresentFrame(void) /*override*/
369{
370 OpenGLCommon::PresentFrame();
371
372 //GL_CHECK;
373}
374
375auto OpenGLCommonShader::CreateCamera(const ComponentPtr_t & _pComponent) -> Render_t /*override*/
376{
377 const auto CameraRender = (_pComponent->Kind == uT("Perspective")) ?
378 GetCameraPerspective(_pComponent) : GetCameraOrthographic(_pComponent);
379
380 const auto CameraId = _pComponent->Id;
381
382 return [=](void)
383 {
384 CameraRender();
385 };
386}
387
388class FrameBuffer
389{
390public:
391 inline void Bind(void) const
392 {
393 GLint CurrentFrameBufferId = 0;
394 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &CurrentFrameBufferId);
395 m_CurrentFrameBufferId = static_cast<GLuint>(CurrentFrameBufferId);
396
397 glBindFramebuffer(GL_FRAMEBUFFER, m_Id);
398 }
399
400 inline void Unbind(void) const
401 {
402 glBindFramebuffer(GL_FRAMEBUFFER, m_CurrentFrameBufferId);
403 }
404
405private:
406 GLuint m_Id = 0;
407 mutable GLuint m_CurrentFrameBufferId = 0;
408
409public:
410 FrameBuffer(void) noexcept
411 {
412 glGenFramebuffers(1, &m_Id);
413 }
414 FrameBuffer(const FrameBuffer &) = delete;
415 FrameBuffer(FrameBuffer &&) = delete;
416 FrameBuffer & operator= (const FrameBuffer &) = delete;
417 FrameBuffer & operator= (FrameBuffer &&) = delete;
418 ~FrameBuffer(void) noexcept
419 {
420 glDeleteFramebuffers(1, &m_Id);
421 }
422};
423
424auto OpenGLCommonShader::CreateBkSurface(
425 const ComponentPtr_t & _pComponent) -> Render_t /*override*/
426{
427 // !!!std::tuple не компилируется на Android!!!
428 using Size_t = ::std::pair<GLint, GLint>;
429 using fnBkSurfaceSize_t = ::std::function<Size_t(void)>;
430
431 const auto pBkSurface = _pComponent;
432
433 const fnBkSurfaceSize_t GetBkSurfaceSize = [=](void)
434 {
435 GLint Viewport[4] = { 0 };
436 glGetIntegerv(GL_VIEWPORT, Viewport);
437
438 const int Width = static_cast<int>(Viewport[2]);
439 const int Height = static_cast<int>(Viewport[3]);
440
441 (*pBkSurface)[uT("width")] = Width;
442 (*pBkSurface)[uT("height")] = Height;
443
444 return Size_t{ static_cast<GLint>(Width), static_cast<GLint>(Height) };
445 };
446
447 const auto Size = GetBkSurfaceSize();
448 const auto Width = Size.first;
449 const auto Height = Size.second;
450
451 const auto pFrameBuffer = ::std::make_shared<FrameBuffer>();
452 pFrameBuffer->Bind();
453
454 ::std::vector<GLenum> AttachmentIndexes;
455 ::std::vector<::std::pair<ComponentPtr_t, Texture::Ptr_t>> Textures;
456
457 const auto DoDataTexture = [&](const ComponentPtr_t & _pDataTexture)
458 {
459 (*_pDataTexture)[uT("width")] = Width;
460 (*_pDataTexture)[uT("height")] = Height;
461
462 const Component::Texture TextureData{ *_pDataTexture, uT("diffuse") };
463
464 const auto pTexture = ::std::make_shared<Texture>(TextureData);
465 Textures.push_back({ _pDataTexture, pTexture });
466
467 if (pTexture->m_Format != GL_DEPTH_COMPONENT)
468 {
469 const auto Attachment = GL_COLOR_ATTACHMENT0 +
470 static_cast<unsigned int>(AttachmentIndexes.size());
471 AttachmentIndexes.push_back(Attachment);
472 glFramebufferTexture2D(GL_FRAMEBUFFER, Attachment, GL_TEXTURE_2D,
473 pTexture->m_TextureId, 0);
474 }
475 else
476 {
477 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
478 GL_TEXTURE_2D, pTexture->m_TextureId, 0);
479 }
480
481 (*_pDataTexture)[uT("entity")].Default(pTexture);
482 };
483
484 CapturingServiceComponent::Process(_pComponent,
485 { { uT("Texture"), DoDataTexture } });
486
487 const auto FramebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
488 pFrameBuffer->Unbind();
489
490 if (FramebufferStatus != GL_FRAMEBUFFER_COMPLETE)
491 {
492 throw STD_EXCEPTION << "Create Framebuffer fail: " << _pComponent->Id;
493 }
494
495 return [=](void)
496 {
497 const auto [Width, Height] = GetBkSurfaceSize();
498
499 if (m_IsResizeWindow)
500 {
501 for (const auto & Texture : Textures)
502 {
503 (*Texture.first)[uT("width")] = Width;
504 (*Texture.first)[uT("height")] = Height;
505 Texture.second->MakeContent(Width, Height, nullptr);
506 }
507 }
508
509 pFrameBuffer->Bind();
510 glDrawBuffers(static_cast<GLsizei>(AttachmentIndexes.size()),
511 AttachmentIndexes.data());
512 };
513}
514
515auto OpenGLCommonShader::CreateState(const ComponentPtr_t & _pComponent) -> Render_t /*override*/
516{
517 if (_pComponent->Kind == uT("AlphaTest")) return nullptr;
518
519 return OpenGLCommon::CreateState(_pComponent);
520}
521
522auto OpenGLCommonShader::CreateTexture(const ComponentPtr_t & _pComponent) -> Render_t /*override*/
523{
524 using BufferMapper_t = cbBufferMap_t<const void>;
525
526 const auto pTextureDataComponent = CapturingServiceComponent::Get(_pComponent,
527 { { uT("Texture"), _pComponent } })[0];
528
529 const auto pTextureData =
530 ::std::make_shared<Component::Texture>(*pTextureDataComponent, uT("diffuse"));
531
532 ::std::function<GLint(void)> GetTexMinFilter =
533 [=](void) { return m_TexParameters.MinFilter; };
534
535 Texture::Ptr_t pTexture =
536 (*pTextureDataComponent)[uT("entity")].Default(Texture::Ptr_t{});
537 if (pTexture == nullptr)
538 {
539 pTexture = ::std::make_shared<Texture>(*pTextureData);
540
541 if (pTextureData->IsUsingMipmapping)
542 {
543 GetTexMinFilter = [](void) { return GL_LINEAR_MIPMAP_LINEAR; };
544
545 pTexture->Bind();
546 glGenerateMipmap(GL_TEXTURE_2D);
547 pTexture->Bind(false);
548 }
549 }
550 else
551 {
552 (*pTextureDataComponent)[uT("entity")] = Texture::Ptr_t{};
553 }
554
555 const Render_t TextureRender = [=](void)
556 {
557 const auto Index = pTexture->m_Destination.first;
558
559 glActiveTexture(GL_TEXTURE0 + Index);
560 pTexture->Bind();
561
562 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GetTexMinFilter());
563 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_TexParameters.MagFilter);
564 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_TexParameters.WrapS);
565 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_TexParameters.WrapT);
566
567 const auto ProgramId = glGetProgramId();
568
569 const auto LocationId = glGetUniformLocation(ProgramId,
570 pTexture->m_Destination.second.c_str());
571
572 // -1 означает, что в шейдере такой текстуры нет, но glGetError() при этом
573 // не возвращает ошибку, поэтому просто игнорируем ситуацию.
574 if (LocationId == -1) return;
575
576 glUniform1i(LocationId, Index);
577 };
578
579 if (!(*pTextureDataComponent)[uT("mapper")].IsType<const BufferMapper_t &>())
580 {
581 return TextureRender;
582 }
583
584 const BufferMapper_t cbBufferMapper =
585 (*pTextureDataComponent)[uT("mapper")].Default(BufferMapper_t{});
586 const auto pFrameBuffer = ::std::make_shared<FrameBuffer>();
587
588 return [=](void)
589 {
590 if (cbBufferMapper(nullptr))
591 {
592 pFrameBuffer->Bind();
593 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
594 GL_TEXTURE_2D, pTexture->m_TextureId, 0);
595
596 auto * pData = reinterpret_cast<uint32_t *>(pTexture->m_ReadCopyData.data());
597
598 glReadPixels(0, 0, pTextureData->Width, pTextureData->Height, GL_RGBA,
599 GL_UNSIGNED_BYTE, pData);
600
601 // Изображение в текстуре OpenGL перевернуто по Y, поэтому...
602 for (int y = 0; y < pTextureData->Height / 2; y++)
603 {
604 auto * pLineUp =
605 pData + (static_cast<size_t>(y) * pTextureData->Width);
606 auto * pLineDown =
607 pData + ((static_cast<size_t>(pTextureData->Height) - y - 1) * pTextureData->Width);
608
609 for (int x = 0; x < pTextureData->Width; x++)
610 {
611 auto & Up = *(pLineUp + x);
612 auto & Down = *(pLineDown + x);
613
614 ::std::swap(Up, Down);
615 }
616 }
617
618 cbBufferMapper(pData);
619 pFrameBuffer->Unbind();
620 }
621
622 TextureRender();
623 };
624}
625
626class OpenGLCommonShader::Programs final
627{
628 class Program final
629 {
630 public:
631 void Link(const GLuint _VsShaderId, const GLuint & _PsShaderId)
632 {
633 glAttachShader(Id, _VsShaderId);
634 glAttachShader(Id, _PsShaderId);
635 glLinkProgram(Id);
636
637 GLint Result = GL_FALSE;
638 glGetProgramiv(Id, GL_LINK_STATUS, &Result);
639 if (Result == GL_FALSE)
640 {
641 char InfoLog[512] = { 0 };
642 glGetProgramInfoLog(Id, sizeof(InfoLog), NULL, InfoLog);
643
644 throw STD_EXCEPTION << "Link program fail: " << InfoLog;
645 }
646 }
647
648 public:
649 const GLuint Id;
650
651 public:
652 Program(void) noexcept :
653 Id{ glCreateProgram() }
654 {
655 }
656 Program(const Program &) = delete;
657 Program(Program &&) = delete;
658 Program & operator= (const Program &) = delete;
659 Program & operator= (Program &&) = delete;
660 ~Program(void) noexcept
661 {
662 glDeleteProgram(Id);
663 }
664 };
665
666 class Shader final
667 {
668 public:
669 void Compile(void)
670 {
671 glCompileShader(Id);
672
673 GLint Result = GL_FALSE;
674 glGetShaderiv(Id, GL_COMPILE_STATUS, &Result);
675 if (Result == GL_FALSE)
676 {
677 // Так делать правильно, но это не работает на Android'e:
678 //GLint InfoLogLenght = 0;
679 //glGetShaderiv(Id, GL_INFO_LOG_LENGTH, &InfoLogLenght);
680 //const auto Size = static_cast<::std::size_t>(InfoLogLenght + 1);
681 //::std::vector<GLchar> InfoLog(Size, (GLchar)0x00);
682
683 ::std::vector<GLchar> InfoLog(512, 0x00);
684 glGetShaderInfoLog(Id, InfoLog.size(), nullptr, InfoLog.data());
685
686 throw STD_EXCEPTION << "Compile shader fail "
687 << "[header line: " << GetHeaderLines(m_Header) << "]: "
688 << InfoLog.data();
689 };
690 }
691
692 private:
693 static ::std::size_t GetHeaderLines(const ::std::string & _Header)
694 {
695 ::std::size_t Result = 0;
696
697 auto itPosition = _Header.cbegin();
698 while (true)
699 {
700 itPosition = ::std::find(itPosition, _Header.cend(), '\n');
701 if (itPosition == _Header.cend()) break;
702
703 ++itPosition;
704 Result++;
705 }
706
707 return Result;
708 }
709
710 public:
711 Programs & m_Programs;
712 const GLenum Type;
713 const GLuint Id;
714 const ::std::string m_Header;
715
716 public:
717 Shader(Programs & _Programs, const GLenum _Type,
718 const GLchar * _Header, const GLchar * _Body) :
719 m_Programs{ _Programs },
720 Type{ _Type },
721 Id{ glCreateShader(_Type) },
722 m_Header(_Header)
723 {
724 const auto FullShaderBody = ::std::string{ _Header } +_Body;
725 auto pFullShaderBody = FullShaderBody.c_str();
726 glShaderSource(Id, 1, &pFullShaderBody, nullptr);
727 }
728 Shader(const Shader &) = delete;
729 Shader(Shader &&) = delete;
730 Shader & operator= (const Shader &) = delete;
731 Shader & operator= (Shader &&) = delete;
732 ~Shader(void) noexcept
733 {
734 m_Programs.Clear(Id);
735 glDeleteShader(Id);
736 }
737 };
738
739 using ProgramPtr_t = ::std::shared_ptr<Program>;
740 using Programs_t = ::std::map<GLuint, ::std::map<GLuint, ProgramPtr_t>>;
741 using ShaderPtr_t = ::std::shared_ptr<Shader>;
742
743public:
744 ShaderPtr_t MakeVertex(const Component::Shader & _ShaderData)
745 {
746 using namespace ::alicorn::extension::std;
747
748 const auto HeaderShaderText =
749 ::Predefined +
750 ::Data +
751 _ShaderData.GetInstanceInput(::Input);
752
753 const auto BodyShaderText = _ShaderData.Data;
754
755 return ::std::make_shared<Shader>(*this, GL_VERTEX_SHADER,
756 (m_ShaderHeader +
757 "#define COVELLITE_SHADER_VERTEX\r\n" +
758 ::std::string{ ::std::begin(HeaderShaderText), ::std::end(HeaderShaderText) }).c_str(),
759 (::std::string{ ::std::begin(BodyShaderText), ::std::end(BodyShaderText) } +
760 "out Pixel PixelValue;\r\n"
761 "void main()\r\n"
762 "{\r\n"
763 " Vertex InputData;\r\n"
764 " InputData.Position = Covellite_VertexPosition;\r\n"
765 " InputData.TexCoord = Covellite_VertexTexCoord;\r\n"
766 " InputData.Extra = Covellite_VertexExtra;\r\n" +
767 _ShaderData.GetInstanceCopyData() +
768 " PixelValue = " + _ShaderData.Entry + "(InputData);\r\n"
769 " gl_Position = PixelValue.ScreenPos;\r\n"
770 "}\r\n").c_str());
771 }
772
773 ShaderPtr_t MakePixel(const Component::Shader & _ShaderData)
774 {
775 using namespace ::alicorn::extension::std;
776
777 const auto HeaderShaderText = ::Predefined + ::Data + ::Input;
778 const auto BodyShaderText = ::std::string{
779 _ShaderData.Data.cbegin(), _ShaderData.Data.cend() };
780
781 ::std::string Main;
782
783 if (_ShaderData.ReturnType == "float4" ||
784 _ShaderData.ReturnType == "vec4")
785 {
786 Main =
787 "out vec4 Covellite_OutPixelColor;\r\n"
788 "void main()\r\n"
789 "{\r\n"
790 " Covellite_OutPixelColor = " + _ShaderData.Entry + "(PixelValue);\r\n"
791 "}\r\n";
792 }
793 else if (_ShaderData.ReturnType == "void")
794 {
795 Main =
796 "void main()\r\n"
797 "{\r\n"
798 " " + _ShaderData.Entry + "(PixelValue);\r\n"
799 "}\r\n";
800 }
801 else
802 {
803 Main =
804 "void main()\r\n"
805 "{\r\n"
806 " Covellite_MultiOutPixelColor = " + _ShaderData.Entry + "(PixelValue).Target;\r\n"
807 "}\r\n";
808 }
809
810 return ::std::make_shared<Shader>(*this, GL_FRAGMENT_SHADER,
811 (m_ShaderHeader +
812 "#define COVELLITE_SHADER_PIXEL\r\n" +
813 ::std::string{ ::std::begin(HeaderShaderText), ::std::end(HeaderShaderText) }).c_str(),
814 (BodyShaderText +
815 "in Pixel PixelValue;\r\n" +
816 Main).c_str());
817 }
818
819 void Activate(const ShaderPtr_t & _pShader)
820 {
821 if (_pShader->Type == GL_VERTEX_SHADER) m_VsShaderId = _pShader->Id;
822 if (_pShader->Type == GL_FRAGMENT_SHADER) m_PsShaderId = _pShader->Id;
823 if (m_VsShaderId == InvalidId || m_PsShaderId == InvalidId) return;
824
825 auto & pProgram = m_Programs[m_VsShaderId][m_PsShaderId];
826
827 if (pProgram == nullptr)
828 {
829 pProgram = ::std::make_shared<Program>();
830 pProgram->Link(m_VsShaderId, m_PsShaderId);
831 }
832
833 glUseProgram(pProgram->Id);
834 }
835
836 void Clear(const GLuint _ShaderId)
837 {
838 for (auto & VertexPrograms : m_Programs)
839 {
840 if (VertexPrograms.first == _ShaderId)
841 {
842 for (auto & PixelPrograms : VertexPrograms.second)
843 {
844 PixelPrograms.second.reset();
845 }
846
847 return;
848 }
849
850 for (auto & PixelPrograms : VertexPrograms.second)
851 {
852 if (PixelPrograms.first == _ShaderId)
853 {
854 PixelPrograms.second.reset();
855 }
856 }
857 }
858 }
859
860private:
861 const ::std::string m_ShaderHeader;
862 const GLuint InvalidId = static_cast<GLuint>(-1);
863 GLuint m_VsShaderId = InvalidId;
864 GLuint m_PsShaderId = InvalidId;
865 const uint8_t Align[4] = { 0 };
866 Programs_t m_Programs;
867
868public:
869 explicit Programs(const ::std::string & _ShaderHeader) :
870 m_ShaderHeader(_ShaderHeader + "#define COVELLITE_SHADER_GLSL\r\n")
871 {
872
873 }
874 Programs(const Programs &) = delete;
875 Programs(Programs &&) = delete;
876 Programs & operator= (const Programs &) = delete;
877 Programs & operator= (Programs &&) = delete;
878 ~Programs(void) = default;
879};
880
881auto OpenGLCommonShader::CreateShader(const ComponentPtr_t & _pComponent) -> Render_t /*override*/
882{
883 const auto pShaderDataComponent = CapturingServiceComponent::Get(_pComponent,
884 { { uT("Shader"), _pComponent } })[0];
885
886 const Component::Shader ShaderData{ *pShaderDataComponent, ::Default };
887
888 if (ShaderData.Kind == uT("Vertex"))
889 {
890 const auto pShader = m_pPrograms->MakeVertex(ShaderData);
891 pShader->Compile();
892
893 return [=](void)
894 {
895 m_pPrograms->Activate(pShader);
896 };
897 }
898 else if (ShaderData.Kind == uT("Pixel"))
899 {
900 const auto pShader = m_pPrograms->MakePixel(ShaderData);
901 pShader->Compile();
902
903 return [=](void)
904 {
905 m_pPrograms->Activate(pShader);
906
907# ifndef TEST_CALL_IF
908# define TEST_CALL_IF(n)
909# endif
910
911 TEST_CALL_IF(1) m_pConstants->Update<::Camera>();
912
913# undef TEST_CALL_IF
914 };
915 }
916
917 return nullptr;
918}
919
920auto OpenGLCommonShader::CreateBuffer(const ComponentPtr_t & _pBuffer) -> Render_t /*override*/
921{
922 const auto pBufferData = CapturingServiceComponent::Get(_pBuffer,
923 { { uT("Buffer"), _pBuffer } })[0];
924
925 auto CreateConstantUserBuffer = [&](void) -> Render_t
926 {
927 using Type_t = cbBufferMap_t<void>;
928
929 if (!(*_pBuffer)[uT("mapper")].IsType<const Type_t &>())
930 {
931 throw STD_EXCEPTION << "Unexpected buffer format [" <<
932 "id: " << _pBuffer->Id << ", " <<
933 "type: " << _pBuffer->Type << ", " <<
934 "kind: " << _pBuffer->Kind << "].";
935 }
936
937 const Type_t cbBufferMapper =
938 (*_pBuffer)[uT("mapper")].Default(Type_t{});
939 if (!cbBufferMapper)
940 {
941 throw STD_EXCEPTION << "Unexpected empty mapper: " << _pBuffer->Id;
942 }
943
944 const ::std::size_t BufferSize =
945 (*_pBuffer)[uT("size")].Default((::std::size_t)0);
946 if (BufferSize == 0)
947 {
948 throw STD_EXCEPTION << "Unexpected zero size: " << _pBuffer->Id;
949 }
950
951 const ::std::string cbName =
952 (*_pBuffer)[uT("name")].Default(::std::string{ "cbUserData" });
953
954 const auto pData =
955 ::std::make_shared<BinaryData_t>(BufferSize, (uint8_t)0x00);
956 const auto pBuffer =
957 ::std::make_shared<Buffer>(pData->data(), pData->size());
958
959 return [=](void)
960 {
961 cbBufferMapper(pData->data());
962 pBuffer->UpdateData(cbName, pData->data(), pData->size());
963 };
964 };
965
966 const auto CreateVertexBuffer = [&](void) -> Render_t
967 {
968 using Type_t = ::covellite::api::Vertex;
969 using BufferMapper_t = cbBufferMap_t<Type_t>;
970
971 if (!(*pBufferData)[uT("content")].IsType<Buffer_t<Type_t>>())
972 {
973 return CreateConstantUserBuffer();
974 }
975
976 const Component::Buffer<Type_t> Info{ *pBufferData };
977
978 const BufferMapper_t & cbBufferMapper =
979 (*_pBuffer)[uT("mapper")].Default(BufferMapper_t{});
980
981 if (cbBufferMapper == nullptr)
982 {
983 const auto pBuffer = ::std::make_shared<Buffer>(GL_ARRAY_BUFFER,
984 Info.Data.data(), Info.Data.size() * sizeof(Type_t), GL_STATIC_DRAW);
985
986 return [=](void)
987 {
988 pBuffer->Bind();
989 pBuffer->SetVertexInputData();
990 pBuffer->Bind(false);
991 };
992 }
993
994 const auto pBuffer = ::std::make_shared<Buffer>(GL_ARRAY_BUFFER,
995 Info.Data.data(), Info.Data.size() * sizeof(Type_t), GL_DYNAMIC_DRAW);
996
997 const auto pData = ::std::make_shared<Buffer_t<Type_t>>(Info.Data);
998
999 return [=](void)
1000 {
1001 pBuffer->Bind();
1002
1003 const auto IsDirty = cbBufferMapper(nullptr);
1004 if (IsDirty)
1005 {
1006 cbBufferMapper(pData->data());
1007
1008 pBuffer->UpdateData(pData->data(),
1009 static_cast<size_t>(pData->size() * sizeof(Type_t)));
1010 }
1011
1012 pBuffer->SetVertexInputData();
1013 pBuffer->Bind(false);
1014 };
1015 };
1016
1017 return CreateVertexBuffer();
1018}
1019
1020auto OpenGLCommonShader::CreateTransform(const ComponentPtr_t & _pComponent) -> Render_t /*override*/
1021{
1022 using TransformRender_t = ::std::function<void(void)>;
1023
1024 const auto GetPreRenderDefaultGeometry = [&](void) -> TransformRender_t
1025 {
1026 const auto TransformRender = GetPreRenderGeometry(_pComponent);
1027
1028 return [=](void)
1029 {
1030 auto & World = m_pConstants->Get<::Object>().World;
1031 World = ::glm::identity<::glm::mat4>();
1032 TransformRender(World);
1033 World = ::glm::transpose(World);
1034 };
1035 };
1036
1037 const auto GetPreRenderStaticGeometry = [&](void) -> TransformRender_t
1038 {
1039 ::glm::mat4 World = ::glm::identity<::glm::mat4>();
1040 GetPreRenderGeometry(_pComponent)(World);
1041 World = ::glm::transpose(World);
1042
1043 return [=](void)
1044 {
1045 m_pConstants->Get<::Object>().World = World;
1046 };
1047 };
1048
1049 const auto GetPreRenderBillboardGeometry = [&](void) -> TransformRender_t
1050 {
1051 const auto TransformRender =
1052 OpenGLCommonShader::GetPreRenderBillboardGeometry(_pComponent);
1053
1054 return [=](void)
1055 {
1056 auto & World = m_pConstants->Get<::Object>().World;
1057 World = m_pConstants->Get<::Camera>().View;
1058 TransformRender(World);
1059 World = ::glm::transpose(World);
1060 };
1061 };
1062
1063 // 17 Сентябрь 2019 12:58 (unicornum.verum@gmail.com)
1064 TODO("Тест Transform.Static не проверяет, что матрица меняется у РАЗНЫХ объектов");
1065
1066 // cppcheck-suppress internalAstError
1067 const auto TransformRender =
1068 (_pComponent->Kind == uT("Unknown")) ? GetPreRenderDefaultGeometry() :
1069 (_pComponent->Kind == uT("Static")) ? GetPreRenderStaticGeometry() :
1070 (_pComponent->Kind == uT("Billboard")) ? GetPreRenderBillboardGeometry() :
1071 throw STD_EXCEPTION << "Unexpected transform component: " <<
1072 " [id=" << _pComponent->Id << ", kind=" << _pComponent->Kind << "].";
1073
1074 return [=](void)
1075 {
1076 TransformRender();
1077 m_pConstants->Update<::Object>();
1078 };
1079}
1080
1081auto OpenGLCommonShader::CreatePresentBuffer(const ComponentPtr_t & _pComponent) -> Render_t /*override*/
1082{
1083 using BufferMapperMaxCount_t = ::std::function<bool(void *)>;
1084 using BufferMapperChangeCount_t = ::std::function<bool(void *, ::std::size_t &)>;
1085
1086 ComponentPtr_t pIndexBufferData = _pComponent;
1087 ComponentPtr_t pInstanceBufferData = nullptr;
1088
1089 const auto SaveBuffer = [&](const ComponentPtr_t & _pBufferData)
1090 {
1091 if ((*_pBufferData)[uT("content")].IsType<Buffer_t<int>>())
1092 {
1093 pIndexBufferData = _pBufferData;
1094 }
1095 else if ((*_pBufferData)[uT("mapper")].IsType<const BufferMapperMaxCount_t &>())
1096 {
1097 pInstanceBufferData = _pBufferData;
1098 }
1099 else if ((*_pBufferData)[uT("mapper")].IsType<const BufferMapperChangeCount_t &>())
1100 {
1101 pInstanceBufferData = _pBufferData;
1102 }
1103 else
1104 {
1105 throw STD_EXCEPTION << "Unexpected Presend data component: id = " <<
1106 _pComponent->Id;
1107 }
1108 };
1109
1110 CapturingServiceComponent::Process(_pComponent,
1111 {
1112 { uT("Buffer"), SaveBuffer },
1113 });
1114
1115 const Component::Buffer<int> IndexBufferData{ *pIndexBufferData };
1116
1117 const auto pIndexBuffer = ::std::make_shared<Buffer>(GL_ELEMENT_ARRAY_BUFFER,
1118 IndexBufferData.Data.data(), IndexBufferData.Data.size() * sizeof(int), GL_STATIC_DRAW);
1119 const auto IndexCount = static_cast<GLsizei>(IndexBufferData.Data.size());
1120
1121 if (pInstanceBufferData == nullptr)
1122 {
1123 return [=](void)
1124 {
1125 pIndexBuffer->Bind();
1126 glDrawElements(GL_TRIANGLES, IndexCount, GL_UNSIGNED_INT, nullptr);
1127 pIndexBuffer->Bind(false);
1128 };
1129 }
1130
1131 const ::std::size_t BufferSize = (*pInstanceBufferData)[uT("size")];
1132 if (BufferSize % 16 != 0)
1133 {
1134 throw STD_EXCEPTION << _pComponent->Id << ": size % 16 != 0";
1135 }
1136
1137 const ::std::size_t MaxInstanceCount = (*pInstanceBufferData)[uT("count")];
1138 if (BufferSize % MaxInstanceCount != 0)
1139 {
1140 throw STD_EXCEPTION << _pComponent->Id << ": size % count != 0";
1141 }
1142
1143 const auto Stride = static_cast<GLsizei>(BufferSize / MaxInstanceCount);
1144
1145 const auto pData =
1146 ::std::make_shared<BinaryData_t>(BufferSize, (uint8_t)0x00);
1147 const auto pInstanceBuffer = ::std::make_shared<Buffer>(GL_ARRAY_BUFFER,
1148 nullptr, BufferSize, GL_DYNAMIC_DRAW);
1149
1150 if ((*pInstanceBufferData)[uT("mapper")].IsType<BufferMapperMaxCount_t>())
1151 {
1152 const BufferMapperMaxCount_t cbBufferMapper =
1153 (*pInstanceBufferData)[uT("mapper")];
1154
1155 return [=](void)
1156 {
1157 const auto IsDirty = cbBufferMapper(nullptr);
1158 if (IsDirty)
1159 {
1160 cbBufferMapper(pData->data());
1161 pInstanceBuffer->SetInstanceData(pData->data(), pData->size(), Stride);
1162 }
1163
1164 pIndexBuffer->Bind();
1165 glDrawElementsInstanced(GL_TRIANGLES, IndexCount, GL_UNSIGNED_INT, nullptr,
1166 static_cast<GLsizei>(MaxInstanceCount));
1167 pIndexBuffer->Bind(false);
1168 };
1169 }
1170
1171 const BufferMapperChangeCount_t cbBufferMapper =
1172 (*pInstanceBufferData)[uT("mapper")];
1173
1174 return [=](void)
1175 {
1176 auto InstanceCount = MaxInstanceCount;
1177
1178 const auto IsDirty = cbBufferMapper(nullptr, InstanceCount);
1179 if (IsDirty)
1180 {
1181 cbBufferMapper(pData->data(), InstanceCount);
1182 pInstanceBuffer->SetInstanceData(pData->data(), pData->size(), Stride);
1183 }
1184
1185 InstanceCount = ::std::min(InstanceCount, MaxInstanceCount);
1186
1187 pIndexBuffer->Bind();
1188 glDrawElementsInstanced(GL_TRIANGLES, IndexCount, GL_UNSIGNED_INT, nullptr,
1189 static_cast<GLsizei>(InstanceCount));
1190 pIndexBuffer->Bind(false);
1191 };
1192}
1193
1194auto OpenGLCommonShader::GetCameraCommon(const ComponentPtr_t & _pComponent) -> Render_t
1195{
1196 const auto DisableDepthRender = GetDepthRender(false, false, false);
1197
1198 using Size_t = ::std::tuple<GLint, GLint>;
1199 using fnBkSurfaceSize_t = ::std::function<Size_t(void)>;
1200
1201 const auto pCamera = _pComponent;
1202
1203 const fnBkSurfaceSize_t GetScaleBkSurfaceSize = [=](void)
1204 {
1205 const float Scale = (*pCamera)[uT("scale")];
1206
1207 const auto Width = static_cast<GLint>(Scale * m_Width);
1208 const auto Height = static_cast<GLint>(Scale * (m_Height - m_Top));
1209
1210 return Size_t{ Width, Height };
1211 };
1212
1213 const fnBkSurfaceSize_t GetWindowBkSurfaceSize = [=](void)
1214 {
1215 return Size_t{ m_Width, m_Height - m_Top };
1216 };
1217
1218 const fnBkSurfaceSize_t GetUserBkSurfaceSize = [=](void)
1219 {
1220 const int Width = (*pCamera)[uT("width")];
1221 const int Height = (*pCamera)[uT("height")];
1222
1223 return Size_t{ static_cast<GLint>(Width), static_cast<GLint>(Height) };
1224 };
1225
1226 const auto IsScaleBkSurfaceSize =
1227 (*pCamera)[uT("scale")].IsType<float>();
1228 const auto IsUserBkSurfaceSize =
1229 (*pCamera)[uT("width")].IsType<int>() &&
1230 (*pCamera)[uT("height")].IsType<int>();
1231
1232 const auto GetBkSurfaceSize =
1233 (IsScaleBkSurfaceSize) ? GetScaleBkSurfaceSize :
1234 (IsUserBkSurfaceSize) ? GetUserBkSurfaceSize :
1235 GetWindowBkSurfaceSize;
1236
1237 const auto [Width, Height] = GetBkSurfaceSize();
1238
1239 // Функция должна обязательно вызываться не только при рендеринге, но и при
1240 // создании камеры, чтобы внеэкранная поверхность создавалась с корректными
1241 // размерами.
1242 glViewport(0, 0, Width, Height); // (0, 0) - левый нижний угол!
1243
1244 return [=](void)
1245 {
1246 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1247
1248 DisableDepthRender();
1249
1250 glDisable(GL_BLEND);
1251 glDisable(GL_DITHER);
1252
1253 glEnable(GL_CULL_FACE);
1254 glCullFace(GL_BACK);
1255 glFrontFace(GL_CCW);
1256
1257 const auto [Width, Height] = GetBkSurfaceSize();
1258
1259 glViewport(0, 0, Width, Height); // (0, 0) - левый нижний угол!
1260 };
1261}
1262
1263auto OpenGLCommonShader::GetCameraOrthographic(
1264 const ComponentPtr_t & _pComponent) -> Render_t
1265{
1266 const auto CommonRender = GetCameraCommon(_pComponent);
1267
1268 const auto ServiceComponents = CapturingServiceComponent::Get(_pComponent,
1269 {
1270 { uT("Position"), api::Component::Make({}) },
1271 });
1272
1273 return [=](void)
1274 {
1275 CommonRender();
1276
1277 GLfloat Viewport[4] = { 0 };
1278 glGetFloatv(GL_VIEWPORT, Viewport);
1279
1280 // 25 Август 2019 20:19 (unicornum.verum@gmail.com)
1281 TODO("Тест не проверяет возможность изменения смещения во время работы программы");
1282 const Component::Position Offset{ *ServiceComponents[0] };
1283
1284 auto & Matrix = m_pConstants->Get<::Camera>();
1285
1286 Matrix.Projection = ::glm::transpose(::glm::ortho(
1287 Viewport[0] + Offset.X,
1288 Viewport[0] + Viewport[2] + Offset.X,
1289 Viewport[1] + Viewport[3] + Offset.Y,
1290 Viewport[1] + Offset.Y,
1291 1.0f, -1.0f));
1292 Matrix.View = ::glm::transpose(::glm::identity<::glm::mat4>());
1293 Matrix.ViewInverse = ::glm::transpose(
1294 ::glm::inverse(::glm::identity<::glm::mat4>()));
1295
1296 (*_pComponent)[uT("view")] = Matrix.View;
1297 (*_pComponent)[uT("projection")] = Matrix.Projection;
1298 };
1299}
1300
1301auto OpenGLCommonShader::GetCameraPerspective(
1302 const ComponentPtr_t & _pComponent) -> Render_t
1303{
1304 const auto CommonRender = GetCameraCommon(_pComponent);
1305
1306 const auto ServiceComponents = CapturingServiceComponent::Get(_pComponent,
1307 {
1308 { uT("Position"), api::Component::Make({}) },
1309 { uT("Rotation"), api::Component::Make({}) },
1310 });
1311
1312 return [=](void)
1313 {
1314 CommonRender();
1315
1316 GLfloat Viewport[4] = { 0 };
1317 glGetFloatv(GL_VIEWPORT, Viewport);
1318
1319 auto & Matrix = m_pConstants->Get<::Camera>();
1320
1321 // ************************** Матрица проекции ************************** //
1322
1323 const auto AngleY = (float)(*_pComponent)[uT("fov")].Default(90.0f) *
1324 ::alicorn::extension::cpp::math::Constant<float>::DegreeToRadian;
1325 const float zNear = (*_pComponent)[uT("znear")].Default(0.01f);
1326 const float zFar = (*_pComponent)[uT("zfar")].Default(200.0f);
1327
1328 Matrix.Projection = ::glm::transpose(::glm::perspectiveFovRH(AngleY,
1329 Viewport[2], Viewport[3], zFar, zNear));
1330
1331 // **************************** Матрица вида **************************** //
1332
1333 // Точка, куда смотрит камера - задается как компонент Data.Position.
1334 const Component::Position Look{ *ServiceComponents[0] };
1335
1336 const auto GetEye = [&](void) -> ::glm::vec3
1337 {
1338 // Расстояние от камеры до Look.
1339 const float Distance = (*_pComponent)[uT("distance")].Default(0.0f);
1340
1341 // Точка, где расположена камера - вычисляется на основе Look, Distance и
1342 // компонента Data.Rotation.
1343
1344 const Component::Position Rot{ *ServiceComponents[1] };
1345
1346 ::glm::mat4 Transform = ::glm::identity<::glm::mat4>();
1347
1348 Transform = ::glm::translate(Transform,
1349 ::glm::vec3{ Look.X, Look.Y, Look.Z });
1350 Transform = ::glm::rotate(Transform,
1351 Rot.Z, ::glm::vec3{ 0.0f, 0.0f, 1.0f });
1352 Transform = ::glm::rotate(Transform,
1353 Rot.Y, ::glm::vec3{ 0.0f, 1.0f, 0.0f });
1354 Transform = ::glm::rotate(Transform,
1355 Rot.X, ::glm::vec3{ 1.0f, 0.0f, 0.0f });
1356
1357 return Transform * ::glm::vec4{ Distance + 0.1f, 0.0f, 0.0f, 1.0f };
1358 };
1359
1360 const auto View = ::glm::lookAtRH(
1361 GetEye(),
1362 ::glm::vec3{ Look.X, Look.Y, Look.Z },
1363 ::glm::vec3{ 0.0f, 0.0f, 1.0f }); // Up
1364
1365 Matrix.View = ::glm::transpose(View);
1366 Matrix.ViewInverse = ::glm::transpose(::glm::inverse(View));
1367
1368 (*_pComponent)[uT("view")] = Matrix.View;
1369 (*_pComponent)[uT("projection")] = Matrix.Projection;
1370 };
1371}
1372
1373} // namespace renderer
1374
1375} // namespace api
1376
1377} // namespace covellite
Класс входит в проект Covellite.Api Класс формата вертексного буфера.
Definition Vertex.hpp:34