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

Revision 3147, 3.0 KB checked in by mattausch, 16 years ago (diff)

updated shader loading

Line 
1#include "common.h"
2#include "Material.h"
3#include "Texture.h"
4#include "glInterface.h"
5#include "RenderState.h"
6
7
8using namespace std;
9
10
11namespace CHCDemoEngine
12{
13
14RgbaColor RandomColor(float a, float b)
15{
16        return RgbaColor(a + Random(b), a + Random(b), a + Random(b), 1);
17}
18
19
20void Technique::Init()
21{
22        mTexture = NULL;
23        mAlphaTestEnabled = false;
24        mCullFaceEnabled = true;
25       
26        mVertexProgramParameters.Reset();
27        mFragmentProgramParameters.Reset();
28       
29        mVertexProgram = NULL;
30        mFragmentProgram = NULL;
31
32        // if this material can write colors
33        mColorWriteEnabled = true;
34        // if lighting is used
35        mLightingEnabled = true;
36        /// if depth write should be enabled
37        mDepthWriteEnabled = true;
38        /// the render queue bucket this technique is assigned to
39        mRenderQueueBucket = NULL;
40}
41
42
43Technique::Technique():
44mAmbientColor(RgbaColor(0.2f, 0.2f, 0.2f, 1.0f)),
45mDiffuseColor(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)),
46mSpecularColor(RgbaColor(.0f, .0f, .0f, 1.0f)),
47mEmmisiveColor(RgbaColor(.0f, .0f, .0f, 1.0f))
48{
49        Init();
50}
51
52
53Technique::Technique(const RgbaColor &color):
54mDiffuseColor(color),
55mAmbientColor(color),
56mSpecularColor(0, 0, 0, 1),
57mTexture(NULL)
58{
59        Init();
60}
61
62
63void Technique::Render(RenderState *state, SceneEntity *ent)
64{
65        glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
66        glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
67        glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
68        glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
69
70        state->SetState(this, ent);
71}
72
73
74void Technique::SetFragmentProgram(ShaderProgram *p)
75{
76        if (p->GetProgramType() == ShaderProgram::VERTEX_PROGRAM)
77                cerr << "warning: assigning a vertex program as fragment program!" << endl;
78
79        mFragmentProgram = p;
80        mFragmentProgramParameters.Reset();
81        mFragmentProgramParameters.SetProgram(p);
82}
83       
84
85void Technique::SetVertexProgram(ShaderProgram *p)
86{
87        if (p->GetProgramType() == ShaderProgram::FRAGMENT_PROGRAM)
88                cerr << "warning: assigning a fragment program as vertex program!" << endl;
89
90        mVertexProgram = p;
91        mVertexProgramParameters.Reset();
92        mVertexProgramParameters.SetProgram(p);
93}
94
95
96
97/***************************************************/
98/*          class Material implementation          */
99/***************************************************/
100
101
102void Material::Render(RenderState *state, SceneEntity *parent)
103{
104        // set technique if available
105        int idx = min((int)mTechniques.size() - 1 , state->GetRenderTechnique());
106        mTechniques[idx]->Render(state, parent);
107}
108
109
110Technique *Material::GetDefaultTechnique() const
111{
112        return mTechniques[0];
113}
114
115
116Technique *Material::GetTechnique(int i) const
117{
118        return mTechniques[i];
119}
120
121
122Material::Material()
123{
124        Technique *tech = new Technique();
125        mTechniques.push_back(tech);
126}
127
128
129Material::~Material()
130{
131        CLEAR_CONTAINER(mTechniques);
132}
133
134
135int Material::GetNumTechniques() const
136{
137        return (int)mTechniques.size();
138}
139
140
141void Material::AddTechnique(Technique *tech)
142{
143        mTechniques.push_back(tech);
144}
145
146
147}
Note: See TracBrowser for help on using the repository browser.