~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/KX_BlenderMaterial.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lukas Fittl
  • Date: 2006-09-20 01:57:27 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060920015727-gmoqlxwstx9wwqs3
Tags: 2.42a-1ubuntu1
* Merge from Debian unstable (Closes: Malone #55903). Remaining changes:
  - debian/genpot: Add python scripts from Lee June <blender@eyou.com> to
    generate a reasonable PO template from the sources. Since gettext is used
    in a highly nonstandard way, xgettext does not work for this job.
  - debian/rules: Call the scripts, generate po/blender.pot, and clean it up
    in the clean target.
  - Add a proper header to the generated PO template.
* debian/control: Build depend on libavformat-dev >= 3:0.cvs20060823-3.1,
  otherwise this package will FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
        mPass(0)
83
83
 
84
84
{
85
 
        ///RAS_EXT_support._ARB_multitexture == true if were here
86
 
 
87
85
        // --------------------------------
88
86
        // RAS_IPolyMaterial variables... 
89
87
        m_flag |=RAS_BLENDERMAT;
90
88
        m_flag |=(mMaterial->IdMode>=ONETEX)?RAS_MULTITEX:0;
91
89
        m_flag |=(mMaterial->ras_mode & USE_LIGHT)!=0?RAS_MULTILIGHT:0;
92
 
        
 
90
        m_flag |=(mMaterial->ras_mode &ALPHA_TEST)!=0?RAS_FORCEALPHA:0;
 
91
 
93
92
        // figure max
94
 
        #ifdef GL_ARB_multitexture
95
93
        int enabled = mMaterial->num_enabled;
96
 
        mMaterial->num_enabled = enabled>=bgl::max_texture_units?bgl::max_texture_units:enabled;
97
 
        #else
98
 
        mMaterial->num_enabled=0;
99
 
        #endif
 
94
        int max = BL_Texture::GetMaxUnits();
 
95
        mMaterial->num_enabled = enabled>=max?max:enabled;
100
96
 
 
97
        // base class
101
98
        m_enabled = mMaterial->num_enabled;
102
99
 
103
100
        // test the sum of the various modes for equality
114
111
 
115
112
}
116
113
 
117
 
 
118
114
KX_BlenderMaterial::~KX_BlenderMaterial()
119
115
{
120
116
        // cleanup work
132
128
void KX_BlenderMaterial::OnConstruction()
133
129
{
134
130
        // for each unique material...
135
 
        #ifdef GL_ARB_multitexture
136
 
/*      will be used to switch textures
137
 
        if(!gTextureDict)
138
 
                gTextureDict = PyDict_New();
139
 
*/
140
131
        int i;
141
132
        for(i=0; i<mMaterial->num_enabled; i++) {
142
 
        bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
143
 
                #ifdef GL_ARB_texture_cube_map
 
133
                BL_Texture::ActivateUnit(i);
144
134
                if( mMaterial->mapping[i].mapping & USEENV ) {
145
135
                        if(!RAS_EXT_support._ARB_texture_cube_map) {
146
136
                                spit("CubeMap textures not supported");
147
137
                                continue;
148
138
                        }
149
 
                        if(!mTextures[i].InitCubeMap( mMaterial->cubemap[i] ) )
 
139
                        if(!mTextures[i].InitCubeMap(i, mMaterial->cubemap[i] ) )
150
140
                                spit("unable to initialize image("<<i<<") in "<< 
151
141
                                                mMaterial->matname<< ", image will not be available");
152
142
                } 
153
143
        
154
144
                else {
155
 
                #endif//GL_ARB_texture_cube_map
156
145
                        if( mMaterial->img[i] ) {
157
 
                                if( ! mTextures[i].InitFromImage(mMaterial->img[i], (mMaterial->flag[i] &MIPMAP)!=0 ))
 
146
                                if( ! mTextures[i].InitFromImage(i, mMaterial->img[i], (mMaterial->flag[i] &MIPMAP)!=0 ))
158
147
                                        spit("unable to initialize image("<<i<<") in "<< 
159
148
                                                 mMaterial->matname<< ", image will not be available");
160
149
                        }
161
 
                #ifdef GL_ARB_texture_cube_map
162
150
                }
163
 
                #endif//GL_ARB_texture_cube_map
164
 
                /*PyDict_SetItemString(gTextureDict, mTextures[i].GetName().Ptr(), PyInt_FromLong(mTextures[i]));*/
165
151
        }
166
 
        #endif//GL_ARB_multitexture
167
 
 
168
152
        mBlendFunc[0] =0;
169
153
        mBlendFunc[1] =0;
170
154
}
171
155
 
172
156
void KX_BlenderMaterial::OnExit()
173
157
{
174
 
        #ifdef GL_ARB_multitexture
175
 
        
176
 
        #ifdef GL_ARB_shader_objects
177
 
        if( RAS_EXT_support._ARB_shader_objects && mShader ) {
 
158
        if( mShader ) {
178
159
                 //note, the shader here is allocated, per unique material
179
160
                 //and this function is called per face
180
 
                bgl::blUseProgramObjectARB(0);
 
161
                mShader->SetProg(0);
181
162
                delete mShader;
182
163
                mShader = 0;
183
164
        }
184
 
        #endif //GL_ARB_shader_objects
185
165
 
 
166
        BL_Texture::ActivateFirst();
186
167
        for(int i=0; i<mMaterial->num_enabled; i++) {
187
 
                bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
188
 
 
 
168
                BL_Texture::ActivateUnit(i);
189
169
                mTextures[i].DeleteTex();
190
 
 
191
 
                glMatrixMode(GL_TEXTURE);
192
 
                glLoadIdentity();
193
 
                glMatrixMode(GL_MODELVIEW);
194
 
 
195
 
                #ifdef GL_ARB_texture_cube_map
196
 
                if(RAS_EXT_support._ARB_texture_cube_map)
197
 
                        glDisable(GL_TEXTURE_CUBE_MAP_ARB);
198
 
                #endif//GL_ARB_texture_cube_map
199
 
 
200
 
                glDisable(GL_TEXTURE_2D);
201
 
                glDisable(GL_TEXTURE_GEN_S);
202
 
                glDisable(GL_TEXTURE_GEN_T);
203
 
                glDisable(GL_TEXTURE_GEN_R);
204
 
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
 
170
                mTextures[i].DisableUnit();
205
171
        }
206
 
        
207
 
        /*if (gTextureDict) {
208
 
                PyDict_Clear(gTextureDict);
209
 
                Py_DECREF(gTextureDict);
210
 
                gTextureDict = 0;
211
 
        }*/
212
 
 
213
 
        bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
214
 
 
215
 
        #ifdef GL_ARB_texture_cube_map
216
 
        if(RAS_EXT_support._ARB_texture_cube_map)
217
 
                glDisable(GL_TEXTURE_CUBE_MAP_ARB);
218
 
        #endif//GL_ARB_texture_cube_map
219
 
 
220
 
        glDisable(GL_TEXTURE_2D);
221
 
 
222
 
        #endif//GL_ARB_multitexture
223
 
 
224
 
        // make sure multi texture units 
225
 
        // revert back to blender...
226
 
        // --
 
172
 
227
173
        if( mMaterial->tface ) 
228
174
                set_tpage(mMaterial->tface);
229
175
}
230
176
 
231
177
 
232
 
void KX_BlenderMaterial::DisableTexData()
233
 
{
234
 
        glDisable(GL_BLEND);
235
 
        #ifdef GL_ARB_multitexture
236
 
        int i=(MAXTEX>=bgl::max_texture_units?bgl::max_texture_units:MAXTEX)-1;
237
 
        for(; i>=0; i--) {
238
 
                bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
239
 
                glMatrixMode(GL_TEXTURE);
240
 
                glLoadIdentity();
241
 
                glMatrixMode(GL_MODELVIEW);
242
 
 
243
 
                #ifdef GL_ARB_texture_cube_map
244
 
                if(RAS_EXT_support._ARB_texture_cube_map)
245
 
                        glDisable(GL_TEXTURE_CUBE_MAP_ARB);
246
 
                #endif//GL_ARB_texture_cube_map
247
 
 
248
 
                glDisable(GL_TEXTURE_2D);       
249
 
                glDisable(GL_TEXTURE_GEN_S);
250
 
                glDisable(GL_TEXTURE_GEN_T);
251
 
                glDisable(GL_TEXTURE_GEN_R);
252
 
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
253
 
        }
254
 
        #endif//GL_ARB_multitexture
255
 
}
256
 
 
257
 
 
258
 
void KX_BlenderMaterial::setShaderData( bool enable )
259
 
{
260
 
        #ifdef GL_ARB_multitexture 
261
 
        #ifdef GL_ARB_shader_objects 
262
 
 
 
178
void KX_BlenderMaterial::setShaderData( bool enable, RAS_IRasterizer *ras)
 
179
{
263
180
        MT_assert(RAS_EXT_support._ARB_shader_objects && mShader);
264
181
 
265
182
        int i;
266
183
        if( !enable || !mShader->Ok() ) {
267
184
                // frame cleanup.
268
 
                bgl::blUseProgramObjectARB( 0 );
269
 
                DisableTexData();
 
185
                mShader->SetProg(false);
 
186
                BL_Texture::DisableAllTextures();
270
187
                return;
271
188
        }
272
189
 
273
 
        DisableTexData();
274
 
        bgl::blUseProgramObjectARB( mShader->GetProg() );
 
190
        BL_Texture::DisableAllTextures();
 
191
        mShader->SetProg(true);
275
192
        
 
193
        BL_Texture::ActivateFirst();
 
194
 
 
195
        mShader->ApplyShader();
 
196
 
276
197
        // for each enabled unit
277
198
        for(i=0; i<mMaterial->num_enabled; i++) {
278
 
 
279
 
                const uSampler *samp = mShader->getSampler(i);
280
 
                if( samp->loc == -1 || samp->glTexture == 0 ) continue;
281
 
 
282
 
                bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
283
 
 
284
 
                #ifdef GL_ARB_texture_cube_map
285
 
                if( mMaterial->mapping[i].mapping &USEENV ) {
286
 
                        glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, samp->glTexture /* mTextures[i]*/ );    
287
 
                        glEnable( GL_TEXTURE_CUBE_MAP_ARB );
288
 
                } 
289
 
                else {
290
 
                #endif//GL_ARB_texture_cube_map
291
 
                        glBindTexture( GL_TEXTURE_2D, samp->glTexture   /*mTextures[i]*/ );     
292
 
                        glEnable( GL_TEXTURE_2D );
293
 
                #ifdef GL_ARB_texture_cube_map
294
 
                }
295
 
                #endif//GL_ARB_texture_cube_map
296
 
                // use a sampler
297
 
                bgl::blUniform1iARB(samp->loc, i );
 
199
                if(!mTextures[i].Ok()) continue;
 
200
                mTextures[i].ActivateTexture();
 
201
                mTextures[0].SetMapping(mMaterial->mapping[i].mapping);
298
202
        }
299
203
 
300
204
        if(!mUserDefBlend) {
301
205
                setDefaultBlending();
302
 
        }else
303
 
        {
 
206
        }
 
207
        else {
 
208
                // tested to be valid enums
304
209
                glEnable(GL_BLEND);
305
 
                // tested to be valid enums
306
210
                glBlendFunc(mBlendFunc[0], mBlendFunc[1]);
307
211
        }
308
 
 
309
 
        #endif//GL_ARB_shader_objects
310
 
        #endif//GL_ARB_multitexture
311
212
}
312
213
 
313
214
 
314
 
void KX_BlenderMaterial::setTexData( bool enable )
 
215
void KX_BlenderMaterial::setTexData( bool enable, RAS_IRasterizer *ras)
315
216
{
316
 
        #ifdef GL_ARB_multitexture
317
 
        int i;
318
 
 
319
 
        #ifdef GL_ARB_shader_objects
320
 
        if(RAS_EXT_support._ARB_shader_objects) {
321
 
                // switch back to fixed func
322
 
                bgl::blUseProgramObjectARB( 0 );
323
 
        }
324
 
        #endif//GL_ARB_shader_objects
325
 
 
326
 
        if( !enable ) {
327
 
                 // frame cleanup.
328
 
                DisableTexData();
 
217
        if(RAS_EXT_support._ARB_shader_objects && mShader) 
 
218
                mShader->SetProg(false);
 
219
 
 
220
        BL_Texture::DisableAllTextures();
 
221
        if( !enable )
329
222
                return;
330
 
        }
331
 
        
332
 
        DisableTexData();
 
223
 
 
224
        BL_Texture::ActivateFirst();
333
225
 
334
226
        if( mMaterial->IdMode == DEFAULT_BLENDER ) {
335
227
                setDefaultBlending();
337
229
        }
338
230
 
339
231
        if( mMaterial->IdMode == TEXFACE ) {
340
 
 
341
232
                // no material connected to the object
342
 
                if( mTextures[0] ) {
343
 
                        if( !mTextures[0].Ok() ) return;
344
 
                        bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
345
 
                        glBindTexture( GL_TEXTURE_2D, mTextures[0] );   
346
 
                        glEnable(GL_TEXTURE_2D);
347
 
                        setTextureEnvironment( -1 ); // modulate
348
 
                        setEnvMap( (mMaterial->mapping[0].mapping &USEREFL)!=0 );
 
233
                if( mTextures[0].Ok() ) {
 
234
                        mTextures[0].ActivateTexture();
 
235
                        mTextures[0].setTexEnv(0, true);
 
236
                        mTextures[0].SetMapping(mMaterial->mapping[0].mapping);
349
237
                        setDefaultBlending(); 
350
238
                }
351
239
                return;
352
240
        }
353
241
 
354
 
        int lastblend = 0;
355
 
 
356
 
        // for each enabled unit
 
242
        int mode = 0,i=0;
357
243
        for(i=0; (i<mMaterial->num_enabled); i++) {
358
244
                if( !mTextures[i].Ok() ) continue;
359
245
 
360
 
                bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
361
 
 
362
 
                #ifdef GL_ARB_texture_cube_map
363
 
                // use environment maps
364
 
                if( mMaterial->mapping[i].mapping &USEENV && RAS_EXT_support._ARB_texture_cube_map ) {
365
 
                        glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, mTextures[i] ); 
366
 
                        glEnable(GL_TEXTURE_CUBE_MAP_ARB);
367
 
                        setTextureEnvironment( i );
368
 
 
369
 
                        if( mMaterial->mapping[i].mapping &USEREFL )
370
 
                                setEnvMap( true, true );
371
 
                        else if(mMaterial->mapping[i].mapping &USEOBJ)
372
 
                                setObjectMatrixData(i);
373
 
                        else
374
 
                                setTexMatrixData( i );
375
 
                } 
376
 
                // 2d textures
377
 
                else { 
378
 
                #endif//GL_ARB_texture_cube_map
379
 
                        glBindTexture( GL_TEXTURE_2D, mTextures[i] );   
380
 
                        glEnable( GL_TEXTURE_2D );
381
 
                        setTextureEnvironment( i );
382
 
                        
383
 
                        if( mMaterial->mapping[i].mapping &USEREFL ){
384
 
                                setEnvMap( true );
385
 
                        }
386
 
                        else if(mMaterial->mapping[i].mapping &USEOBJ){
387
 
                                setObjectMatrixData(i);
388
 
                        }
389
 
                        else {
390
 
                                setTexMatrixData( i );
391
 
                        }
392
 
 
393
 
                #ifdef GL_ARB_texture_cube_map
394
 
                }
395
 
                #endif//GL_ARB_texture_cube_map
 
246
                mTextures[i].ActivateTexture();
 
247
                mTextures[i].setTexEnv(mMaterial);
 
248
                mode = mMaterial->mapping[i].mapping;
 
249
 
 
250
                if(mode &USEOBJ)
 
251
                        setObjectMatrixData(i, ras);
 
252
                else
 
253
                        mTextures[i].SetMapping(mode);
 
254
                
 
255
                if(!(mode &USEOBJ))
 
256
                        setTexMatrixData( i );
396
257
        }
 
258
 
397
259
        if(!mUserDefBlend) {
398
260
                setDefaultBlending();
399
 
        }else
400
 
        {
 
261
        }
 
262
        else {
401
263
                glEnable(GL_BLEND);
402
 
                // tested to be valid enums
403
264
                glBlendFunc(mBlendFunc[0], mBlendFunc[1]);
404
265
        }
405
 
 
406
 
        #endif//GL_ARB_multitexture
407
266
}
408
267
 
409
268
void
411
270
        RAS_IRasterizer* rasty, 
412
271
        TCachingInfo& cachingInfo)const
413
272
{
 
273
        KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
 
274
 
 
275
        // reset... 
 
276
        if(tmp->mMaterial->IsShared()) 
 
277
                cachingInfo =0;
 
278
        
414
279
        if (GetCachingInfo() != cachingInfo) {
415
 
                KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
416
280
 
417
281
                if (!cachingInfo)
418
 
                        tmp->setShaderData( false );
 
282
                        tmp->setShaderData( false, rasty);
419
283
                
420
284
                cachingInfo = GetCachingInfo();
421
285
        
422
286
                if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED ) {
423
 
                        tmp->setShaderData( true );
 
287
                        tmp->setShaderData( true, rasty);
424
288
                        rasty->EnableTextures(true);
425
289
                }
426
290
                else {
427
 
                        tmp->setShaderData( false );
 
291
                        tmp->setShaderData( false, rasty);
428
292
                        rasty->EnableTextures(false);
429
293
                }
430
294
 
434
298
                        rasty->SetCullFace(true);
435
299
 
436
300
                if (((mMaterial->ras_mode &WIRE)!=0) || mMaterial->mode & RAS_IRasterizer::KX_LINES)
 
301
                {               
 
302
                        if((mMaterial->ras_mode &WIRE)!=0) 
 
303
                                rasty->SetCullFace(false);
437
304
                        rasty->SetLines(true);
 
305
                }
438
306
                else
439
307
                        rasty->SetLines(false);
440
308
        }
441
 
        
442
 
        // shaders have access to the variables set here
443
 
        // via builtin GLSL variables
444
 
        // eg: gl_FrontMaterial.diffuse
445
 
        // --
446
 
        rasty->SetSpecularity(
447
 
                mMaterial->speccolor[0]*mMaterial->spec_f,
448
 
                mMaterial->speccolor[1]*mMaterial->spec_f,
449
 
                mMaterial->speccolor[2]*mMaterial->spec_f,
450
 
                mMaterial->spec_f
451
 
        );
452
 
 
453
 
        rasty->SetShinyness( mMaterial->hard );
454
 
 
455
 
        rasty->SetDiffuse(
456
 
                mMaterial->matcolor[0]*mMaterial->ref+mMaterial->emit, 
457
 
                mMaterial->matcolor[1]*mMaterial->ref+mMaterial->emit,
458
 
                mMaterial->matcolor[2]*mMaterial->ref+mMaterial->emit,
459
 
                1.0f);
460
 
 
461
 
        rasty->SetEmissive(     
462
 
                mMaterial->matcolor[0]*mMaterial->emit,
463
 
                mMaterial->matcolor[1]*mMaterial->emit,
464
 
                mMaterial->matcolor[2]*mMaterial->emit,
465
 
                1.0
466
 
                );
467
 
 
468
 
        // Lagan's patch...
469
 
        // added material factor
470
 
        rasty->SetAmbient(mMaterial->amb);
471
 
 
472
 
        if (mMaterial->material)
473
 
                rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
 
309
 
 
310
        ActivatGLMaterials(rasty);
 
311
        ActivateTexGen(rasty);
474
312
}
475
313
 
476
314
void
479
317
        TCachingInfo& cachingInfo
480
318
        )const
481
319
{
 
320
        KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
482
321
        if (GetCachingInfo() != cachingInfo) {
483
 
                KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
484
 
 
485
322
                if (!cachingInfo) 
486
 
                        tmp->setTexData( false );
 
323
                        tmp->setTexData( false,rasty );
487
324
                
488
325
                cachingInfo = GetCachingInfo();
489
326
 
490
327
                if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
491
 
                        tmp->setTexData( true );
 
328
                        tmp->setTexData( true,rasty  );
492
329
                        rasty->EnableTextures(true);
493
330
                }
494
331
                else{
495
 
                        tmp->setTexData( false );
 
332
                        tmp->setTexData( false,rasty);
496
333
                        rasty->EnableTextures(false);
497
334
                }
498
335
 
502
339
                        rasty->SetCullFace(true);
503
340
 
504
341
                if (((mMaterial->ras_mode &WIRE)!=0) || mMaterial->mode & RAS_IRasterizer::KX_LINES)
 
342
                {               
 
343
                        if((mMaterial->ras_mode &WIRE)!=0) 
 
344
                                rasty->SetCullFace(false);
505
345
                        rasty->SetLines(true);
 
346
                }
506
347
                else
507
348
                        rasty->SetLines(false);
508
349
        }
509
 
                
510
 
        rasty->SetSpecularity(
511
 
                mMaterial->speccolor[0]*mMaterial->spec_f,
512
 
                mMaterial->speccolor[1]*mMaterial->spec_f,
513
 
                mMaterial->speccolor[2]*mMaterial->spec_f,
514
 
                mMaterial->spec_f
515
 
        );
516
 
 
517
 
        rasty->SetShinyness( mMaterial->hard );
518
 
 
519
 
        rasty->SetDiffuse(
520
 
                mMaterial->matcolor[0]*mMaterial->ref+mMaterial->emit, 
521
 
                mMaterial->matcolor[1]*mMaterial->ref+mMaterial->emit,
522
 
                mMaterial->matcolor[2]*mMaterial->ref+mMaterial->emit,
523
 
                1.0f);
524
 
 
525
 
        rasty->SetEmissive(     
526
 
                mMaterial->matcolor[0]*mMaterial->emit,
527
 
                mMaterial->matcolor[1]*mMaterial->emit,
528
 
                mMaterial->matcolor[2]*mMaterial->emit,
529
 
                1.0
530
 
                );
531
 
        
532
 
        // Lagan's patch...
533
 
        // added material factor
534
 
        rasty->SetAmbient(mMaterial->amb);
535
 
 
536
 
        if (mMaterial->material)
537
 
                rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
 
350
        ActivatGLMaterials(rasty);
 
351
        ActivateTexGen(rasty);
538
352
}
539
353
 
 
354
 
540
355
bool 
541
356
KX_BlenderMaterial::Activate( 
542
357
        RAS_IRasterizer* rasty,  
544
359
        )const
545
360
{
546
361
        bool dopass = false;
547
 
        #ifdef GL_ARB_shader_objects
548
 
        if( RAS_EXT_support._ARB_shader_objects &&
549
 
                ( mShader && mShader->Ok() ) ) {
550
 
 
 
362
        if( RAS_EXT_support._ARB_shader_objects && ( mShader && mShader->Ok() ) ) {
551
363
                if( (mPass++) < mShader->getNumPass() ) {
552
364
                        ActivatShaders(rasty, cachingInfo);
553
365
                        dopass = true;
554
366
                        return dopass;
555
367
                }
556
368
                else {
557
 
                        bgl::blUseProgramObjectARB( 0 );
 
369
                        mShader->SetProg(false);
558
370
                        mPass = 0;
559
371
                        dopass = false;
560
372
                        return dopass;
561
373
                }
562
374
        }
563
375
        else {
564
 
        #endif//GL_ARB_shader_objects
565
376
                switch (mPass++)
566
377
                {
567
378
                        case 0:
573
384
                                dopass = false;
574
385
                                break;
575
386
                }
576
 
        #ifdef GL_ARB_shader_objects
577
387
        }
578
 
        #endif//GL_ARB_shader_objects
579
388
        return dopass;
580
389
}
581
390
 
582
 
void KX_BlenderMaterial::setTextureEnvironment( int textureIndex )
583
 
{
584
 
#ifndef GL_ARB_texture_env_combine
585
 
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
586
 
        return;
587
 
#else
588
 
        if(textureIndex == -1 || !RAS_EXT_support._ARB_texture_env_combine){
589
 
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
590
 
                return;
591
 
        }
592
 
 
593
 
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
594
 
 
595
 
        GLfloat blend_operand           = GL_SRC_COLOR;
596
 
        GLfloat blend_operand_prev  = GL_SRC_COLOR;
597
 
 
598
 
        GLenum combiner = GL_COMBINE_RGB_ARB;
599
 
        GLenum source0 = GL_SOURCE0_RGB_ARB;
600
 
        GLenum source1 = GL_SOURCE1_RGB_ARB;
601
 
        GLenum source2 = GL_SOURCE2_RGB_ARB;
602
 
        GLenum op0 = GL_OPERAND0_RGB_ARB;
603
 
        GLenum op1 = GL_OPERAND1_RGB_ARB;
604
 
        GLenum op2 = GL_OPERAND2_RGB_ARB;
605
 
        GLfloat alphaOp = GL_SRC_ALPHA;
606
 
 
607
 
        // switch to alpha combiners
608
 
        if( (mMaterial->flag[textureIndex] &TEXALPHA) ) {
609
 
                combiner = GL_COMBINE_ALPHA_ARB;
610
 
                source0  = GL_SOURCE0_ALPHA_ARB;
611
 
                source1 = GL_SOURCE1_ALPHA_ARB;
612
 
                source2 = GL_SOURCE2_ALPHA_ARB;
613
 
                op0 = GL_OPERAND0_ALPHA_ARB;
614
 
                op1 = GL_OPERAND1_ALPHA_ARB;
615
 
                op2 = GL_OPERAND2_ALPHA_ARB;
616
 
                blend_operand = GL_SRC_ALPHA;
 
391
void KX_BlenderMaterial::ActivateMeshSlot(const KX_MeshSlot & ms, RAS_IRasterizer* rasty) const
 
392
{
 
393
        if(mShader && RAS_EXT_support._ARB_shader_objects)
 
394
                mShader->Update(ms, rasty);
 
395
}
 
396
 
 
397
void KX_BlenderMaterial::ActivatGLMaterials( RAS_IRasterizer* rasty )const
 
398
{
 
399
        rasty->SetSpecularity(
 
400
                mMaterial->speccolor[0]*mMaterial->spec_f,
 
401
                mMaterial->speccolor[1]*mMaterial->spec_f,
 
402
                mMaterial->speccolor[2]*mMaterial->spec_f,
 
403
                mMaterial->spec_f
 
404
        );
 
405
 
 
406
        rasty->SetShinyness( mMaterial->hard );
 
407
 
 
408
        rasty->SetDiffuse(
 
409
                mMaterial->matcolor[0]*mMaterial->ref+mMaterial->emit, 
 
410
                mMaterial->matcolor[1]*mMaterial->ref+mMaterial->emit,
 
411
                mMaterial->matcolor[2]*mMaterial->ref+mMaterial->emit,
 
412
                1.0f);
 
413
 
 
414
        rasty->SetEmissive(     
 
415
                mMaterial->matcolor[0]*mMaterial->emit,
 
416
                mMaterial->matcolor[1]*mMaterial->emit,
 
417
                mMaterial->matcolor[2]*mMaterial->emit,
 
418
                1.0 );
 
419
 
 
420
        rasty->SetAmbient(mMaterial->amb);
 
421
        if (mMaterial->material)
 
422
                rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
 
423
}
 
424
 
 
425
 
 
426
void KX_BlenderMaterial::ActivateTexGen(RAS_IRasterizer *ras) const
 
427
{
 
428
        if(mShader && RAS_EXT_support._ARB_shader_objects)
 
429
                if(mShader->GetAttribute() == BL_Shader::SHD_TANGENT)
 
430
                        ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT);
 
431
 
 
432
        for(int i=0; i<mMaterial->num_enabled; i++) {
 
433
                int mode = mMaterial->mapping[i].mapping;
617
434
                
618
 
                // invert
619
 
                if(mMaterial->flag[textureIndex] &TEXNEG) {
620
 
                        blend_operand_prev = GL_ONE_MINUS_SRC_ALPHA;
621
 
                        blend_operand = GL_ONE_MINUS_SRC_ALPHA;
622
 
                }
623
 
        }
624
 
        else {
625
 
                if(mMaterial->flag[textureIndex] &TEXNEG) {
626
 
                        blend_operand_prev=GL_ONE_MINUS_SRC_COLOR;
627
 
                        blend_operand = GL_ONE_MINUS_SRC_COLOR;
628
 
                }
629
 
        }
630
 
        bool using_alpha = false;
631
 
 
632
 
        if(mMaterial->flag[textureIndex] &USEALPHA){
633
 
                alphaOp = GL_ONE_MINUS_SRC_ALPHA;
634
 
                using_alpha=true;
635
 
        }
636
 
        else if(mMaterial->flag[textureIndex] &USENEGALPHA){
637
 
                alphaOp = GL_SRC_ALPHA;
638
 
                using_alpha = true;
639
 
        }
640
 
 
641
 
        switch( mMaterial->blend_mode[textureIndex] ) {
642
 
                case BLEND_MIX:
643
 
                        {
644
 
                                // ------------------------------
645
 
                                if(!using_alpha) {
646
 
                                        GLfloat base_col[4];
647
 
                                        base_col[0]      = base_col[1]  = base_col[2]  = 0.f;
648
 
                                        base_col[3]      = 1.f-mMaterial->color_blend[textureIndex];
649
 
                                        glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR,base_col );
650
 
                                }
651
 
                                glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_INTERPOLATE_ARB);
652
 
                                glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB);
653
 
                                glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
654
 
                                glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
655
 
                                glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
656
 
                                if(!using_alpha)
657
 
                                        glTexEnvf(      GL_TEXTURE_ENV, source2,        GL_CONSTANT_ARB );
658
 
                                else
659
 
                                        glTexEnvf(      GL_TEXTURE_ENV, source2,        GL_TEXTURE );
660
 
 
661
 
                                glTexEnvf(      GL_TEXTURE_ENV, op2,            alphaOp);
662
 
                        }break;
663
 
                case BLEND_MUL: 
664
 
                        {
665
 
                                // ------------------------------
666
 
                                glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_MODULATE);
667
 
                                glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB);
668
 
                                glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev);
669
 
                                glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
670
 
                                if(using_alpha)
671
 
                                        glTexEnvf(      GL_TEXTURE_ENV, op1,            alphaOp);
672
 
                                else
673
 
                                        glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
674
 
                        }break;
675
 
                case BLEND_ADD: 
676
 
                        {
677
 
                                // ------------------------------
678
 
                                glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_ADD_SIGNED_ARB);
679
 
                                glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB );
680
 
                                glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
681
 
                                glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
682
 
                                if(using_alpha)
683
 
                                        glTexEnvf(      GL_TEXTURE_ENV, op1,            alphaOp);
684
 
                                else
685
 
                                        glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
686
 
                        }break;
687
 
                case BLEND_SUB: 
688
 
                        {
689
 
                                // ------------------------------
690
 
                                glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_SUBTRACT_ARB);
691
 
                                glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB );
692
 
                                glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
693
 
                                glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
694
 
                                glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
695
 
                        }break;
696
 
                case BLEND_SCR: 
697
 
                        {
698
 
                                // ------------------------------
699
 
                                glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_ADD);
700
 
                                glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB );
701
 
                                glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
702
 
                                glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
703
 
                                if(using_alpha)
704
 
                                        glTexEnvf(      GL_TEXTURE_ENV, op1,            alphaOp);
705
 
                                else
706
 
                                        glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
707
 
                        } break;
708
 
        }
709
 
        glTexEnvf(      GL_TEXTURE_ENV, GL_RGB_SCALE_ARB,       1.0);
710
 
#endif //!GL_ARB_texture_env_combine
 
435
                if( mode &(USEREFL|USEOBJ))
 
436
                        ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_GEN, i);
 
437
                else if(mode &USEORCO)
 
438
                        ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_ORCO, i);
 
439
                else if(mode &USENORM)
 
440
                        ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_NORM, i);
 
441
                else if(mode &USEUV)
 
442
                        ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_UV1, i);
 
443
                else if(mode &USETANG)
 
444
                        ras->SetTexCoords(RAS_IRasterizer::RAS_TEXTANGENT, i);
 
445
                else 
 
446
                        ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
 
447
        }
711
448
}
712
449
 
713
450
bool KX_BlenderMaterial::setDefaultBlending()
728
465
        return false;
729
466
}
730
467
 
731
 
void KX_BlenderMaterial::setEnvMap(bool val, bool cube)
732
 
{
733
 
        #ifdef GL_ARB_texture_cube_map
734
 
        if( cube && RAS_EXT_support._ARB_texture_cube_map ) 
735
 
        {
736
 
                glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB );
737
 
                glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
738
 
                glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
739
 
 
740
 
                glEnable(GL_TEXTURE_GEN_S);
741
 
                glEnable(GL_TEXTURE_GEN_T);
742
 
                glEnable(GL_TEXTURE_GEN_R);
743
 
        }
744
 
        else {
745
 
        #endif//GL_ARB_texture_cube_map
746
 
                if( val ) {
747
 
                        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
748
 
                        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
749
 
                        
750
 
                        glEnable(GL_TEXTURE_GEN_S);
751
 
                        glEnable(GL_TEXTURE_GEN_T);
752
 
                        glEnable(GL_TEXTURE_GEN_R);
753
 
                }
754
 
                else {
755
 
                        glDisable(GL_TEXTURE_GEN_S);
756
 
                        glDisable(GL_TEXTURE_GEN_T);
757
 
                        glDisable(GL_TEXTURE_GEN_R);
758
 
                }
759
 
        #ifdef GL_ARB_texture_cube_map
760
 
        }
761
 
        #endif//GL_ARB_texture_cube_map
762
 
}
763
 
 
764
 
 
765
468
void KX_BlenderMaterial::setTexMatrixData(int i)
766
469
{
767
470
        glMatrixMode(GL_TEXTURE);
768
471
        glLoadIdentity();
769
472
 
770
 
        glScalef( 
771
 
                mMaterial->mapping[i].scale[0], 
772
 
                mMaterial->mapping[i].scale[1], 
773
 
                mMaterial->mapping[i].scale[2]
774
 
        );
 
473
#ifdef GL_ARB_texture_cube_map
 
474
        if( RAS_EXT_support._ARB_texture_cube_map && 
 
475
                mTextures[i].GetTextureType() == GL_TEXTURE_CUBE_MAP_ARB && 
 
476
                mMaterial->mapping[i].mapping & USEREFL) {
 
477
                glScalef( 
 
478
                        mMaterial->mapping[i].scale[0], 
 
479
                        -mMaterial->mapping[i].scale[1], 
 
480
                        -mMaterial->mapping[i].scale[2]
 
481
                );
 
482
        }
 
483
        else
 
484
#endif
 
485
        {
 
486
                glScalef( 
 
487
                        mMaterial->mapping[i].scale[0], 
 
488
                        mMaterial->mapping[i].scale[1], 
 
489
                        mMaterial->mapping[i].scale[2]
 
490
                );
 
491
        }
775
492
        glTranslatef(
776
493
                mMaterial->mapping[i].offsets[0],
777
494
                mMaterial->mapping[i].offsets[1], 
793
510
                param[2] = 1.f;
794
511
}
795
512
 
796
 
 
797
 
void KX_BlenderMaterial::setObjectMatrixData(int i)
 
513
void KX_BlenderMaterial::setObjectMatrixData(int i, RAS_IRasterizer *ras)
798
514
{
799
 
        // will work without cubemaps
800
 
        // but a cubemap will look the best
801
515
        KX_GameObject *obj = 
802
516
                (KX_GameObject*)
803
517
                mScene->GetObjectList()->FindValue(mMaterial->mapping[i].objconame);
804
518
 
805
 
        if(!obj)
806
 
                return;
 
519
        if(!obj) return;
807
520
 
808
521
        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
809
522
        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
826
539
        glEnable(GL_TEXTURE_GEN_T);
827
540
        glEnable(GL_TEXTURE_GEN_R);
828
541
 
829
 
        float matr[16];
830
 
        glGetFloatv(GL_MODELVIEW_MATRIX, matr);
831
 
        MT_Matrix4x4 mvmat(matr);
 
542
        MT_Matrix4x4 mvmat;
 
543
        ras->GetViewMatrix(mvmat);
832
544
 
833
545
        glMatrixMode(GL_TEXTURE);
834
546
        glLoadIdentity();
848
560
 
849
561
}
850
562
 
851
 
 
852
563
// ------------------------------------
853
564
void KX_BlenderMaterial::UpdateIPO(
854
565
        MT_Vector4 rgba,
879
590
        KX_PYMETHODTABLE( KX_BlenderMaterial, getShader ),
880
591
        KX_PYMETHODTABLE( KX_BlenderMaterial, getMaterialIndex ),
881
592
        KX_PYMETHODTABLE( KX_BlenderMaterial, setBlending ),
882
 
//      KX_PYMETHODTABLE( KX_BlenderMaterial, getTexture ),
883
 
//      KX_PYMETHODTABLE( KX_BlenderMaterial, setTexture ),
884
 
 
885
593
        {NULL,NULL} //Sentinel
886
594
};
887
595
 
911
619
 
912
620
PyObject* KX_BlenderMaterial::_getattr(const STR_String& attr)
913
621
{
914
 
        // nodda ?
915
622
        _getattr_up(PyObjectPlus);
916
623
}
917
624
 
956
663
 
957
664
                if(!mShader && !mModified) {
958
665
                        mShader = new BL_Shader();
959
 
                        for(int i= 0; i<mMaterial->num_enabled; i++) {
960
 
                                if(mMaterial->mapping[i].mapping & USEENV )
961
 
                                        mShader->InitializeSampler(SAMP_CUBE, i, 0, mTextures[i]);
962
 
                                else
963
 
                                        mShader->InitializeSampler(SAMP_2D, i, 0, mTextures[i]);
964
 
                        }
965
666
                        mModified = true;
966
667
                }
967
668
 
968
669
                if(mShader && !mShader->GetError()) {
 
670
                        mMaterial->SetSharedMaterial(true);
969
671
                        Py_INCREF(mShader);
970
672
                        return mShader;
971
673
                }else
994
696
#endif//GL_ARB_shader_objects
995
697
}
996
698
 
 
699
 
997
700
KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getMaterialIndex, "getMaterialIndex()")
998
701
{
999
702
        return PyInt_FromLong( mMaterial->material_index );