Back to index

nux  3.0.0
PaintLayer.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "NuxGraphics/GraphicsEngine.h"
00025 #include "NuxGraphics/RenderingPipe.h"
00026 #include "Utils.h"
00027 #include "PaintLayer.h"
00028 
00029 namespace nux
00030 {
00031 
00032   ColorLayer::ColorLayer(const Color& color, bool write_alpha, const ROPConfig& ROP)
00033   {
00034     _color = color;
00035     m_write_alpha = write_alpha;
00036     m_rop = ROP;
00037   }
00038 
00039   void ColorLayer::Renderlayer(GraphicsEngine& graphics_engine)
00040   {
00041     unsigned int current_red_mask;
00042     unsigned int current_green_mask;
00043     unsigned int current_blue_mask;
00044     unsigned int current_alpha_mask;
00045     unsigned int current_alpha_blend;
00046     unsigned int current_src_blend_factor;
00047     unsigned int current_dest_blend_factor;
00048 
00049     // Get the current color mask and blend states. They will be restored later.
00050     graphics_engine.GetRenderStates().GetColorMask(current_red_mask, current_green_mask, current_blue_mask, current_alpha_mask);
00051     // Get the current blend states. They will be restored later.
00052     graphics_engine.GetRenderStates().GetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00053     
00054     graphics_engine.GetRenderStates().SetColorMask(GL_TRUE, GL_TRUE, GL_TRUE, m_write_alpha ? GL_TRUE : GL_FALSE);
00055     graphics_engine.GetRenderStates().SetBlend(m_rop.Blend, m_rop.SrcBlend, m_rop.DstBlend);
00056     graphics_engine.QRP_Color(geometry_.x, geometry_.y, geometry_.GetWidth(), geometry_.GetHeight(), _color);
00057 
00058     // Restore Color mask and blend states.
00059     graphics_engine.GetRenderStates().SetColorMask(current_red_mask, current_green_mask, current_blue_mask, current_alpha_mask);
00060     // Restore the blend state
00061     graphics_engine.GetRenderStates().SetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00062   }
00063 
00064   AbstractPaintLayer* ColorLayer::Clone() const
00065   {
00066     return new ColorLayer(*this);
00067   }
00068 
00069   void ColorLayer::SetColor(const Color& color)
00070   {
00071     _color = color;
00072   }
00073 
00074   Color ColorLayer::GetColor() const
00075   {
00076     return _color;
00077   }
00078 
00080   ShapeLayer::ShapeLayer(UXStyleImageRef image_style, const Color& color, unsigned long corners, bool write_alpha, const ROPConfig& ROP)
00081   {
00082     m_image_style = image_style;
00083     m_color = color;
00084     m_write_alpha = write_alpha;
00085     m_rop = ROP;
00086     m_rop.Blend = true;
00087     m_corners = corners;
00088   }
00089 
00090   void ShapeLayer::Renderlayer(GraphicsEngine& graphics_engine)
00091   {
00092     unsigned int current_alpha_blend;
00093     unsigned int current_src_blend_factor;
00094     unsigned int current_dest_blend_factor;
00095 
00096     // Get the current blend states. They will be restored later.
00097     graphics_engine.GetRenderStates().GetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00098     GetPainter().PaintShapeCornerROP(graphics_engine, geometry_, m_color, m_image_style, m_corners, m_write_alpha, m_rop);
00099 
00100     graphics_engine.GetRenderStates().SetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00101   }
00102 
00103   AbstractPaintLayer* ShapeLayer::Clone() const
00104   {
00105     return new ShapeLayer(*this);
00106   }
00107 
00109   SliceScaledTextureLayer::SliceScaledTextureLayer(UXStyleImageRef image_style, const Color& color, unsigned long corners, bool write_alpha, const ROPConfig& ROP)
00110   {
00111     m_image_style = image_style;
00112     m_color = color;
00113     m_write_alpha = write_alpha;
00114     m_rop = ROP;
00115     m_corners = corners;
00116   }
00117 
00118   void SliceScaledTextureLayer::Renderlayer(GraphicsEngine& graphics_engine)
00119   {
00120     GetPainter().PaintTextureShape(graphics_engine, geometry_, m_image_style);
00121   }
00122 
00123   AbstractPaintLayer* SliceScaledTextureLayer::Clone() const
00124   {
00125     return new SliceScaledTextureLayer(*this);
00126   }
00127 
00129   
00130   CompositionLayer::CompositionLayer (ObjectPtr <IOpenGLBaseTexture> texture0, TexCoordXForm texxform0, const Color& color0,
00131               ObjectPtr <IOpenGLBaseTexture> texture1, TexCoordXForm texxform1, const Color& color1,
00132               LayerBlendMode layer_blend_mode, bool write_alpha, const ROPConfig& ROP)
00133   {
00134     m_source_texture = texture0;
00135     m_source_texture_color = color0;
00136     m_source_texture_texxform = texxform0;
00137 
00138     m_foreground_texture = texture1;
00139     m_foreground_texture_color = color1;
00140     m_foreground_texture_texxform = texxform1;
00141     
00142     m_blend_mode = layer_blend_mode;
00143     
00144     m_rop = ROP;
00145     m_write_alpha = write_alpha;
00146   }
00147 
00148   CompositionLayer::CompositionLayer (ObjectPtr <IOpenGLBaseTexture> texture0, TexCoordXForm texxform0, const Color& color0,
00149           const Color& blend_color, LayerBlendMode layer_blend_mode,
00150           bool write_alpha, const ROPConfig& ROP)
00151   {
00152     m_source_texture = texture0;
00153     m_source_texture_color = color0;
00154     m_source_texture_texxform = texxform0;
00155     
00156     m_foreground_color = blend_color;
00157     
00158     m_blend_mode = layer_blend_mode;
00159     
00160     m_write_alpha = write_alpha;
00161     m_rop = ROP;
00162   }
00163 
00164   CompositionLayer::CompositionLayer(const Color&base_color, ObjectPtr<IOpenGLBaseTexture> texture0,
00165           TexCoordXForm texxform0, const Color& color0,
00166           LayerBlendMode layer_blend_mode,
00167           bool write_alpha, const ROPConfig& ROP)
00168   {
00169     m_source_color = base_color;
00170     
00171     m_foreground_texture = texture0;
00172     m_foreground_texture_texxform = texxform0;
00173     m_foreground_texture_color = color0;
00174     
00175     m_blend_mode = layer_blend_mode;
00176     m_write_alpha = write_alpha;
00177     
00178     m_rop = ROP;
00179   }
00180 
00181   CompositionLayer::~CompositionLayer ()
00182   {
00183     if (m_foreground_texture.IsValid())
00184       m_foreground_texture.Release();
00185     if (m_source_texture.IsValid())
00186       m_source_texture.Release();
00187   }
00188 
00189   void CompositionLayer::Renderlayer(GraphicsEngine& graphics_engine)
00190   {
00191     unsigned int current_alpha_blend;
00192     unsigned int current_src_blend_factor;
00193     unsigned int current_dest_blend_factor;
00194     unsigned int current_red_mask;
00195     unsigned int current_green_mask;
00196     unsigned int current_blue_mask;
00197     unsigned int current_alpha_mask;
00198     
00199     // Get the current color mask and blend states. They will be restored later.
00200     graphics_engine.GetRenderStates().GetColorMask(current_red_mask, current_green_mask, current_blue_mask, current_alpha_mask);
00201     graphics_engine.GetRenderStates().GetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00202 
00203     graphics_engine.GetRenderStates().SetColorMask(GL_TRUE, GL_TRUE, GL_TRUE, m_write_alpha ? GL_TRUE : GL_FALSE);
00204     graphics_engine.GetRenderStates().SetBlend(m_rop.Blend, m_rop.SrcBlend, m_rop.DstBlend);
00205     
00206     if (m_source_texture.IsValid())
00207     {
00208         if (m_foreground_texture.IsValid())
00209         {
00210           graphics_engine.QRP_GLSL_TexBlendOverTex(geometry_.x, geometry_.y, geometry_.GetWidth(),
00211             geometry_.GetHeight(), m_source_texture,
00212             m_source_texture_texxform, m_source_texture_color,
00213             m_foreground_texture, m_foreground_texture_texxform,
00214             m_foreground_texture_color, m_blend_mode);
00215         }
00216         else
00217         {
00218           graphics_engine.QRP_GLSL_ColorBlendOverTex(geometry_.x, geometry_.y, geometry_.GetWidth(),
00219             geometry_.GetHeight(), m_source_texture,
00220             m_source_texture_texxform, m_source_texture_color,
00221             m_foreground_color, m_blend_mode);
00222         }
00223     }
00224     else
00225     {
00226       if (m_foreground_texture.IsValid())
00227       {
00228         graphics_engine.QRP_GLSL_TexBlendOverColor(geometry_.x, geometry_.y, geometry_.GetWidth(),
00229           geometry_.GetHeight(), m_source_color,
00230           m_foreground_texture, m_foreground_texture_texxform,
00231           m_foreground_texture_color,
00232           m_blend_mode);
00233       }
00234     }
00235 
00236     graphics_engine.GetRenderStates().SetColorMask(current_red_mask, current_green_mask, current_blue_mask, current_alpha_mask);
00237     graphics_engine.GetRenderStates().SetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00238   }
00239 
00240   AbstractPaintLayer* CompositionLayer::Clone() const
00241   {
00242     return new CompositionLayer(*this);
00243   }
00244 
00246   TextureLayer::TextureLayer(ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm texxform, const Color& color, bool write_alpha, const ROPConfig& ROP)
00247   {
00248     m_device_texture = device_texture;
00249     m_color = color;
00250     m_write_alpha = write_alpha;
00251     m_rop = ROP;
00252     m_texxform = texxform;
00253     
00254     m_color_blend_mode = LAYER_BLEND_MODE_LAST;
00255   }
00256   
00257   TextureLayer::TextureLayer(ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm texxform, const Color& color0,
00258             bool write_alpha, const ROPConfig& ROP, const Color& blend_color, LayerBlendMode color_blend_mode)
00259   {
00260     m_device_texture = device_texture;
00261     m_color = color0;
00262     m_write_alpha = write_alpha;
00263     m_rop = ROP;
00264     m_texxform = texxform;
00265     
00266     m_blend_color = blend_color;
00267     m_color_blend_mode = color_blend_mode;
00268   }
00269 
00270   TextureLayer::~TextureLayer()
00271   {
00272     m_device_texture.Release();
00273   }
00274 
00275   void TextureLayer::Renderlayer(GraphicsEngine& graphics_engine)
00276   {
00277     unsigned int current_alpha_blend;
00278     unsigned int current_src_blend_factor;
00279     unsigned int current_dest_blend_factor;
00280     unsigned int current_red_mask;
00281     unsigned int current_green_mask;
00282     unsigned int current_blue_mask;
00283     unsigned int current_alpha_mask;
00284     
00285     // Get the current color mask and blend states. They will be restored later.
00286     graphics_engine.GetRenderStates().GetColorMask(current_red_mask, current_green_mask, current_blue_mask, current_alpha_mask);
00287     graphics_engine.GetRenderStates().GetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00288 
00289     graphics_engine.GetRenderStates().SetColorMask(GL_TRUE, GL_TRUE, GL_TRUE, m_write_alpha ? GL_TRUE : GL_FALSE);
00290     graphics_engine.GetRenderStates().SetBlend(m_rop.Blend, m_rop.SrcBlend, m_rop.DstBlend);
00291 
00292     if (m_color_blend_mode == LAYER_BLEND_MODE_LAST)
00293     {
00294       graphics_engine.QRP_1Tex(geometry_.x, geometry_.y, geometry_.GetWidth(), geometry_.GetHeight(), m_device_texture,
00295         m_texxform, m_color);
00296     }
00297     else
00298     {
00299       graphics_engine.QRP_GLSL_ColorBlendOverTex(geometry_.x, geometry_.y, geometry_.GetWidth(), geometry_.GetHeight(), m_device_texture,
00300         m_texxform, m_color, m_blend_color, m_color_blend_mode);
00301     }
00302     
00303     // Restore Color mask and blend states.
00304     graphics_engine.GetRenderStates().SetColorMask(current_red_mask, current_green_mask, current_blue_mask, current_alpha_mask);
00305     graphics_engine.GetRenderStates().SetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
00306     
00307   }
00308 
00309   AbstractPaintLayer* TextureLayer::Clone() const
00310   {
00311     return new TextureLayer(*this);
00312   }
00313 
00314   ObjectPtr< IOpenGLBaseTexture> TextureLayer::GetDeviceTexture()
00315   {
00316     return m_device_texture;
00317   }
00318 
00319 
00320 }