Khaotic Engine Reborn
Loading...
Searching...
No Matches
render_texture_class Class Reference

Public Member Functions

 render_texture_class (const render_texture_class &)
 
bool Initialize (ID3D11Device *, int, int, float, float, int)
 
void Shutdown ()
 
void SetRenderTarget (ID3D11DeviceContext *)
 
void ClearRenderTarget (ID3D11DeviceContext *, float, float, float, float)
 
ID3D11ShaderResourceView * GetShaderResourceView ()
 
void GetProjectionMatrix (XMMATRIX &)
 
void GetOrthoMatrix (XMMATRIX &)
 
int GetTextureWidth ()
 
int GetTextureHeight ()
 

Detailed Description

Definition at line 20 of file render_texture_class.h.

Constructor & Destructor Documentation

◆ render_texture_class() [1/2]

render_texture_class::render_texture_class ( )

Definition at line 3 of file render_texture_class.cpp.

4{
5 m_renderTargetTexture = 0;
6 m_renderTargetView = 0;
7 m_shaderResourceView = 0;
8 m_depthStencilBuffer = 0;
9 m_depthStencilView = 0;
10}

◆ render_texture_class() [2/2]

render_texture_class::render_texture_class ( const render_texture_class & other)

Definition at line 13 of file render_texture_class.cpp.

14{
15}

◆ ~render_texture_class()

render_texture_class::~render_texture_class ( )

Definition at line 18 of file render_texture_class.cpp.

19{
20}

Member Function Documentation

◆ ClearRenderTarget()

void render_texture_class::ClearRenderTarget ( ID3D11DeviceContext * deviceContext,
float red,
float green,
float blue,
float alpha )

Definition at line 213 of file render_texture_class.cpp.

214{
215 float color[4];
216
217
218 // Setup the color to clear the buffer to.
219 color[0] = red;
220 color[1] = green;
221 color[2] = blue;
222 color[3] = alpha;
223
224 // Clear the back buffer.
225 deviceContext->ClearRenderTargetView(m_renderTargetView, color);
226
227 // Clear the depth buffer.
228 deviceContext->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
229
230 return;
231}

◆ GetOrthoMatrix()

void render_texture_class::GetOrthoMatrix ( XMMATRIX & orthoMatrix)

Definition at line 245 of file render_texture_class.cpp.

246{
247 orthoMatrix = m_orthoMatrix;
248 return;
249}

◆ GetProjectionMatrix()

void render_texture_class::GetProjectionMatrix ( XMMATRIX & projectionMatrix)

Definition at line 238 of file render_texture_class.cpp.

239{
240 projectionMatrix = m_projectionMatrix;
241 return;
242}

◆ GetShaderResourceView()

ID3D11ShaderResourceView * render_texture_class::GetShaderResourceView ( )

Definition at line 233 of file render_texture_class.cpp.

234{
235 return m_shaderResourceView;
236}

◆ GetTextureHeight()

int render_texture_class::GetTextureHeight ( )

Definition at line 258 of file render_texture_class.cpp.

259{
260 return m_textureHeight;
261}

◆ GetTextureWidth()

int render_texture_class::GetTextureWidth ( )

Definition at line 252 of file render_texture_class.cpp.

253{
254 return m_textureWidth;
255}

◆ Initialize()

bool render_texture_class::Initialize ( ID3D11Device * device,
int textureWidth,
int textureHeight,
float screenDepth,
float screenNear,
int format )

Definition at line 22 of file render_texture_class.cpp.

23{
24 Logger::Get().Log("Initializing render_texture_class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
25
26 D3D11_TEXTURE2D_DESC textureDesc;
27 HRESULT result;
28 D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
29 D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
30 D3D11_TEXTURE2D_DESC depthBufferDesc;
31 D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
32 DXGI_FORMAT textureFormat;
33
34
35 // Set the texture format.
36 switch (format)
37 {
38 case 1:
39 {
40 textureFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
41 break;
42 }
43 default:
44 {
45 textureFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
46 break;
47 }
48 }
49
50 // Store the width and height of the render texture.
51 m_textureWidth = textureWidth;
52 m_textureHeight = textureHeight;
53
54 // Initialize the render target texture description.
55 ZeroMemory(&textureDesc, sizeof(textureDesc));
56
57 // Setup the render target texture description.
58 textureDesc.Width = textureWidth;
59 textureDesc.Height = textureHeight;
60 textureDesc.MipLevels = 1;
61 textureDesc.ArraySize = 1;
62 textureDesc.Format = textureFormat;
63 textureDesc.SampleDesc.Count = 1;
64 textureDesc.Usage = D3D11_USAGE_DEFAULT;
65 textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
66 textureDesc.CPUAccessFlags = 0;
67 textureDesc.MiscFlags = 0;
68
69 // Create the render target texture.
70 result = device->CreateTexture2D(&textureDesc, NULL, &m_renderTargetTexture);
71 if (FAILED(result))
72 {
73 Logger::Get().Log("Failed to create render target texture", __FILE__, __LINE__, Logger::LogLevel::Error);
74 return false;
75 }
76
77 // Setup the description of the render target view.
78 renderTargetViewDesc.Format = textureDesc.Format;
79 renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
80 renderTargetViewDesc.Texture2D.MipSlice = 0;
81
82 // Create the render target view.
83 result = device->CreateRenderTargetView(m_renderTargetTexture, &renderTargetViewDesc, &m_renderTargetView);
84 if (FAILED(result))
85 {
86 Logger::Get().Log("Failed to create render target view", __FILE__, __LINE__, Logger::LogLevel::Error);
87 return false;
88 }
89
90 // Setup the description of the shader resource view.
91 shaderResourceViewDesc.Format = textureDesc.Format;
92 shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
93 shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
94 shaderResourceViewDesc.Texture2D.MipLevels = 1;
95
96 // Create the shader resource view.
97 result = device->CreateShaderResourceView(m_renderTargetTexture, &shaderResourceViewDesc, &m_shaderResourceView);
98 if (FAILED(result))
99 {
100 Logger::Get().Log("Failed to create shader resource view", __FILE__, __LINE__, Logger::LogLevel::Error);
101 return false;
102 }
103
104 // Initialize the description of the depth buffer.
105 ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
106
107 // Set up the description of the depth buffer.
108 depthBufferDesc.Width = textureWidth;
109 depthBufferDesc.Height = textureHeight;
110 depthBufferDesc.MipLevels = 1;
111 depthBufferDesc.ArraySize = 1;
112 depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
113 depthBufferDesc.SampleDesc.Count = 1;
114 depthBufferDesc.SampleDesc.Quality = 0;
115 depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
116 depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
117 depthBufferDesc.CPUAccessFlags = 0;
118 depthBufferDesc.MiscFlags = 0;
119
120 // Create the texture for the depth buffer using the filled out description.
121 result = device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer);
122 if (FAILED(result))
123 {
124 Logger::Get().Log("Failed to create depth buffer texture", __FILE__, __LINE__, Logger::LogLevel::Error);
125 return false;
126 }
127
128 // Initailze the depth stencil view description.
129 ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
130
131 // Set up the depth stencil view description.
132 depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
133 depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
134 depthStencilViewDesc.Texture2D.MipSlice = 0;
135
136 // Create the depth stencil view.
137 result = device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView);
138 if (FAILED(result))
139 {
140 Logger::Get().Log("Failed to create depth stencil view", __FILE__, __LINE__, Logger::LogLevel::Error);
141 return false;
142 }
143
144 // Setup the viewport for rendering.
145 m_viewport.Width = (float)textureWidth;
146 m_viewport.Height = (float)textureHeight;
147 m_viewport.MinDepth = 0.0f;
148 m_viewport.MaxDepth = 1.0f;
149 m_viewport.TopLeftX = 0;
150 m_viewport.TopLeftY = 0;
151
152 // Setup the projection matrix.
153 m_projectionMatrix = XMMatrixPerspectiveFovLH((3.141592654f / 4.0f), ((float)textureWidth / (float)textureHeight), screenNear, screenDepth);
154
155 // Create an orthographic projection matrix for 2D rendering.
156 m_orthoMatrix = XMMatrixOrthographicLH((float)textureWidth, (float)textureHeight, screenNear, screenDepth);
157
158 Logger::Get().Log("render_texture_class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
159
160 return true;
161}
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158

◆ SetRenderTarget()

void render_texture_class::SetRenderTarget ( ID3D11DeviceContext * deviceContext)

Definition at line 202 of file render_texture_class.cpp.

203{
204 // Bind the render target view and depth stencil buffer to the output render pipeline.
205 deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView);
206
207 // Set the viewport.
208 deviceContext->RSSetViewports(1, &m_viewport);
209
210 return;
211}

◆ Shutdown()

void render_texture_class::Shutdown ( )

Definition at line 163 of file render_texture_class.cpp.

164{
165 Logger::Get().Log("Shutting down render_texture_class", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
166
167 if (m_depthStencilView)
168 {
169 m_depthStencilView->Release();
170 m_depthStencilView = 0;
171 }
172
173 if (m_depthStencilBuffer)
174 {
175 m_depthStencilBuffer->Release();
176 m_depthStencilBuffer = 0;
177 }
178
179 if (m_shaderResourceView)
180 {
181 m_shaderResourceView->Release();
182 m_shaderResourceView = 0;
183 }
184
185 if (m_renderTargetView)
186 {
187 m_renderTargetView->Release();
188 m_renderTargetView = 0;
189 }
190
191 if (m_renderTargetTexture)
192 {
193 m_renderTargetTexture->Release();
194 m_renderTargetTexture = 0;
195 }
196
197 Logger::Get().Log("render_texture_class shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
198
199 return;
200}

The documentation for this class was generated from the following files: