Fix a few compiler warnings reported by clang.
[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         mMat(ma),
48         mLightLayer(lightlayer),
49         mGPUMat(NULL)
50 {
51         mBlenderScene = scene->GetBlenderScene();
52         mAlphaBlend = GPU_BLEND_SOLID;
53
54         ReloadMaterial();
55 }
56
57 BL_BlenderShader::~BL_BlenderShader()
58 {
59         if (mGPUMat)
60                 GPU_material_unbind(mGPUMat);
61 }
62
63 void BL_BlenderShader::ReloadMaterial()
64 {
65         mGPUMat = (mMat) ? GPU_material_from_blender(mBlenderScene, mMat) : NULL;
66 }
67
68 void BL_BlenderShader::SetProg(bool enable, double time, RAS_IRasterizer* rasty)
69 {
70         if (VerifyShader()) {
71                 if (enable) {
72                         assert(rasty != NULL); // XXX Kinda hacky, but SetProg() should always have the rasterizer if enable is true
73
74                         float viewmat[4][4], viewinvmat[4][4];
75                         const MT_Matrix4x4& view = rasty->GetViewMatrix();
76                         const MT_Matrix4x4& viewinv = rasty->GetViewInvMatrix();
77                         view.getValue((float*)viewmat);
78                         viewinv.getValue((float*)viewinvmat);
79
80                         GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1, viewmat, viewinvmat);
81                 }
82                 else
83                         GPU_material_unbind(mGPUMat);
84         }
85 }
86
87 int BL_BlenderShader::GetAttribNum()
88 {
89         GPUVertexAttribs attribs;
90         int i, enabled = 0;
91
92         if (!VerifyShader())
93                 return enabled;
94
95         GPU_material_vertex_attributes(mGPUMat, &attribs);
96
97         for (i = 0; i < attribs.totlayer; i++)
98                 if (attribs.layer[i].glindex+1 > enabled)
99                         enabled= attribs.layer[i].glindex+1;
100         
101         if (enabled > BL_MAX_ATTRIB)
102                 enabled = BL_MAX_ATTRIB;
103
104         return enabled;
105 }
106
107 void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
108 {
109         GPUVertexAttribs attribs;
110         GPUMaterial *gpumat;
111         int i, attrib_num, uv = 0;
112
113         ras->SetAttribNum(0);
114
115         if (!VerifyShader())
116                 return;
117         
118         gpumat = mGPUMat;
119         if (ras->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED || (ras->GetDrawingMode() == RAS_IRasterizer::KX_SHADOW &&
120                         mat->alphablend != GEMAT_SOLID && !ras->GetUsingOverrideShader())) {
121                 GPU_material_vertex_attributes(gpumat, &attribs);
122                 attrib_num = GetAttribNum();
123
124                 ras->SetTexCoordNum(0);
125                 ras->SetAttribNum(attrib_num);
126                 for (i = 0; i < attrib_num; i++)
127                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
128
129                 for (i = 0; i < attribs.totlayer; i++) {
130                         if (attribs.layer[i].glindex > attrib_num)
131                                 continue;
132
133                         if (attribs.layer[i].type == CD_MTFACE)
134                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV, attribs.layer[i].glindex, uv++);
135                         else if (attribs.layer[i].type == CD_TANGENT)
136                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT, attribs.layer[i].glindex);
137                         else if (attribs.layer[i].type == CD_ORCO)
138                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_ORCO, attribs.layer[i].glindex);
139                         else if (attribs.layer[i].type == CD_NORMAL)
140                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_NORM, attribs.layer[i].glindex);
141                         else if (attribs.layer[i].type == CD_MCOL)
142                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_VCOL, attribs.layer[i].glindex);
143                         else
144                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, attribs.layer[i].glindex);
145                 }
146         }
147 }
148
149 void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
150 {
151         float obmat[4][4], obcol[4];
152         GPUMaterial *gpumat;
153
154         gpumat = mGPUMat;
155
156         if (!gpumat || !GPU_material_bound(gpumat))
157                 return;
158
159         MT_Matrix4x4 model;
160         model.setValue(ms.m_OpenGLMatrix);
161
162         // note: getValue gives back column major as needed by OpenGL
163         model.getValue((float*)obmat);
164
165         if (ms.m_bObjectColor)
166                 ms.m_RGBAcolor.getValue((float *)obcol);
167         else
168                 obcol[0] = obcol[1] = obcol[2] = obcol[3] = 1.0f;
169
170         float auto_bump_scale = ms.m_pDerivedMesh!=0 ? ms.m_pDerivedMesh->auto_bump_scale : 1.0f;
171         GPU_material_bind_uniforms(gpumat, obmat, obcol, auto_bump_scale);
172
173         mAlphaBlend = GPU_material_alpha_blend(gpumat, obcol);
174 }
175
176 int BL_BlenderShader::GetAlphaBlend()
177 {
178         return mAlphaBlend;
179 }
180
181 bool BL_BlenderShader::Equals(BL_BlenderShader *blshader)
182 {
183         /* to avoid unneeded state switches */
184         return (blshader && mMat == blshader->mMat && mLightLayer == blshader->mLightLayer);
185 }
186
187 // eof