doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / Ketsji / BL_BlenderShader.cpp
1
2 #include "DNA_customdata_types.h"
3 #include "DNA_material_types.h"
4 #include "DNA_scene_types.h"
5
6 #include "BKE_global.h"
7 #include "BKE_main.h"
8
9 #include "BL_BlenderShader.h"
10 #include "BL_Material.h"
11
12 #include "GPU_extensions.h"
13 #include "GPU_material.h"
14
15 #include "RAS_BucketManager.h"
16 #include "RAS_MeshObject.h"
17 #include "RAS_IRasterizer.h"
18  
19 BL_BlenderShader::BL_BlenderShader(KX_Scene *scene, struct Material *ma, int lightlayer)
20 :
21         mScene(scene),
22         mMat(ma),
23         mLightLayer(lightlayer),
24         mGPUMat(NULL)
25 {
26         mBlenderScene = scene->GetBlenderScene();
27         mBlendMode = GPU_BLEND_SOLID;
28
29         ReloadMaterial();
30 }
31
32 BL_BlenderShader::~BL_BlenderShader()
33 {
34         if(mGPUMat)
35                 GPU_material_unbind(mGPUMat);
36 }
37
38 void BL_BlenderShader::ReloadMaterial()
39 {
40         mGPUMat = (mMat) ? GPU_material_from_blender(mBlenderScene, mMat) : NULL;
41 }
42
43 void BL_BlenderShader::SetProg(bool enable, double time)
44 {
45         if(VerifyShader()) {
46                 if(enable)
47                         GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1);
48                 else
49                         GPU_material_unbind(mGPUMat);
50         }
51 }
52
53 int BL_BlenderShader::GetAttribNum()
54 {
55         GPUVertexAttribs attribs;
56         int i, enabled = 0;
57
58         if(!VerifyShader())
59                 return enabled;
60
61         GPU_material_vertex_attributes(mGPUMat, &attribs);
62
63     for(i = 0; i < attribs.totlayer; i++)
64                 if(attribs.layer[i].glindex+1 > enabled)
65                         enabled= attribs.layer[i].glindex+1;
66         
67         if(enabled > BL_MAX_ATTRIB)
68                 enabled = BL_MAX_ATTRIB;
69
70         return enabled;
71 }
72
73 void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
74 {
75         GPUVertexAttribs attribs;
76         GPUMaterial *gpumat;
77         int i, attrib_num;
78
79         ras->SetAttribNum(0);
80
81         if(!VerifyShader())
82                 return;
83         
84         gpumat = mGPUMat;
85
86         if(ras->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
87                 GPU_material_vertex_attributes(gpumat, &attribs);
88                 attrib_num = GetAttribNum();
89
90                 ras->SetTexCoordNum(0);
91                 ras->SetAttribNum(attrib_num);
92                 for(i=0; i<attrib_num; i++)
93                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
94
95                 for(i = 0; i < attribs.totlayer; i++) {
96                         if(attribs.layer[i].glindex > attrib_num)
97                                 continue;
98
99                         if(attribs.layer[i].type == CD_MTFACE) {
100                                 if(!mat->uvName.IsEmpty() && strcmp(mat->uvName.ReadPtr(), attribs.layer[i].name) == 0)
101                                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV1, attribs.layer[i].glindex);
102                                 else if(!mat->uv2Name.IsEmpty() && strcmp(mat->uv2Name.ReadPtr(), attribs.layer[i].name) == 0)
103                                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV2, attribs.layer[i].glindex);
104                                 else
105                                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV1, attribs.layer[i].glindex);
106                         }
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         GPU_material_bind_uniforms(gpumat, obmat, viewmat, viewinvmat, obcol);
147
148         mBlendMode = GPU_material_blend_mode(gpumat, obcol);
149 }
150
151 int BL_BlenderShader::GetBlendMode()
152 {
153         return mBlendMode;
154 }
155
156 bool BL_BlenderShader::Equals(BL_BlenderShader *blshader)
157 {
158         /* to avoid unneeded state switches */
159         return (blshader && mMat == blshader->mMat && mLightLayer == blshader->mLightLayer);
160 }
161
162 // eof