lines_material_generator.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <rve_render_server/batching/lines_material_generator.h>
00031 #include <rve_render_server/batching/lines_renderer_desc.h>
00032 #include <rve_render_server/material_definition.h>
00033 #include <rve_render_server/ogre_material_generator.h>
00034 #include <rve_render_server/init.h>
00035 #include <rve_render_server/renderer.h>
00036 
00037 #include <rve_msgs/Lines.h>
00038 
00039 #include <sstream>
00040 
00041 #include <OGRE/OgreRoot.h>
00042 #include <OGRE/OgreMaterialManager.h>
00043 #include <OGRE/OgreTextureManager.h>
00044 #include <OGRE/OgreTexture.h>
00045 #include <OGRE/OgreGpuProgramManager.h>
00046 #include <OGRE/OgreHighLevelGpuProgramManager.h>
00047 #include <OGRE/OgreTechnique.h>
00048 #include <OGRE/OgrePass.h>
00049 
00050 namespace rve_render_server
00051 {
00052 
00053 std::string descToStringID(const LinesRendererDesc& desc, bool alpha)
00054 {
00055   std::stringstream ss;
00056   ss << "GenLines_" << (uint32_t)desc.type;
00057 
00058   ss << "Scale " << std::setprecision(3) << desc.scale.x << desc.scale.y << desc.scale.z;
00059 
00060   if (alpha)
00061   {
00062     ss << "Alpha";
00063   }
00064 
00065   return ss.str();
00066 }
00067 
00068 void fillVertexShaderDefinition(const LinesRendererDesc& desc, ShaderDefinition& def)
00069 {
00070   bool is_billboard = desc.type == rve_msgs::Lines::TYPE_BILLBOARD_LIST;
00071 
00072   def.input("float4", "position", "POSITION");
00073   def.input("float4", "color", "COLOR");
00074   if (is_billboard)
00075   {
00076     def.include("lines_billboard_vp.cg");
00077     def.input("float4", "offset_and_direction", "TEXCOORD0");
00078   }
00079 
00080   def.output("float4", "position", "POSITION");
00081   def.output("float4", "color", "COLOR");
00082   def.output("float3", "view_pos", "TEXCOORD0");
00083   def.output("float3", "normal", "TEXCOORD1");
00084 
00085   def.uniform("float4", "camera_pos", Ogre::GpuProgramParameters::ACT_CAMERA_POSITION_OBJECT_SPACE, 0);
00086   def.uniform("float4x4", "worldviewproj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX, 0);
00087   def.uniform("float4x4", "worldview", Ogre::GpuProgramParameters::ACT_WORLDVIEW_MATRIX, 0);
00088   def.uniform("float4", "size", Ogre::GpuProgramParameters::ACT_CUSTOM, LinesRendererDesc::CustomParam_Size);
00089 
00090   std::stringstream ss;
00091   if (!is_billboard)
00092   {
00093     ss << " float4 pos = in_position;\n";
00094     ss << " float3 normal = normalize(camera_pos.xyz - pos.xyz);\n";
00095   }
00096   else
00097   {
00098     ss << " PosAndNormal posn = calculateBillboardVertexPositionAndNormal(in_position, in_offset_and_direction.w, in_offset_and_direction.xyz, camera_pos, size);\n";
00099     ss << " float4 pos = posn.pos;\n";
00100     ss << " float3 normal = posn.normal;\n";
00101   }
00102 
00103   ss << " out_position = mul(worldviewproj, pos);\n" << std::endl;
00104   ss << " out_normal = mul(worldview, float4(normal, 0)).xyz;\n" << std::endl;
00105   ss << " out_view_pos = mul(worldview, pos).xyz;\n" << std::endl;
00106   ss << " out_color = in_color;\n";
00107   def.body = ss.str();
00108 }
00109 
00110 void fillFragmentShaderDefinition(const LinesRendererDesc& desc, ShaderDefinition& def)
00111 {
00112   def.input("float4", "color", "COLOR");
00113   def.input("float3", "view_pos", "TEXCOORD0");
00114   def.input("float3", "normal", "TEXCOORD1");
00115 
00116   std::stringstream ss;
00117   ss << " float3 normal = in_normal;" << std::endl;
00118   ss << "float4 color = in_color;\n";
00119   //ss << "float4 color = float4(1.0, 0.0, 0.0, 1.0);\n";
00120   def.body = ss.str();
00121 }
00122 
00123 std::pair<Ogre::MaterialPtr, Ogre::MaterialPtr> generateMaterialsForLines(const LinesRendererDesc& desc)
00124 {
00125   std::string material_name_opaque = descToStringID(desc, false);
00126   std::string material_name_alpha = descToStringID(desc, true);
00127 
00128   MaterialDefinition mat_def;
00129   fillVertexShaderDefinition(desc, mat_def.vertex_def);
00130   fillFragmentShaderDefinition(desc, mat_def.fragment_def);
00131 
00132   mat_def.name = material_name_opaque;
00133   Ogre::MaterialPtr mat_opaque = generateOgreMaterial(mat_def);
00134   mat_opaque->setPointSize(5);
00135   mat_opaque->setCullingMode(Ogre::CULL_NONE);
00136 
00137   mat_def.name = material_name_alpha;
00138   mat_def.transparent = true;
00139   Ogre::MaterialPtr mat_alpha = generateOgreMaterial(mat_def);
00140   mat_alpha->setPointSize(5);
00141   mat_alpha->setCullingMode(Ogre::CULL_NONE);
00142 
00143   return std::make_pair(mat_opaque, mat_alpha);
00144 }
00145 
00146 } // namespace rve_render_server
00147 


rve_render_server
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:15