Line | |
---|
1 | #include "common.h"
|
---|
2 | #include "Material.h"
|
---|
3 | #include "Texture.h"
|
---|
4 | #include "glInterface.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace CHCDemo
|
---|
8 | {
|
---|
9 |
|
---|
10 | RgbColor RandomColor(float a, float b)
|
---|
11 | {
|
---|
12 | return RgbColor(a + Random(b), a + Random(b), a + Random(b));
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | void Material::InitMaterial()
|
---|
17 | {
|
---|
18 | mTexture = NULL;
|
---|
19 |
|
---|
20 | mAmbientColor = RgbColor(0, 0, 0);
|
---|
21 | mDiffuseColor = RgbColor(1, 1, 1);
|
---|
22 | mSpecularColor = RgbColor(0, 0, 0);
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | Material::Material(): mId(0)
|
---|
27 | {
|
---|
28 | InitMaterial();
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | Material::Material(int id):
|
---|
33 | mId(id)
|
---|
34 | {
|
---|
35 | InitMaterial();
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | Material::Material(const RgbColor &color):
|
---|
40 | mDiffuseColor(color),
|
---|
41 | mAmbientColor(color),
|
---|
42 | mSpecularColor(0, 0, 0),
|
---|
43 | mId(0),
|
---|
44 | mTexture(NULL)
|
---|
45 | {
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | int Material::GetId() const
|
---|
50 | {
|
---|
51 | return mId;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | Material RandomMaterial()
|
---|
56 | {
|
---|
57 | float a = 0.1f;
|
---|
58 | float b = 0.9f;
|
---|
59 |
|
---|
60 | Material m;
|
---|
61 | m.mDiffuseColor = RandomColor(a, b);
|
---|
62 |
|
---|
63 | return m;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | void Material::Render()
|
---|
68 | {
|
---|
69 | if (mTexture)
|
---|
70 | mTexture->Bind();
|
---|
71 | else
|
---|
72 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
73 |
|
---|
74 | glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
|
---|
75 | glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
|
---|
76 | glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.