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 | RgbaColor RandomColor(float a, float b)
|
---|
12 | {
|
---|
13 | return RgbaColor(a + Random(b), a + Random(b), a + Random(b), 1);
|
---|
14 | }
|
---|
15 |
|
---|
16 |
|
---|
17 | void Material::InitMaterial()
|
---|
18 | {
|
---|
19 | mTexture = NULL;
|
---|
20 | mAlphaTestEnabled = false;
|
---|
21 |
|
---|
22 | mAmbientColor = RgbaColor(0.2f, 0.2f, 0.2f, 1.0f);
|
---|
23 | mDiffuseColor = RgbaColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
24 | //mSpecularColor = RgbaColor(.0f, .0f, .0f, 1.0f);
|
---|
25 | mSpecularColor = RgbaColor(0.5f, 0.5f, 0.5f, 1.0f);
|
---|
26 | mEmmisiveColor = RgbaColor(.0f, .0f, .0f, 1.0f);
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | Material::Material()
|
---|
31 | {
|
---|
32 | InitMaterial();
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | Material::Material(const RgbaColor &color):
|
---|
37 | mDiffuseColor(color),
|
---|
38 | mAmbientColor(color),
|
---|
39 | mSpecularColor(0, 0, 0, 1),
|
---|
40 | mTexture(NULL)
|
---|
41 | {
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | Material RandomMaterial()
|
---|
46 | {
|
---|
47 | float a = 0.1f;
|
---|
48 | float b = 0.9f;
|
---|
49 |
|
---|
50 | Material m;
|
---|
51 | m.mDiffuseColor = RandomColor(a, b);
|
---|
52 |
|
---|
53 | return m;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | //void Material::Render(RenderState *state)
|
---|
58 | void Material::Render(RenderState *state)
|
---|
59 | {
|
---|
60 | state->SetState(mTexture != NULL, mAlphaTestEnabled);
|
---|
61 |
|
---|
62 | if (mTexture)
|
---|
63 | mTexture->Bind();
|
---|
64 | else
|
---|
65 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
66 |
|
---|
67 | glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
|
---|
68 | glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
|
---|
69 | glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
|
---|
70 | glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | }
|
---|