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