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);
168 return dumpCurrentRenderObjectsToString(&sortedObjects_[0], _outputShaderInfo, &modifiers);
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);
214 for (
int i = 0; i < getNumRenderObjects(); ++i) {
232 prog->
setUniform(
"g_celPaletteSize", paletteSize_);
234 renderObject(sortedObjects_[i], prog);
237 viewRes->scene_->unbind();
243 progOutline_ =
GLSL::loadProgram(OUTLINE_VERTEXSHADER_FILE, OUTLINE_FRAGMENTSHADER_FILE);
251 glColorMask(1,1,1,1);
257 glEnable(GL_DEPTH_TEST);
258 glDepthFunc(GL_ALWAYS);
263 progOutline_->setUniform(
"samplerScene", 0);
264 progOutline_->setUniform(
"samplerDepth", 1);
265 progOutline_->setUniform(
"texcoordOffset",
ACG::Vec2f(1.0f /
float(viewRes->scene_->width()), 1.0f /
float(viewRes->scene_->height()) ));
267 progOutline_->setUniform(
"outlineColor", outlineCol_);
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));
278 progOutline_->disable();
281 glDepthFunc(GL_LESS);
287 finishRenderingPipeline();
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 ) {
301 paletteSize_ = float(_val) / 100.0f;
304 void ToonRenderer::outlineColorChanged( QColor _col ) {
305 outlineCol_[0] = _col.redF();
306 outlineCol_[1] = _col.greenF();
307 outlineCol_[2] = _col.blueF();
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);
322 scene_->attachTexture2DDepth(_newWidth, _newHeight);
325 if (scene_->height() == _newHeight &&
326 scene_->width() == _newWidth)
329 scene_->resize(_newWidth, _newHeight);
334 #if QT_VERSION < 0x050000 void bindFragDataLocation(unsigned int _index, const char *_name)
Bind fragment output data to name.
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
void modifyFragmentIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the fragment shader.
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
Collection of fbos for each viewport.
void link()
Links the shader objects to the program.
QString renderObjectsInfo(bool _outputShaderInfo)
Return a qstring of the current render objects.
QAction * optionsAction()
Return options menu.
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)
bool replaceDefaultLightingCode()
Specify whether this modifier replaces or extends the default lighting code.
int viewport_width() const
get viewport width
void use()
Enables the program object for using.
static QString getShaderDir()
void addIncludeFile(QString _fileName)
Imports another shader, same as #include.
void modifyVertexIO(ACG::ShaderGenerator *_shader)
Add your own inputs/outputs to the vertex shader.
bool openGLVersion(const int _major, const int _minor)
int getFragDataLocation(const char *_name)
Get location of the fragment data output.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
void addUniform(QString _uniform, QString _comment="")
Add one GLSL uniform specifier.
int viewerId()
Get the id of the viewer this viewerproperties belongs to.
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.
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
double near_plane() const
get near clipping distance
void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
set draw mode (No test if this mode is available!)
bool checkExtensionSupported(const std::string &_extension)
int viewport_height() const
get viewport height
ACG::SceneGraph::BaseNode * getSceneGraphRootNode()
get scenegraph root node
static unsigned int registerModifier(ShaderModifier *_modifier)
Shader modifiers have to be registered before they can be used. They also must remain allocated for t...
ACG::Vec4f backgroundColor()
Get current background color.
void addOutput(const QString &_output)
Add one GLSL output specifier.
double far_plane() const
get far clipping distance