Big i18n commit: add "reports" from bmesh/readfile/tracking/dynapaint (and a few...
[blender.git] / source / blender / editors / render / render_opengl.c
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  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_opengl.c
26  *  \ingroup edrend
27  */
28
29
30 #include <math.h>
31 #include <string.h>
32 #include <stddef.h>
33
34 #include <GL/glew.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_dlrbTree.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_jitter.h"
43
44 #include "DNA_scene_types.h"
45 #include "DNA_object_types.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_image.h"
50 #include "BKE_main.h"
51 #include "BKE_report.h"
52 #include "BKE_scene.h"
53 #include "BKE_sequencer.h"
54 #include "BKE_writeavi.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "ED_screen.h"
60 #include "ED_view3d.h"
61 #include "ED_image.h"
62
63 #include "RE_pipeline.h"
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
66 #include "IMB_colormanagement.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70
71
72 #include "GPU_extensions.h"
73
74 #include "wm_window.h"
75
76 #include "render_intern.h"
77
78 typedef struct OGLRender {
79         Main *bmain;
80         Render *re;
81         Scene *scene;
82
83         View3D *v3d;
84         RegionView3D *rv3d;
85         ARegion *ar;
86
87         ScrArea *prevsa;
88         ARegion *prevar;
89
90         short obcenter_dia_back; /* temp overwrite */
91
92         short is_sequencer;
93         SpaceSeq *sseq;
94
95
96         Image *ima;
97         ImageUser iuser;
98
99         GPUOffScreen *ofs;
100         int sizex, sizey;
101         int write_still;
102
103         ReportList *reports;
104         bMovieHandle *mh;
105         int cfrao, nfra;
106
107         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
108 } OGLRender;
109
110 /* added because v3d is not always valid */
111 static unsigned int screen_opengl_layers(OGLRender *oglrender)
112 {
113         if (oglrender->v3d) {
114                 return oglrender->scene->lay | oglrender->v3d->lay;
115         }
116         else {
117                 return oglrender->scene->lay;
118         }
119 }
120
121 static void screen_opengl_render_apply(OGLRender *oglrender)
122 {
123         Scene *scene = oglrender->scene;
124         ARegion *ar = oglrender->ar;
125         View3D *v3d = oglrender->v3d;
126         RegionView3D *rv3d = oglrender->rv3d;
127         RenderResult *rr;
128         Object *camera = NULL;
129         ImBuf *ibuf;
130         void *lock;
131         float winmat[4][4];
132         int sizex = oglrender->sizex;
133         int sizey = oglrender->sizey;
134         const short view_context = (v3d != NULL);
135
136         rr = RE_AcquireResultRead(oglrender->re);
137
138         if (oglrender->is_sequencer) {
139                 SeqRenderData context;
140                 int chanshown = oglrender->sseq ? oglrender->sseq->chanshown : 0;
141
142                 context = BKE_sequencer_new_render_data(oglrender->bmain, scene, oglrender->sizex, oglrender->sizey, 100.0f);
143
144                 ibuf = BKE_sequencer_give_ibuf(context, CFRA, chanshown);
145
146                 if (ibuf) {
147                         ImBuf *linear_ibuf;
148
149                         BLI_assert((oglrender->sizex == ibuf->x) && (oglrender->sizey == ibuf->y));
150
151                         linear_ibuf = IMB_dupImBuf(ibuf);
152                         IMB_freeImBuf(ibuf);
153
154                         if (linear_ibuf->rect_float == NULL) {
155                                 /* internally sequencer working in display space and stores both bytes and float buffers in that space.
156                                  * It is possible that byte->float onversion didn't happen in sequencer (e.g. when adding image sequence/movie
157                                  * into sequencer) there'll be only byte buffer. Create float buffer from existing byte buffer, making it linear
158                                  */
159
160                                 IMB_float_from_rect(linear_ibuf);
161                         }
162                         else {
163                                 /* ensure float buffer is in linear space, not in display space */
164                                 BKE_sequencer_imbuf_from_sequencer_space(scene, linear_ibuf);
165                         }
166
167
168                         memcpy(rr->rectf, linear_ibuf->rect_float, sizeof(float) * 4 * oglrender->sizex * oglrender->sizey);
169
170                         IMB_freeImBuf(linear_ibuf);
171                 }
172         }
173         else if (view_context) {
174                 ED_view3d_draw_offscreen_init(scene, v3d);
175
176                 GPU_offscreen_bind(oglrender->ofs); /* bind */
177
178                 /* render 3d view */
179                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
180                         /*int is_ortho = scene->r.mode & R_ORTHO;*/
181                         camera = v3d->camera;
182                         RE_GetCameraWindow(oglrender->re, camera, scene->r.cfra, winmat);
183                         
184                 }
185                 else {
186                         rctf viewplane;
187                         float clipsta, clipend;
188
189                         int is_ortho = ED_view3d_viewplane_get(v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend);
190                         if (is_ortho) orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
191                         else perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
192                 }
193
194                 if ((scene->r.mode & R_OSA) == 0) {
195                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat, TRUE, FALSE);
196                         GPU_offscreen_read_pixels(oglrender->ofs, GL_FLOAT, rr->rectf);
197                 }
198                 else {
199                         /* simple accumulation, less hassle then FSAA FBO's */
200                         static float jit_ofs[32][2];
201                         float winmat_jitter[4][4];
202                         float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float) * 4, "accum1");
203                         float *accum_tmp = MEM_mallocN(sizex * sizey * sizeof(float) * 4, "accum2");
204                         int j;
205
206                         BLI_jitter_init(jit_ofs[0], scene->r.osa);
207
208                         /* first sample buffer, also initializes 'rv3d->persmat' */
209                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat, TRUE, FALSE);
210                         GPU_offscreen_read_pixels(oglrender->ofs, GL_FLOAT, accum_buffer);
211
212                         /* skip the first sample */
213                         for (j = 1; j < scene->r.osa; j++) {
214                                 copy_m4_m4(winmat_jitter, winmat);
215                                 window_translate_m4(winmat_jitter, rv3d->persmat,
216                                                     (jit_ofs[j][0] * 2.0f) / sizex,
217                                                     (jit_ofs[j][1] * 2.0f) / sizey);
218
219                                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat_jitter, TRUE, FALSE);
220                                 GPU_offscreen_read_pixels(oglrender->ofs, GL_FLOAT, accum_tmp);
221                                 add_vn_vn(accum_buffer, accum_tmp, sizex * sizey * sizeof(float));
222                         }
223
224                         mul_vn_vn_fl(rr->rectf, accum_buffer, sizex * sizey * sizeof(float), 1.0f / scene->r.osa);
225
226                         MEM_freeN(accum_buffer);
227                         MEM_freeN(accum_tmp);
228                 }
229
230                 GPU_offscreen_unbind(oglrender->ofs); /* unbind */
231         }
232         else {
233                 /* shouldnt suddenly give errors mid-render but possible */
234                 char err_out[256] = "unknown";
235                 ImBuf *ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(scene, scene->camera, oglrender->sizex, oglrender->sizey, IB_rectfloat, OB_SOLID, TRUE, FALSE, err_out);
236                 camera = scene->camera;
237
238                 if (ibuf_view) {
239                         memcpy(rr->rectf, ibuf_view->rect_float, sizeof(float) * 4 * oglrender->sizex * oglrender->sizey);
240                         IMB_freeImBuf(ibuf_view);
241                 }
242                 else {
243                         fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
244                 }
245         }
246         
247         /* note on color management:
248          *
249          * OpenGL renders into sRGB colors, but render buffers are expected to be
250          * linear So we convert to linear here, so the conversion back to bytes can make it
251          * sRGB (or other display space) again, and so that e.g. openexr saving also saves the
252          * correct linear float buffer.
253          */
254
255         if (!oglrender->is_sequencer) {
256                 /* sequencer has got tricker ocnversion happened above */
257
258                 IMB_buffer_float_from_float(rr->rectf, rr->rectf,
259                                             4, IB_PROFILE_LINEAR_RGB, IB_PROFILE_SRGB, FALSE,
260                                             oglrender->sizex, oglrender->sizey, oglrender->sizex, oglrender->sizex);
261         }
262
263         /* rr->rectf is now filled with image data */
264
265         if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
266                 BKE_stamp_buf(scene, camera, NULL, rr->rectf, rr->rectx, rr->recty, 4);
267
268         RE_ReleaseResult(oglrender->re);
269
270         /* update byte from float buffer */
271         ibuf = BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
272
273         if (ibuf) {
274                 image_buffer_rect_update(scene, rr, ibuf, NULL);
275
276                 if (oglrender->write_still) {
277                         char name[FILE_MAX];
278                         int ok;
279
280                         if (scene->r.im_format.planes == R_IMF_CHAN_DEPTH_8) {
281                                 IMB_color_to_bw(ibuf);
282                         }
283
284                         BKE_makepicstring(name, scene->r.pic, oglrender->bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, FALSE);
285                         ok = BKE_imbuf_write_as(ibuf, name, &scene->r.im_format, TRUE); /* no need to stamp here */
286                         if (ok) printf("OpenGL Render written to '%s'\n", name);
287                         else printf("OpenGL Render failed to write '%s'\n", name);
288                 }
289         }
290         
291         BKE_image_release_ibuf(oglrender->ima, lock);
292 }
293
294 static int screen_opengl_render_init(bContext *C, wmOperator *op)
295 {
296         /* new render clears all callbacks */
297         Scene *scene = CTX_data_scene(C);
298         ScrArea *prevsa = CTX_wm_area(C);
299         ARegion *prevar = CTX_wm_region(C);
300         RenderResult *rr;
301         GPUOffScreen *ofs;
302         OGLRender *oglrender;
303         int sizex, sizey;
304         short is_view_context = RNA_boolean_get(op->ptr, "view_context");
305         const short is_animation = RNA_boolean_get(op->ptr, "animation");
306         const short is_sequencer = RNA_boolean_get(op->ptr, "sequencer");
307         const short is_write_still = RNA_boolean_get(op->ptr, "write_still");
308         char err_out[256] = "unknown";
309
310         if (G.background) {
311                 BKE_report(op->reports, RPT_ERROR, "Cannot use OpenGL render in background mode (no opengl context)");
312                 return 0;
313         }
314
315         /* ensure we have a 3d view */
316
317         if (!ED_view3d_context_activate(C)) {
318                 RNA_boolean_set(op->ptr, "view_context", FALSE);
319                 is_view_context = 0;
320         }
321
322         /* only one render job at a time */
323         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER))
324                 return 0;
325         
326         if (!is_view_context && scene->camera == NULL) {
327                 BKE_report(op->reports, RPT_ERROR, "Scene has no camera");
328                 return 0;
329         }
330
331         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
332                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
333                 return 0;
334         }
335
336         /* stop all running jobs, currently previews frustrate Render */
337         WM_jobs_stop_all(CTX_wm_manager(C));
338
339         /* create offscreen buffer */
340         sizex = (scene->r.size * scene->r.xsch) / 100;
341         sizey = (scene->r.size * scene->r.ysch) / 100;
342
343         /* corrects render size with actual size, not every card supports non-power-of-two dimensions */
344         ofs = GPU_offscreen_create(sizex, sizey, err_out);
345
346         if (!ofs) {
347                 BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL off-screen buffer, %s", err_out);
348                 return 0;
349         }
350
351         /* handle UI stuff */
352         WM_cursor_wait(1);
353
354         /* allocate opengl render */
355         oglrender = MEM_callocN(sizeof(OGLRender), "OGLRender");
356         op->customdata = oglrender;
357
358         oglrender->ofs = ofs;
359         oglrender->sizex = sizex;
360         oglrender->sizey = sizey;
361         oglrender->bmain = CTX_data_main(C);
362         oglrender->scene = scene;
363         oglrender->cfrao = scene->r.cfra;
364
365         oglrender->write_still = is_write_still && !is_animation;
366
367         oglrender->is_sequencer = is_sequencer;
368         if (is_sequencer) {
369                 oglrender->sseq = CTX_wm_space_seq(C);
370         }
371
372
373         oglrender->obcenter_dia_back = U.obcenter_dia;
374         U.obcenter_dia = 0;
375
376         oglrender->prevsa = prevsa;
377         oglrender->prevar = prevar;
378
379         if (is_view_context) {
380                 ED_view3d_context_user_region(C, &oglrender->v3d, &oglrender->ar); /* so quad view renders camera */
381                 oglrender->rv3d = oglrender->ar->regiondata;
382
383                 /* MUST be cleared on exit */
384                 oglrender->scene->customdata_mask_modal = (ED_view3d_datamask(oglrender->scene, oglrender->v3d) |
385                                                            ED_view3d_object_datamask(oglrender->scene));
386
387                 /* apply immediately in case we're rendering from a script,
388                  * running notifiers again will overwrite */
389                 oglrender->scene->customdata_mask |= oglrender->scene->customdata_mask_modal;
390
391         }
392
393         /* create render */
394         oglrender->re = RE_NewRender(scene->id.name);
395
396         /* create image and image user */
397         oglrender->ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
398         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
399         BKE_image_backup_render(oglrender->scene, oglrender->ima);
400
401         oglrender->iuser.scene = scene;
402         oglrender->iuser.ok = 1;
403
404         /* create render result */
405         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
406
407         rr = RE_AcquireResultWrite(oglrender->re);
408         if (rr->rectf == NULL)
409                 rr->rectf = MEM_callocN(sizeof(float) * 4 * sizex * sizey, "screen_opengl_render_init rect");
410         RE_ReleaseResult(oglrender->re);
411
412         return 1;
413 }
414
415 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
416 {
417         Main *bmain = CTX_data_main(C);
418         Scene *scene = oglrender->scene;
419
420         if (oglrender->mh) {
421                 if (BKE_imtype_is_movie(scene->r.im_format.imtype))
422                         oglrender->mh->end_movie();
423         }
424
425         if (oglrender->timer) { /* exec will not have a timer */
426                 scene->r.cfra = oglrender->cfrao;
427                 BKE_scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
428
429                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), oglrender->timer);
430         }
431
432         WM_cursor_wait(0);
433         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
434
435         U.obcenter_dia = oglrender->obcenter_dia_back;
436
437         GPU_offscreen_free(oglrender->ofs);
438
439         oglrender->scene->customdata_mask_modal = 0;
440
441         CTX_wm_area_set(C, oglrender->prevsa);
442         CTX_wm_region_set(C, oglrender->prevar);
443
444         MEM_freeN(oglrender);
445 }
446
447 static int screen_opengl_render_cancel(bContext *C, wmOperator *op)
448 {
449         screen_opengl_render_end(C, op->customdata);
450
451         return OPERATOR_CANCELLED;
452 }
453
454 /* share between invoke and exec */
455 static int screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
456 {
457         /* initialize animation */
458         OGLRender *oglrender;
459         Scene *scene;
460
461         oglrender = op->customdata;
462         scene = oglrender->scene;
463
464         oglrender->reports = op->reports;
465         oglrender->mh = BKE_movie_handle_get(scene->r.im_format.imtype);
466         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
467                 if (!oglrender->mh->start_movie(scene, &scene->r, oglrender->sizex, oglrender->sizey, oglrender->reports)) {
468                         screen_opengl_render_end(C, oglrender);
469                         return 0;
470                 }
471         }
472
473         oglrender->cfrao = scene->r.cfra;
474         oglrender->nfra = PSFRA;
475         scene->r.cfra = PSFRA;
476
477         return 1;
478 }
479 static int screen_opengl_render_anim_step(bContext *C, wmOperator *op)
480 {
481         Main *bmain = CTX_data_main(C);
482         OGLRender *oglrender = op->customdata;
483         Scene *scene = oglrender->scene;
484         ImBuf *ibuf;
485         void *lock;
486         char name[FILE_MAX];
487         int ok = 0;
488         const short view_context = (oglrender->v3d != NULL);
489         Object *camera = NULL;
490         int is_movie;
491
492         /* go to next frame */
493         if (CFRA < oglrender->nfra)
494                 CFRA++;
495         while (CFRA < oglrender->nfra) {
496                 unsigned int lay = screen_opengl_layers(oglrender);
497
498                 if (lay & 0xFF000000)
499                         lay &= 0xFF000000;
500
501                 BKE_scene_update_for_newframe(bmain, scene, lay);
502                 CFRA++;
503         }
504
505         is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
506
507         if (!is_movie) {
508                 BKE_makepicstring(name, scene->r.pic, oglrender->bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, TRUE);
509
510                 if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
511                         printf("skipping existing frame \"%s\"\n", name);
512
513                         /* go to next frame */
514                         oglrender->nfra += scene->r.frame_step;
515
516                         return 1;
517                 }
518         }
519
520         /* update animated image textures for gpu, etc,
521          * call before BKE_scene_update_for_newframe so modifiers with textures don't lag 1 frame */
522         ED_image_update_frame(bmain, CFRA);
523
524         BKE_scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
525
526         if (view_context) {
527                 if (oglrender->rv3d->persp == RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
528                         /* since BKE_scene_update_for_newframe() is used rather
529                          * then ED_update_for_newframe() the camera needs to be set */
530                         if (BKE_scene_camera_switch_update(scene)) {
531                                 oglrender->v3d->camera = scene->camera;
532                         }
533
534                         camera = oglrender->v3d->camera;
535                 }
536         }
537         else {
538                 BKE_scene_camera_switch_update(scene);
539
540                 camera = scene->camera;
541         }
542
543         /* render into offscreen buffer */
544         screen_opengl_render_apply(oglrender);
545
546         /* save to disk */
547         ibuf = BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
548
549         if (ibuf) {
550                 int needs_free = FALSE;
551
552                 if (is_movie || !BKE_imtype_requires_linear_float(scene->r.im_format.imtype)) {
553                         ImBuf *colormanage_ibuf;
554
555                         colormanage_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, TRUE, TRUE, &scene->view_settings,
556                                                                                &scene->display_settings, &scene->r.im_format);
557
558                         // IMB_freeImBuf(ibuf); /* owned by the image */
559                         ibuf = colormanage_ibuf;
560                         needs_free = TRUE;
561                 }
562
563                 /* color -> grayscale */
564                 /* editing directly would alter the render view */
565                 if (scene->r.im_format.planes == R_IMF_PLANES_BW) {
566                          ImBuf *ibuf_bw = IMB_dupImBuf(ibuf);
567                         IMB_color_to_bw(ibuf_bw);
568
569                         if (needs_free)
570                                 IMB_freeImBuf(ibuf);
571
572                         ibuf = ibuf_bw;
573                 }
574                 else {
575                         /* this is lightweight & doesnt re-alloc the buffers, only do this
576                          * to save the correct bit depth since the image is always RGBA */
577                         ImBuf *ibuf_cpy = IMB_allocImBuf(ibuf->x, ibuf->y, scene->r.im_format.planes, 0);
578                         ibuf_cpy->rect = ibuf->rect;
579                         ibuf_cpy->rect_float = ibuf->rect_float;
580                         ibuf_cpy->zbuf_float = ibuf->zbuf_float;
581
582                         if (needs_free) {
583                                 ibuf_cpy->mall = ibuf->mall;
584                                 ibuf->mall = 0;
585                                 IMB_freeImBuf(ibuf);
586                         }
587
588                         ibuf = ibuf_cpy;
589                 }
590
591                 if (is_movie) {
592                         ok = oglrender->mh->append_movie(&scene->r, SFRA, CFRA, (int *)ibuf->rect,
593                                                          oglrender->sizex, oglrender->sizey, oglrender->reports);
594                         if (ok) {
595                                 printf("Append frame %d", scene->r.cfra);
596                                 BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
597                         }
598                 }
599                 else {
600                         ok = BKE_imbuf_write_stamp(scene, camera, ibuf, name, &scene->r.im_format);
601
602                         if (ok == 0) {
603                                 printf("Write error: cannot save %s\n", name);
604                                 BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
605                         }
606                         else {
607                                 printf("Saved: %s", name);
608                                 BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
609                         }
610                 }
611
612                 /* imbuf knows which rects are not part of ibuf */
613                 IMB_freeImBuf(ibuf);
614         }
615
616         BKE_image_release_ibuf(oglrender->ima, lock);
617
618         /* movie stats prints have no line break */
619         printf("\n");
620
621         /* go to next frame */
622         oglrender->nfra += scene->r.frame_step;
623
624         /* stop at the end or on error */
625         if (CFRA >= PEFRA || !ok) {
626                 screen_opengl_render_end(C, op->customdata);
627                 return 0;
628         }
629
630         return 1;
631 }
632
633
634 static int screen_opengl_render_modal(bContext *C, wmOperator *op, wmEvent *event)
635 {
636         OGLRender *oglrender = op->customdata;
637         int anim = RNA_boolean_get(op->ptr, "animation");
638         int ret;
639
640         switch (event->type) {
641                 case ESCKEY:
642                         /* cancel */
643                         screen_opengl_render_end(C, op->customdata);
644                         return OPERATOR_FINISHED;
645                 case TIMER:
646                         /* render frame? */
647                         if (oglrender->timer == event->customdata)
648                                 break;
649                 default:
650                         /* nothing to do */
651                         return OPERATOR_RUNNING_MODAL;
652         }
653
654         /* run first because screen_opengl_render_anim_step can free oglrender */
655         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
656         
657         if (anim == 0) {
658                 screen_opengl_render_apply(op->customdata);
659                 screen_opengl_render_end(C, op->customdata);
660                 return OPERATOR_FINISHED;
661         }
662         else
663                 ret = screen_opengl_render_anim_step(C, op);
664
665         /* stop at the end or on error */
666         if (ret == 0) {
667                 return OPERATOR_FINISHED;
668         }
669
670         return OPERATOR_RUNNING_MODAL;
671 }
672
673 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
674 {
675         OGLRender *oglrender;
676         int anim = RNA_boolean_get(op->ptr, "animation");
677
678         if (!screen_opengl_render_init(C, op))
679                 return OPERATOR_CANCELLED;
680
681         if (anim) {
682                 if (!screen_opengl_render_anim_initialize(C, op))
683                         return OPERATOR_CANCELLED;
684         }
685         
686         oglrender = op->customdata;
687         render_view_open(C, event->x, event->y);
688         
689         WM_event_add_modal_handler(C, op);
690         oglrender->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
691         
692         return OPERATOR_RUNNING_MODAL;
693 }
694
695 /* executes blocking render */
696 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
697 {
698         const short is_animation = RNA_boolean_get(op->ptr, "animation");
699
700         if (!screen_opengl_render_init(C, op))
701                 return OPERATOR_CANCELLED;
702
703         if (!is_animation) { /* same as invoke */
704                 /* render image */
705                 screen_opengl_render_apply(op->customdata);
706                 screen_opengl_render_end(C, op->customdata);
707
708                 return OPERATOR_FINISHED;
709         }
710         else {
711                 int ret = 1;
712
713                 if (!screen_opengl_render_anim_initialize(C, op))
714                         return OPERATOR_CANCELLED;
715
716                 while (ret) {
717                         ret = screen_opengl_render_anim_step(C, op);
718                 }
719         }
720
721         /* no redraw needed, we leave state as we entered it */
722 //      ED_update_for_newframe(C, 1);
723         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, CTX_data_scene(C));
724
725         return OPERATOR_FINISHED;
726 }
727
728 void RENDER_OT_opengl(wmOperatorType *ot)
729 {
730         PropertyRNA *prop;
731
732         /* identifiers */
733         ot->name = "OpenGL Render";
734         ot->description = "OpenGL render active viewport";
735         ot->idname = "RENDER_OT_opengl";
736
737         /* api callbacks */
738         ot->invoke = screen_opengl_render_invoke;
739         ot->exec = screen_opengl_render_exec; /* blocking */
740         ot->modal = screen_opengl_render_modal;
741         ot->cancel = screen_opengl_render_cancel;
742
743         ot->poll = ED_operator_screenactive;
744
745         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
746         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
747         prop = RNA_def_boolean(ot->srna, "sequencer", 0, "Sequencer", "Render using the sequencer's OpenGL display");
748         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
749         prop = RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
750         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
751         prop = RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings");
752         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
753
754 }
755
756 /* function for getting an opengl buffer from a View3D, used by sequencer */
757 // extern void *sequencer_view3d_cb;