2008-05-25 17:34:21 +00:00
|
|
|
#include "AppGLWidget.h"
|
2008-05-19 05:34:31 +00:00
|
|
|
#include "Controller.h"
|
2008-05-25 17:34:21 +00:00
|
|
|
#include "AppConfig.h"
|
|
|
|
|
|
2008-05-19 05:34:31 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2008-05-29 00:27:09 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-07-02 12:16:36 +00:00
|
|
|
#include "DNA_camera_types.h"
|
|
|
|
|
|
2008-05-29 00:27:09 +00:00
|
|
|
#include "render_types.h"
|
|
|
|
|
#include "renderpipeline.h"
|
|
|
|
|
|
2008-06-08 19:35:20 +00:00
|
|
|
#include "BLI_blenlib.h"
|
|
|
|
|
#include "BPY_extern.h"
|
|
|
|
|
|
2008-05-29 00:27:09 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-05-19 05:34:31 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-07-04 07:59:19 +00:00
|
|
|
static Config::Path *pathconfig = NULL;
|
2008-06-08 19:35:20 +00:00
|
|
|
static Controller *controller = NULL;
|
|
|
|
|
static AppGLWidget *view = NULL;
|
2008-07-04 07:59:19 +00:00
|
|
|
|
2008-06-13 22:23:24 +00:00
|
|
|
|
2008-06-08 19:35:20 +00:00
|
|
|
void FRS_initialize(){
|
2008-05-25 17:34:21 +00:00
|
|
|
|
2008-07-04 07:59:19 +00:00
|
|
|
if( pathconfig == NULL )
|
|
|
|
|
pathconfig = new Config::Path;
|
|
|
|
|
|
2008-06-08 19:35:20 +00:00
|
|
|
if( controller == NULL )
|
|
|
|
|
controller = new Controller;
|
|
|
|
|
|
|
|
|
|
if( view == NULL )
|
|
|
|
|
view = new AppGLWidget;
|
2008-06-16 00:51:19 +00:00
|
|
|
|
|
|
|
|
controller->SetView(view);
|
2008-06-08 19:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-04 07:59:19 +00:00
|
|
|
|
|
|
|
|
void FRS_init_view(Render* re){
|
|
|
|
|
view->setWidth( re->winx );
|
|
|
|
|
view->setHeight( re->winy );
|
|
|
|
|
view->_camera->setScreenWidthAndHeight( re->winx, re->winy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FRS_init_camera(Render* re){
|
2008-06-16 00:51:19 +00:00
|
|
|
Object* maincam_obj = re->scene->camera;
|
2008-07-02 12:16:36 +00:00
|
|
|
Camera *cam = (Camera*) maincam_obj->data;
|
|
|
|
|
|
|
|
|
|
if(cam->type == CAM_PERSP){
|
|
|
|
|
view->_camera->setType(AppGLWidget_Camera::PERSPECTIVE);
|
|
|
|
|
view->_camera->setHorizontalFieldOfView( M_PI / 180.0f * cam->angle );
|
|
|
|
|
}
|
2008-07-04 07:59:19 +00:00
|
|
|
// else if (cam->type == CAM_ORTHO){
|
|
|
|
|
// view->_camera->setType(AppGLWidget_Camera::ORTHOGRAPHIC);
|
|
|
|
|
// // view->_camera->setFocusDistance does not seem to work
|
|
|
|
|
// // integrate cam->ortho_scale parameter
|
|
|
|
|
// }
|
2008-07-02 12:16:36 +00:00
|
|
|
|
2008-06-16 00:51:19 +00:00
|
|
|
Vec camPosition(maincam_obj->obmat[3][0], maincam_obj->obmat[3][1], maincam_obj->obmat[3][2]);
|
|
|
|
|
Vec camUp( re->viewmat[0][1], re->viewmat[1][1], re->viewmat[2][1]);
|
2008-07-02 12:16:36 +00:00
|
|
|
Vec camDirection( -re->viewmat[0][2], -re->viewmat[1][2], -re->viewmat[2][2]);
|
2008-06-16 00:51:19 +00:00
|
|
|
view->_camera->setPosition(camPosition);
|
|
|
|
|
view->_camera->setUpVector(camUp);
|
2008-07-04 07:59:19 +00:00
|
|
|
view->_camera->setViewDirection(camDirection);
|
|
|
|
|
}
|
2008-07-02 12:16:36 +00:00
|
|
|
|
2008-07-04 07:59:19 +00:00
|
|
|
void FRS_scene_3ds_export(Render* re) {
|
2008-06-16 00:51:19 +00:00
|
|
|
// export scene to 3ds format
|
2008-07-04 07:59:19 +00:00
|
|
|
string script_3ds_export = pathconfig->getProjectDir() +
|
2008-06-13 22:23:24 +00:00
|
|
|
Config::DIR_SEP + "python" +
|
|
|
|
|
Config::DIR_SEP + "3ds_export.py";
|
|
|
|
|
BPY_run_python_script( const_cast<char *>(script_3ds_export.c_str()) );
|
2008-05-25 17:34:21 +00:00
|
|
|
|
2008-06-16 00:51:19 +00:00
|
|
|
// load 3ds scene
|
2008-06-08 19:35:20 +00:00
|
|
|
char btempdir[255];
|
|
|
|
|
BLI_where_is_temp(btempdir,1);
|
|
|
|
|
string exported_3ds_file = btempdir;
|
2008-06-13 22:23:24 +00:00
|
|
|
exported_3ds_file += Config::DIR_SEP + "tmp_scene_freestyle.3ds";
|
2008-06-08 19:35:20 +00:00
|
|
|
if( BLI_exists( const_cast<char *>(exported_3ds_file.c_str()) ) ) {
|
|
|
|
|
controller->Load3DSFile( exported_3ds_file.c_str() );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cout << "Cannot find" << exported_3ds_file << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-07-04 07:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FRS_prepare(Render* re) {
|
|
|
|
|
FRS_initialize();
|
|
|
|
|
|
|
|
|
|
FRS_init_view(re);
|
|
|
|
|
FRS_init_camera(re);
|
2008-05-29 00:27:09 +00:00
|
|
|
|
2008-07-04 07:59:19 +00:00
|
|
|
FRS_scene_3ds_export(re);
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
void FRS_render(Render* re, int render_in_layer) {
|
|
|
|
|
|
2008-07-08 22:55:00 +00:00
|
|
|
// if(render_in_layer) {
|
|
|
|
|
// view->workingBuffer = GL_COLOR_ATTACHMENT0_EXT;
|
|
|
|
|
// } else {
|
|
|
|
|
// view->workingBuffer = GL_BACK;
|
|
|
|
|
// }
|
|
|
|
|
view->workingBuffer = GL_BACK;
|
2008-07-05 06:22:15 +00:00
|
|
|
|
2008-06-16 00:51:19 +00:00
|
|
|
// add style module
|
2008-07-04 07:59:19 +00:00
|
|
|
string style_module = pathconfig->getProjectDir() +
|
2008-06-13 22:23:24 +00:00
|
|
|
Config::DIR_SEP + "style_modules" +
|
|
|
|
|
Config::DIR_SEP + "contour.py";
|
|
|
|
|
controller->InsertStyleModule( 0, const_cast<char *>(style_module.c_str()) );
|
2008-06-08 19:35:20 +00:00
|
|
|
controller->toggleLayer(0, true);
|
2008-06-16 00:51:19 +00:00
|
|
|
|
|
|
|
|
// compute view map
|
2008-06-08 19:35:20 +00:00
|
|
|
controller->ComputeViewMap();
|
|
|
|
|
|
2008-06-16 00:51:19 +00:00
|
|
|
// build strokes
|
|
|
|
|
controller->DrawStrokes();
|
|
|
|
|
|
|
|
|
|
// render final result
|
|
|
|
|
view->draw();
|
2008-05-19 05:34:31 +00:00
|
|
|
}
|
2008-07-04 07:59:19 +00:00
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
void FRS_execute(Render* re, int render_in_layer) {
|
|
|
|
|
|
2008-07-05 08:50:41 +00:00
|
|
|
GLuint framebuffer, renderbuffers[2];
|
2008-07-05 06:22:15 +00:00
|
|
|
GLenum status;
|
|
|
|
|
RenderLayer *rl;
|
2008-07-08 22:55:00 +00:00
|
|
|
GLubyte *pixc;
|
2008-07-04 07:59:19 +00:00
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
if(render_in_layer) {
|
2008-07-08 22:55:00 +00:00
|
|
|
|
|
|
|
|
pixc = (GLubyte *) malloc( 4 * re->winx * re->winy * sizeof(GLubyte) );
|
|
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
cout << "Freestyle as a render layer - SETUP" << endl;
|
2008-07-04 07:59:19 +00:00
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
// set up frame buffer
|
|
|
|
|
glGenFramebuffersEXT(1, &framebuffer);
|
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
|
|
|
|
|
|
2008-07-05 08:50:41 +00:00
|
|
|
// set up render buffer: one color buffer, one depth buffer
|
|
|
|
|
glGenRenderbuffersEXT(2, renderbuffers);
|
|
|
|
|
|
|
|
|
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffers[0]);
|
2008-07-08 22:55:00 +00:00
|
|
|
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA, re->winx, re->winy);
|
2008-07-05 08:50:41 +00:00
|
|
|
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, renderbuffers[0]);
|
|
|
|
|
|
|
|
|
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffers[1]);
|
|
|
|
|
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, re->winx, re->winy);
|
|
|
|
|
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderbuffers[1]);
|
2008-07-05 06:22:15 +00:00
|
|
|
|
|
|
|
|
// status verification
|
|
|
|
|
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT){
|
|
|
|
|
cout << "Framebuffer setup error" << endl;
|
2008-07-05 08:50:41 +00:00
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
|
glDeleteRenderbuffersEXT(2, renderbuffers);
|
|
|
|
|
glDeleteFramebuffersEXT(1, &framebuffer);
|
2008-07-05 06:22:15 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-08 22:55:00 +00:00
|
|
|
glPushAttrib(GL_VIEWPORT_BIT);
|
|
|
|
|
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); // should not be needed
|
2008-07-05 11:16:03 +00:00
|
|
|
glViewport(0, 0, re->winx, re->winy);
|
2008-07-08 22:55:00 +00:00
|
|
|
|
|
|
|
|
FRS_render(re, render_in_layer);
|
|
|
|
|
|
|
|
|
|
// keep first Freestyle layer
|
|
|
|
|
for(rl = (RenderLayer *)re->result->layers.first; rl; rl= rl->next)
|
|
|
|
|
if(rl->layflag & SCE_LAY_FRS)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
cout << "Freestyle as a render layer - RESULT" << endl;
|
|
|
|
|
|
|
|
|
|
// transfer render to layer
|
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
|
|
|
|
|
glReadPixels(0, 0, re->winx, re->winy, GL_RGBA, GL_UNSIGNED_BYTE, pixc );
|
|
|
|
|
|
|
|
|
|
int p;
|
|
|
|
|
for(int i = 0; i < re->winx; i++) {
|
|
|
|
|
for(int j = 0; j < re->winy; j++){
|
|
|
|
|
p = 4*(i*re->winy + j);
|
|
|
|
|
*(rl->rectf + p ) = 1.0*pixc[ p ]/255.0;
|
|
|
|
|
*(rl->rectf + p + 1) = 1.0*pixc[ p+1 ]/255.0;
|
|
|
|
|
*(rl->rectf + p + 2) = 1.0*pixc[ p+2 ]/255.0;
|
|
|
|
|
*(rl->rectf + p + 3) = 1.0*pixc[ p+3 ]/255.0;
|
2008-07-05 06:22:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-07-08 22:55:00 +00:00
|
|
|
|
|
|
|
|
// bind window
|
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
|
glDrawBuffer(GL_BACK);
|
|
|
|
|
glPopAttrib();
|
|
|
|
|
glDeleteRenderbuffersEXT(2, renderbuffers);
|
|
|
|
|
glDeleteFramebuffersEXT(1, &framebuffer);
|
|
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
} else {
|
2008-07-08 22:55:00 +00:00
|
|
|
FRS_render(re, render_in_layer);
|
|
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
RenderResult rres;
|
|
|
|
|
RE_GetResultImage(re, &rres);
|
|
|
|
|
view->readPixels(0, 0, re->winx, re->winy, AppGLWidget::RGBA, rres.rectf );
|
|
|
|
|
re->result->renlay = render_get_active_layer(re, re->result);
|
|
|
|
|
re->display_draw(re->result, NULL);
|
|
|
|
|
}
|
2008-07-08 22:55:00 +00:00
|
|
|
|
2008-07-05 06:22:15 +00:00
|
|
|
|
|
|
|
|
controller->CloseFile();
|
2008-07-04 07:59:19 +00:00
|
|
|
}
|
2008-05-19 05:34:31 +00:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|