source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp @ 2959

Revision 2959, 2.2 KB checked in by mattausch, 16 years ago (diff)

preetham working but big hack

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