1 | #include "common.h"
|
---|
2 | #include "Material.h"
|
---|
3 | #include "Texture.h"
|
---|
4 | #include "glInterface.h"
|
---|
5 | #include "RenderState.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace CHCDemo
|
---|
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(.2f, .2f, .2f, 1.0f);
|
---|
23 | mDiffuseColor = RgbaColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
24 | //mSpecularColor = RgbaColor(0.0f, 0.0f, 0.0f, 1.0f);
|
---|
25 | mSpecularColor = RgbaColor(0.5f, 0.5f, 0.5f, 1.0f);
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | Material::Material(): mId(0)
|
---|
30 | {
|
---|
31 | InitMaterial();
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | Material::Material(int id):
|
---|
36 | mId(id)
|
---|
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 | mId(0),
|
---|
47 | mTexture(NULL)
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | int Material::GetId() const
|
---|
53 | {
|
---|
54 | return mId;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | Material RandomMaterial()
|
---|
59 | {
|
---|
60 | float a = 0.1f;
|
---|
61 | float b = 0.9f;
|
---|
62 |
|
---|
63 | Material m;
|
---|
64 | m.mDiffuseColor = RandomColor(a, b);
|
---|
65 |
|
---|
66 | return m;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | //void Material::Render(RenderState *state)
|
---|
71 | void Material::Render(RenderState *state)
|
---|
72 | {
|
---|
73 | state->SetState(mTexture != NULL, mAlphaTestEnabled);
|
---|
74 |
|
---|
75 | if (mTexture)
|
---|
76 | mTexture->Bind();
|
---|
77 | else
|
---|
78 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
79 |
|
---|
80 | glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
|
---|
81 | glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
|
---|
82 | glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | }
|
---|