-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSkybox.cpp
112 lines (97 loc) · 2.87 KB
/
Skybox.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
#include "Skybox.h"
Skybox::Skybox() : vertexBuffer(0)
{
}
Skybox::~Skybox()
{
for (int i = 0; i < 6; ++i)
{
textures[i]->Release();
textures[i] = 0;
}
if (vertexBuffer)
{
vertexBuffer->Release();
vertexBuffer = NULL;
}
}
bool Skybox::generate()
{
float size = 100;
D3D::EVertex mesh[24] =
{
//Front
{ -size,-size,size, 0,1 },
{ -size,size,size, 0,0 },
{ size,-size,size, 1,1 },
{ size,size,size, 1,0 },
//Back
{ size, -size, -size, 0,1 },
{ size, size, -size, 0,0 },
{ -size, -size, -size, 1,1 },
{ -size, size, -size, 1,0 },
//Left
{ -size, -size, -size, 0,1 },
{ -size, size, -size, 0,0 },
{ -size, -size, size, 1,1 },
{ -size, size, size, 1,0 },
//Right
{ size, -size, size, 0,1 },
{ size, size, size, 0,0 },
{ size, -size, -size, 1,1 },
{ size, size, -size, 1,0 },
//Top
{ -size, size, size, 0,1 },
{ -size, size, -size, 0,0 },
{ size, size, size, 1,1 },
{ size, size, -size, 1,0 },
//Bottom
{ -size, -size, -size, 0,1 },
{ -size, -size, size, 0,0 },
{ size, -size, -size, 1,1 },
{ size, -size, size, 1,0 }
};
HRESULT hRet;
// IDirect3DDevice9* Device = graphics.GetDevice();
hRet = Device->CreateVertexBuffer((int)sizeof(D3D::EVertex) * 24, 0, D3D::EVertex::FVF,
D3DPOOL_MANAGED, &vertexBuffer, 0);
if (FAILED(hRet))
{
MessageBox(NULL, "Failed to create skybox VB", "Error", MB_OK);
return false;
}
void* pVertices = NULL;
vertexBuffer->Lock(0, (UINT)sizeof(D3D::EVertex) * 24, (void**)&pVertices, 0);
memcpy(pVertices, mesh, sizeof(D3D::EVertex) * 24);
vertexBuffer->Unlock();
////load textures
hRet = D3DXCreateTextureFromFile(Device, "textures/front.png", &textures[0]);
hRet = D3DXCreateTextureFromFile(Device, "textures/back.png", &textures[1]);
hRet = D3DXCreateTextureFromFile(Device, "textures/left.png", &textures[2]);
hRet = D3DXCreateTextureFromFile(Device, "textures/right.png", &textures[3]);
hRet = D3DXCreateTextureFromFile(Device, "textures/top.png", &textures[4]);
hRet = D3DXCreateTextureFromFile(Device, "textures/bottom.png", &textures[5]);
if (FAILED(hRet))
MessageBox(NULL, "Skybox Texture BAD", "Error with Skybox", MB_OK);
return true;
}
void Skybox::render()
{
// IDirect3DDevice9* Device = graphics.GetDevice();
// disable lighting and z-buffer
Device->SetRenderState(D3DRS_LIGHTING, false);
Device->SetRenderState(D3DRS_ZENABLE, false);
// set address mode to clamp to hide of seams
Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
Device->SetFVF(D3D::EVertex::FVF);
Device->SetStreamSource(0, vertexBuffer, 0, sizeof(D3D::EVertex));
for (int i = 0; i < 6; ++i)
{
Device->SetTexture(0, textures[i]);
Device->DrawPrimitive(D3DPT_TRIANGLESTRIP, (int)i * 4, 2);
}
// enable lighting and z-buffer
Device->SetRenderState(D3DRS_LIGHTING, true);
Device->SetRenderState(D3DRS_ZENABLE, true);
}