-
Notifications
You must be signed in to change notification settings - Fork 4
/
MeshComponent.cpp
48 lines (44 loc) · 1.05 KB
/
MeshComponent.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
// Ahmed S. Tolba 2015-2018
#include "MeshComponent.h"
#include "Shader.h"
#include "Mesh.h"
#include "Actor.h"
#include "Game.h"
#include "Renderer.h"
#include "Texture.h"
#include "VertexArray.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);
}
}