diff --git a/src/meshlabplugins/render_radiance_scaling/framebufferObject.cpp b/src/meshlabplugins/render_radiance_scaling/framebufferObject.cpp new file mode 100644 index 000000000..2cb1c2975 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/framebufferObject.cpp @@ -0,0 +1,237 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include "framebufferObject.h" +#include +#include + +using namespace std; + +std::vector _buffers; + +FramebufferObject::FramebufferObject() + : fbo_id(0) { + glGenFramebuffersEXT(1,&fbo_id); +} + +FramebufferObject::~FramebufferObject() { + glDeleteFramebuffersEXT(1,&fbo_id); +} + +void FramebufferObject::attachTexture(GLenum tex_target,GLuint tex_id, + GLenum attachment,int mip_level,int z_slice) { + unbindCurrentBindThis(); + + glBindTexture(tex_target,tex_id); + + if(tex_target==GL_TEXTURE_1D) + glFramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,attachment, + GL_TEXTURE_1D,tex_id,mip_level); + else if(tex_target == GL_TEXTURE_3D) + glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,attachment, + GL_TEXTURE_3D,tex_id,mip_level,z_slice); + else + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,attachment, + tex_target,tex_id,mip_level); + + unbindThisBindCurrent(); +} + +void FramebufferObject::attachRenderBuffer(GLuint buff_id,GLenum attachment) { + unbindCurrentBindThis(); + + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,attachment, + GL_RENDERBUFFER_EXT,buff_id); + + unbindThisBindCurrent(); +} + +void FramebufferObject::unattach(GLenum attachment) { + unbindCurrentBindThis(); + + GLenum type = getAttachedType(attachment); + switch(type){ + case GL_RENDERBUFFER_EXT: + attachRenderBuffer(0, attachment); + break; + case GL_TEXTURE: + attachTexture(GL_TEXTURE_2D, 0, attachment); + break; + default: + break; + } + + unbindThisBindCurrent(); +} + +void FramebufferObject::unattachAll() { + int nb_attachments = getMaxColorAttachments(); + for(int i=0;i +#include + +class FramebufferObject { + public: + FramebufferObject(); + ~FramebufferObject(); + + inline void bind(); + inline static void unbind(); + + void attachTexture(GLenum tex_target, + GLuint tex_id, + GLenum attachment = GL_COLOR_ATTACHMENT0_EXT, + int mip_level = 0, + int z_slice = 0); + + void attachRenderBuffer(GLuint rb_id, + GLenum attachment = GL_COLOR_ATTACHMENT0_EXT); + + void unattach(GLenum attachment); + void unattachAll(); + + bool isValid(); + + GLenum getAttachedType(GLenum attachment); + GLuint getAttachedId(GLenum attachment); + GLint getAttachedMipLevel(GLenum attachment); + GLint getAttachedCubeFace(GLenum attachment); + GLint getAttachedZSlice(GLenum attachment); + + static int getMaxColorAttachments(); + static GLenum *buffers(unsigned int i=0); + + private: + void unbindCurrentBindThis(); + void unbindThisBindCurrent(); + + GLuint fbo_id; + GLint current_fbo_id; +}; + +inline void FramebufferObject::bind() { + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fbo_id); +} + +inline void FramebufferObject::unbind() { + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); +} + + +#endif // FRAMEBUFFER_OBJECT diff --git a/src/meshlabplugins/render_radiance_scaling/gpl.txt b/src/meshlabplugins/render_radiance_scaling/gpl.txt new file mode 100644 index 000000000..b6ee268ef --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/gpl.txt @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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 St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/src/meshlabplugins/render_radiance_scaling/gpuProgram.cpp b/src/meshlabplugins/render_radiance_scaling/gpuProgram.cpp new file mode 100644 index 000000000..3cfea127f --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/gpuProgram.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include "gpuProgram.h" +#include + +using namespace std; + + +GPUProgram::GPUProgram(GPUShader* vs,GPUShader* fs,GPUShader *gs, + int inputGeometry,int outputGeometry,int outVertices) + : _vs(vs), + _fs(fs), + _gs(gs), + _inputGeometry(inputGeometry), + _outputGeometry(outputGeometry), + _outVertices(outVertices) { + + _programId = glCreateProgram(); + setGeometryParameters(_inputGeometry,_outputGeometry,_outVertices); + attachAndLink(); + } + +GPUProgram::GPUProgram(const string &vsFile, + const string &fsFile, + const string &gsFile, + int inputGeometry, + int outputGeometry, + int outVertices) + :_inputGeometry(inputGeometry), + _outputGeometry(outputGeometry), + _outVertices(outVertices) { + + _vs = _fs = _gs = NULL; + + if(vsFile!="") + _vs = new GPUShader(VERT,vsFile); + + if(fsFile!="") + _fs = new GPUShader(FRAG,fsFile); + + if(gsFile!="") + _gs = new GPUShader(GEOM,gsFile); + + _programId = glCreateProgram(); + setGeometryParameters(_inputGeometry,_outputGeometry,_outVertices); + attachAndLink(); + } + +GPUProgram::~GPUProgram() { + + detach(); + + if(_vs!=NULL) { + delete _vs; + } + + if(_fs!=NULL) { + delete _fs; + } + + if(_gs!=NULL) { + delete _gs; + } + + glDeleteProgram(_programId); +} + +void GPUProgram::setGeometryParameters(int inputGeometry,int outputGeometry,int outVertices) { +#ifdef GL_EXT_geometry_shader4 + if(GL_EXT_geometry_shader4 && _gs!=NULL && _gs->id()!=0) { + glProgramParameteriEXT(_programId,GL_GEOMETRY_INPUT_TYPE_EXT,inputGeometry); + glProgramParameteriEXT(_programId,GL_GEOMETRY_OUTPUT_TYPE_EXT,outputGeometry); + glProgramParameteriEXT(_programId,GL_GEOMETRY_VERTICES_OUT_EXT,outVertices); + } +#endif +} + +void GPUProgram::attach() { + if(_vs!=NULL) { + glAttachShader(_programId,_vs->id()); + } + + if(_fs!=NULL) { + glAttachShader(_programId,_fs->id()); + } + + if(_gs!=NULL) { + glAttachShader(_programId,_gs->id()); + } +} + +void GPUProgram::detach() { + + if(_vs!=NULL) { + glDetachShader(_programId,_vs->id()); + } + + if(_fs!=NULL) { + glDetachShader(_programId,_fs->id()); + } + + if(_gs!=NULL) { + glDetachShader(_programId,_gs->id()); + } +} + +bool GPUProgram::link() { + int linked = 1; + + glLinkProgram(_programId); + + glGetObjectParameterivARB(_programId,GL_OBJECT_LINK_STATUS_ARB,&linked); + + if(linked) + return true; + + return false; +} + +bool GPUProgram::attachAndLink() { + attach(); + return link(); +} + +void GPUProgram::reload() { + + detach(); + + bool allOk = true; + if(_vs!=NULL) { + allOk = allOk && _vs->loadAndCompile(); + } + + if(_fs!=NULL) { + allOk = allOk && _fs->loadAndCompile(); + } + + if(_gs!=NULL) { + allOk = allOk && _gs->loadAndCompile(); + } + + if(!allOk){ + std::cout << "reload fail, maybe missing file" << std::endl; + } + setGeometryParameters(_inputGeometry,_outputGeometry,_outVertices); + attachAndLink(); + + // reload uniforms + for(map::iterator i=_uniformLocations.begin();i!=_uniformLocations.end();i++) { + _uniformLocations[(*i).first] = glGetUniformLocation(_programId,(*i).first.c_str()); + } + + // reload attributes + for(map::iterator i=_attributeLocations.begin();i!=_attributeLocations.end();i++) { + _uniformLocations[(*i).first] = glGetAttribLocation(_programId,(*i).first.c_str()); + } + + // free textures + _textures.clear(); +} + +void GPUProgram::addUniform(const string &uniformName) { + GLint location = glGetUniformLocation(_programId,uniformName.c_str()); + + _uniformLocations[uniformName] = location; +} + +void GPUProgram::addAttribute(const string &attributeName) { + GLint location = glGetAttribLocation(_programId,attributeName.c_str()); + + _attributeLocations[attributeName] = location; +} + +bool GPUProgram::haveShaderOfType(SHADER_TYPE type) { + + if(type==VERT) + return _vs!=NULL; + + if(type==FRAG) + return _fs!=NULL; + + if(type==GEOM) + return _gs!=NULL; + + cout << "Warning : unknown type !" << endl; + + return false; +} + +string GPUProgram::filename(SHADER_TYPE type) { + + if(type==VERT && _vs!=NULL) + return _vs->filename(); + + if(type==FRAG && _fs!=NULL) + return _fs->filename(); + + if(type==GEOM && _gs!=NULL) + return _gs->filename(); + + cout << "Warning : unknown type !" << endl; + + return ""; +} diff --git a/src/meshlabplugins/render_radiance_scaling/gpuProgram.h b/src/meshlabplugins/render_radiance_scaling/gpuProgram.h new file mode 100644 index 000000000..16d8d32b0 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/gpuProgram.h @@ -0,0 +1,404 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#ifndef GPUPROGRAM_H +#define GPUPROGRAM_H + +#include +#include +#include +#include +#include +#include + +class GPUProgram { + + public: + GPUProgram(GPUShader *vs=NULL,GPUShader *fs=NULL,GPUShader *geom=NULL, + int inputGeometry=0,int outputGeometry=0,int outVertices=0); + + GPUProgram(const std::string &vsFile="", + const std::string &fsFile="", + const std::string &gsFile="", + int inputGeometry=0, + int outputGeometry=0, + int outVertices=0); + + ~GPUProgram(); + + void addUniform(const std::string &uniformName); + void addAttribute(const std::string &attributeName); + void reload(); + + inline GLuint id() const; + bool haveShaderOfType(SHADER_TYPE type); + std::string filename(SHADER_TYPE type); + + inline void enable(); + inline void disable(); + + inline GLint getUniformLocation(const std::string &uniformName); + inline GLint getAttributeLocation(const std::string &attributeName); + + inline void setUniform1f(const std::string &uniformName,GLfloat v); + inline void setUniform2f(const std::string &uniformName,GLfloat v1,GLfloat v2); + inline void setUniform3f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3); + inline void setUniform4f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4); + inline void setUniform1f(GLint loc,GLfloat v); + inline void setUniform2f(GLint loc,GLfloat v1,GLfloat v2); + inline void setUniform3f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3); + inline void setUniform4f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4); + + inline void setUniform1i(const std::string &uniformName,GLint v); + inline void setUniform2i(const std::string &uniformName,GLint v1,GLint v2); + inline void setUniform3i(const std::string &uniformName,GLint v1,GLint v2,GLint v3); + inline void setUniform4i(const std::string &uniformName,GLint v1,GLint v2,GLint v3,GLint v4); + inline void setUniform1i(GLint loc,GLint v); + inline void setUniform2i(GLint loc,GLint v1,GLint v2); + inline void setUniform3i(GLint loc,GLint v1,GLint v2,GLint v3); + inline void setUniform4i(GLint loc,GLint v1,GLint v2,GLint v3,GLint v4); + + inline void setUniform1fv(const std::string &uniformName,GLfloat *v,GLsizei count=1); + inline void setUniform2fv(const std::string &uniformName,GLfloat *v,GLsizei count=1); + inline void setUniform3fv(const std::string &uniformName,GLfloat *v,GLsizei count=1); + inline void setUniform4fv(const std::string &uniformName,GLfloat *v,GLsizei count=1); + inline void setUniform1fv(GLint loc,GLfloat *v,GLsizei count=1); + inline void setUniform2fv(GLint loc,GLfloat *v,GLsizei count=1); + inline void setUniform3fv(GLint loc,GLfloat *v,GLsizei count=1); + inline void setUniform4fv(GLint loc,GLfloat *v,GLsizei count=1); + + inline void setUniform1iv(const std::string &uniformName,GLint *v,GLsizei count=1); + inline void setUniform2iv(const std::string &uniformName,GLint *v,GLsizei count=1); + inline void setUniform3iv(const std::string &uniformName,GLint *v,GLsizei count=1); + inline void setUniform4iv(const std::string &uniformName,GLint *v,GLsizei count=1); + inline void setUniform1iv(GLint loc,GLint *v,GLsizei count=1); + inline void setUniform2iv(GLint loc,GLint *v,GLsizei count=1); + inline void setUniform3iv(GLint loc,GLint *v,GLsizei count=1); + inline void setUniform4iv(GLint loc,GLint *v,GLsizei count=1); + + inline void setUniformMatrix2fv(const std::string &uniformName,GLfloat *v,GLsizei count=1,GLboolean transpose=false); + inline void setUniformMatrix3fv(const std::string &uniformName,GLfloat *v,GLsizei count=1,GLboolean transpose=false); + inline void setUniformMatrix4fv(const std::string &uniformName,GLfloat *v,GLsizei count=1,GLboolean transpose=false); + inline void setUniformMatrix2fv(GLint loc,GLfloat *v,GLsizei count=1,GLboolean transpose=false); + inline void setUniformMatrix3fv(GLint loc,GLfloat *v,GLsizei count=1,GLboolean transpose=false); + inline void setUniformMatrix4fv(GLint loc,GLfloat *v,GLsizei count=1,GLboolean transpose=false); + + inline void setUniformTexture(const std::string &uniformName,GLint num,GLenum type,GLuint textureName); + inline void setUniformTexture(GLint loc,GLint num,GLenum type,GLuint textureName); + + inline void setUniformBuffer(const std::string &uniformName,GLuint buffer); + inline void setUniformBuffer(GLint loc,GLuint buffer); + inline GLint getUniformBufferSize(const std::string &uniformName); + inline GLint getUniformBufferSize(GLint loc); + + protected: + void attach(); + bool link(); + bool attachAndLink(); + void detach(); + void setGeometryParameters(int inputGeometry,int outputGeometry,int outVertices); + + private: + + GPUShader* _vs; + GPUShader* _fs; + GPUShader* _gs; + GLuint _programId; + + std::map _uniformLocations; + std::map _attributeLocations; + std::map > _textures; + + int _inputGeometry; + int _outputGeometry; + int _outVertices; +}; + +inline void GPUProgram::enable() { + glUseProgramObjectARB(_programId); + + for(std::map >::iterator i=_textures.begin();i!=_textures.end();++i) { + glActiveTexture((*i).second.first); + glBindTexture((*i).second.second,(*i).first); + glEnable((*i).second.second); + } + +} + +inline void GPUProgram::disable() { + for(std::map >::reverse_iterator i=_textures.rbegin();i!=_textures.rend();++i) { + glActiveTexture((*i).second.first); + glDisable((*i).second.second); + } + + glUseProgramObjectARB(0); +} + +inline GLuint GPUProgram::id() const { + return _programId; +} + +inline void GPUProgram::setUniform1f(const std::string &uniformName,GLfloat v) { + glUniform1f(_uniformLocations[uniformName],v); +} + +inline void GPUProgram::setUniform2f(const std::string &uniformName,GLfloat v1,GLfloat v2) { + glUniform2f(_uniformLocations[uniformName],v1,v2); +} + +inline void GPUProgram::setUniform3f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3) { + glUniform3f(_uniformLocations[uniformName],v1,v2,v3); +} + +inline void GPUProgram::setUniform4f(const std::string &uniformName,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4) { + glUniform4f(_uniformLocations[uniformName],v1,v2,v3,v4); +} + +inline void GPUProgram::setUniform1i(const std::string &uniformName,GLint v) { + glUniform1i(_uniformLocations[uniformName],v); +} + +inline void GPUProgram::setUniform2i(const std::string &uniformName,GLint v1,GLint v2) { + glUniform2i(_uniformLocations[uniformName],v1,v2); +} + +inline void GPUProgram::setUniform3i(const std::string &uniformName,GLint v1,GLint v2,GLint v3) { + glUniform3i(_uniformLocations[uniformName],v1,v2,v3); +} + +inline void GPUProgram::setUniform4i(const std::string &uniformName,GLint v1,GLint v2,GLint v3,GLint v4) { + glUniform4i(_uniformLocations[uniformName],v1,v2,v3,v4); +} + +inline void GPUProgram::setUniform1fv(const std::string &uniformName,GLfloat *v,GLsizei count) { + glUniform1fv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform2fv(const std::string &uniformName,GLfloat *v,GLsizei count) { + glUniform2fv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform3fv(const std::string &uniformName,GLfloat *v,GLsizei count) { + glUniform3fv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform4fv(const std::string &uniformName,GLfloat *v,GLsizei count) { + glUniform4fv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform1iv(const std::string &uniformName,GLint *v,GLsizei count) { + glUniform1iv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform2iv(const std::string &uniformName,GLint *v,GLsizei count) { + glUniform2iv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform3iv(const std::string &uniformName,GLint *v,GLsizei count) { + glUniform3iv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniform4iv(const std::string &uniformName,GLint *v,GLsizei count) { + glUniform4iv(_uniformLocations[uniformName],count,v); +} + +inline void GPUProgram::setUniformMatrix2fv(const std::string &uniformName,GLfloat *v,GLsizei count,GLboolean transpose) { + glUniformMatrix2fv(_uniformLocations[uniformName],count,transpose,v); +} + +inline void GPUProgram::setUniformMatrix3fv(const std::string &uniformName,GLfloat *v,GLsizei count,GLboolean transpose) { + glUniformMatrix3fv(_uniformLocations[uniformName],count,transpose,v); +} + +inline void GPUProgram::setUniformMatrix4fv(const std::string &uniformName,GLfloat *v,GLsizei count,GLboolean transpose) { + glUniformMatrix4fv(_uniformLocations[uniformName],count,transpose,v); +} + +inline void GPUProgram::setUniformTexture(const std::string &uniformName,GLint num,GLenum type,GLuint textureName) { + GLenum textureNum; + + const std::map >::iterator it = _textures.find(textureName); + + if(it==_textures.end()) { + textureNum = GL_TEXTURE0+_textures.size(); + } else { + textureNum = (*it).second.first; + } + + glPushAttrib(GL_TEXTURE_BIT); + glActiveTexture(textureNum); + glBindTexture(type,textureName); + glEnable(type); + + glUniform1i(_uniformLocations[uniformName],num); + _textures[textureName] = std::pair(textureNum,type); + + glDisable(type); + glPopAttrib(); +} + +inline void GPUProgram::setUniform1f(GLint loc,GLfloat v) { + glUniform1f(loc,v); +} + +inline void GPUProgram::setUniform2f(GLint loc,GLfloat v1,GLfloat v2) { + glUniform2f(loc,v1,v2); +} + +inline void GPUProgram::setUniform3f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3) { + glUniform3f(loc,v1,v2,v3); +} + +inline void GPUProgram::setUniform4f(GLint loc,GLfloat v1,GLfloat v2,GLfloat v3,GLfloat v4) { + glUniform4f(loc,v1,v2,v3,v4); +} + +inline void GPUProgram::setUniform1i(GLint loc,GLint v) { + glUniform1i(loc,v); +} + +inline void GPUProgram::setUniform2i(GLint loc,GLint v1,GLint v2) { + glUniform2i(loc,v1,v2); +} + +inline void GPUProgram::setUniform3i(GLint loc,GLint v1,GLint v2,GLint v3) { + glUniform3i(loc,v1,v2,v3); +} + +inline void GPUProgram::setUniform4i(GLint loc,GLint v1,GLint v2,GLint v3,GLint v4) { + glUniform4i(loc,v1,v2,v3,v4); +} + +inline void GPUProgram::setUniform1fv(GLint loc,GLfloat *v,GLsizei count) { + glUniform1fv(loc,count,v); +} + +inline void GPUProgram::setUniform2fv(GLint loc,GLfloat *v,GLsizei count) { + glUniform2fv(loc,count,v); +} + +inline void GPUProgram::setUniform3fv(GLint loc,GLfloat *v,GLsizei count) { + glUniform3fv(loc,count,v); +} + +inline void GPUProgram::setUniform4fv(GLint loc,GLfloat *v,GLsizei count) { + glUniform4fv(loc,count,v); +} + +inline void GPUProgram::setUniform1iv(GLint loc,GLint *v,GLsizei count) { + glUniform1iv(loc,count,v); +} + +inline void GPUProgram::setUniform2iv(GLint loc,GLint *v,GLsizei count) { + glUniform2iv(loc,count,v); +} + +inline void GPUProgram::setUniform3iv(GLint loc,GLint *v,GLsizei count) { + glUniform3iv(loc,count,v); +} + +inline void GPUProgram::setUniform4iv(GLint loc,GLint *v,GLsizei count) { + glUniform4iv(loc,count,v); +} + +inline void GPUProgram::setUniformMatrix2fv(GLint loc,GLfloat *v,GLsizei count,GLboolean transpose) { + glUniformMatrix2fv(loc,count,transpose,v); +} + +inline void GPUProgram::setUniformMatrix3fv(GLint loc,GLfloat *v,GLsizei count,GLboolean transpose) { + glUniformMatrix3fv(loc,count,transpose,v); +} + +inline void GPUProgram::setUniformMatrix4fv(GLint loc,GLfloat *v,GLsizei count,GLboolean transpose) { + glUniformMatrix4fv(loc,count,transpose,v); +} + +inline void GPUProgram::setUniformTexture(GLint loc,GLint num,GLenum type,GLuint textureName) { + GLenum textureNum; + + const std::map >::iterator it = _textures.find(textureName); + + if(it==_textures.end()) { + textureNum = GL_TEXTURE0+_textures.size(); + } else { + textureNum = (*it).second.first; + } + + glPushAttrib(GL_TEXTURE_BIT); + glActiveTexture(textureNum); + glBindTexture(type,textureName); + glEnable(type); + + glUniform1i(loc,num); + _textures[textureName] = std::pair(textureNum,type); + + glDisable(type); + glPopAttrib(); +} + +inline GLint GPUProgram::getUniformLocation(const std::string &uniformName) { + return _uniformLocations[uniformName]; +} + +inline GLint GPUProgram::getAttributeLocation(const std::string &attributeName) { + return _attributeLocations[attributeName]; +} + +inline void GPUProgram::setUniformBuffer(const std::string &uniformName,GLuint buffer) { + glUniformBufferEXT(_programId,_uniformLocations[uniformName],buffer); +} + +inline void GPUProgram::setUniformBuffer(GLint loc,GLuint buffer) { + glUniformBufferEXT(_programId,loc,buffer); +} + +inline GLint GPUProgram::getUniformBufferSize(const std::string &uniformName) { + return glGetUniformBufferSizeEXT(_programId,_uniformLocations[uniformName]); +} + +inline GLint GPUProgram::getUniformBufferSize(GLint loc) { + return glGetUniformBufferSizeEXT(_programId,loc); +} + +static inline +void CheckErrorsGL(const char* location=NULL,std::ostream& ostr = std::cerr) { + + GLuint errnum; + const char *errstr; + while((errnum=glGetError())) { + ostr << " **************************************************** " << std::endl; + ostr << " Checking OpenGL Error " << std::endl; + + std::cout << " **************************************************** " << std::endl; + std::cout << " Checking OpenGL Error " << std::endl; + + errstr = reinterpret_cast(gluErrorString(errnum)); + ostr << errstr; + + if(location) ostr << " at " << location; + ostr << std::endl; + std::cout << " **************************************************** " << std::endl; + ostr << " **************************************************** " << std::endl; + } +} + +#endif // GPUPROGRAM_H diff --git a/src/meshlabplugins/render_radiance_scaling/gpuShader.cpp b/src/meshlabplugins/render_radiance_scaling/gpuShader.cpp new file mode 100644 index 000000000..ae78fbb6a --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/gpuShader.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "gpuShader.h" + +using namespace std; + +GPUShader::GPUShader(SHADER_TYPE type,const string &filename,bool printLog) + : _filename(filename), + _type(type), + _shaderId(0), + _printLog(printLog) { + _created = createShader(); + + loadAndCompile(); +} + +GPUShader::~GPUShader() { + if(_created){ + glDeleteShader(_shaderId); + } +} + +bool GPUShader::load() { + QString res; + QFile f(QString(_filename.c_str())); + + if (!f.open(QFile::ReadOnly)) { + std::cerr << "failed to load shader file " << _filename << "\n"; + return false; + } + + QTextStream stream(&f); + res = stream.readAll(); + f.close(); + + string tmp = res.toStdString(); + const char *s = tmp.c_str(); + + glShaderSource(_shaderId,1,&s,NULL); + + return true; +} + + +bool GPUShader::compile() { + glCompileShader(_shaderId); + + if(_printLog) + printInfoLog(); + + return true; +} + +void GPUShader::printInfoLog() { + int infologLength = 0; + int charsWritten = 0; + char* infolog; + + glGetObjectParameterivARB(_shaderId,GL_OBJECT_INFO_LOG_LENGTH_ARB,&infologLength); + + if(infologLength>0) { + infolog = (char*)malloc(infologLength); + glGetInfoLogARB(_shaderId,infologLength,&charsWritten,infolog); + if(infolog[0]!='\0') { + printf("InfoLog ---> %s\n",_filename.c_str()); + printf("%s",infolog); + } + free(infolog); + } +} + +bool GPUShader::loadAndCompile() { + return _created && load() && compile(); +} + +bool GPUShader::createShader() { + switch(_type) { + + case VERT: + if(GLEW_ARB_vertex_shader) + _shaderId = glCreateShader(GL_VERTEX_SHADER); + else { + cout << "Warning : vertex shader not supported !" << endl; + return false; + } + break; + + case FRAG: + if(GLEW_ARB_fragment_shader){ + _shaderId = glCreateShader(GL_FRAGMENT_SHADER); + } + else { + cout << "Warning : fragment shader not supported !" << endl; + return false; + } + break; + + case GEOM: +#ifdef GL_EXT_geometry_shader4 + if(GL_EXT_geometry_shader4){ + _shaderId = glCreateShader(GL_GEOMETRY_SHADER_EXT); + } + else { + cout << "Warning : geometry shader not supported !" << endl; + return false; + } +#else + cout << "Warning : geometry shader not supported !" << endl; + return false; +#endif + break; + + default: + cout << "Warning : unknown shader type !" << endl; + return false; + break; + } + + if(_shaderId==0) { + cout << "Warning : shader " << _filename << " is not created !" << endl; + return false; + } + + return true; +} diff --git a/src/meshlabplugins/render_radiance_scaling/gpuShader.h b/src/meshlabplugins/render_radiance_scaling/gpuShader.h new file mode 100644 index 000000000..4b687c97c --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/gpuShader.h @@ -0,0 +1,72 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#ifndef GPUSHADER_H +#define GPUSHADER_H + +#include +#include + +enum SHADER_TYPE {VERT,FRAG,GEOM}; + +class GPUShader { + + public: + + GPUShader(SHADER_TYPE type,const std::string &filename,bool printLog=true); + ~GPUShader(); + + bool load(); + bool compile(); + bool loadAndCompile(); + + inline GLuint id() const; + inline SHADER_TYPE type() const; + inline std::string filename() const; + + protected: + std::string _filename; + + private: + SHADER_TYPE _type; + + GLuint _shaderId; + bool _printLog; + bool _created; + + bool createShader(); + void printInfoLog(); +}; + +inline GLuint GPUShader::id() const { + return _shaderId; +} + +inline SHADER_TYPE GPUShader::type() const { + return _type; +} + +inline std::string GPUShader::filename() const { + return _filename; +} + +#endif // GPUSHADER diff --git a/src/meshlabplugins/render_radiance_scaling/litSpheres/ls01.png b/src/meshlabplugins/render_radiance_scaling/litSpheres/ls01.png new file mode 100644 index 000000000..f4472fc9d Binary files /dev/null and b/src/meshlabplugins/render_radiance_scaling/litSpheres/ls01.png differ diff --git a/src/meshlabplugins/render_radiance_scaling/litSpheres/ls02.png b/src/meshlabplugins/render_radiance_scaling/litSpheres/ls02.png new file mode 100644 index 000000000..b11caf5be Binary files /dev/null and b/src/meshlabplugins/render_radiance_scaling/litSpheres/ls02.png differ diff --git a/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.cpp b/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.cpp new file mode 100644 index 000000000..37d100899 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.cpp @@ -0,0 +1,273 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include "radianceScalingRenderer.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define GL_TEST_ERR \ + { \ + GLenum eCode; \ + if((eCode=glGetError())!=GL_NO_ERROR) \ + std::cerr << "OpenGL error : " << gluErrorString(eCode) << " in " << __FILE__ << " : " << __LINE__ << std::endl; \ + } + +RadianceScalingRendererPlugin::RadianceScalingRendererPlugin() + : _supported(false), + _sDialog(0), + _fbo(NULL), + _buffPass(NULL), + _rsPass(NULL), + _depthTex(NULL), + _gradTex(NULL), + _normTex(NULL), + _convexLS(NULL), + _concavLS(NULL) { + +} + +void RadianceScalingRendererPlugin::Init(QAction *, MeshDocument &, RenderMode &, QGLWidget *gla) { + if(_sDialog) { + _sDialog->close(); + delete _sDialog; + _sDialog=0; + } + + gla->makeCurrent(); + glewInit(); + + GL_TEST_ERR + + if(!GLEW_ARB_vertex_program || + !GLEW_ARB_fragment_program || + !GLEW_ARB_texture_float || + !GLEW_ARB_draw_buffers || + !GLEW_EXT_framebuffer_object) { + + _supported = false; + return; + } + + _supported = true; + _sDialog = new ShaderDialog(this,gla); + _sDialog->move(10,100); + _sDialog->show(); + + _sDialog->changeIcon(":/RadianceScalingRenderer/litSpheres/ls02.png",ShaderDialog::convex_icon); + _sDialog->changeIcon(":/RadianceScalingRenderer/litSpheres/ls01.png",ShaderDialog::concav_icon); + + createLit(":/RadianceScalingRenderer/litSpheres/ls02.png",0); + createLit(":/RadianceScalingRenderer/litSpheres/ls01.png",1); + + initFBOs(); + GL_TEST_ERR + + initShaders(); + GL_TEST_ERR +} + +void RadianceScalingRendererPlugin::Render(QAction *, MeshDocument &md, RenderMode &rm, QGLWidget *) { + checkViewport(); + + // first pass: buffers + _fbo->bind(); + glDrawBuffers(2,FramebufferObject::buffers(0)); + glClearColor(0.0,0.0,0.0,1.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + _buffPass->enable(); + foreach(MeshModel *mp,md.meshList) { + if(mp->visible) mp->Render(rm.drawMode,rm.colorMode,rm.textureMode); + } + _buffPass->disable(); + + // second pass: descriptor, radiance scaling, lighting + FramebufferObject::unbind(); + swapToScreenMode(); + _rsPass->enable(); + drawQuad(); + _rsPass->disable(); + swapToWorldMode(); +} + +void RadianceScalingRendererPlugin::Finalize(QAction *, MeshDocument &, GLArea *) { + cleanShaders(); + cleanFBOs(); + + if(_sDialog) { + _sDialog->close(); + delete _sDialog; + _sDialog=0; + } + + if(_convexLS!=NULL) { + delete _convexLS; + _convexLS = NULL; + } + + if(_concavLS!=NULL) { + delete _concavLS; + _concavLS = NULL; + } +} + +void RadianceScalingRendererPlugin::initActionList() { + _actionList << new QAction("Radiance Scaling",this); +} + +void RadianceScalingRendererPlugin::createLit(const QString &filename,int type) { + QImage t; + QImage b; + + if(!b.load(filename)) { + return; + } + + t = QGLWidget::convertToGLFormat(b); + + if(type==0) { + if(_convexLS!=NULL) { + delete _convexLS; + _convexLS = NULL; + } + + _convexLS = new UbyteTexture2D(TextureFormat(GL_TEXTURE_2D,t.width(),t.height(),3,GL_RGBA,GL_UNSIGNED_BYTE),TextureParams(GL_LINEAR,GL_LINEAR),t.bits()); + } else { + + if(_concavLS!=NULL) { + delete _concavLS; + _concavLS = NULL; + } + + _concavLS = new UbyteTexture2D(TextureFormat(GL_TEXTURE_2D,t.width(),t.height(),3,GL_RGBA,GL_UNSIGNED_BYTE),TextureParams(GL_LINEAR,GL_LINEAR),t.bits()); + } +} + +void RadianceScalingRendererPlugin::initShaders(bool reload) { + + if(!reload) { + string path = ":/RadianceScalingRenderer/shaders/"; + _buffPass = new GPUProgram(path+"01_buffer.vs",path+"01_buffer.fs"); + _rsPass = new GPUProgram(path+"02_rs.vs" ,path+"02_rs.fs"); + GL_TEST_ERR + + _rsPass->enable(); + _rsPass->addUniform("sw"); + _rsPass->addUniform("sh"); + _rsPass->addUniform("enhancement"); + _rsPass->addUniform("transition"); + _rsPass->addUniform("enabled"); + _rsPass->addUniform("invert"); + _rsPass->addUniform("twoLS"); + _rsPass->addUniform("display"); + _rsPass->addUniform("grad"); + _rsPass->addUniform("norm"); + _rsPass->addUniform("convexLS"); + _rsPass->addUniform("concavLS"); + _rsPass->disable(); + + GL_TEST_ERR + } else { + _buffPass->reload(); + _rsPass->reload(); + GL_TEST_ERR + } + + float sw = 1.0f/(float)_w; + float sh = 1.0f/(float)_h; + + _rsPass->enable(); + _rsPass->setUniform1f("sw",sw); + _rsPass->setUniform1f("sh",sh); + _rsPass->setUniform1f("enhancement",_sDialog->getEnhancement()); + _rsPass->setUniform1f("transition",_sDialog->getTransition()); + _rsPass->setUniform1i("enabled",_sDialog->getEnable()); + _rsPass->setUniform1i("display",_sDialog->getDisplay()); + _rsPass->setUniform1i("invert",_sDialog->getInvert()); + _rsPass->setUniform1i("twoLS",_sDialog->getTwoLS()); + _rsPass->setUniformTexture("grad",0,_gradTex->format().target(),_gradTex->id()); + _rsPass->setUniformTexture("norm",1,_normTex->format().target(),_normTex->id()); + _rsPass->setUniformTexture("convexLS",2,_convexLS->format().target(),_convexLS->id()); + _rsPass->setUniformTexture("concavLS",3,_concavLS->format().target(),_concavLS->id()); + _rsPass->disable(); + GL_TEST_ERR +} + +void RadianceScalingRendererPlugin::initFBOs() { + int v[4]; + glGetIntegerv(GL_VIEWPORT,v); + _w = v[2]; + _h = v[3]; + + if(_fbo==NULL) { + GLenum filter = GL_LINEAR; + GLenum format = GL_RGBA16F_ARB; + _fbo = new FramebufferObject(); + _depthTex = new FloatTexture2D(TextureFormat(GL_TEXTURE_2D,_w,_h,GL_DEPTH_COMPONENT24,GL_DEPTH_COMPONENT,GL_FLOAT),TextureParams(filter,filter)); + _gradTex = new FloatTexture2D(TextureFormat(GL_TEXTURE_2D,_w,_h,format,GL_RGBA,GL_FLOAT),TextureParams(filter,filter)); + _normTex = new FloatTexture2D(_gradTex->format(),_gradTex->params()); + } + + _fbo->bind(); + _fbo->unattachAll(); + _depthTex->bind(); + _fbo->attachTexture(_depthTex->format().target(),_depthTex->id(),GL_DEPTH_ATTACHMENT_EXT); + _gradTex->bind(); + _fbo->attachTexture(_gradTex->format().target(),_gradTex->id(),GL_COLOR_ATTACHMENT0_EXT); + _normTex->bind(); + _fbo->attachTexture(_normTex->format().target(),_normTex->id(),GL_COLOR_ATTACHMENT1_EXT); + _fbo->isValid(); + + FramebufferObject::unbind(); +} + +void RadianceScalingRendererPlugin::cleanShaders() { + if(_buffPass!=NULL) { + delete _buffPass; + delete _rsPass; + + _buffPass = NULL; + _rsPass = NULL; + } +} + +void RadianceScalingRendererPlugin::cleanFBOs() { + if(_fbo!=NULL) { + delete _fbo; + delete _depthTex; + delete _gradTex; + delete _normTex; + + _fbo = NULL; + _depthTex = NULL; + _gradTex = NULL; + _normTex = NULL; + } +} + +Q_EXPORT_PLUGIN(RadianceScalingRendererPlugin) diff --git a/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.h b/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.h new file mode 100644 index 000000000..7fb67f0ee --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.h @@ -0,0 +1,192 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#ifndef RADIANCESCALINGRENDERER_H +#define RADIANCESCALINGRENDERER_H + +#include +#include +#include + +#include +#include +#include +#include + +#include "shaderDialog.h" +#include "gpuProgram.h" +#include "framebufferObject.h" +#include "texture2D.h" + +class RadianceScalingRendererPlugin : public QObject, public MeshRenderInterface { + Q_OBJECT + Q_INTERFACES(MeshRenderInterface) + + bool _supported; + QList _actionList; + ShaderDialog *_sDialog; + + public: + RadianceScalingRendererPlugin(); + + QList actions() { + if(_actionList.isEmpty()) initActionList(); + return _actionList; + } + + void initActionList(); + + virtual bool isSupported() {return _supported;} + virtual void Init(QAction *a, MeshDocument &m, RenderMode &rm, QGLWidget *gla); + virtual void Finalize(QAction *a, MeshDocument &m, GLArea * gla); + virtual void Render(QAction *a, MeshDocument &m, RenderMode &rm, QGLWidget *gla); + + inline void setEnable(bool enabled); + inline void setLit(bool lit); + inline void setInvert(int invert); + inline void setDisplay(int index); + inline void setEnhancement(float enhancement); + inline void setTransition(float transition); + inline void setConvexLit(const QString &filename); + inline void setConcavLit(const QString &filename); + + private: + FramebufferObject *_fbo; + GPUProgram *_buffPass; + GPUProgram *_rsPass; + FloatTexture2D *_depthTex; + FloatTexture2D *_gradTex; + FloatTexture2D *_normTex; + UbyteTexture2D *_convexLS; + UbyteTexture2D *_concavLS; + int _w,_h; + + void initShaders(bool reload=false); + void initFBOs(); + void cleanShaders(); + void cleanFBOs(); + + void createLit(const QString &filename,int type); + + inline void drawQuad(); + inline void swapToScreenMode(); + inline void swapToWorldMode(); + inline void checkViewport(); +}; + +inline void RadianceScalingRendererPlugin::swapToScreenMode() { + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glDepthMask(GL_FALSE); +} + +inline void RadianceScalingRendererPlugin::swapToWorldMode() { + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glDepthMask(GL_TRUE); +} + +inline void RadianceScalingRendererPlugin::drawQuad() { + glBegin(GL_QUADS); + + glTexCoord2f(0.0,0.0); glVertex2f(-1.0,-1.0); + glTexCoord2f(1.0,0.0); glVertex2f( 1.0,-1.0); + glTexCoord2f(1.0,1.0); glVertex2f( 1.0, 1.0); + glTexCoord2f(0.0,1.0); glVertex2f(-1.0, 1.0); + + glEnd(); +} + +inline void RadianceScalingRendererPlugin::checkViewport() { + int v[4]; + glGetIntegerv(GL_VIEWPORT,v); + + if(v[2]!=_w || v[3]!=_h) { + _w = v[2]; + _h = v[3]; + + cleanFBOs(); + initFBOs(); + initShaders(true); + } +} + +inline void RadianceScalingRendererPlugin::setEnable(bool enabled) { + _rsPass->enable(); + _rsPass->setUniform1i("enabled",enabled); + _rsPass->disable(); +} + +inline void RadianceScalingRendererPlugin::setLit(bool lit) { + _rsPass->enable(); + _rsPass->setUniform1i("lit",lit); + _rsPass->disable(); + + initShaders(false); +} + +inline void RadianceScalingRendererPlugin::setInvert(int invert) { + _rsPass->enable(); + _rsPass->setUniform1i("invert",invert); + _rsPass->disable(); +} + +inline void RadianceScalingRendererPlugin::setDisplay(int index) { + _rsPass->enable(); + _rsPass->setUniform1i("display",index); + _rsPass->disable(); + + if(index==1) { + initShaders(false); + } +} + +inline void RadianceScalingRendererPlugin::setEnhancement(float enhancement) { + _rsPass->enable(); + _rsPass->setUniform1f("enhancement",enhancement); + _rsPass->disable(); +} + +inline void RadianceScalingRendererPlugin::setTransition(float transition) { + _rsPass->enable(); + _rsPass->setUniform1f("transition",transition); + _rsPass->disable(); +} + +inline void RadianceScalingRendererPlugin::setConvexLit(const QString &filename) { + createLit(filename,0); + initShaders(false); +} + +inline void RadianceScalingRendererPlugin::setConcavLit(const QString &filename) { + createLit(filename,1); + initShaders(false); +} + +#endif + diff --git a/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.qrc b/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.qrc new file mode 100644 index 000000000..2d51674f4 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/radianceScalingRenderer.qrc @@ -0,0 +1,10 @@ + + + shaders/01_buffer.vs + shaders/01_buffer.fs + shaders/02_rs.vs + shaders/02_rs.fs + litSpheres/ls01.png + litSpheres/ls02.png + + diff --git a/src/meshlabplugins/render_radiance_scaling/readme.txt b/src/meshlabplugins/render_radiance_scaling/readme.txt new file mode 100644 index 000000000..d670338b8 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/readme.txt @@ -0,0 +1,50 @@ +--- License Info --- + +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 +(http://www.gnu.org/licenses/gpl.txt) for more details. + +--- References ---- + +Please, when using this tool, cite the following reference: + + +Render Radiance Scaling +Vergne Romain, Dumas Olivier +Inria +http://iparla.labri.fr/collaborations/animare + +@InProceedings{VPBGS10, + author = "Vergne, Romain and Pacanowski, Romain and Barla, Pascal and Granier, Xavier and Schlick, Christophe", + title = "Radiance Scaling for Versatile Surface Enhancement", + booktitle = "I3D '10: Proc. symposium on Interactive 3D graphics and games", + year = "2010", + publisher = "ACM", + keywords = "ANR SeARCH, ANR Animare", + url = "http://iparla.labri.fr/publications/2010/VPBGS10" +} + + + +MeshLab +Visual Computing Lab - ISTI - CNR +http://meshlab.sourceforge.net/ + +@misc{Meshlab + author = {Visual Computing Lab ISTI - CNR}, + title = {MeshLab}, + note = {http://meshlab.sourceforge.net/} +} + + +--- How To --- + +To use it lauch meshlab, open an .obj, go inside the render->shader menu and choose Radiance Scaling. +Do not forget to choose smooth shading display mode with the cylindrical icon. +That's all folks :) diff --git a/src/meshlabplugins/render_radiance_scaling/render_radiance_scaling.pro b/src/meshlabplugins/render_radiance_scaling/render_radiance_scaling.pro new file mode 100644 index 000000000..cd8887963 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/render_radiance_scaling.pro @@ -0,0 +1,13 @@ +include (../../shared.pri) + +HEADERS = textureParams.h textureFormat.h texture2D.h framebufferObject.h gpuShader.h gpuProgram.h radianceScalingRenderer.h shaderDialog.h +SOURCES = textureParams.cpp textureFormat.cpp framebufferObject.cpp gpuShader.cpp gpuProgram.cpp radianceScalingRenderer.cpp shaderDialog.cpp $$GLEWCODE + +TARGET = render_radiance_scaling +FORMS = shaderDialog.ui + +RESOURCES = radianceScalingRenderer.qrc + +QT += opengl + +# CONFIG += debug diff --git a/src/meshlabplugins/render_radiance_scaling/shaderDialog.cpp b/src/meshlabplugins/render_radiance_scaling/shaderDialog.cpp new file mode 100644 index 000000000..bedc09bf3 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/shaderDialog.cpp @@ -0,0 +1,187 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include "shaderDialog.h" +#include "radianceScalingRenderer.h" + +using namespace std; + +ShaderDialog::ShaderDialog(RadianceScalingRendererPlugin* wrp,QGLWidget* gla,QWidget *parent) + : QDockWidget(parent), + _wrp(wrp), + _gla(gla) { + + _ui.setupUi(this); + this->setWidget(_ui.frame); + this->setFeatures(QDockWidget::AllDockWidgetFeatures); + this->setAllowedAreas(Qt::LeftDockWidgetArea); + this->setFloating(true); + + connect(_ui.enableCheckBox,SIGNAL(stateChanged(int)),this,SLOT(enableChanged(int))); + connect(_ui.invertCheckBox,SIGNAL(stateChanged(int)),this,SLOT(invertChanged(int))); + connect(_ui.displayBox,SIGNAL(currentIndexChanged(int)),this,SLOT(displayChanged(int))); + connect(_ui.enSlider,SIGNAL(valueChanged(int)),this,SLOT(enhancementChanged(int))); + connect(_ui.transitionSlider,SIGNAL(valueChanged(int)),this,SLOT(transitionChanged(int))); + connect(_ui.litCheckBox,SIGNAL(stateChanged(int)),this,SLOT(litChanged(int))); + connect(_ui.loadButton1,SIGNAL(clicked()),this,SLOT(load1Clicked())); + connect(_ui.loadButton2,SIGNAL(clicked()),this,SLOT(load2Clicked())); + + _ui.litCheckBox->hide(); + _ui.litIcon1->hide(); + _ui.litIcon2->hide(); + _ui.litLabel1->hide(); + _ui.litLabel2->hide(); + _ui.loadButton1->hide(); + _ui.loadButton2->hide(); + _ui.transitionTitle->hide(); + _ui.transitionSlider->hide(); + _ui.transitionLabel->hide(); +} + +ShaderDialog::~ShaderDialog() { + +} + +void ShaderDialog::enableChanged(int) { + bool enableChecked = (_ui.enableCheckBox->checkState()==Qt::Checked) ? true : false; + + _wrp->setEnable(enableChecked); + _gla->updateGL(); +} + +void ShaderDialog::invertChanged(int) { + (_ui.invertCheckBox->checkState()==Qt::Checked) ? _wrp->setInvert(true) : _wrp->setInvert(false); + _gla->updateGL(); +} + +void ShaderDialog::displayChanged(int index) { + if(index==1) { + // special case of lit sphere rendering + _ui.litCheckBox->show(); + _ui.litIcon1->show(); + _ui.litLabel1->show(); + _ui.loadButton1->show(); + litChanged(0); + } else { + // other cases + _ui.litCheckBox->hide(); + _ui.litIcon1->hide(); + _ui.litIcon2->hide(); + _ui.litLabel1->hide(); + _ui.litLabel2->hide(); + _ui.loadButton1->hide(); + _ui.loadButton2->hide(); + _ui.transitionTitle->hide(); + _ui.transitionSlider->hide(); + _ui.transitionLabel->hide(); + } + + _wrp->setDisplay(index); + _gla->updateGL(); +} + +void ShaderDialog::enhancementChanged(int value) { + const float scale = 100.0f; + float val = (float)value/scale; + QString s; + s.setNum(val,'f',2); + _ui.enLabel->setText(s); + _wrp->setEnhancement(val); + _gla->updateGL(); +} + +void ShaderDialog::transitionChanged(int value) { + const float scale = 100.0f; + float val = (float)value/scale; + QString s; + s.setNum(val,'f',2); + _ui.transitionLabel->setText(s); + _wrp->setTransition(val); + _gla->updateGL(); +} + +void ShaderDialog::litChanged(int) { + bool doubleLit = (_ui.litCheckBox->checkState()==Qt::Checked) ? true : false; + if(doubleLit) { + _ui.litIcon2->show(); + _ui.litLabel2->show(); + _ui.loadButton2->show(); + _ui.transitionTitle->show(); + _ui.transitionSlider->show(); + _ui.transitionLabel->show(); + _ui.litLabel1->setText("Convexities"); + } else { + _ui.litIcon2->hide(); + _ui.litLabel2->hide(); + _ui.loadButton2->hide(); + _ui.transitionTitle->hide(); + _ui.transitionSlider->hide(); + _ui.transitionLabel->hide(); + _ui.litLabel1->setText("Convexities and Concavities"); + } + + _wrp->setLit(doubleLit); + _gla->updateGL(); +} + +void ShaderDialog::changeIcon(QString path,int icon) { + if(icon!=convex_icon && icon!=concav_icon) + return; + + const int w = 128; + + QPixmap pix(path); + + pix = pix.scaledToWidth(w,Qt::SmoothTransformation); + + if(icon==convex_icon) { + _ui.litIcon1->setPixmap(pix); + } else if(icon==concav_icon) { + _ui.litIcon2->setPixmap(pix); + } +} + +void ShaderDialog::load1Clicked() { + QString filename = QFileDialog::getOpenFileName(0, QString(),QString(), + tr("Images (*.png *.xpm *.jpg *.bmp *.tif)")); + + if(filename.isNull()) + return; + + changeIcon(filename,convex_icon); + + _wrp->setConvexLit(filename); + _gla->updateGL(); +} + +void ShaderDialog::load2Clicked() { + QString filename = QFileDialog::getOpenFileName(0, QString(),QString(), + tr("Images (*.png *.xpm *.jpg *.bmp *.tif)")); + + if(filename.isNull()) + return; + + changeIcon(filename,concav_icon); + + _wrp->setConcavLit(filename); + _gla->updateGL(); +} diff --git a/src/meshlabplugins/render_radiance_scaling/shaderDialog.h b/src/meshlabplugins/render_radiance_scaling/shaderDialog.h new file mode 100644 index 000000000..d2bfc179b --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/shaderDialog.h @@ -0,0 +1,96 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#ifndef SHADERDIALOG_H +#define SHADERDIALOG_H + +#include +#include +#include +#include +#include "ui_shaderDialog.h" + +class RadianceScalingRendererPlugin; + +class ShaderDialog : public QDockWidget { + Q_OBJECT + + public: + ShaderDialog(RadianceScalingRendererPlugin* wrp,QGLWidget* gla,QWidget *parent = 0); + ~ShaderDialog(); + + void changeIcon(QString path,int icon); + + static const int convex_icon=0; + static const int concav_icon=1; + + private: + RadianceScalingRendererPlugin *_wrp; + Ui::ShaderDialogClass _ui; + QGLWidget *_gla; + + private slots: + void enableChanged(int); + void displayChanged(int); + void invertChanged(int); + void enhancementChanged(int); + void transitionChanged(int); + void litChanged(int); + void load1Clicked(); + void load2Clicked(); + + public: + inline bool getEnable() const; + inline int getDisplay() const; + inline float getEnhancement() const; + inline float getTransition() const; + inline bool getInvert() const; + inline bool getTwoLS() const; +}; + +inline bool ShaderDialog::getEnable() const { + return (_ui.enableCheckBox->checkState()==Qt::Checked); +} + +inline int ShaderDialog::getDisplay() const { + return _ui.displayBox->currentIndex(); +} + +inline float ShaderDialog::getEnhancement() const { + const float scale = 100.0f; + return (float)(_ui.enSlider->value())/scale; +} + +inline float ShaderDialog::getTransition() const { + const float scale = 100.0f; + return (float)(_ui.transitionSlider->value())/scale; +} + +inline bool ShaderDialog::getInvert() const { + return (_ui.invertCheckBox->checkState()==Qt::Checked); +} + +inline bool ShaderDialog::getTwoLS() const { + return (_ui.litCheckBox->checkState()==Qt::Checked); +} + +#endif // SHADERDIALOG_H diff --git a/src/meshlabplugins/render_radiance_scaling/shaderDialog.ui b/src/meshlabplugins/render_radiance_scaling/shaderDialog.ui new file mode 100644 index 000000000..61d3b9adc --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/shaderDialog.ui @@ -0,0 +1,257 @@ + + ShaderDialogClass + + + + 0 + 0 + 381 + 566 + + + + Form + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + Radiance Scaling parameters + + + + + + + + + Lambertian Radiance Scaling + + + + + Lit Sphere Radiance Scaling + + + + + Colored Descriptor + + + + + Grey Descriptor + + + + + + + + Display Mode: + + + + + + + Qt::LeftToRight + + + Enable Radiance Scaling + + + true + + + + + + + 100 + + + 50 + + + Qt::Horizontal + + + + + + + Qt::LeftToRight + + + 0.5 + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + + Enhancement: + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Invert Effect + + + + + + + + + + + + + + Convexities + + + Qt::AlignHCenter|Qt::AlignTop + + + + + + + Concavities + + + Qt::AlignHCenter|Qt::AlignTop + + + + + + + Qt::LeftToRight + + + Use 2 Lit Spheres + + + true + + + + + + + Load + + + + + + + Load + + + + + + + + + + Qt::AlignCenter + + + + + + + + + + Qt::AlignCenter + + + + + + + + + + + Transition: + + + + + + + 100 + + + 50 + + + Qt::Horizontal + + + + + + + 0.5 + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + diff --git a/src/meshlabplugins/render_radiance_scaling/shaders/01_buffer.fs b/src/meshlabplugins/render_radiance_scaling/shaders/01_buffer.fs new file mode 100644 index 000000000..2b0746d31 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/shaders/01_buffer.fs @@ -0,0 +1,45 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#version 120 +#extension GL_ARB_draw_buffers : enable + +varying vec3 normal; +varying vec3 view; +varying float depth; + +void main(void) { + const float eps = 0.01; + const float foreshortening = 0.4; + + vec3 n = normalize(normal); + + float gs = n.zt) { + a = (c-t)/(1.0-t); + rgb = mix(convMin,convMax,a); + } else if(c<0.0) { + a = -c/t; + rgb = mix(plane,concMin,a); + } else { + a = c/t; + rgb = mix(plane,convMin,a); + } + + if(s<1.0) + rgb = vec3(0.2); + + return vec4(rgb,1.0); +} + +vec4 greyDescriptor(in float c, in float s) { + return vec4((c*0.5+0.5)-(1.0-s)); +} + +// **** LIT SPHERE FUNCTIONS **** +vec4 oneLitSphere(in vec3 n,in float c) { + vec4 color = texture2D(convexLS,(n.xy*0.5)+vec2(0.5)); + + return enabled ? color*warp(length(color),c) : color; +} + +vec4 twoLitSphere(in vec3 n,in float w,in vec3 h,in float c) { + const float eps = 0.2; + + vec2 coord = (n.xy*0.5)+vec2(0.5); + vec4 cconv = texture2D(convexLS,coord); + vec4 cconc = texture2D(concavLS,coord); + vec4 color = mix(cconc,cconv,smoothstep(0.5-eps,0.5+eps,curvature(w,h,transition)*0.5+0.5)); + + return enabled ? color*warp(length(color),c) : color; +} + +// **** LIGHTING COMPUTATION **** +void main(void) { + vec3 n = texture2D(norm,gl_TexCoord[0].st).xyz; + + if(n==vec3(0.0)) { + gl_FragColor = vec4(1.0); + return; + //discard; + } + + // data + loadValues(); + float w = weight(); + vec3 h = hessian(); + float c = curvature(w,h,enhancement); + vec3 l = normalize(gl_LightSource[0].position.xyz); + vec4 m = gl_FrontMaterial.diffuse; + + if(display==0) { + // lambertian lighting + float cosineTerm = max(dot(n,l),0.0); + float warpedTerm = enabled ? cosineTerm*warp(cosineTerm,c) : cosineTerm; + gl_FragColor = m*warpedTerm; + + } else if(display==1) { + // using lit spheres + if(twoLS) { + gl_FragColor = twoLitSphere(n,w,h,c); + } else { + gl_FragColor = oneLitSphere(n,c); + } + + } else if(display==2) { + // colored descriptor + gl_FragColor = coloredDescriptor(c,w); + + } else if(display==3) { + // grey descriptor + gl_FragColor = greyDescriptor(c,w); + + } +} diff --git a/src/meshlabplugins/render_radiance_scaling/shaders/02_rs.vs b/src/meshlabplugins/render_radiance_scaling/shaders/02_rs.vs new file mode 100644 index 000000000..8f4e34025 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/shaders/02_rs.vs @@ -0,0 +1,28 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#version 120 + +void main(void) { + gl_TexCoord[0] = gl_MultiTexCoord0; + gl_Position = ftransform(); +} diff --git a/src/meshlabplugins/render_radiance_scaling/texture2D.h b/src/meshlabplugins/render_radiance_scaling/texture2D.h new file mode 100644 index 000000000..011a9616e --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/texture2D.h @@ -0,0 +1,149 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#ifndef TEXTURE_2D_H +#define TEXTURE_2D_H + +#include +#include +#include + +#include +#include "textureFormat.h" +#include "textureParams.h" + +template + class Texture2D { + public: + + Texture2D(const TextureFormat &tf=TextureFormat(), + const TextureParams &tp=TextureParams(), + T* map=NULL, + int id=-1); + + Texture2D(const Texture2D &tex); + + ~Texture2D(); + + inline GLuint id () const; + inline TextureFormat format() const; + inline TextureParams params() const; + inline void bind () const; + + protected: + GLuint _id; + TextureFormat _format; + TextureParams _params; +}; + +template +inline GLuint Texture2D::id() const { + return _id; +} + +template +inline TextureFormat Texture2D::format() const { + return _format; +} + +template +inline TextureParams Texture2D::params() const { + return _params; +} + +template +inline void Texture2D::bind() const { + + glBindTexture(_format.target(),_id); +} + +template +Texture2D::Texture2D(const Texture2D &tex) +: _id(tex.id()), + _format(tex.format()), + _params(tex.params()) { + +} + +template +Texture2D::Texture2D(const TextureFormat &tf,const TextureParams &tp,T* map,int id) +: _id(id), + _format(tf), + _params(tp) { + + assert(_format.target()==GL_TEXTURE_2D); + glEnable(GL_TEXTURE_2D); + + if(id<0 || glIsTexture(id)==GL_FALSE) { + glGenTextures(1,&_id); + } else { + _id = id; + } + + glBindTexture(_format.target(),_id); + + if(_format.mipmapmode()==TextureFormat::MIPMAP_GLU_AUTOM) { + + gluBuild2DMipmaps(_format.target(), + _format.internalformat(), + _format.width(), + _format.height(), + _format.format(), + _format.type(), + (const GLvoid *)map); + } else { + + glTexImage2D(_format.target(), + _format.level(), + _format.internalformat(), + _format.width(), + _format.height(), + _format.border(), + _format.format(), + _format.type(), + (const GLvoid *)map); + + if(_format.mipmapmode()==TextureFormat::MIPMAP_FBO_AUTOM) { + assert(map==NULL || map==0); + glGenerateMipmapEXT(_format.target()); + } + + } + + glTexParameteri(_format.target(),GL_TEXTURE_MIN_FILTER,_params.minfilter()); + glTexParameteri(_format.target(),GL_TEXTURE_MAG_FILTER,_params.maxfilter()); + glTexParameteri(_format.target(),GL_TEXTURE_WRAP_S,_params.wraps()); + glTexParameteri(_format.target(),GL_TEXTURE_WRAP_T,_params.wrapt()); + + //glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,_params.mode()); + } + +template +Texture2D::~Texture2D() { + glDeleteTextures(1,&_id); +} + +typedef Texture2D FloatTexture2D; +typedef Texture2D UbyteTexture2D; +typedef Texture2D UintTexture2D; + +#endif // TEXTURE_2D_H diff --git a/src/meshlabplugins/render_radiance_scaling/textureFormat.cpp b/src/meshlabplugins/render_radiance_scaling/textureFormat.cpp new file mode 100644 index 000000000..5c5dfbc2d --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/textureFormat.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include "textureFormat.h" + + +TextureFormat::TextureFormat(GLenum target, + GLsizei width, + GLsizei height, + GLenum internalformat, + GLenum format, + GLenum type, + GLsizei depth, + int mipmapmode, + GLint level, + GLint border) + : _target(target), + _width(width), + _height(height), + _internalformat(internalformat), + _format(format), + _type(type), + _depth(depth), + _mipmapmode(mipmapmode), + _level(level), + _border(border) { + + + } + +TextureFormat::TextureFormat(const TextureFormat &tf) + : _target(tf.target()), + _width(tf.width()), + _height(tf.height()), + _internalformat(tf.internalformat()), + _format(tf.format()), + _type(tf.type()), + _depth(tf.depth()), + _mipmapmode(tf.mipmapmode()), + _level(tf.level()), + _border(tf.border()) { + +} + diff --git a/src/meshlabplugins/render_radiance_scaling/textureFormat.h b/src/meshlabplugins/render_radiance_scaling/textureFormat.h new file mode 100644 index 000000000..67e7189f5 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/textureFormat.h @@ -0,0 +1,85 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ + +#ifndef TEXTURE_FORMAT_H +#define TEXTURE_FORMAT_H + +#include +#include + +class TextureFormat { + public: + static const int MIPMAP_MANUAL = 0; + static const int MIPMAP_GLU_AUTOM = 1; + static const int MIPMAP_FBO_AUTOM = 2; + + TextureFormat(GLenum target = GL_TEXTURE_2D, + GLsizei width = 0, + GLsizei height = 0, + GLenum internalformat = GL_RGBA, + GLenum format = GL_RGBA, + GLenum type = GL_FLOAT, + GLsizei depth = 0, + int mipmapmode = MIPMAP_MANUAL, + GLint level = 0, + GLint border = 0); + + TextureFormat(const TextureFormat &tf); + + inline void setTarget (GLenum target ) {_target = target; } + inline void setWidth (GLsizei width ) {_width = width; } + inline void setHeight (GLsizei height ) {_height = height; } + inline void setInternalformat(GLenum internalformat) {_internalformat = internalformat;} + inline void setFormat (GLenum format ) {_format = format; } + inline void setType (GLenum type ) {_type = type; } + inline void setDepth (GLsizei depth ) {_depth = depth; } + inline void setmipmapmode (int mipmapmode ) {_mipmapmode = mipmapmode; } + inline void setLevel (GLint level ) {_level = level; } + inline void setBorder (GLint border ) {_border = border; } + + inline GLenum target () const {return _target; } + inline GLsizei width () const {return _width; } + inline GLsizei height () const {return _height; } + inline GLenum internalformat() const {return _internalformat;} + inline GLenum format () const {return _format; } + inline GLenum type () const {return _type; } + inline GLsizei depth () const {return _depth; } + inline int mipmapmode () const {return _mipmapmode; } + inline GLint level () const {return _level; } + inline GLint border () const {return _border; } + + protected: + GLenum _target; + GLsizei _width; + GLsizei _height; + GLenum _internalformat; + GLenum _format; + GLenum _type; + GLsizei _depth; + int _mipmapmode; + GLint _level; + GLint _border; +}; + + +#endif // TEXTURE_FORMAT_H diff --git a/src/meshlabplugins/render_radiance_scaling/textureParams.cpp b/src/meshlabplugins/render_radiance_scaling/textureParams.cpp new file mode 100644 index 000000000..7cf885674 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/textureParams.cpp @@ -0,0 +1,49 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#include "textureParams.h" + +TextureParams::TextureParams(GLint minfilter, + GLint maxfilter, + GLint wrapr, + GLint wraps, + GLint wrapt, + GLint mode) + : _minfilter(minfilter), + _maxfilter(maxfilter), + _wrapr(wrapr), + _wraps(wraps), + _wrapt(wrapt), + _mode(mode) { + + } + +TextureParams::TextureParams(const TextureParams &tp) + : _minfilter(tp.minfilter()), + _maxfilter(tp.maxfilter()), + _wrapr(tp.wrapr()), + _wraps(tp.wraps()), + _wrapt(tp.wrapt()), + _mode(tp.mode()) { + +} + diff --git a/src/meshlabplugins/render_radiance_scaling/textureParams.h b/src/meshlabplugins/render_radiance_scaling/textureParams.h new file mode 100644 index 000000000..92a888690 --- /dev/null +++ b/src/meshlabplugins/render_radiance_scaling/textureParams.h @@ -0,0 +1,65 @@ +/**************************************************************************** +* Render Radiance Scaling * +* Meshlab's plugin * +* * +* Copyright(C) 2010 * +* Vergne Romain, Dumas Olivier * +* INRIA - Institut Nationnal de Recherche en Informatique et Automatique * +* * +* All rights reserved. * +* * +* 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +#ifndef TEXTURE_PARAMS_H +#define TEXTURE_PARAMS_H + +#include +#include + +class TextureParams { + public: + TextureParams(const TextureParams &tp); + TextureParams(GLint minfilter = GL_LINEAR, + GLint maxfilter = GL_LINEAR, + GLint wrapr = GL_CLAMP_TO_EDGE, + GLint wraps = GL_CLAMP_TO_EDGE, + GLint wrapt = GL_CLAMP_TO_EDGE, + GLint mode = GL_REPLACE); + + ~TextureParams() {} + + inline void setMinfilter(GLint minfilter) {_minfilter = minfilter;} + inline void setMaxfilter(GLint maxfilter) {_maxfilter = maxfilter;} + inline void setWrapr (GLint wrapr ) {_wrapr = wrapr; } + inline void setWraps (GLint wraps ) {_wraps = wraps; } + inline void setWrapt (GLint wrapt ) {_wrapt = wrapt; } + inline void setMode (GLint mode ) {_mode = mode; } + + inline GLint minfilter() const {return _minfilter;} + inline GLint maxfilter() const {return _maxfilter;} + inline GLint wrapr () const {return _wrapr; } + inline GLint wraps () const {return _wraps; } + inline GLint wrapt () const {return _wrapt; } + inline GLint mode () const {return _mode; } + + protected: + GLint _minfilter; + GLint _maxfilter; + GLint _wrapr; + GLint _wraps; + GLint _wrapt; + GLint _mode; +}; + + +#endif // TEXTURE_PARAMS_H