* Fixed compiler warnings concerning the lack of a newline at the
[blender.git] / source / blender / freestyle / intern / blender_interface / BlenderStrokeRenderer.cpp
1 # include "BlenderStrokeRenderer.h"
2 # include "../stroke/Canvas.h"
3 # include "../application/AppConfig.h"
4
5 # include "../rendering/GLStrokeRenderer.h"
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 #include "MEM_guardedalloc.h"
12
13 #include "DNA_camera_types.h"
14 #include "DNA_customdata_types.h"
15 #include "DNA_listBase.h"
16 #include "DNA_mesh_types.h"
17 #include "DNA_meshdata_types.h"
18 #include "DNA_screen_types.h"
19
20 #include "BKE_customdata.h"
21 #include "BKE_global.h"
22 #include "BKE_library.h" /* free_libblock */
23 #include "BKE_material.h"
24 #include "BKE_main.h" /* struct Main */
25 #include "BKE_object.h"
26 #include "BKE_scene.h"
27
28 #include "RE_pipeline.h"
29
30 #ifdef __cplusplus
31 }
32 #endif
33
34
35 BlenderStrokeRenderer::BlenderStrokeRenderer(Render* re)
36 :StrokeRenderer(){
37         
38         // TEMPORARY - need a  texture manager
39         _textureManager = new GLTextureManager;
40         _textureManager->load();
41
42         // Scene.New("FreestyleStrokes")
43         old_scene = re->scene;
44
45         objects.first = objects.last = NULL;
46         
47         ListBase lb;
48         freestyle_scene = add_scene("freestyle_strokes");
49         lb = freestyle_scene->r.layers;
50         freestyle_scene->r= old_scene->r;
51         freestyle_scene->r.layers= lb;
52         set_scene_bg( freestyle_scene );
53
54         // image dimensions
55         float width = freestyle_scene->r.xsch;
56         float height = freestyle_scene->r.ysch;
57
58         // Camera
59         Object* object_camera = add_object(freestyle_scene, OB_CAMERA);
60         
61         Camera* camera = (Camera *) object_camera->data;
62         camera->type = CAM_ORTHO;
63         camera->ortho_scale = max(width,height);
64         
65         object_camera->loc[0] = 0.5 * width;
66         object_camera->loc[1] = 0.5 * height;
67         object_camera->loc[2] = 1.0;
68         
69         freestyle_scene->camera = object_camera;
70
71         store_object(object_camera);
72         
73         // Material
74         material = add_material("stroke_material");
75         material->mode |= MA_VERTEXCOLP;
76         material->mode |= MA_SHLESS;
77 }
78
79 BlenderStrokeRenderer::~BlenderStrokeRenderer(){
80         
81           if(0 != _textureManager)
82           {
83             delete _textureManager;
84             _textureManager = 0;
85           }
86
87         // release scene
88         free_libblock( &G.main->scene, freestyle_scene );
89
90         // release objects and data blocks
91         LinkData *link = (LinkData *)objects.first;
92         while(link) {
93                 Object *ob = (Object *)link->data;
94                 void *data = ob->data;
95                 char name[24];
96                 strcpy(name, ob->id.name);
97                 //cout << "removing " << name[0] << name[1] << ":" << (name+2) << endl;
98                 switch (ob->type) {
99                 case OB_MESH:
100                         free_libblock( &G.main->object, ob );
101                         free_libblock( &G.main->mesh, data );
102                         break;
103                 case OB_CAMERA:
104                         free_libblock( &G.main->object, ob );
105                         free_libblock( &G.main->camera, data );
106                         break;
107                 default:
108                         cerr << "Warning: unexpected object in the scene: " << name[0] << name[1] << ":" << (name+2) << endl;
109                 }
110                 link = link->next;
111         }
112         BLI_freelistN( &objects );
113
114         // release material
115         free_libblock( &G.main->mat, material );
116         
117         set_scene_bg( old_scene );
118 }
119
120 void BlenderStrokeRenderer::store_object(Object *ob) const {
121
122         LinkData *link = (LinkData *)MEM_callocN(sizeof(LinkData), "temporary object" );
123         link->data = ob;
124         BLI_addhead(const_cast<ListBase *>(&objects), link);
125 }
126
127 void BlenderStrokeRenderer::RenderStrokeRep(StrokeRep *iStrokeRep) const{
128   RenderStrokeRepBasic(iStrokeRep);
129 }
130
131 void BlenderStrokeRenderer::RenderStrokeRepBasic(StrokeRep *iStrokeRep) const{
132         
133         ////////////////////
134         //  Build up scene
135         ////////////////////
136         
137           vector<Strip*>& strips = iStrokeRep->getStrips();
138           Strip::vertex_container::iterator v[3];
139           StrokeVertexRep *svRep[3];
140           Vec3r color[3];
141           unsigned int face_index;
142         
143           for(vector<Strip*>::iterator s=strips.begin(), send=strips.end();
144           s!=send;
145           ++s){         
146                 
147                 // me = Mesh.New()
148                 Object* object_mesh = add_object(freestyle_scene, OB_MESH);
149                 Mesh* mesh = (Mesh *) object_mesh->data;
150                 MEM_freeN(mesh->bb);
151                 mesh->bb= NULL;
152                 mesh->id.us = 0;
153
154                 store_object(object_mesh);
155                 
156 #if 1
157                 // me.materials = [mat]
158                 mesh->mat = ( Material ** ) MEM_mallocN( 1 * sizeof( Material * ), "MaterialList" );
159                 mesh->mat[0] = material;
160                 mesh->totcol = 1;
161                 test_object_materials( (ID*) mesh );
162 #else
163                 assign_material(object_mesh, material, object_mesh->totcol+1);
164                 object_mesh->actcol= object_mesh->totcol;
165 #endif
166                 
167                 int strip_vertex_count = (*s)->sizeStrip();
168         
169                 // vertices allocation
170                 mesh->totvert = strip_vertex_count;
171                 mesh->mvert = (MVert*) CustomData_add_layer( &mesh->vdata, CD_MVERT, CD_CALLOC, NULL, mesh->totvert);
172                         
173                 // faces allocation
174                 mesh->totface = strip_vertex_count - 2;
175                 mesh->mface = (MFace*) CustomData_add_layer( &mesh->fdata, CD_MFACE, CD_CALLOC, NULL, mesh->totface);
176                 
177                 // colors allocation  - me.vertexColors = True
178                 mesh->mcol = (MCol *) CustomData_add_layer( &mesh->fdata, CD_MCOL, CD_CALLOC, NULL, mesh->totface );
179
180                 ////////////////////
181                 //  Data copy
182                 ////////////////////
183                 
184                 MVert* vertices = mesh->mvert;
185                 MFace* faces = mesh->mface;
186                 MCol* colors = mesh->mcol;
187                 
188             Strip::vertex_container& strip_vertices = (*s)->vertices();
189             v[0] = strip_vertices.begin();
190             v[1] = v[0]; ++(v[1]);
191             v[2] = v[1]; ++(v[2]);
192
193                 // first vertex
194                 svRep[0] = *(v[0]);
195                 vertices->co[0] = svRep[0]->point2d()[0];
196                 vertices->co[1] = svRep[0]->point2d()[1];
197                 vertices->co[2] = 0.0;
198                 ++vertices;
199                 
200                 // second vertex
201                 svRep[1] = *(v[1]);
202                 vertices->co[0] = svRep[1]->point2d()[0];
203                 vertices->co[1] = svRep[1]->point2d()[1];
204                 vertices->co[2] = 0.0;
205                 ++vertices;
206                 
207                 // iterating over subsequent vertices: each vertex adds a new face
208                 face_index = 0;
209                 
210             while( v[2] != strip_vertices.end() ) 
211                 {
212                         // INPUT
213                         svRep[0] = *(v[0]);
214                         svRep[1] = *(v[1]);
215                         svRep[2] = *(v[2]);
216                         
217                         color[0] = svRep[0]->color();
218                         color[1] = svRep[1]->color();
219                         color[2] = svRep[2]->color();
220                         
221                         // vertex
222                         vertices->co[0] = svRep[2]->point2d()[0];
223                         vertices->co[1] = svRep[2]->point2d()[1];
224                         vertices->co[2] = 0.0;
225                         
226                         // faces
227                         faces->v1 = face_index;
228                         faces->v2 = face_index + 1;
229                         faces->v3 = face_index + 2;
230                         faces->v4 = 0;
231                         
232                         // colors
233                         // red and blue are swapped - cf DNA_meshdata_types.h : MCol    
234                         colors->r = (short)(255.0f*(color[0])[2]);
235                         colors->g = (short)(255.0f*(color[0])[1]);
236                         colors->b = (short)(255.0f*(color[0])[0]);
237                         colors->a = (short)(255.0f*svRep[0]->alpha());
238                         ++colors;
239                         
240                         colors->r = (short)(255.0f*(color[1])[2]);
241                         colors->g = (short)(255.0f*(color[1])[1]);
242                         colors->b = (short)(255.0f*(color[1])[0]);
243                         colors->a = (short)(255.0f*svRep[1]->alpha());
244                         ++colors;
245                         
246                         colors->r = (short)(255.0f*(color[2])[2]);
247                         colors->g = (short)(255.0f*(color[2])[1]);
248                         colors->b = (short)(255.0f*(color[2])[0]);
249                         colors->a = (short)(255.0f*svRep[2]->alpha());
250                         ++colors;
251                         
252                         // ITERATION
253                         ++v[0]; ++v[1]; ++v[2];
254                         ++faces; ++vertices; ++colors;
255                         ++face_index;
256                 
257                 } // loop over strip vertices 
258         
259         } // loop over strips   
260
261 }
262
263 Render* BlenderStrokeRenderer::RenderScene( Render *re ) {
264         freestyle_scene->r.mode &= ~( R_EDGE_FRS | R_SHADOW | R_SSS | R_PANORAMA | R_ENVMAP | R_MBLUR );
265         freestyle_scene->r.scemode &= ~( R_SINGLE_LAYER );
266         freestyle_scene->r.planes = R_PLANES32;
267         freestyle_scene->r.imtype = R_PNG;
268         
269         Render* freestyle_render = RE_NewRender(freestyle_scene->id.name);
270         
271         RE_BlenderFrame( freestyle_render, freestyle_scene, 1);
272         return freestyle_render;
273 }