1 | #include "common.h"
|
---|
2 | #include "Material.h"
|
---|
3 | #include "Texture.h"
|
---|
4 | #include "glInterface.h"
|
---|
5 | #include "RenderState.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace CHCDemoEngine
|
---|
9 | {
|
---|
10 |
|
---|
11 | CGparameter Material::sDiffuseParam;
|
---|
12 | CGparameter Material::sDiffuseTexParam;
|
---|
13 | CGparameter Material::sAmbientParam;
|
---|
14 | CGparameter Material::sAmbientTexParam;
|
---|
15 |
|
---|
16 |
|
---|
17 | RgbaColor RandomColor(float a, float b)
|
---|
18 | {
|
---|
19 | return RgbaColor(a + Random(b), a + Random(b), a + Random(b), 1);
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | void Material::InitMaterial()
|
---|
24 | {
|
---|
25 | mTexture = NULL;
|
---|
26 | mAlphaTestEnabled = false;
|
---|
27 | mCullFaceEnabled = true;
|
---|
28 |
|
---|
29 | mAmbientColor = RgbaColor(0.2f, 0.2f, 0.2f, 1.0f);
|
---|
30 | mDiffuseColor = RgbaColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
31 | mSpecularColor = RgbaColor(.0f, .0f, .0f, 1.0f);
|
---|
32 | mEmmisiveColor = RgbaColor(.0f, .0f, .0f, 1.0f);
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | Material::Material()
|
---|
37 | {
|
---|
38 | InitMaterial();
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | Material::Material(const RgbaColor &color):
|
---|
43 | mDiffuseColor(color),
|
---|
44 | mAmbientColor(color),
|
---|
45 | mSpecularColor(0, 0, 0, 1),
|
---|
46 | mTexture(NULL)
|
---|
47 | {
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | Material RandomMaterial()
|
---|
52 | {
|
---|
53 | float a = 0.1f;
|
---|
54 | float b = 0.9f;
|
---|
55 |
|
---|
56 | Material m;
|
---|
57 | m.mDiffuseColor = RandomColor(a, b);
|
---|
58 |
|
---|
59 | return m;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | void Material::Render(RenderState *state)
|
---|
64 | {
|
---|
65 | state->SetState(mTexture != NULL, mAlphaTestEnabled, mCullFaceEnabled);
|
---|
66 |
|
---|
67 | if (mTexture)
|
---|
68 | {
|
---|
69 | mTexture->Bind();
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (state->GetRenderType() == RenderState::DEFERRED)
|
---|
77 | {
|
---|
78 | if (mTexture)
|
---|
79 | {
|
---|
80 | cgGLSetParameter4f(sAmbientTexParam, mEmmisiveColor.r, mEmmisiveColor.g, mEmmisiveColor.b, mEmmisiveColor.a);
|
---|
81 | cgGLSetParameter4f(sDiffuseTexParam, mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a);
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | cgGLSetParameter4f(sAmbientParam, mEmmisiveColor.r, mEmmisiveColor.g, mEmmisiveColor.b, mEmmisiveColor.a);
|
---|
86 | cgGLSetParameter4f(sDiffuseParam, mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | else if (state->GetRenderType() == RenderState::FIXED)
|
---|
90 | {
|
---|
91 | glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
|
---|
92 | glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
|
---|
93 | glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
|
---|
94 | glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | }
|
---|