libgui/source/gui/GridBackground.cpp

106 lines
3.6 KiB
C++
Raw Normal View History

2018-06-21 20:44:58 +02:00
#include <gui/GridBackground.h>
#include <video/CVideo.h>
#include <video/shaders/Shader3D.h>
2017-10-29 10:28:14 +01:00
static const float bgRepeat = 1000.0f;
static const float bgTexRotate = 39.0f;
GridBackground::GridBackground(GuiImageData *img)
2018-06-21 20:44:58 +02:00
: GuiImage(img) {
2017-10-29 10:28:14 +01:00
colorIntensity = glm::vec4(1.0f, 1.0f, 1.0f, 0.9f);
alphaFadeOut = glm::vec4(0.0f);
distanceFadeOut = 0.15f;
vtxCount = 4;
//! texture and vertex coordinates
2018-06-21 20:44:58 +02:00
float *m_posVtxs = (float*)memalign(GX2_VERTEX_BUFFER_ALIGNMENT, vtxCount * Shader3D::cuVertexAttrSize);
float *m_texCoords = (float*)memalign(GX2_VERTEX_BUFFER_ALIGNMENT, vtxCount * Shader3D::cuTexCoordAttrSize);
if(m_posVtxs) {
int32_t i = 0;
m_posVtxs[i++] = -1.0f;
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;
m_posVtxs[i++] = 1.0f;
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
}
2018-06-21 20:44:58 +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));
glm::mat2 texRotateMtx({
cosRot, -sinRot,
sinRot, cosRot
});
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; i < 4; i++) {
2017-10-29 10:28:14 +01:00
texCoordVec[i] = texRotateMtx * texCoordVec[i];
m_texCoords[i*2 + 0] = texCoordVec[i][0];
m_texCoords[i*2 + 1] = texCoordVec[i][1];
}
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
posVtxs = m_posVtxs;
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
2018-06-21 20:44:58 +02:00
if(posVtxs) {
2017-10-29 10:28:14 +01:00
free((void*)posVtxs);
posVtxs = NULL;
}
2018-06-21 20:44:58 +02:00
if(texCoords) {
2017-10-29 10:28:14 +01:00
free((void*)texCoords);
texCoords = NULL;
}
}
2018-06-21 20:44:58 +02:00
void GridBackground::draw(CVideo *pVideo, const glm::mat4 & modelView) {
2017-10-29 10:28:14 +01:00
//! first setup 2D GUI positions
2018-06-21 20:44:58 +02:00
float currScaleX = bgRepeat * scaleX * (float)getWidth() * pVideo->getWidthScaleFactor();
float currScaleY = 1.0f;
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
}