remove for bad-level-call & some minor changes to make camera switching neater.
[blender-staging.git] / source / blender / editors / render / render_opengl.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <math.h>
28 #include <string.h>
29 #include <stddef.h>
30
31 #include <GL/glew.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_math.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_editVert.h"
38 #include "BLI_dlrbTree.h"
39
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_view3d_types.h"
43
44 #include "BKE_blender.h"
45 #include "BKE_object.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_image.h"
49 #include "BKE_library.h"
50 #include "BKE_main.h"
51 #include "BKE_mesh.h"
52 #include "BKE_multires.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_screen.h"
56 #include "BKE_utildefines.h"
57 #include "BKE_writeavi.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "ED_util.h"
63 #include "ED_screen.h"
64 #include "ED_mesh.h"
65 #include "ED_object.h"
66 #include "ED_screen_types.h"
67 #include "ED_keyframes_draw.h"
68 #include "ED_view3d.h"
69
70 #include "RE_pipeline.h"
71 #include "IMB_imbuf.h"
72 #include "IMB_imbuf_types.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "UI_interface.h"
78 #include "UI_resources.h"
79
80 #include "GPU_extensions.h"
81
82 #include "wm_window.h"
83
84 #include "render_intern.h"
85
86 typedef struct OGLRender {
87         Render *re;
88         Scene *scene;
89
90         View3D *v3d;
91         RegionView3D *rv3d;
92         ARegion *ar;
93
94         Image *ima;
95         ImageUser iuser;
96
97         GPUOffScreen *ofs;
98         int sizex, sizey;
99
100         ReportList *reports;
101         bMovieHandle *mh;
102         int cfrao, nfra;
103
104         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
105 } OGLRender;
106
107 static void screen_opengl_render_apply(OGLRender *oglrender)
108 {
109         Scene *scene= oglrender->scene;
110         ARegion *ar= oglrender->ar;
111         View3D *v3d= oglrender->v3d;
112         RegionView3D *rv3d= oglrender->rv3d;
113         RenderResult *rr;
114         ImBuf *ibuf;
115         void *lock;
116         float winmat[4][4];
117         int sizex= oglrender->sizex;
118         int sizey= oglrender->sizey;
119
120         /* bind */
121         GPU_offscreen_bind(oglrender->ofs);
122
123         /* render 3d view */
124         if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
125                 RE_GetCameraWindow(oglrender->re, v3d->camera, scene->r.cfra, winmat);
126                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
127         }
128         else
129                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL);
130
131         /* read in pixels & stamp */
132         rr= RE_AcquireResultRead(oglrender->re);
133         glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_FLOAT, rr->rectf);
134         if((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
135                 BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
136         RE_ReleaseResult(oglrender->re);
137
138         /* update byte from float buffer */
139         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
140         if(ibuf) image_buffer_rect_update(NULL, rr, ibuf, NULL);
141         BKE_image_release_ibuf(oglrender->ima, lock);
142
143         /* unbind */
144         GPU_offscreen_unbind(oglrender->ofs);
145 }
146
147 static int screen_opengl_render_init(bContext *C, wmOperator *op)
148 {
149         /* new render clears all callbacks */
150         Scene *scene= CTX_data_scene(C);
151         RenderResult *rr;
152         GPUOffScreen *ofs;
153         OGLRender *oglrender;
154         int sizex, sizey;
155
156         /* ensure we have a 3d view */
157         if(!ED_view3d_context_activate(C))
158                 return 0;
159
160         /* only one render job at a time */
161         if(WM_jobs_test(CTX_wm_manager(C), scene))
162                 return 0;
163
164         /* stop all running jobs, currently previews frustrate Render */
165         WM_jobs_stop_all(CTX_wm_manager(C));
166
167         /* handle UI stuff */
168         WM_cursor_wait(1);
169
170         /* create offscreen buffer */
171         sizex= (scene->r.size*scene->r.xsch)/100;
172         sizey= (scene->r.size*scene->r.ysch)/100;
173
174         ofs= GPU_offscreen_create(sizex, sizey);
175
176         if(!ofs) {
177                 BKE_report(op->reports, RPT_ERROR, "Failed to create OpenGL offscreen buffer.");
178                 return 0;
179         }
180
181         /* allocate opengl render */
182         oglrender= MEM_callocN(sizeof(OGLRender), "OGLRender");
183         op->customdata= oglrender;
184
185         oglrender->ofs= ofs;
186         oglrender->sizex= sizex;
187         oglrender->sizey= sizey;
188         oglrender->scene= scene;
189
190         oglrender->v3d= CTX_wm_view3d(C);
191         oglrender->ar= CTX_wm_region(C);
192         oglrender->rv3d= CTX_wm_region_view3d(C);
193
194         /* create image and image user */
195         oglrender->ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
196         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
197
198         oglrender->iuser.scene= scene;
199         oglrender->iuser.ok= 1;
200
201         /* create render and render result */
202         oglrender->re= RE_NewRender(scene->id.name, RE_SLOT_VIEW);
203         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
204
205         rr= RE_AcquireResultWrite(oglrender->re);
206         if(rr->rectf==NULL)
207                 rr->rectf= MEM_mallocN(sizeof(float)*4*sizex*sizey, "32 bits rects");
208         RE_ReleaseResult(oglrender->re);
209
210         return 1;
211 }
212
213 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
214 {
215         Scene *scene= oglrender->scene;
216
217         if(oglrender->mh) {
218                 if(BKE_imtype_is_movie(scene->r.imtype))
219                         oglrender->mh->end_movie();
220         }
221
222         if(oglrender->timer) { /* exec will not have a timer */
223                 scene->r.cfra= oglrender->cfrao;
224                 scene_update_for_newframe(scene, scene->lay);
225
226                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), oglrender->timer);
227         }
228
229         WM_cursor_wait(0);
230         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
231
232         GPU_offscreen_free(oglrender->ofs);
233
234         MEM_freeN(oglrender);
235 }
236
237 static int screen_opengl_render_cancel(bContext *C, wmOperator *op)
238 {
239         screen_opengl_render_end(C, op->customdata);
240
241         return OPERATOR_CANCELLED;
242 }
243
244 /* share between invoke and exec */
245 static int screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
246 {
247         /* initialize animation */
248         OGLRender *oglrender;
249         Scene *scene;
250
251         oglrender= op->customdata;
252         scene= oglrender->scene;
253
254         oglrender->reports= op->reports;
255         oglrender->mh= BKE_get_movie_handle(scene->r.imtype);
256         if(BKE_imtype_is_movie(scene->r.imtype)) {
257                 if(!oglrender->mh->start_movie(scene, &scene->r, oglrender->sizex, oglrender->sizey, oglrender->reports)) {
258                         screen_opengl_render_end(C, oglrender);
259                         return 0;
260                 }
261         }
262
263         oglrender->cfrao= scene->r.cfra;
264         oglrender->nfra= SFRA;
265         scene->r.cfra= SFRA;
266
267         return 1;
268 }
269 static int screen_opengl_render_anim_step(bContext *C, wmOperator *op)
270 {
271         OGLRender *oglrender= op->customdata;
272         Scene *scene= oglrender->scene;
273         ImBuf *ibuf;
274         void *lock;
275         char name[FILE_MAXDIR+FILE_MAXFILE];
276         unsigned int lay;
277         int ok= 0;
278
279         /* go to next frame */
280         while(CFRA<oglrender->nfra) {
281                 if(scene->lay & 0xFF000000)
282                         lay= scene->lay & 0xFF000000;
283                 else
284                         lay= scene->lay;
285
286                 scene_update_for_newframe(scene, lay);
287                 CFRA++;
288         }
289
290         scene_update_for_newframe(scene, scene->lay);
291
292         if(oglrender->rv3d->persp==RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
293                 /* since scene_update_for_newframe() is used rather
294                  * then ED_update_for_newframe() the camera needs to be set */
295                 if(scene_camera_switch_update(scene))
296                         oglrender->v3d->camera= scene->camera;
297         }
298
299         /* render into offscreen buffer */
300         screen_opengl_render_apply(oglrender);
301
302         /* save to disk */
303         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
304
305         if(ibuf) {
306                 if(BKE_imtype_is_movie(scene->r.imtype)) {
307                         ok= oglrender->mh->append_movie(&scene->r, CFRA, (int*)ibuf->rect, oglrender->sizex, oglrender->sizey, oglrender->reports);
308                         if(ok) {
309                                 printf("Append frame %d", scene->r.cfra);
310                                 BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
311                         }
312                 }
313                 else {
314                         BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION);
315                         ok= BKE_write_ibuf(scene, ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
316
317                         if(ok==0) {
318                                 printf("Write error: cannot save %s\n", name);
319                                 BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
320                         }
321                         else {
322                                 printf("Saved: %s", name);
323                                 BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
324                         }
325                 }
326         }
327
328         BKE_image_release_ibuf(oglrender->ima, lock);
329
330         /* movie stats prints have no line break */
331         printf("\n");
332
333         /* go to next frame */
334         oglrender->nfra += scene->r.frame_step;
335         scene->r.cfra++;
336
337         /* stop at the end or on error */
338         if(scene->r.cfra > EFRA || !ok) {
339                 screen_opengl_render_end(C, op->customdata);
340                 return 0;
341         }
342
343         return 1;
344 }
345
346
347 static int screen_opengl_render_modal(bContext *C, wmOperator *op, wmEvent *event)
348 {
349         OGLRender *oglrender= op->customdata;
350
351         int ret;
352
353         switch(event->type) {
354                 case ESCKEY:
355                         /* cancel */
356                         screen_opengl_render_end(C, op->customdata);
357                         return OPERATOR_FINISHED;
358                 case TIMER:
359                         /* render frame? */
360                         if(oglrender->timer == event->customdata)
361                                 break;
362                 default:
363                         /* nothing to do */
364                         return OPERATOR_RUNNING_MODAL;
365         }
366
367         ret= screen_opengl_render_anim_step(C, op);
368
369         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
370
371         /* stop at the end or on error */
372         if(ret == 0) {
373                 return OPERATOR_FINISHED;
374         }
375
376         return OPERATOR_RUNNING_MODAL;
377 }
378
379 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
380 {
381         int anim= RNA_boolean_get(op->ptr, "animation");
382
383         if(!screen_opengl_render_init(C, op))
384                 return OPERATOR_CANCELLED;
385
386         if(!anim) {
387                 /* render image */
388                 screen_opengl_render_apply(op->customdata);
389                 screen_opengl_render_end(C, op->customdata);
390                 screen_set_image_output(C, event->x, event->y);
391
392                 return OPERATOR_FINISHED;
393         }
394         else {
395                 OGLRender *oglrender= op->customdata;
396
397                 if(!screen_opengl_render_anim_initialize(C, op))
398                         return OPERATOR_CANCELLED;
399
400                 screen_set_image_output(C, event->x, event->y);
401
402                 WM_event_add_modal_handler(C, op);
403                 oglrender->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
404
405                 return OPERATOR_RUNNING_MODAL;
406         }
407 }
408
409 /* executes blocking render */
410 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
411 {
412         int anim= RNA_boolean_get(op->ptr, "animation");
413
414         if(!screen_opengl_render_init(C, op))
415                 return OPERATOR_CANCELLED;
416
417         if(!anim) { /* same as invoke */
418                 /* render image */
419                 screen_opengl_render_apply(op->customdata);
420                 screen_opengl_render_end(C, op->customdata);
421
422                 return OPERATOR_FINISHED;
423         }
424         else {
425                 int ret= 1;
426
427                 if(!screen_opengl_render_anim_initialize(C, op))
428                         return OPERATOR_CANCELLED;
429
430                 while(ret) {
431                         ret= screen_opengl_render_anim_step(C, op);
432                 }
433         }
434
435         // no redraw needed, we leave state as we entered it
436 //      ED_update_for_newframe(C, 1);
437         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, CTX_data_scene(C));
438
439         return OPERATOR_FINISHED;
440 }
441
442 void RENDER_OT_opengl(wmOperatorType *ot)
443 {
444         /* identifiers */
445         ot->name= "OpenGL Render";
446         ot->description= "OpenGL render active viewport";
447         ot->idname= "RENDER_OT_opengl";
448
449         /* api callbacks */
450         ot->invoke= screen_opengl_render_invoke;
451         ot->exec= screen_opengl_render_exec; /* blocking */
452         ot->modal= screen_opengl_render_modal;
453         ot->cancel= screen_opengl_render_cancel;
454
455         ot->poll= ED_operator_screenactive;
456
457         RNA_def_boolean(ot->srna, "animation", 0, "Animation", "");
458 }
459
460 /* function for getting an opengl buffer from a View3D, used by sequencer */
461 // extern void *sequencer_view3d_cb;