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 | if (p->GetProgramType() == ShaderProgram::VERTEX_PROGRAM)
|
---|
77 | cerr << "warning: assigning a vertex program as fragment program!" << endl;
|
---|
78 |
|
---|
79 | mFragmentProgram = p;
|
---|
80 | mFragmentProgramParameters.Reset();
|
---|
81 | mFragmentProgramParameters.SetProgram(p);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | void Technique::SetVertexProgram(ShaderProgram *p)
|
---|
86 | {
|
---|
87 | if (p->GetProgramType() == ShaderProgram::FRAGMENT_PROGRAM)
|
---|
88 | cerr << "warning: assigning a fragment program as vertex program!" << endl;
|
---|
89 |
|
---|
90 | mVertexProgram = p;
|
---|
91 | mVertexProgramParameters.Reset();
|
---|
92 | mVertexProgramParameters.SetProgram(p);
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 | /***************************************************/
|
---|
98 | /* class Material implementation */
|
---|
99 | /***************************************************/
|
---|
100 |
|
---|
101 |
|
---|
102 | void Material::Render(RenderState *state, SceneEntity *parent)
|
---|
103 | {
|
---|
104 | // set technique if available
|
---|
105 | int idx = min((int)mTechniques.size() - 1 , state->GetRenderTechnique());
|
---|
106 | mTechniques[idx]->Render(state, parent);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | Technique *Material::GetDefaultTechnique() const
|
---|
111 | {
|
---|
112 | return mTechniques[0];
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | Technique *Material::GetTechnique(int i) const
|
---|
117 | {
|
---|
118 | return mTechniques[i];
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | Material::Material()
|
---|
123 | {
|
---|
124 | Technique *tech = new Technique();
|
---|
125 | mTechniques.push_back(tech);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | Material::~Material()
|
---|
130 | {
|
---|
131 | CLEAR_CONTAINER(mTechniques);
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | int Material::GetNumTechniques() const
|
---|
136 | {
|
---|
137 | return (int)mTechniques.size();
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | void Material::AddTechnique(Technique *tech)
|
---|
142 | {
|
---|
143 | mTechniques.push_back(tech);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | }
|
---|