50 #include <ACG/GL/acg_glew.hh>
51 #include "ToonRenderer.hh"
55 #include <ACG/GL/ShaderCache.hh>
56 #include <ACG/GL/ScreenQuad.hh>
57 #include <ACG/GL/GLError.hh>
61 #define CELSHADING_INCLUDE_FILE "ToonRenderer/celshading.glsl"
62 #define OUTLINE_VERTEXSHADER_FILE "ToonRenderer/screenquad.glsl"
63 #define OUTLINE_FRAGMENTSHADER_FILE "ToonRenderer/outline.glsl"
74 _shader->
addUniform(
"float g_celPaletteSize",
"//number of palettes/intensity levels for cel shading");
90 _shader->
addUniform(
"float g_celPaletteSize",
"//number of palettes/intensity levels for cel shading");
95 _code->push_back(
"outDepth = gl_FragCoord.z;");
106 switch (_lightType) {
107 case ACG::SG_LIGHT_DIRECTIONAL:
108 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);
111 case ACG::SG_LIGHT_POINT:
112 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);
115 case ACG::SG_LIGHT_SPOT:
116 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);
122 _code->push_back(buf);
134 ToonRenderer::ToonRenderer()
135 : progOutline_(0), paletteSize_(2.0f), outlineCol_(0.0f, 0.0f, 0.0f)
141 ToonRenderer::~ToonRenderer() {
145 QString ToonRenderer::checkOpenGL() {
147 return QString(
"Insufficient OpenGL Version! OpenGL 3.2 or higher required");
152 missing +=
"GL_ARB_vertex_buffer_object extension missing\n";
156 missing +=
"GL_ARB_vertex_program extension missing\n";
162 void ToonRenderer::initializePlugin() {
166 std::vector<ACG::ShaderModifier*> modifiers;
167 modifiers.push_back(&CelShadingModifier::instance);
190 glColorMask(1,1,1,1);
193 viewRes->scene_->bind();
198 glDrawBuffer(GL_COLOR_ATTACHMENT1);
199 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
200 glClear(GL_COLOR_BUFFER_BIT);
203 glDrawBuffer(GL_COLOR_ATTACHMENT0);
205 glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0f);
206 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
210 GLenum colorDepthTarget[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
211 glDrawBuffers(2, colorDepthTarget);
237 viewRes->scene_->unbind();
251 glColorMask(1,1,1,1);
257 glEnable(GL_DEPTH_TEST);
258 glDepthFunc(GL_ALWAYS);
269 glActiveTexture(GL_TEXTURE1);
270 glBindTexture(GL_TEXTURE_2D, viewRes->scene_->getAttachment(GL_COLOR_ATTACHMENT1));
272 glActiveTexture(GL_TEXTURE0);
273 glBindTexture(GL_TEXTURE_2D, viewRes->scene_->getAttachment(GL_COLOR_ATTACHMENT0));
281 glDepthFunc(GL_LESS);
293 QAction * action =
new QAction(
"Toon Renderer Options" ,
this );
295 connect(action,SIGNAL(triggered(
bool )),
this,SLOT(actionDialog(
bool )));
300 void ToonRenderer::paletteSizeChanged(
int _val ) {
304 void ToonRenderer::outlineColorChanged( QColor _col ) {
310 void ToonRenderer::ViewerResources::resize(
int _newWidth,
int _newHeight ) {
311 if (!_newHeight || !_newWidth)
return;
320 scene_->
attachTexture2D(GL_COLOR_ATTACHMENT0, _newWidth, _newHeight, GL_RGBA, GL_RGBA);
321 scene_->
attachTexture2D(GL_COLOR_ATTACHMENT1, _newWidth, _newHeight, GL_R32F, GL_RED);
325 if (scene_->
height() == _newHeight &&
326 scene_->
width() == _newWidth)
329 scene_->
resize(_newWidth, _newHeight);
334 #if QT_VERSION < 0x050000
void addUniform(QString _uniform, QString _comment="")
Add one GLSL uniform specifier.
float paletteSize_
size of cel shading palette
double far_plane() const
get far clipping distance
int getFragDataLocation(const char *_name)
Get location of the fragment data output.
virtual void renderObject(ACG::RenderObject *_obj, GLSL::Program *_prog=0, bool _constRenderStates=false, const std::vector< unsigned int > *_shaderModifiers=0)
Render one renderobject.
void modifyLightingCode(QStringList *_code, int _lightId, ACG::ShaderGenLightType _lightType)
Modify the default lighting code of the shader generator.
void attachTexture2D(GLenum _attachment, GLsizei _width, GLsizei _height, GLuint _internalFmt, GLenum _format, GLint _wrapMode=GL_CLAMP, GLint _minFilter=GL_NEAREST, GLint _magFilter=GL_NEAREST)
function to attach a texture to fbo
void bindFragDataLocation(unsigned int _index, const char *_name)
Bind fragment output data to name.
std::vector< ACG::RenderObject * > sortedObjects_
sorted list of renderobjects without overlay objects (sorted in rendering order)
GLsizei width() const
get width of fbo texture
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)
QAction * optionsAction()
Return options menu.
static unsigned int registerModifier(ShaderModifier *_modifier)
Shader modifiers have to be registered before they can be used. They also must remain allocated for t...
void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
set draw mode (No test if this mode is available!)
bool openGLVersion(const int _major, const int _minor)
ACG::SceneGraph::BaseNode * getSceneGraphRootNode()
get scenegraph root node
QString renderObjectsInfo(bool _outputShaderInfo)
Return a qstring of the current render objects.
void addOutput(const QString &_output)
Add one GLSL output specifier.
GLSL::Program * progOutline_
outline shader: multiply scene color with edge factor derived from edges in depth buffer ...
ACG::Vec3f outlineCol_
outline color
bool checkExtensionSupported(const std::string &_extension)
virtual QString dumpCurrentRenderObjectsToString(ACG::RenderObject **_list=0, bool _outputShaders=false, std::vector< ACG::ShaderModifier * > *_modifiers=0)
Outputs the current render objects to the string.
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
void use()
Enables the program object for using.
int viewport_width() const
get viewport width
void resize(GLsizei _width, GLsizei _height, bool _forceResize=false)
resize function (if textures created by this class)
void modifyFragmentIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the fragment shader.
void addIncludeFile(QString _fileName)
Imports another shader, same as #include.
int viewerId()
Get the id of the viewer this viewerproperties belongs to.
void attachTexture2DDepth(GLsizei _width, GLsizei _height, GLuint _internalFmt=GL_DEPTH_COMPONENT32, GLenum _format=GL_DEPTH_COMPONENT)
function to attach a depth-buffer texture to fbo (using GL_DEPTH_ATTACHMENT)
double near_plane() const
get near clipping distance
std::map< int, ViewerResources > viewerRes_
void modifyVertexIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the vertex shader.
void link()
Links the shader objects to the program.
bool replaceDefaultLightingCode()
Specify whether this modifier replaces or extends the default lighting code.
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
void modifyFragmentEndCode(QStringList *_code)
Append code the the fragment shader.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
void init()
function to generate the framebuffer object
virtual void finishRenderingPipeline(bool _drawOverlay=true)
Draw overlay objects and reset OpenGL state.
virtual void restoreInputFbo()
Restore the previously saved input Fbo configuration (fbo id + viewport)
virtual void prepareRenderingPipeline(ACG::GLState *_glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode *_scenegraphRoot)
Prepares renderer and OpenGL for any draw-related calls including.
void disable()
Resets to standard rendering pipeline.
GLsizei height() const
get height of fbo texture
static QString getShaderDir()
int viewport_height() const
get viewport height
ACG::Vec4f backgroundColor()
Get current background color.
int getNumRenderObjects() const
Get the number of collected render objects (not including overlay objects or gl4.2 line objects) ...