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