add missing gpl headers
[blender.git] / source / gameengine / Ketsji / BL_BlenderShader.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file gameengine/Ketsji/BL_BlenderShader.cpp
22  *  \ingroup ketsji
23  */
24
25 #include "DNA_customdata_types.h"
26 #include "DNA_material_types.h"
27 #include "DNA_scene_types.h"
28
29 #include "BLI_utildefines.h"
30
31 #include "BKE_global.h"
32 #include "BKE_main.h"
33 #include "BKE_DerivedMesh.h"
34
35 #include "BL_BlenderShader.h"
36 #include "BL_Material.h"
37
38 #include "GPU_extensions.h"
39 #include "GPU_material.h"
40
41 #include "RAS_BucketManager.h"
42 #include "RAS_MeshObject.h"
43 #include "RAS_IRasterizer.h"
44  
45 BL_BlenderShader::BL_BlenderShader(KX_Scene *scene, struct Material *ma, int lightlayer)
46 :
47         mScene(scene),
48         mMat(ma),
49         mLightLayer(lightlayer),
50         mGPUMat(NULL)
51 {
52         mBlenderScene = scene->GetBlenderScene();
53         mAlphaBlend = GPU_BLEND_SOLID;
54
55         ReloadMaterial();
56 }
57
58 BL_BlenderShader::~BL_BlenderShader()
59 {
60         if (mGPUMat)
61                 GPU_material_unbind(mGPUMat);
62 }
63
64 void BL_BlenderShader::ReloadMaterial()
65 {
66         mGPUMat = (mMat) ? GPU_material_from_blender(mBlenderScene, mMat) : NULL;
67 }
68
69 void BL_BlenderShader::SetProg(bool enable, double time, RAS_IRasterizer* rasty)
70 {
71         if (VerifyShader()) {
72                 if (enable) {
73                         assert(rasty != NULL); // XXX Kinda hacky, but SetProg() should always have the rasterizer if enable is true
74
75                         float viewmat[4][4], viewinvmat[4][4];
76                         const MT_Matrix4x4& view = rasty->GetViewMatrix();
77                         const MT_Matrix4x4& viewinv = rasty->GetViewInvMatrix();
78                         view.getValue((float*)viewmat);
79                         viewinv.getValue((float*)viewinvmat);
80
81                         GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1, viewmat, viewinvmat);
82                 }
83                 else
84                         GPU_material_unbind(mGPUMat);
85         }
86 }
87
88 int BL_BlenderShader::GetAttribNum()
89 {
90         GPUVertexAttribs attribs;
91         int i, enabled = 0;
92
93         if (!VerifyShader())
94                 return enabled;
95
96         GPU_material_vertex_attributes(mGPUMat, &attribs);
97
98         for (i = 0; i < attribs.totlayer; i++)
99                 if (attribs.layer[i].glindex+1 > enabled)
100                         enabled= attribs.layer[i].glindex+1;
101         
102         if (enabled > BL_MAX_ATTRIB)
103                 enabled = BL_MAX_ATTRIB;
104
105         return enabled;
106 }
107
108 void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
109 {
110         GPUVertexAttribs attribs;
111         GPUMaterial *gpumat;
112         int i, attrib_num, uv = 0;
113
114         ras->SetAttribNum(0);
115
116         if (!VerifyShader())
117                 return;
118         
119         gpumat = mGPUMat;
120         if (ras->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED || (ras->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW &&
121                         mat->alphablend != GEMAT_SOLID && !ras->GetUsingOverrideShader())) {
122                 GPU_material_vertex_attributes(gpumat, &attribs);
123                 attrib_num = GetAttribNum();
124
125                 ras->SetTexCoordNum(0);
126                 ras->SetAttribNum(attrib_num);
127                 for (i = 0; i < attrib_num; i++)
128                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
129
130                 for (i = 0; i < attribs.totlayer; i++) {
131                         if (attribs.layer[i].glindex > attrib_num)
132                                 continue;
133
134                         if (attribs.layer[i].type == CD_MTFACE)
135                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV, attribs.layer[i].glindex, uv++);
136                         else if (attribs.layer[i].type == CD_TANGENT)
137                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT, attribs.layer[i].glindex);
138                         else if (attribs.layer[i].type == CD_ORCO)
139                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_ORCO, attribs.layer[i].glindex);
140                         else if (attribs.layer[i].type == CD_NORMAL)
141                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_NORM, attribs.layer[i].glindex);
142                         else if (attribs.layer[i].type == CD_MCOL)
143                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_VCOL, attribs.layer[i].glindex);
144                         else
145                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, attribs.layer[i].glindex);
146                 }
147         }
148 }
149
150 void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
151 {
152         float obmat[4][4], obcol[4];
153         GPUMaterial *gpumat;
154
155         gpumat = mGPUMat;
156
157         if (!gpumat || !GPU_material_bound(gpumat))
158                 return;
159
160         MT_Matrix4x4 model;
161         model.setValue(ms.m_OpenGLMatrix);
162
163         // note: getValue gives back column major as needed by OpenGL
164         model.getValue((float*)obmat);
165
166         if (ms.m_bObjectColor)
167                 ms.m_RGBAcolor.getValue((float *)obcol);
168         else
169                 obcol[0] = obcol[1] = obcol[2] = obcol[3] = 1.0f;
170
171         float auto_bump_scale = ms.m_pDerivedMesh!=0 ? ms.m_pDerivedMesh->auto_bump_scale : 1.0f;
172         GPU_material_bind_uniforms(gpumat, obmat, obcol, auto_bump_scale);
173
174         mAlphaBlend = GPU_material_alpha_blend(gpumat, obcol);
175 }
176
177 int BL_BlenderShader::GetAlphaBlend()
178 {
179         return mAlphaBlend;
180 }
181
182 bool BL_BlenderShader::Equals(BL_BlenderShader *blshader)
183 {
184         /* to avoid unneeded state switches */
185         return (blshader && mMat == blshader->mMat && mLightLayer == blshader->mLightLayer);
186 }
187
188 // eof