This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/freestyle/intern/stroke/Canvas.cpp

489 lines
11 KiB
C++
Raw Normal View History

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup freestyle
* \brief Class to define a canvas designed to draw style modules
*/
#include <sstream>
2008-04-30 15:41:54 +00:00
#include <vector>
2008-04-30 15:41:54 +00:00
#include "Canvas.h"
#include "StrokeRenderer.h"
#include "StyleModule.h"
2008-04-30 15:41:54 +00:00
#include "../image/Image.h"
#include "../image/GaussianFilter.h"
#include "../image/ImagePyramid.h"
#include "../system/FreestyleConfig.h"
#include "../system/TimeStamp.h"
#include "../system/PseudoNoise.h"
2008-04-30 15:41:54 +00:00
#include "../view_map/SteerableViewMap.h"
#include "BKE_global.h"
// soc #include <qimage.h>
// soc #include <QString>
extern "C" {
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
}
2008-04-30 15:41:54 +00:00
using namespace std;
Attempt to fix a potential name conflict between Freestyle and the compositor. A crash in the Freestyle renderer was reported by Ton on IRC with a stack trace below. Note that #2 is in Freestyle, whereas #1 is in the compositor. The problem was observed in a debug build on OS X 10.7 (gcc 4.2, openmp disabled, no llvm). ---------------------------------------------------------------------- Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: 13 at address: 0x0000000000000000 [Switching to process 72386 thread 0xf303] 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 43 delete (this->m_outputsockets.back()); Current language: auto; currently c++ (gdb) where #0 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 #1 0x0000000100c29066 in Node::~Node (this=0x10e501c80) at COM_Node.h:49 #2 0x000000010089c273 in NodeShape::~NodeShape (this=0x10e501c80) at NodeShape.cpp:43 #3 0x000000010089910b in NodeGroup::destroy (this=0x10e501da0) at NodeGroup.cpp:61 #4 0x00000001008990cd in NodeGroup::destroy (this=0x10e5014b0) at NodeGroup.cpp:59 #5 0x00000001008990cd in NodeGroup::destroy (this=0x114e18da0) at NodeGroup.cpp:59 #6 0x00000001007e6602 in Controller::ClearRootNode (this=0x114e19640) at Controller.cpp:329 #7 0x00000001007ea52e in Controller::LoadMesh (this=0x114e19640, re=0x10aba4638, srl=0x1140f5258) at Controller.cpp:302 #8 0x00000001008030ad in prepare (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:302 #9 0x000000010080457a in FRS_do_stroke_rendering (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:600 #10 0x00000001006aeb9d in add_freestyle (re=0x10aba4638) at pipeline.c:1584 #11 0x00000001006aceb7 in do_render_3d (re=0x10aba4638) at pipeline.c:1094 #12 0x00000001006ae061 in do_render_fields_blur_3d (re=0x10aba4638) at pipeline.c:1367 #13 0x00000001006afa16 in do_render_composite_fields_blur_3d (re=0x10aba4638) at pipeline.c:1815 #14 0x00000001006b04e4 in do_render_all_options (re=0x10aba4638) at pipeline.c:2021 ---------------------------------------------------------------------- Apparently a name conflict between the two Blender modules is taking place. The present commit hence intends to address it by putting all the Freestyle C++ classes in the namespace 'Freestyle'. This revision will also prevent potential name conflicts with other Blender modules in the future. Special thanks to Lukas Toenne for the help with C++ namespace.
2013-04-09 00:46:49 +00:00
namespace Freestyle {
Canvas *Canvas::_pInstance = 0;
2008-04-30 15:41:54 +00:00
const char *Canvas::_MapsPath = 0;
2008-04-30 15:41:54 +00:00
Canvas::Canvas()
{
_SelectedFEdge = 0;
_pInstance = this;
PseudoNoise::init(42);
_Renderer = 0;
_current_sm = NULL;
_steerableViewMap = new SteerableViewMap(NB_STEERABLE_VIEWMAP - 1);
_basic = false;
2008-04-30 15:41:54 +00:00
}
Canvas::Canvas(const Canvas &iBrother)
2008-04-30 15:41:54 +00:00
{
_SelectedFEdge = iBrother._SelectedFEdge;
_pInstance = this;
PseudoNoise::init(42);
_Renderer = iBrother._Renderer;
_current_sm = iBrother._current_sm;
_steerableViewMap = new SteerableViewMap(*(iBrother._steerableViewMap));
_basic = iBrother._basic;
2008-04-30 15:41:54 +00:00
}
Canvas::~Canvas()
{
_pInstance = 0;
Clear();
if (_Renderer) {
delete _Renderer;
_Renderer = 0;
}
// FIXME: think about an easy control for the maps memory management...
if (!_maps.empty()) {
for (mapsMap::iterator m = _maps.begin(), mend = _maps.end(); m != mend; ++m) {
delete ((*m).second);
}
_maps.clear();
}
if (_steerableViewMap) {
delete _steerableViewMap;
}
2008-04-30 15:41:54 +00:00
}
void Canvas::preDraw()
{
}
2008-04-30 15:41:54 +00:00
void Canvas::Draw()
{
if (_StyleModules.empty()) {
return;
}
preDraw();
TimeStamp *timestamp = TimeStamp::instance();
2008-04-30 15:41:54 +00:00
for (unsigned int i = 0; i < _StyleModules.size(); ++i) {
_current_sm = _StyleModules[i];
if (i < _Layers.size() && _Layers[i]) {
delete _Layers[i];
}
2008-04-30 15:41:54 +00:00
_Layers[i] = _StyleModules[i]->execute();
if (!_Layers[i]) {
continue;
}
2008-04-30 15:41:54 +00:00
stroke_count += _Layers[i]->strokes_size();
2008-12-10 22:06:27 +00:00
timestamp->increment();
}
postDraw();
2008-04-30 15:41:54 +00:00
}
void Canvas::postDraw()
{
update();
2008-04-30 15:41:54 +00:00
}
void Canvas::Clear()
{
if (!_Layers.empty()) {
for (deque<StrokeLayer *>::iterator sl = _Layers.begin(), slend = _Layers.end(); sl != slend;
++sl) {
if (*sl) {
delete (*sl);
}
}
_Layers.clear();
}
if (!_StyleModules.empty()) {
for (deque<StyleModule *>::iterator s = _StyleModules.begin(), send = _StyleModules.end();
s != send;
++s) {
if (*s) {
delete (*s);
}
}
_StyleModules.clear();
}
if (_steerableViewMap) {
_steerableViewMap->Reset();
}
stroke_count = 0;
2008-04-30 15:41:54 +00:00
}
void Canvas::Erase()
{
if (!_Layers.empty()) {
for (deque<StrokeLayer *>::iterator sl = _Layers.begin(), slend = _Layers.end(); sl != slend;
++sl) {
if (*sl) {
(*sl)->clear();
}
}
}
if (_steerableViewMap) {
_steerableViewMap->Reset();
}
update();
stroke_count = 0;
2008-04-30 15:41:54 +00:00
}
void Canvas::PushBackStyleModule(StyleModule *iStyleModule)
{
StrokeLayer *layer = new StrokeLayer();
_StyleModules.push_back(iStyleModule);
_Layers.push_back(layer);
SUMMARY: Freestyle's pipeline is now fully controllable at the layer level. It can be used: - in any render layer - with as many style modules per layer DETAILS: Freestyle usage has not changed: - all the configuration happens in the "Freestyle" render panel, after it is enabled in the "Output" panel with the 'Freestyle' toggle. - each render layer can choose to render Freestyle strokes by togglingo on 'FrSt' (in the "Render Layers" panel) - it is fully compatible with compositor nodes In the "Freestyle" panel, a render layer is selected via the menu list next to the "Render Layer:" label. The options displayed below are those of the currently selected render layer (and are not global to all render layers, as was previously the case). Style modules are added by pressing the lower button "Add style module". Once added, the following operations are possible: - deletion (cross) - reordering (up/down arrows) - toggling of display (check) The order of the style modules follows Freestyle's original convention: the modules in the list from top to bottom are respectively the first to the last composited in the render layer. For example, if the module list is "contour" followed by "cartoon", the "cartoon" strokes are rendered on top of the "contour" strokes. The "Freestyle" panel is constantly synchronized with the "Render Layers" panel: if render layers are added, deleted or toggled off display, Freestyle will take note of the changes. The current pipeline works as follows: ---------------------------------------------------------------------------------------------- for every scene that is being rendered if Freestyle is enabled globally Freestyle is initialized camera and view settings are transferred from Blender to Freestyle for every render layer if: - layer is enabled - layer enabled Freestyle - the number of displayed style modules is non-zero canvas is cleared geometry is transferred from Blender to Freestyle settings are fixed for current iteration view map is calculated strokes are computed in the canvas (based on view map and style modules) strokes are rendered in separate Blender scene scene is composited in current layer ---------------------------------------------------------------------------------------------- A number of changes were made on the codebase: - the rendering interface between Freestyle and Blender was simplified. The following naming convention was used: functions that are called from within Blender pipeline are prefixed with 'FRS_', while the variables are prefixed with 'freestyle_' - Freestyle data structures that were put in Blender's render pipeline were removed - Freestyle cleans up its data structures on Blender exit and shouldn't leak memory - to ease the configuration in the "Freestyle" panel, a centralized configuration data structure was used and can be easily extended LIMITATIONS Even though the current commit is stable and achieves the intended result, it is not as efficient as it could be: - the canvas and the style modules are at cleared at each layer-level render - geometry is reloaded at each frame and is duplicated across render layers This revision clarifies my understanding of the future role of the view map in the compositor. Unfortunately, contrary to what the original proposal said, it is impossible to provide the view map as a render pass because render passes are defined (RE_pipeline.h) as raw floating-point rects. We will have to determine whether or not to extend the notion of render pass to fully integrate the view map in the compositor.
2009-04-07 18:38:23 +00:00
}
void Canvas::InsertStyleModule(unsigned index, StyleModule *iStyleModule)
{
unsigned size = _StyleModules.size();
StrokeLayer *layer = new StrokeLayer();
if ((_StyleModules.empty()) || (index == size)) {
_StyleModules.push_back(iStyleModule);
_Layers.push_back(layer);
return;
}
_StyleModules.insert(_StyleModules.begin() + index, iStyleModule);
_Layers.insert(_Layers.begin() + index, layer);
2008-04-30 15:41:54 +00:00
}
void Canvas::RemoveStyleModule(unsigned index)
{
unsigned int i = 0;
if (!_StyleModules.empty()) {
for (deque<StyleModule *>::iterator s = _StyleModules.begin(), send = _StyleModules.end();
s != send;
++s, ++i) {
if (i == index) {
// remove shader
if (*s) {
delete *s;
}
_StyleModules.erase(s);
break;
}
}
}
if (!_Layers.empty()) {
i = 0;
for (deque<StrokeLayer *>::iterator sl = _Layers.begin(), slend = _Layers.end(); sl != slend;
++sl, ++i) {
if (i == index) {
// remove layer
if (*sl) {
delete *sl;
}
_Layers.erase(sl);
break;
}
}
}
2008-04-30 15:41:54 +00:00
}
void Canvas::SwapStyleModules(unsigned i1, unsigned i2)
{
StyleModule *tmp;
tmp = _StyleModules[i1];
_StyleModules[i1] = _StyleModules[i2];
_StyleModules[i2] = tmp;
StrokeLayer *tmp2;
tmp2 = _Layers[i1];
_Layers[i1] = _Layers[i2];
_Layers[i2] = tmp2;
2008-04-30 15:41:54 +00:00
}
void Canvas::ReplaceStyleModule(unsigned index, StyleModule *iStyleModule)
{
unsigned i = 0;
for (deque<StyleModule *>::iterator s = _StyleModules.begin(), send = _StyleModules.end();
s != send;
++s, ++i) {
if (i == index) {
if (*s) {
delete *s;
}
*s = iStyleModule;
break;
}
}
2008-04-30 15:41:54 +00:00
}
void Canvas::setVisible(unsigned index, bool iVisible)
{
_StyleModules[index]->setDisplayed(iVisible);
2008-04-30 15:41:54 +00:00
}
2008-04-30 15:41:54 +00:00
void Canvas::setModified(unsigned index, bool iMod)
{
_StyleModules[index]->setModified(iMod);
2008-04-30 15:41:54 +00:00
}
void Canvas::resetModified(bool iMod /* = false */)
2008-04-30 15:41:54 +00:00
{
unsigned int size = _StyleModules.size();
for (unsigned int i = 0; i < size; ++i) {
setModified(i, iMod);
}
2008-04-30 15:41:54 +00:00
}
void Canvas::causalStyleModules(vector<unsigned> &vec, unsigned index)
{
unsigned int size = _StyleModules.size();
2008-04-30 15:41:54 +00:00
for (unsigned int i = index; i < size; ++i) {
if (_StyleModules[i]->getCausal()) {
vec.push_back(i);
}
}
2008-04-30 15:41:54 +00:00
}
void Canvas::Render(const StrokeRenderer *iRenderer)
{
for (unsigned int i = 0; i < _StyleModules.size(); ++i) {
if (!_StyleModules[i]->getDisplayed() || !_Layers[i]) {
continue;
}
_Layers[i]->Render(iRenderer);
}
2008-04-30 15:41:54 +00:00
}
void Canvas::RenderBasic(const StrokeRenderer *iRenderer)
{
for (unsigned int i = 0; i < _StyleModules.size(); ++i) {
if (!_StyleModules[i]->getDisplayed() || !_Layers[i]) {
continue;
}
_Layers[i]->RenderBasic(iRenderer);
}
2008-04-30 15:41:54 +00:00
}
void Canvas::loadMap(const char *iFileName,
const char *iMapName,
unsigned int iNbLevels,
float iSigma)
{
// check whether this map was already loaded:
if (!_maps.empty()) {
mapsMap::iterator m = _maps.find(iMapName);
if (m != _maps.end()) {
// lazy check for size changes
ImagePyramid *pyramid = (*m).second;
if ((pyramid->width() != width()) || (pyramid->height() != height())) {
delete pyramid;
}
else {
return;
}
}
}
string filePath;
if (_MapsPath) {
filePath = _MapsPath;
filePath += iFileName;
}
else {
filePath = iFileName;
}
#if 0 // soc
QImage *qimg;
QImage newMap(filePath.c_str());
if (newMap.isNull()) {
cerr << "Could not load image file " << filePath << endl;
return;
}
qimg = &newMap;
#endif
/* OCIO_TODO: support different input color space */
ImBuf *qimg = IMB_loadiffname(filePath.c_str(), 0, NULL);
if (qimg == 0) {
cerr << "Could not load image file " << filePath << endl;
return;
}
#if 0 // soc
// resize
QImage scaledImg;
if ((newMap.width() != width()) || (newMap.height() != height())) {
scaledImg = newMap.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qimg = &scaledImg;
}
#endif
ImBuf *scaledImg;
if ((qimg->x != width()) || (qimg->y != height())) {
scaledImg = IMB_dupImBuf(qimg);
IMB_scaleImBuf(scaledImg, width(), height());
}
// deal with color image
#if 0
if (newMap->depth() != 8) {
int w = newMap->width();
int h = newMap->height();
QImage *tmp = new QImage(w, h, 8);
for (unsigned int y = 0; y < h; ++y) {
for (unsigned int x = 0; x < w; ++x) {
int c = qGray(newMap->pixel(x, y));
tmp->setPixel(x, y, c);
}
}
delete newMap;
newMap = tmp;
}
#endif
int x, y;
int w = qimg->x;
int h = qimg->y;
int rowbytes = w * 4;
GrayImage tmp(w, h);
char *pix;
for (y = 0; y < h; ++y) {
for (x = 0; x < w; ++x) {
pix = (char *)qimg->rect + y * rowbytes + x * 4;
float c = (pix[0] * 11 + pix[1] * 16 + pix[2] * 5) / 32;
tmp.setPixel(x, y, c);
}
}
#if 0
GrayImage blur(w, h);
GaussianFilter gf(4.0f);
//int bound = gf.getBound();
for (y = 0; y < h; ++y) {
for (x = 0; x < w; ++x) {
int c = gf.getSmoothedPixel<GrayImage>(&tmp, x, y);
blur.setPixel(x, y, c);
}
}
#endif
GaussianPyramid *pyramid = new GaussianPyramid(tmp, iNbLevels, iSigma);
int ow = pyramid->width(0);
int oh = pyramid->height(0);
string base(iMapName); // soc
for (int i = 0; i < pyramid->getNumberOfLevels(); ++i) {
// save each image:
#if 0
w = pyramid.width(i);
h = pyramid.height(i);
#endif
// soc QImage qtmp(ow, oh, QImage::Format_RGB32);
ImBuf *qtmp = IMB_allocImBuf(ow, oh, 32, IB_rect);
// int k = (1 << i);
for (y = 0; y < oh; ++y) {
for (x = 0; x < ow; ++x) {
int c = pyramid->pixel(x, y, i); // 255 * pyramid->pixel(x, y, i);
// soc qtmp.setPixel(x, y, qRgb(c, c, c));
pix = (char *)qtmp->rect + y * rowbytes + x * 4;
pix[0] = pix[1] = pix[2] = c;
}
}
// soc qtmp.save(base + QString::number(i) + ".bmp", "BMP");
stringstream filename;
filename << base;
filename << i << ".bmp";
qtmp->ftype = IMB_FTYPE_BMP;
IMB_saveiff(qtmp, const_cast<char *>(filename.str().c_str()), 0);
}
#if 0
QImage *qtmp = new QImage(w, h, 32);
for (y = 0; y < h; ++y) {
for (x = 0; x < w; ++x) {
int c = (int)blur.pixel(x, y);
qtmp->setPixel(x, y, qRgb(c, c, c));
}
}
delete newMap;
newMap = qtmp;
#endif
_maps[iMapName] = pyramid;
// newMap->save("toto.bmp", "BMP");
2008-04-30 15:41:54 +00:00
}
float Canvas::readMapPixel(const char *iMapName, int level, int x, int y)
{
if (_maps.empty()) {
if (G.debug & G_DEBUG_FREESTYLE) {
cout << "readMapPixel warning: no map was loaded " << endl;
}
return -1;
}
mapsMap::iterator m = _maps.find(iMapName);
if (m == _maps.end()) {
if (G.debug & G_DEBUG_FREESTYLE) {
cout << "readMapPixel warning: no map was loaded with the name " << iMapName << endl;
}
return -1;
}
ImagePyramid *pyramid = (*m).second;
if ((x < 0) || (x >= pyramid->width()) || (y < 0) || (y >= pyramid->height())) {
return 0;
}
return pyramid->pixel(x, height() - 1 - y, level);
2008-04-30 15:41:54 +00:00
}
Attempt to fix a potential name conflict between Freestyle and the compositor. A crash in the Freestyle renderer was reported by Ton on IRC with a stack trace below. Note that #2 is in Freestyle, whereas #1 is in the compositor. The problem was observed in a debug build on OS X 10.7 (gcc 4.2, openmp disabled, no llvm). ---------------------------------------------------------------------- Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: 13 at address: 0x0000000000000000 [Switching to process 72386 thread 0xf303] 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 43 delete (this->m_outputsockets.back()); Current language: auto; currently c++ (gdb) where #0 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 #1 0x0000000100c29066 in Node::~Node (this=0x10e501c80) at COM_Node.h:49 #2 0x000000010089c273 in NodeShape::~NodeShape (this=0x10e501c80) at NodeShape.cpp:43 #3 0x000000010089910b in NodeGroup::destroy (this=0x10e501da0) at NodeGroup.cpp:61 #4 0x00000001008990cd in NodeGroup::destroy (this=0x10e5014b0) at NodeGroup.cpp:59 #5 0x00000001008990cd in NodeGroup::destroy (this=0x114e18da0) at NodeGroup.cpp:59 #6 0x00000001007e6602 in Controller::ClearRootNode (this=0x114e19640) at Controller.cpp:329 #7 0x00000001007ea52e in Controller::LoadMesh (this=0x114e19640, re=0x10aba4638, srl=0x1140f5258) at Controller.cpp:302 #8 0x00000001008030ad in prepare (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:302 #9 0x000000010080457a in FRS_do_stroke_rendering (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:600 #10 0x00000001006aeb9d in add_freestyle (re=0x10aba4638) at pipeline.c:1584 #11 0x00000001006aceb7 in do_render_3d (re=0x10aba4638) at pipeline.c:1094 #12 0x00000001006ae061 in do_render_fields_blur_3d (re=0x10aba4638) at pipeline.c:1367 #13 0x00000001006afa16 in do_render_composite_fields_blur_3d (re=0x10aba4638) at pipeline.c:1815 #14 0x00000001006b04e4 in do_render_all_options (re=0x10aba4638) at pipeline.c:2021 ---------------------------------------------------------------------- Apparently a name conflict between the two Blender modules is taking place. The present commit hence intends to address it by putting all the Freestyle C++ classes in the namespace 'Freestyle'. This revision will also prevent potential name conflicts with other Blender modules in the future. Special thanks to Lukas Toenne for the help with C++ namespace.
2013-04-09 00:46:49 +00:00
} /* namespace Freestyle */