1 | #include "common.h"
|
---|
2 | #include "Material.h"
|
---|
3 | #include "Texture.h"
|
---|
4 | #include "glInterface.h"
|
---|
5 | #include "RenderState.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 |
|
---|
11 | namespace CHCDemoEngine
|
---|
12 | {
|
---|
13 |
|
---|
14 | RgbaColor RandomColor(float a, float b)
|
---|
15 | {
|
---|
16 | return RgbaColor(a + Random(b), a + Random(b), a + Random(b), 1);
|
---|
17 | }
|
---|
18 |
|
---|
19 |
|
---|
20 | void Technique::Init()
|
---|
21 | {
|
---|
22 | mTexture = NULL;
|
---|
23 | mAlphaTestEnabled = false;
|
---|
24 | mCullFaceEnabled = true;
|
---|
25 |
|
---|
26 | mVertexProgramParameters.Reset();
|
---|
27 | mFragmentProgramParameters.Reset();
|
---|
28 |
|
---|
29 | mVertexProgram = NULL;
|
---|
30 | mFragmentProgram = NULL;
|
---|
31 |
|
---|
32 | // if this material can write colors
|
---|
33 | mColorWriteEnabled = true;
|
---|
34 | // if lighting is used
|
---|
35 | mLightingEnabled = true;
|
---|
36 | /// if depth write should be enabled
|
---|
37 | mDepthWriteEnabled = true;
|
---|
38 | /// the render queue bucket this technique is assigned to
|
---|
39 | mRenderQueueBucket = NULL;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | Technique::Technique():
|
---|
44 | mAmbientColor(RgbaColor(0.2f, 0.2f, 0.2f, 1.0f)),
|
---|
45 | mDiffuseColor(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)),
|
---|
46 | mSpecularColor(RgbaColor(.0f, .0f, .0f, 1.0f)),
|
---|
47 | mEmmisiveColor(RgbaColor(.0f, .0f, .0f, 1.0f))
|
---|
48 | {
|
---|
49 | Init();
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | Technique::Technique(const RgbaColor &color):
|
---|
54 | mDiffuseColor(color),
|
---|
55 | mAmbientColor(color),
|
---|
56 | mSpecularColor(0, 0, 0, 1),
|
---|
57 | mTexture(NULL)
|
---|
58 | {
|
---|
59 | Init();
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | void Technique::Render(RenderState *state, SceneEntity *ent)
|
---|
64 | {
|
---|
65 | glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
|
---|
66 | glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
|
---|
67 | glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
|
---|
68 | glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
|
---|
69 |
|
---|
70 | state->SetState(this, ent);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void Technique::SetFragmentProgram(ShaderProgram *p)
|
---|
75 | {
|
---|
76 | mFragmentProgram = p;
|
---|
77 | mFragmentProgramParameters.Reset();
|
---|
78 | mFragmentProgramParameters.SetProgram(p);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | void Technique::SetVertexProgram(ShaderProgram *p)
|
---|
83 | {
|
---|
84 | mVertexProgram = p;
|
---|
85 | mVertexProgramParameters.Reset();
|
---|
86 | mVertexProgramParameters.SetProgram(p);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | /***************************************************/
|
---|
92 | /* class Material implementation */
|
---|
93 | /***************************************************/
|
---|
94 |
|
---|
95 |
|
---|
96 | void Material::Render(RenderState *state, SceneEntity *parent)
|
---|
97 | {
|
---|
98 | // set technique if available
|
---|
99 | int idx = min((int)mTechniques.size() - 1 , state->GetRenderTechnique());
|
---|
100 | mTechniques[idx]->Render(state, parent);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | Technique *Material::GetDefaultTechnique() const
|
---|
105 | {
|
---|
106 | return mTechniques[0];
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | Technique *Material::GetTechnique(int i) const
|
---|
111 | {
|
---|
112 | return mTechniques[i];
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | Material::Material()
|
---|
117 | {
|
---|
118 | Technique *tech = new Technique();
|
---|
119 | mTechniques.push_back(tech);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | Material::~Material()
|
---|
124 | {
|
---|
125 | CLEAR_CONTAINER(mTechniques);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | int Material::GetNumTechniques() const
|
---|
130 | {
|
---|
131 | return (int)mTechniques.size();
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | void Material::AddTechnique(Technique *tech)
|
---|
136 | {
|
---|
137 | mTechniques.push_back(tech);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | }
|
---|