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