-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureViewRenderer.cpp
123 lines (100 loc) · 3.34 KB
/
TextureViewRenderer.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
#include "TextureViewRenderer.hpp"
#include "Engine.hpp"
#include "Painter.hpp"
#include "../inanity/graphics/Device.hpp"
#include "../inanity/graphics/Context.hpp"
#include "../inanity/graphics/FrameBuffer.hpp"
#include "../inanity/graphics/Texture.hpp"
#include "../inanity/graphics/SamplerState.hpp"
#include "../inanity/graphics/RawTextureData.hpp"
#include "../inanity/graphics/shaders/Uniform.ipp"
BEGIN_INANITY_OIL
using namespace Inanity::Graphics;
TextureViewRenderer::TextureViewRenderer(ptr<Engine> engine) :
engine(engine), offset(0, 0), scale(1), mipMode(0), mipLod(0), mipBias(0),
colorTransform(Graphics::identity_mat<float, 4>()), colorOffset(0, 0, 0, 0)
{
}
TextureViewRenderer::~TextureViewRenderer() {}
void TextureViewRenderer::Render()
{
ptr<Context> context = engine->GetGraphicsContext();
context->ClearColor(0, vec4(0, 1, 1, 1));
if(rawTextureData)
{
if(!samplerState)
samplerState = engine->GetGraphicsDevice()->CreateSamplerState(samplerSettings);
ptr<Painter::TextureQuad> textureQuad = engine->GetPainter()->textureQuad;
float viewportWidth = (float)context->GetViewportWidth();
float viewportHeight = (float)context->GetViewportHeight();
float imageWidth = (float)rawTextureData->GetImageWidth();
float imageHeight = (float)rawTextureData->GetImageHeight();
float scaleXCoef = 1.0f / (imageWidth * scale);
float scaleYCoef = 1.0f / (imageHeight * scale);
textureQuad->uTextureScale.Set(vec2(viewportWidth * scaleXCoef, viewportHeight * scaleYCoef));
textureQuad->uBackgroundScale.Set(vec2(viewportWidth * 0.1f, viewportHeight * 0.1f));
textureQuad->uOffset.Set(vec2(-offset.x * scaleXCoef, -offset.y * scaleYCoef));
textureQuad->uLod.Set(mipLod);
textureQuad->uBias.Set(mipBias);
textureQuad->uColorTransform.Set(colorTransform);
textureQuad->uColorOffset.Set(colorOffset);
Painter::TextureQuad::Let tql(context, textureQuad, texture, samplerState, (Painter::TextureQuad::Let::MipMode)mipMode);
context->Draw();
}
}
void TextureViewRenderer::SetTexture(ptr<RawTextureData> rawTextureData)
{
this->rawTextureData = rawTextureData;
this->texture = engine->GetGraphicsDevice()->CreateStaticTexture(rawTextureData, SamplerSettings());
}
void TextureViewRenderer::SetOffset(const vec2& offset)
{
this->offset = offset;
}
void TextureViewRenderer::SetScale(float scale)
{
this->scale = scale;
}
void TextureViewRenderer::SetFilter(int filterType, int filterValue)
{
SamplerSettings::Filter filter = (SamplerSettings::Filter)filterValue;
switch(filterType)
{
case 0:
samplerSettings.minFilter = filter;
break;
case 1:
samplerSettings.mipFilter = filter;
break;
case 2:
samplerSettings.magFilter = filter;
break;
}
samplerState = nullptr;
}
void TextureViewRenderer::SetTile(bool tile)
{
samplerSettings.SetWrap(tile ? SamplerSettings::wrapRepeat : SamplerSettings::wrapBorder);
samplerState = nullptr;
}
void TextureViewRenderer::SetMipMode(int mipMode)
{
this->mipMode = mipMode;
}
void TextureViewRenderer::SetMipLod(float mipLod)
{
this->mipLod = mipLod;
}
void TextureViewRenderer::SetMipBias(float mipBias)
{
this->mipBias = mipBias;
}
void TextureViewRenderer::SetColorTransform(const Graphics::mat4x4& colorTransform)
{
this->colorTransform = colorTransform;
}
void TextureViewRenderer::SetColorOffset(const Graphics::vec4& colorOffset)
{
this->colorOffset = colorOffset;
}
END_INANITY_OIL