-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.cpp
83 lines (76 loc) · 2.95 KB
/
Piece.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
#include <map>
#include "Piece.h"
Piece::Piece(const char* vertexShaderPath, const char* fragmentShaderPath, const char* outlineVertexShaderPath, const char* outlineFragmentShaderPath, Color color, Type type, glm::mat4 *projectionMatrix, Camera* camera, Light* light, Material* material) {
m_GL_Shader = Shader(vertexShaderPath, fragmentShaderPath);
m_GL_OutlineShader = Shader(outlineVertexShaderPath, outlineFragmentShaderPath);
m_GL_ViewMatrix = camera->getViewMatrix();
m_GL_ProjectionMatrix = projectionMatrix;
m_Color = color;
m_Type = type;
m_GL_Model = loadModel();
m_GL_Camera = camera;
m_GL_Light = light;
m_GL_Material = material;
m_GL_IsSelected = false;
}
Model Piece::loadModel() {
std::string modelLocation = "./Assets/Chess";
switch (m_Color) {
case Black:
modelLocation += "/Black";
break;
case White:
modelLocation += "/White";
break;
}
switch (m_Type) {
case Bishop:
modelLocation += "/Bishop/Bishop.obj";
break;
case King:
modelLocation += "/King/King.obj";
break;
case Knight:
modelLocation += "/Knight/Knight.obj";
break;
case Pawn:
modelLocation += "/Pawn/Pawn.obj";
break;
case Queen:
modelLocation += "/Queen/Queen.obj";
break;
case Rook:
modelLocation += "/Rook/Rook.obj";
break;
}
return Model(modelLocation.c_str());
}
void Piece::Draw(glm::vec3 mouseRay, glm::vec3 position) {
m_GL_Shader.use();
glm::mat4 modelMatrix = glm::mat4(1.0f);
modelMatrix = glm::translate(modelMatrix, position);
modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
modelMatrix = glm::scale(modelMatrix, glm::vec3(0.05f, 0.05f, 0.05f));
m_GL_ModelMatrix = modelMatrix;
m_GL_Shader.setMat4Uniform("model", modelMatrix);
m_GL_Shader.setMat4Uniform("projection", *m_GL_ProjectionMatrix);
m_GL_Shader.setMat4Uniform("view", m_GL_ViewMatrix);
m_GL_Shader.setVec3Uniform("viewPos", m_GL_Camera->position);
m_GL_Shader.setVec3Uniform("light.position", m_GL_Light->position);
m_GL_Shader.setVec3Uniform("light.ambient", m_GL_Light->ambient);
m_GL_Shader.setVec3Uniform("light.diffuse", m_GL_Light->diffuse);
m_GL_Shader.setVec3Uniform("light.specular", m_GL_Light->specular);
m_GL_Shader.setVec3Uniform("material.ambient", m_GL_Material->ambient);
m_GL_Shader.setVec3Uniform("material.diffuse", m_GL_Material->diffuse);
m_GL_Shader.setVec3Uniform("material.specular", m_GL_Material->specular);
m_GL_Shader.setFloatUniform("material.shininess", m_GL_Material->shininess);
m_GL_Shader.setBoolUniform("isSelected", m_GL_IsSelected);
m_GL_Model.Draw(m_GL_Shader);
}
bool Piece::IsSelected(glm::vec3 mouseRay) {
glm::vec3 center = m_GL_ModelMatrix * glm::vec4((m_GL_Model.minVertexPos.x + m_GL_Model.maxVertexPos.x) / 2, (m_GL_Model.minVertexPos.y + m_GL_Model.maxVertexPos.y) / 2, (m_GL_Model.minVertexPos.z + m_GL_Model.maxVertexPos.z) / 2, 1.0);
glm::vec3 oc = m_GL_Camera->position - center;
float b = glm::dot(oc, mouseRay);
float c = glm::dot(oc, oc) - 0.5f * 0.5f;
return b * b - c >= 0;
}