43 #include <ACG/GL/acg_glew.hh> 44 #include "ToonRenderer.hh" 48 #include <ACG/GL/ShaderCache.hh> 49 #include <ACG/GL/ScreenQuad.hh> 50 #include <ACG/GL/GLError.hh> 54 #define CELSHADING_INCLUDE_FILE "ToonRenderer/celshading.glsl" 55 #define OUTLINE_VERTEXSHADER_FILE "ToonRenderer/screenquad.glsl" 56 #define OUTLINE_FRAGMENTSHADER_FILE "ToonRenderer/outline.glsl" 67 _shader->
addUniform(
"float g_celPaletteSize",
"//number of palettes/intensity levels for cel shading");
83 _shader->
addUniform(
"float g_celPaletteSize",
"//number of palettes/intensity levels for cel shading");
88 _code->push_back(
"outDepth = gl_FragCoord.z;");
100 case ACG::SG_LIGHT_DIRECTIONAL:
101 buf.sprintf(
"sg_cColor.xyz += LitDirLight_Cel(sg_vPosVS.xyz, sg_vNormalVS, g_vLightDir_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_celPaletteSize);", _lightId, _lightId, _lightId, _lightId);
104 case ACG::SG_LIGHT_POINT:
105 buf.sprintf(
"sg_cColor.xyz += LitPointLight_Cel(sg_vPosVS.xyz, sg_vNormalVS, g_vLightPos_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_vLightAtten_%d, g_celPaletteSize);", _lightId, _lightId, _lightId, _lightId, _lightId);
108 case ACG::SG_LIGHT_SPOT:
109 buf.sprintf(
"sg_cColor.xyz += LitSpotLight_Cel(sg_vPosVS.xyz, sg_vNormalVS, g_vLightPos_%d, g_vLightDir_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_vLightAtten_%d, g_vLightAngleExp_%d, g_celPaletteSize);", _lightId, _lightId, _lightId, _lightId, _lightId, _lightId, _lightId);
115 _code->push_back(buf);
127 ToonRenderer::ToonRenderer()
128 : progOutline_(0), paletteSize_(2.0f), outlineCol_(0.0f, 0.0f, 0.0f)
134 ToonRenderer::~ToonRenderer() {
137 QString ToonRenderer::checkOpenGL() {
139 return QString(
"Insufficient OpenGL Version! OpenGL 3.2 or higher required");
147 missing +=
"GL_ARB_vertex_buffer_object extension missing\n";
153 missing +=
"GL_ARB_vertex_program extension missing\n";
159 void ToonRenderer::initializePlugin() {
162 void ToonRenderer::exit() {
170 std::vector<ACG::ShaderModifier*> modifiers;
171 modifiers.push_back(&CelShadingModifier::instance);
172 return dumpCurrentRenderObjectsToString(&sortedObjects_[0], _outputShaderInfo, &modifiers);
194 glColorMask(1,1,1,1);
197 viewRes->scene_->bind();
202 glDrawBuffer(GL_COLOR_ATTACHMENT1);
203 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
204 glClear(GL_COLOR_BUFFER_BIT);
207 glDrawBuffer(GL_COLOR_ATTACHMENT0);
209 glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0f);
210 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
214 GLenum colorDepthTarget[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
215 glDrawBuffers(2, colorDepthTarget);
218 for (
int i = 0; i < getNumRenderObjects(); ++i) {
236 prog->
setUniform(
"g_celPaletteSize", paletteSize_);
238 renderObject(sortedObjects_[i], prog);
241 viewRes->scene_->unbind();
247 progOutline_ =
GLSL::loadProgram(OUTLINE_VERTEXSHADER_FILE, OUTLINE_FRAGMENTSHADER_FILE);
255 glColorMask(1,1,1,1);
261 glEnable(GL_DEPTH_TEST);
262 glDepthFunc(GL_ALWAYS);
267 progOutline_->setUniform(
"samplerScene", 0);
268 progOutline_->setUniform(
"samplerDepth", 1);
269 progOutline_->setUniform(
"texcoordOffset",
ACG::Vec2f(1.0f /
float(viewRes->scene_->width()), 1.0f /
float(viewRes->scene_->height()) ));
271 progOutline_->setUniform(
"outlineColor", outlineCol_);
273 glActiveTexture(GL_TEXTURE1);
274 glBindTexture(GL_TEXTURE_2D, viewRes->scene_->getAttachment(GL_COLOR_ATTACHMENT1));
276 glActiveTexture(GL_TEXTURE0);
277 glBindTexture(GL_TEXTURE_2D, viewRes->scene_->getAttachment(GL_COLOR_ATTACHMENT0));
282 progOutline_->disable();
285 glDepthFunc(GL_LESS);
291 finishRenderingPipeline();
297 QAction * action =
new QAction(
"Toon Renderer Options" ,
this );
299 connect(action,SIGNAL(triggered(
bool )),
this,SLOT(actionDialog(
bool )));
304 void ToonRenderer::paletteSizeChanged(
int _val ) {
305 paletteSize_ = float(_val) / 100.0f;
308 void ToonRenderer::outlineColorChanged( QColor _col ) {
309 outlineCol_[0] = _col.redF();
310 outlineCol_[1] = _col.greenF();
311 outlineCol_[2] = _col.blueF();
314 void ToonRenderer::ViewerResources::resize(
int _newWidth,
int _newHeight ) {
315 if (!_newHeight || !_newWidth)
return;
324 scene_->attachTexture2D(GL_COLOR_ATTACHMENT0, _newWidth, _newHeight, GL_RGBA, GL_RGBA);
325 scene_->attachTexture2D(GL_COLOR_ATTACHMENT1, _newWidth, _newHeight, GL_R32F, GL_RED);
326 scene_->attachTexture2DDepth(_newWidth, _newHeight);
329 if (scene_->height() == _newHeight &&
330 scene_->width() == _newWidth)
333 scene_->resize(_newWidth, _newHeight);
static QString getShaderDir()
Collection of fbos for each viewport.
void addOutput(const QString &_output)
Add one GLSL output specifier.
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
void use()
Enables the program object for using.
int viewport_width() const
get viewport width
bool replaceDefaultLightingCode()
Specify whether this modifier replaces or extends the default lighting code.
void addIncludeFile(QString _fileName)
Imports another shader, same as #include.
double near_plane() const
get near clipping distance
QString renderObjectsInfo(bool _outputShaderInfo)
Return a qstring of the current render objects.
QAction * optionsAction()
Return options menu.
ACG::SceneGraph::BaseNode * getSceneGraphRootNode()
get scenegraph root node
void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
set draw mode (No test if this mode is available!)
int getFragDataLocation(const char *_name)
Get location of the fragment data output.
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
void modifyLightingCode(QStringList *_code, int _lightId, ACG::ShaderGenLightType _lightType)
Modify the default lighting code of the shader generator.
void modifyFragmentEndCode(QStringList *_code)
Append code the the fragment shader.
int viewport_height() const
get viewport height
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
bool openGLVersion(const int _major, const int _minor, bool _verbose)
void addUniform(QString _uniform, QString _comment="")
Add one GLSL uniform specifier.
void modifyVertexIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the vertex shader.
ACG::Vec4f backgroundColor()
Get current background color.
void bindFragDataLocation(unsigned int _index, const char *_name)
Bind fragment output data to name.
bool checkExtensionSupported(const std::string &_extension)
static unsigned int registerModifier(ShaderModifier *_modifier)
Shader modifiers have to be registered before they can be used. They also must remain allocated for t...
int viewerId()
Get the id of the viewer this viewerproperties belongs to.
void modifyFragmentIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the fragment shader.
void link()
Links the shader objects to the program.
double far_plane() const
get far clipping distance