source: GTP/trunk/App/Demos/Vis/CHC_revisited/Material.cpp @ 2769

Revision 2769, 1.4 KB checked in by mattausch, 17 years ago (diff)
Line 
1#include "common.h"
2#include "Material.h"
3#include "Texture.h"
4#include "glInterface.h"
5#include "RenderState.h"
6
7
8namespace CHCDemo
9{
10
11RgbaColor RandomColor(float a, float b)
12{
13        return RgbaColor(a + Random(b), a + Random(b), a + Random(b), 1);
14}
15
16
17void 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
29Material::Material(): mId(0)
30{
31        InitMaterial();
32}
33
34
35Material::Material(int id):
36mId(id)
37{
38        InitMaterial();
39}
40
41
42Material::Material(const RgbaColor &color):
43mDiffuseColor(color),
44mAmbientColor(color),
45mSpecularColor(0, 0, 0, 1),
46mId(0),
47mTexture(NULL)
48{
49}
50
51
52int Material::GetId() const
53{
54        return mId;
55}
56
57
58Material 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)
71void 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}
Note: See TracBrowser for help on using the repository browser.