Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
Skybox.cpp
Ir a la documentación de este archivo.
1
7#include "Device.h"
8#include "DeviceContext.h"
10
11
12HRESULT
13Skybox::init(Device& device, DeviceContext* deviceContext, Texture& cubemap) {
14 destroy();
15 // Cargar el cubemap
16 m_skyboxTexture = cubemap;
17
18 // 1) Geometría (cubo)
19 // Cubo unitario centrado en origen. (tamaño no importa si quitas traslación)
20 const SkyboxVertex vertices[] = {
21 {-1,-1,-1}, {-1,+1,-1}, {+1,+1,-1}, {+1,-1,-1}, // back
22 {-1,-1,+1}, {-1,+1,+1}, {+1,+1,+1}, {+1,-1,+1}, // front
23 };
24
25 const unsigned int indices[] =
26 {
27 // back (-Z)
28 0,1,2, 0,2,3,
29 // front (+Z)
30 4,6,5, 4,7,6,
31 // left (-X)
32 4,5,1, 4,1,0,
33 // right (+X)
34 3,2,6, 3,6,7,
35 // top (+Y)
36 1,5,6, 1,6,2,
37 // bottom (-Y)
38 4,0,3, 4,3,7
39 };
40
41 // Load Model
42 m_skybox = EU::MakeShared<Actor>(device);
43
44 if (!m_skybox.isNull()) {
45 // Crear vertex buffer y index buffer para el skybox
46 std::vector<MeshComponent> skybox;
47 m_cubeModel = new Model3D("Skybox", vertices, indices);
48
49 skybox = m_cubeModel->GetMeshes();
50
51 // No texture loading
52
53 m_skybox->setMesh(device, skybox);
54 m_skybox->setName("skybox");
55 }
56 else {
57 ERROR("Skybox", "Init", "Failed to create Skybox Actor.");
58 return E_FAIL;
59 }
60
61
62 // Define the input layout
63 LayoutBuilder builder;
64
65 builder.Add("POSITION", DXGI_FORMAT_R32G32B32_FLOAT);
66
67 HRESULT hr = S_OK;
68 // Create the Shader Program
69 hr = m_shaderProgram.init(device, "Skybox.hlsl", builder);
70 if (FAILED(hr)) {
71 ERROR("Skybox", "init",
72 ("Failed to initialize ShaderProgram. HRESULT: " + std::to_string(hr)).c_str());
73 return hr;
74 }
75
76 // Create the constant buffers
77 hr = m_constantBuffer.init(device, sizeof(CBSkybox)); // View
78 if (FAILED(hr)) {
79 ERROR("Skybox", "init",
80 ("Failed to initialize NeverChanges Buffer. HRESULT: " + std::to_string(hr)).c_str());
81 return hr;
82 }
83
84 // Init SamplerState
85 hr = m_samplerState.init(device);
86 if (FAILED(hr)) {
87 ERROR("Skybox", "init", "Failed to create new SamplerState");
88 }
89
90 // Init Rasterizer
91 hr = m_rasterizerState.init(device, D3D11_FILL_SOLID, D3D11_CULL_FRONT, false, true);
92 if (FAILED(hr)) {
93 ERROR("Skybox", "init", "Failed to create new RasterizerState");
94 }
95
96 // Init DepthStencilState
97 hr = m_depthStencilState.init(device, true,
98 D3D11_DEPTH_WRITE_MASK_ZERO,
99 D3D11_COMPARISON_LESS_EQUAL);
100 if (FAILED(hr)) {
101 ERROR("Skybox", "init", "Failed to create new DepthStencilState");
102 }
103
104 return S_OK;
105}
106
107void Skybox::update(DeviceContext& deviceContext, Camera& camera) {
108 // 2) View sin traslación + VP (SOLO una transpuesta al final)
109 XMMATRIX viewNoT = camera.GetViewNoTranslation();
110 XMMATRIX vp = viewNoT * camera.getProj();
111 CBSkybox cb{};
112 cb.mviewProj = XMMatrixTranspose(vp);
113 m_constantBuffer.update(deviceContext, nullptr, 0, nullptr, &cb, 0, 0);
114}
115
116void
118 // Guard: si no se inicializó bien, no intentes renderizar
120
121 // 1) States del skybox
122 m_rasterizerState.render(deviceContext);
123 m_depthStencilState.render(deviceContext, 0, false);
124
125 m_constantBuffer.render(deviceContext, 0, 1);
126
127 // 3) Shader + sampler (slot 10)
128 m_shaderProgram.render(deviceContext);
129 m_samplerState.render(deviceContext, 10, 1);
130
131 // 4) IMPORTANTÍSIMO: bindea cubemap ANTES del draw (slot 10)
132 m_skyboxTexture.render(deviceContext, 10, 1);
133
134 // 5) Asegura IA (topology + VB/IB) antes del DrawIndexed
135 m_skybox->renderForSkybox(deviceContext);
136
137 // 3) Limpia t0 para evitar mismatch por shaders 2D que usen t0
138 ID3D11ShaderResourceView* nullSRV[1] = { nullptr };
139 deviceContext.m_deviceContext->PSSetShaderResources(10, 1, nullSRV);
140
141 // 5) Unbind t10
142 deviceContext.m_deviceContext->PSSetShaderResources(0, 1, nullSRV);
143}
144
145
Declara la API de DeviceContext dentro del subsistema Core.
Declara la API de Device dentro del subsistema Core.
Declara la API de LayoutBuilder dentro del subsistema Utilities.
#define ERROR(classObj, method, errorMSG)
Declara la API de Skybox dentro del subsistema Utilities.
void setName(const std::string &name)
Establece el nombre del actor.
Definition Actor.h:118
void setMesh(Device &device, std::vector< MeshComponent > meshes)
Establece las mallas del actor.
Definition Actor.cpp:152
void renderForSkybox(DeviceContext &deviceContext)
Definition Actor.cpp:119
void update(DeviceContext &deviceContext, ID3D11Resource *pDstResource, unsigned int DstSubresource, const D3D11_BOX *pDstBox, const void *pSrcData, unsigned int SrcRowPitch, unsigned int SrcDepthPitch)
Actualiza el contenido del buffer (típicamente mediante UpdateSubresource).
Definition Buffer.cpp:76
HRESULT init(Device &device, const MeshComponent &mesh, unsigned int bindFlag)
Inicializa el buffer como Vertex o Index Buffer usando un MeshComponent.
Definition Buffer.cpp:11
void render(DeviceContext &deviceContext, unsigned int StartSlot, unsigned int NumBuffers, bool setPixelShader=false, DXGI_FORMAT format=DXGI_FORMAT_UNKNOWN)
Enlaza el buffer a la etapa correspondiente del pipeline para el frame de render.
Definition Buffer.cpp:102
XMMATRIX getProj() const
Matriz Projection (vista->clip).
Definition Camera.h:119
XMMATRIX GetViewNoTranslation() const
View sin traslación (solo rotación).
Definition Camera.h:128
HRESULT init(Device &device, bool depthEnable, D3D11_DEPTH_WRITE_MASK writeMask, D3D11_COMPARISON_FUNC depthFunc)
Crea el objeto ID3D11DepthStencilState a partir de flags comunes.
void render(DeviceContext &deviceContext, unsigned int stencilRef=0, bool reset=false)
Aplica el estado de profundidad/esténcil al contexto (OMSetDepthStencilState).
ID3D11DeviceContext * m_deviceContext
Puntero al contexto inmediato de Direct3D 11.
Encapsula un ID3D11Device y facilita la creación de recursos gráficos en Direct3D 11.
Definition Device.h:21
bool isNull() const
Comprobar si el puntero es nulo.
LayoutBuilder & Add(const char *semantic, DXGI_FORMAT format, UINT semanticIndex=0, UINT inputSlot=0, UINT alignedByteOffset=D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_CLASSIFICATION slotClass=D3D11_INPUT_PER_VERTEX_DATA, UINT instanceStepRate=0)
const std::vector< MeshComponent > & GetMeshes() const
Definition Model3D.h:52
void render(DeviceContext &deviceContext)
Aplica el Rasterizer State al contexto de dispositivo.
HRESULT init(Device device)
Inicializa el Rasterizer State.
HRESULT init(Device &device)
Inicializa el Sampler State con una configuración predeterminada.
void render(DeviceContext &deviceContext, unsigned int StartSlot, unsigned int NumSamplers)
Asigna el Sampler State a la etapa de Pixel Shader.
void render(DeviceContext &deviceContext)
Aplica el Vertex Shader, Pixel Shader e Input Layout al pipeline.
HRESULT init(Device &device, const std::string &fileName, LayoutBuilder layoutBuilder)
Inicializa el programa de shaders desde un archivo HLSL.
Buffer m_constantBuffer
Definition Skybox.h:41
HRESULT init(Device &device, DeviceContext *deviceContext, Texture &cubemap)
Definition Skybox.cpp:13
void destroy()
Definition Skybox.h:37
EU::TSharedPointer< Actor > m_skybox
Definition Skybox.h:47
Texture m_skyboxTexture
Definition Skybox.h:45
ShaderProgram m_shaderProgram
Definition Skybox.h:40
SamplerState m_samplerState
Definition Skybox.h:42
Model3D * m_cubeModel
Definition Skybox.h:46
DepthStencilState m_depthStencilState
Definition Skybox.h:44
void render(DeviceContext &deviceContext)
Definition Skybox.cpp:117
void update(DeviceContext &deviceContext, Camera &camera)
Definition Skybox.cpp:107
RasterizerState m_rasterizerState
Definition Skybox.h:43
Encapsula una textura 2D en Direct3D 11, incluyendo su recurso y vista como Shader Resource.
Definition Texture.h:24
void render(DeviceContext &deviceContext, unsigned int StartSlot, unsigned int NumViews)
Asigna la textura al pipeline de render.
Definition Texture.cpp:313
ID3D11ShaderResourceView * m_textureFromImg
Vista de la textura como recurso de shader.
Definition Texture.h:170
XMMATRIX mviewProj