-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexturedCube.cpp
More file actions
159 lines (131 loc) · 5.14 KB
/
TexturedCube.cpp
File metadata and controls
159 lines (131 loc) · 5.14 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "TexturedCube.h"
#include "GraphicsEngine.h"
#include "ShaderLibrary.h"
#include "SceneCameraHandler.h"
#include "TextureManager.h"
TexturedCube::TexturedCube(String name) : Cube(name, true)
{
ShaderNames shaderNames;
void* shaderByteCode = NULL;
size_t sizeShader = 0;
ShaderLibrary::getInstance()->requestVertexShaderData(shaderNames.TEXTURED_VERTEX_SHADER_NAME, &shaderByteCode, &sizeShader);
//create buffers for drawing. vertex data that needs to be drawn are temporarily placed here.
Vector3D position_list[] =
{
{ Vector3D(-0.5f,-0.5f,-0.5f)},
{ Vector3D(-0.5f,0.5f,-0.5f) },
{ Vector3D(0.5f,0.5f,-0.5f) },
{ Vector3D(0.5f,-0.5f,-0.5f)},
//BACK FACE
{ Vector3D(0.5f,-0.5f,0.5f) },
{ Vector3D(0.5f,0.5f,0.5f) },
{ Vector3D(-0.5f,0.5f,0.5f)},
{ Vector3D(-0.5f,-0.5f,0.5f) }
};
Vector2D texcoord_list[] =
{
{ Vector2D(0.0f,0.0f) },
{ Vector2D(0.0f,1.0f) },
{ Vector2D(1.0f,0.0f) },
{ Vector2D(1.0f,1.0f) }
};
TexturedCube::Vertex quadList[] =
{
//X - Y - Z
//FRONT FACE
{ position_list[0],texcoord_list[1] },
{ position_list[1],texcoord_list[0] },
{ position_list[2],texcoord_list[2] },
{ position_list[3],texcoord_list[3] },
{ position_list[4],texcoord_list[1] },
{ position_list[5],texcoord_list[0] },
{ position_list[6],texcoord_list[2] },
{ position_list[7],texcoord_list[3] },
{ position_list[1],texcoord_list[1] },
{ position_list[6],texcoord_list[0] },
{ position_list[5],texcoord_list[2] },
{ position_list[2],texcoord_list[3] },
{ position_list[7],texcoord_list[1] },
{ position_list[0],texcoord_list[0] },
{ position_list[3],texcoord_list[2] },
{ position_list[4],texcoord_list[3] },
{ position_list[3],texcoord_list[1] },
{ position_list[2],texcoord_list[0] },
{ position_list[5],texcoord_list[2] },
{ position_list[4],texcoord_list[3] },
{ position_list[7],texcoord_list[1] },
{ position_list[6],texcoord_list[0] },
{ position_list[1],texcoord_list[2] },
{ position_list[0],texcoord_list[3] }
};
this->vertexBuffer = GraphicsEngine::get()->createTexturedBuffer();
this->vertexBuffer->load(quadList, sizeof(Vertex), ARRAYSIZE(quadList), shaderByteCode, sizeShader);
unsigned int indexList[] =
{
//FRONT SIDE
0,1,2, //FIRST TRIANGLE
2,3,0, //SECOND TRIANGLE
//BACK SIDE
4,5,6,
6,7,4,
//TOP SIDE
1,6,5,
5,2,1,
//BOTTOM SIDE
7,0,3,
3,4,7,
//RIGHT SIDE
3,2,5,
5,4,3,
//LEFT SIDE
7,6,1,
1,0,7
};
this->indexBuffer = GraphicsEngine::get()->createIndexBuffer();
this->indexBuffer->load(indexList, ARRAYSIZE(indexList));
//create constant buffer
CBData cbData = {};
cbData.time = 0;
this->constantBuffer = GraphicsEngine::get()->createConstantBuffer();
this->constantBuffer->load(&cbData, sizeof(CBData));
//set vertex shader and pixel shader for the object
DeviceContext* deviceContext = GraphicsEngine::get()->getImmediateDeviceContext();
deviceContext->setRenderConfig(ShaderLibrary::getInstance()->getVertexShader(shaderNames.TEXTURED_VERTEX_SHADER_NAME),
ShaderLibrary::getInstance()->getPixelShader(shaderNames.TEXTURED_PIXEL_SHADER_NAME));
}
TexturedCube::~TexturedCube()
{
}
void TexturedCube::draw(int width, int height)
{
ShaderNames shaderNames;
DeviceContext* deviceContext = GraphicsEngine::get()->getImmediateDeviceContext();
Texture* woodTex = (Texture*)TextureManager::getInstance()->createTextureFromFile(L"C:/Users/neilr/source/repos/nromblon/GDENG03_DirectXCPlusPlus/Assets/Textures/wood.jpg");
//set vertex shader and pixel shader for the object
deviceContext->setTexture(woodTex);
deviceContext->setRenderConfig(ShaderLibrary::getInstance()->getVertexShader(shaderNames.TEXTURED_VERTEX_SHADER_NAME), ShaderLibrary::getInstance()->getPixelShader(shaderNames.TEXTURED_PIXEL_SHADER_NAME));
CBData cbData = {};
Matrix4x4 allMatrix; allMatrix.setIdentity();
Matrix4x4 translationMatrix; translationMatrix.setIdentity(); translationMatrix.setTranslation(this->getLocalPosition());
Matrix4x4 scaleMatrix; scaleMatrix.setScale(this->getLocalScale());
Vector3D rotation = this->getLocalRotation();
Matrix4x4 zMatrix; zMatrix.setRotationZ(rotation.getZ());
Matrix4x4 xMatrix; xMatrix.setRotationX(rotation.getX());
Matrix4x4 yMatrix; yMatrix.setRotationY(rotation.getY());
//Scale --> Rotate --> Transform as recommended order.
Matrix4x4 rotMatrix; rotMatrix.setIdentity();
rotMatrix = rotMatrix.multiplyTo(zMatrix.multiplyTo(yMatrix.multiplyTo(xMatrix)));
allMatrix = allMatrix.multiplyTo(scaleMatrix.multiplyTo(rotMatrix));
allMatrix = allMatrix.multiplyTo(translationMatrix);
cbData.worldMatrix = allMatrix;
Matrix4x4 cameraMatrix = SceneCameraHandler::getInstance()->getSceneCameraViewMatrix();
cbData.viewMatrix = cameraMatrix;
//cbData.projMatrix.setOrthoLH(width / 400.0f, height / 400.0f, -4.0f, 4.0f);
float aspectRatio = (float)width / (float)height;
cbData.projMatrix.setPerspectiveFovLH(aspectRatio, aspectRatio, 0.1f, 1000.0f);
this->constantBuffer->update(deviceContext, &cbData);
deviceContext->setConstantBuffer(this->constantBuffer);
deviceContext->setIndexBuffer(this->indexBuffer);
deviceContext->setVertexBuffer(this->vertexBuffer);
deviceContext->drawIndexedTriangleList(this->indexBuffer->getIndexSize(), 0, 0);
}