Merged changes in the trunk up to revision 35203.
[blender.git] / source / gameengine / Ketsji / BL_BlenderShader.cpp
1 /** \file gameengine/Ketsji/BL_BlenderShader.cpp
2  *  \ingroup ketsji
3  */
4
5 #include "DNA_customdata_types.h"
6 #include "DNA_material_types.h"
7 #include "DNA_scene_types.h"
8
9 #include "BKE_global.h"
10 #include "BKE_main.h"
11
12 #include "BL_BlenderShader.h"
13 #include "BL_Material.h"
14
15 #include "GPU_extensions.h"
16 #include "GPU_material.h"
17
18 #include "RAS_BucketManager.h"
19 #include "RAS_MeshObject.h"
20 #include "RAS_IRasterizer.h"
21  
22 BL_BlenderShader::BL_BlenderShader(KX_Scene *scene, struct Material *ma, int lightlayer)
23 :
24         mScene(scene),
25         mMat(ma),
26         mLightLayer(lightlayer),
27         mGPUMat(NULL)
28 {
29         mBlenderScene = scene->GetBlenderScene();
30         mBlendMode = GPU_BLEND_SOLID;
31
32         ReloadMaterial();
33 }
34
35 BL_BlenderShader::~BL_BlenderShader()
36 {
37         if(mGPUMat)
38                 GPU_material_unbind(mGPUMat);
39 }
40
41 void BL_BlenderShader::ReloadMaterial()
42 {
43         mGPUMat = (mMat) ? GPU_material_from_blender(mBlenderScene, mMat) : NULL;
44 }
45
46 void BL_BlenderShader::SetProg(bool enable, double time)
47 {
48         if(VerifyShader()) {
49                 if(enable)
50                         GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1);
51                 else
52                         GPU_material_unbind(mGPUMat);
53         }
54 }
55
56 int BL_BlenderShader::GetAttribNum()
57 {
58         GPUVertexAttribs attribs;
59         int i, enabled = 0;
60
61         if(!VerifyShader())
62                 return enabled;
63
64         GPU_material_vertex_attributes(mGPUMat, &attribs);
65
66     for(i = 0; i < attribs.totlayer; i++)
67                 if(attribs.layer[i].glindex+1 > enabled)
68                         enabled= attribs.layer[i].glindex+1;
69         
70         if(enabled > BL_MAX_ATTRIB)
71                 enabled = BL_MAX_ATTRIB;
72
73         return enabled;
74 }
75
76 void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
77 {
78         GPUVertexAttribs attribs;
79         GPUMaterial *gpumat;
80         int i, attrib_num;
81
82         ras->SetAttribNum(0);
83
84         if(!VerifyShader())
85                 return;
86         
87         gpumat = mGPUMat;
88
89         if(ras->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
90                 GPU_material_vertex_attributes(gpumat, &attribs);
91                 attrib_num = GetAttribNum();
92
93                 ras->SetTexCoordNum(0);
94                 ras->SetAttribNum(attrib_num);
95                 for(i=0; i<attrib_num; i++)
96                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
97
98                 for(i = 0; i < attribs.totlayer; i++) {
99                         if(attribs.layer[i].glindex > attrib_num)
100                                 continue;
101
102                         if(attribs.layer[i].type == CD_MTFACE) {
103                                 if(!mat->uvName.IsEmpty() && strcmp(mat->uvName.ReadPtr(), attribs.layer[i].name) == 0)
104                                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV1, attribs.layer[i].glindex);
105                                 else if(!mat->uv2Name.IsEmpty() && strcmp(mat->uv2Name.ReadPtr(), attribs.layer[i].name) == 0)
106                                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV2, attribs.layer[i].glindex);
107                                 else
108                                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV1, attribs.layer[i].glindex);
109                         }
110                         else if(attribs.layer[i].type == CD_TANGENT)
111                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT, attribs.layer[i].glindex);
112                         else if(attribs.layer[i].type == CD_ORCO)
113                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_ORCO, attribs.layer[i].glindex);
114                         else if(attribs.layer[i].type == CD_NORMAL)
115                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_NORM, attribs.layer[i].glindex);
116                         else if(attribs.layer[i].type == CD_MCOL)
117                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_VCOL, attribs.layer[i].glindex);
118                         else
119                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, attribs.layer[i].glindex);
120                 }
121         }
122 }
123
124 void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
125 {
126         float obmat[4][4], viewmat[4][4], viewinvmat[4][4], obcol[4];
127         GPUMaterial *gpumat;
128
129         gpumat = mGPUMat;
130
131         if(!gpumat || !GPU_material_bound(gpumat))
132                 return;
133
134         MT_Matrix4x4 model;
135         model.setValue(ms.m_OpenGLMatrix);
136         const MT_Matrix4x4& view = rasty->GetViewMatrix();
137         const MT_Matrix4x4& viewinv = rasty->GetViewInvMatrix();
138
139         // note: getValue gives back column major as needed by OpenGL
140         model.getValue((float*)obmat);
141         view.getValue((float*)viewmat);
142         viewinv.getValue((float*)viewinvmat);
143
144         if(ms.m_bObjectColor)
145                 ms.m_RGBAcolor.getValue((float*)obcol);
146         else
147                 obcol[0]= obcol[1]= obcol[2]= obcol[3]= 1.0f;
148
149         GPU_material_bind_uniforms(gpumat, obmat, viewmat, viewinvmat, obcol);
150
151         mBlendMode = GPU_material_blend_mode(gpumat, obcol);
152 }
153
154 int BL_BlenderShader::GetBlendMode()
155 {
156         return mBlendMode;
157 }
158
159 bool BL_BlenderShader::Equals(BL_BlenderShader *blshader)
160 {
161         /* to avoid unneeded state switches */
162         return (blshader && mMat == blshader->mMat && mLightLayer == blshader->mLightLayer);
163 }
164
165 // eof