-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathMeshComponent.cpp
More file actions
88 lines (78 loc) · 2.28 KB
/
MeshComponent.cpp
File metadata and controls
88 lines (78 loc) · 2.28 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
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "MeshComponent.h"
#include "Shader.h"
#include "Mesh.h"
#include "Actor.h"
#include "Game.h"
#include "Renderer.h"
#include "Texture.h"
#include "VertexArray.h"
#include "LevelLoader.h"
MeshComponent::MeshComponent(Actor* owner, bool isSkeletal)
:Component(owner)
,mMesh(nullptr)
,mTextureIndex(0)
,mVisible(true)
,mIsSkeletal(isSkeletal)
{
mOwner->GetGame()->GetRenderer()->AddMeshComp(this);
}
MeshComponent::~MeshComponent()
{
mOwner->GetGame()->GetRenderer()->RemoveMeshComp(this);
}
void MeshComponent::Draw(Shader* shader)
{
if (mMesh)
{
// Set the world transform
shader->SetMatrixUniform("uWorldTransform",
mOwner->GetWorldTransform());
// Set specular power
shader->SetFloatUniform("uSpecPower", mMesh->GetSpecPower());
// Set the active texture
Texture* t = mMesh->GetTexture(mTextureIndex);
if (t)
{
t->SetActive();
}
// Set the mesh's vertex array as active
VertexArray* va = mMesh->GetVertexArray();
va->SetActive();
// Draw
glDrawElements(GL_TRIANGLES, va->GetNumIndices(), GL_UNSIGNED_INT, nullptr);
}
}
void MeshComponent::LoadProperties(const rapidjson::Value& inObj)
{
Component::LoadProperties(inObj);
std::string meshFile;
if (JsonHelper::GetString(inObj, "meshFile", meshFile))
{
SetMesh(mOwner->GetGame()->GetRenderer()->GetMesh(meshFile));
}
int idx;
if (JsonHelper::GetInt(inObj, "textureIndex", idx))
{
mTextureIndex = static_cast<size_t>(idx);
}
JsonHelper::GetBool(inObj, "visible", mVisible);
JsonHelper::GetBool(inObj, "isSkeletal", mIsSkeletal);
}
void MeshComponent::SaveProperties(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& inObj) const
{
Component::SaveProperties(alloc, inObj);
if (mMesh)
{
JsonHelper::AddString(alloc, inObj, "meshFile", mMesh->GetFileName());
}
JsonHelper::AddInt(alloc, inObj, "textureIndex", static_cast<int>(mTextureIndex));
JsonHelper::AddBool(alloc, inObj, "visible", mVisible);
JsonHelper::AddBool(alloc, inObj, "isSkeletal", mIsSkeletal);
}