libgui/source/gui/GridBackground.cpp

105 lines
3.7 KiB
C++
Raw Normal View History

2022-02-05 14:28:08 +01:00
#include "utils/utils.h"
2018-06-21 20:44:58 +02:00
#include <gui/GridBackground.h>
2019-08-14 23:24:55 +02:00
#include <gui/video/CVideo.h>
#include <gui/video/shaders/Shader3D.h>
2017-10-29 10:28:14 +01:00
2022-02-05 14:28:08 +01:00
static const float bgRepeat = 1000.0f;
2017-10-29 10:28:14 +01:00
static const float bgTexRotate = 39.0f;
GridBackground::GridBackground(GuiImageData *img)
2022-02-05 14:28:08 +01:00
: GuiImage(img) {
colorIntensity = glm::vec4(1.0f, 1.0f, 1.0f, 0.9f);
alphaFadeOut = glm::vec4(0.0f);
2017-10-29 10:28:14 +01:00
distanceFadeOut = 0.15f;
vtxCount = 4;
//! texture and vertex coordinates
2022-02-05 14:28:08 +01:00
float *m_posVtxs = (float *) memalign(GX2_VERTEX_BUFFER_ALIGNMENT, vtxCount * Shader3D::cuVertexAttrSize);
2020-08-13 12:38:07 +02:00
float *m_texCoords = (float *) memalign(GX2_VERTEX_BUFFER_ALIGNMENT, vtxCount * Shader3D::cuTexCoordAttrSize);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (m_posVtxs) {
2022-02-05 14:28:08 +01:00
int32_t i = 0;
2018-06-21 20:44:58 +02:00
m_posVtxs[i++] = -1.0f;
m_posVtxs[i++] = 0.0f;
m_posVtxs[i++] = 1.0f;
2020-08-13 12:38:07 +02:00
m_posVtxs[i++] = 1.0f;
2018-06-21 20:44:58 +02:00
m_posVtxs[i++] = 0.0f;
m_posVtxs[i++] = 1.0f;
2020-08-13 12:38:07 +02:00
m_posVtxs[i++] = 1.0f;
2018-06-21 20:44:58 +02:00
m_posVtxs[i++] = 0.0f;
m_posVtxs[i++] = -1.0f;
m_posVtxs[i++] = -1.0f;
m_posVtxs[i++] = 0.0f;
m_posVtxs[i++] = -1.0f;
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, m_posVtxs, vtxCount * Shader3D::cuVertexAttrSize);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
if (m_texCoords) {
2017-10-29 10:28:14 +01:00
glm::vec2 texCoordVec[4];
2018-06-21 20:44:58 +02:00
texCoordVec[0][0] = -0.5f * bgRepeat;
texCoordVec[0][1] = 0.5f * bgRepeat;
texCoordVec[1][0] = 0.5f * bgRepeat;
texCoordVec[1][1] = 0.5f * bgRepeat;
texCoordVec[2][0] = 0.5f * bgRepeat;
texCoordVec[2][1] = -0.5f * bgRepeat;
texCoordVec[3][0] = -0.5f * bgRepeat;
texCoordVec[3][1] = -0.5f * bgRepeat;
2017-10-29 10:28:14 +01:00
const float cosRot = cosf(DegToRad(bgTexRotate));
const float sinRot = sinf(DegToRad(bgTexRotate));
2022-02-05 14:28:08 +01:00
glm::mat2 texRotateMtx({cosRot, -sinRot,
sinRot, cosRot});
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < 4; i++) {
2022-02-05 14:28:08 +01:00
texCoordVec[i] = texRotateMtx * texCoordVec[i];
2020-08-13 12:38:07 +02:00
m_texCoords[i * 2 + 0] = texCoordVec[i][0];
m_texCoords[i * 2 + 1] = texCoordVec[i][1];
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, m_texCoords, vtxCount * Shader3D::cuTexCoordAttrSize);
2017-10-29 10:28:14 +01:00
}
//! assign to internal variables which are const but oh well
2022-02-05 14:28:08 +01:00
posVtxs = m_posVtxs;
2017-10-29 10:28:14 +01:00
texCoords = m_texCoords;
}
2018-06-21 20:44:58 +02:00
GridBackground::~GridBackground() {
2017-10-29 10:28:14 +01:00
//! remove image so it can not be drawn anymore from this point on
imageData = NULL;
//! main image vertexes
2020-08-13 12:38:07 +02:00
if (posVtxs) {
free((void *) posVtxs);
2017-10-29 10:28:14 +01:00
posVtxs = NULL;
}
2020-08-13 12:38:07 +02:00
if (texCoords) {
free((void *) texCoords);
2017-10-29 10:28:14 +01:00
texCoords = NULL;
}
}
2020-08-13 12:38:07 +02:00
void GridBackground::draw(CVideo *pVideo, const glm::mat4 &modelView) {
2017-10-29 10:28:14 +01:00
//! first setup 2D GUI positions
2020-08-13 12:38:07 +02:00
float currScaleX = bgRepeat * scaleX * (float) getWidth() * pVideo->getWidthScaleFactor();
2018-06-21 20:44:58 +02:00
float currScaleY = 1.0f;
2020-08-13 12:38:07 +02:00
float currScaleZ = bgRepeat * scaleZ * (float) getHeight() * pVideo->getDepthScaleFactor();
2017-10-29 10:28:14 +01:00
m_modelView = glm::scale(modelView, glm::vec3(currScaleX, currScaleY, currScaleZ));
colorIntensity[3] = getAlpha();
Shader3D::instance()->setShaders();
Shader3D::instance()->setTextureAndSampler(imageData->getTexture(), imageData->getSampler());
Shader3D::instance()->setProjectionMtx(pVideo->getProjectionMtx());
Shader3D::instance()->setViewMtx(pVideo->getViewMtx());
Shader3D::instance()->setModelViewMtx(m_modelView);
Shader3D::instance()->setDistanceFadeOut(distanceFadeOut);
Shader3D::instance()->setAlphaFadeOut(alphaFadeOut);
Shader3D::instance()->setColorIntensity(colorIntensity);
Shader3D::instance()->setAttributeBuffer(vtxCount, posVtxs, texCoords);
2018-06-21 20:44:58 +02:00
Shader3D::instance()->draw(GX2_PRIMITIVE_MODE_QUADS, vtxCount);
2017-10-29 10:28:14 +01:00
}