-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.cpp
More file actions
132 lines (110 loc) · 3.77 KB
/
Cube.cpp
File metadata and controls
132 lines (110 loc) · 3.77 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
#include "Cube.h"
#include "GraphicsEngine.h"
#include "SceneCameraHandler.h"
#include "ShaderLibrary.h"
#include "SwapChain.h"
Cube::Cube(String name, bool skipInit) :AGameObject(name)
{
if (skipInit)
return;
ShaderNames shaderNames;
void* shaderByteCode = NULL;
size_t sizeShader = 0;
ShaderLibrary::getInstance()->requestVertexShaderData(shaderNames.BASE_VERTEX_SHADER_NAME, &shaderByteCode, &sizeShader);
//create buffers for drawing. vertex data that needs to be drawn are temporarily placed here.
Vertex quadList[] = {
//X, Y, Z
//FRONT FACE
{Vector3D(-0.5f,-0.5f,-0.5f), Vector3D(1,0,0), Vector3D(0.2f,0,0) },
{Vector3D(-0.5f,0.5f,-0.5f), Vector3D(1,1,0), Vector3D(0.2f,0.2f,0) },
{Vector3D(0.5f,0.5f,-0.5f), Vector3D(1,1,0), Vector3D(0.2f,0.2f,0) },
{Vector3D(0.5f,-0.5f,-0.5f), Vector3D(1,0,0), Vector3D(0.2f,0,0) },
//BACK FACE
{Vector3D(0.5f,-0.5f,0.5f), Vector3D(0,1,0), Vector3D(0,0.2f,0) },
{Vector3D(0.5f,0.5f,0.5f), Vector3D(0,1,1), Vector3D(0,0.2f,0.2f) },
{Vector3D(-0.5f,0.5f,0.5f), Vector3D(0,1,1), Vector3D(0,0.2f,0.2f) },
{Vector3D(-0.5f,-0.5f,0.5f), Vector3D(0,1,0), Vector3D(0,0.2f,0) },
};
this->vertexBuffer = GraphicsEngine::get()->createVertexBuffer();
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));
}
Cube::~Cube()
{
this->vertexBuffer->release();
this->indexBuffer->release();
AGameObject::~AGameObject();
}
void Cube::update(float deltaTime)
{
this->deltaTime = deltaTime;
/*if (this->speed <= 1.0f) {
this->ticks += deltaTime;
float rotSpeed = this->ticks * this->speed;
this->setRotation(rotSpeed, rotSpeed, rotSpeed);
}
else if (this->speed >= 10.0f) {
this->ticks -= deltaTime;
float rotSpeed = this->ticks * this->speed;
this->setRotation(rotSpeed, rotSpeed, rotSpeed);
}*/
}
void Cube::draw(int width, int height)
{
GraphicsEngine* graphEngine = GraphicsEngine::get();
DeviceContext* deviceContext = graphEngine->getImmediateDeviceContext();
//set vertex shader and pixel shader for the object
ShaderNames shaderNames;
deviceContext->setRenderConfig(ShaderLibrary::getInstance()->getVertexShader(shaderNames.BASE_VERTEX_SHADER_NAME), ShaderLibrary::getInstance()->getPixelShader(shaderNames.BASE_PIXEL_SHADER_NAME));
CBData cbData = {};
if (this->overrideMatrix)
{
cbData.worldMatrix = this->localMatrix;
}
else
{
this->updateLocalMatrix();
cbData.worldMatrix = this->localMatrix;
}
cbData.viewMatrix = SceneCameraHandler::getInstance()->getSceneCameraViewMatrix();
//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);
//graphEngine->getSwapChain()->present(true); // we do not present immediately. We draw all objects first, before displaying
}
void Cube::setAnimSpeed(float speed)
{
this->speed = speed;
}