triangles_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/triangles_material_generator.h>
00031 #include <rve_render_server/batching/triangles_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/Triangles.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 TrianglesRendererDesc& desc, bool alpha)
00054 {
00055   std::stringstream ss;
00056   ss << "GenTriangles_";
00057 
00058   if (alpha)
00059   {
00060     ss << "Alpha";
00061   }
00062 
00063   return ss.str();
00064 }
00065 
00066 void fillVertexShaderDefinition(const TrianglesRendererDesc& desc, ShaderDefinition& def)
00067 {
00068   def.input("float4", "position", "POSITION");
00069   def.input("float3", "normal", "NORMAL");
00070   def.input("float4", "color", "COLOR");
00071 
00072   def.output("float4", "position", "POSITION");
00073   def.output("float4", "color", "COLOR");
00074   def.output("float3", "view_pos", "TEXCOORD0");
00075   def.output("float3", "normal", "TEXCOORD1");
00076 
00077   def.uniform("float4", "camera_pos", Ogre::GpuProgramParameters::ACT_CAMERA_POSITION_OBJECT_SPACE, 0);
00078   def.uniform("float4x4", "worldviewproj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX, 0);
00079   def.uniform("float4x4", "worldview", Ogre::GpuProgramParameters::ACT_WORLDVIEW_MATRIX, 0);
00080 
00081   std::stringstream ss;
00082 
00083   ss << " float4 pos = in_position;\n";
00084   ss << " float3 view_dir = worldview._m02_m12_m22;\n";
00085   ss << " float3 normal = -sign(dot(view_dir, in_normal)) * in_normal;\n";
00086 
00087   ss << " out_position = mul(worldviewproj, pos);\n" << std::endl;
00088   ss << " out_normal = mul(worldview, float4(normal, 0)).xyz;\n" << std::endl;
00089   ss << " out_view_pos = mul(worldview, pos).xyz;\n" << std::endl;
00090   ss << " out_color = in_color;\n";
00091   def.body = ss.str();
00092 }
00093 
00094 void fillFragmentShaderDefinition(const TrianglesRendererDesc& desc, ShaderDefinition& def)
00095 {
00096   def.input("float4", "color", "COLOR");
00097   def.input("float3", "view_pos", "TEXCOORD0");
00098   def.input("float3", "normal", "TEXCOORD1");
00099 
00100   std::stringstream ss;
00101   ss << " float3 normal = in_normal;" << std::endl;
00102   ss << " float4 color = in_color;\n";
00103   def.body = ss.str();
00104 }
00105 
00106 std::pair<Ogre::MaterialPtr, Ogre::MaterialPtr> generateMaterialsForTriangles(const TrianglesRendererDesc& desc)
00107 {
00108   std::string material_name_opaque = descToStringID(desc, false);
00109   std::string material_name_alpha = descToStringID(desc, true);
00110 
00111   MaterialDefinition mat_def;
00112   fillVertexShaderDefinition(desc, mat_def.vertex_def);
00113   fillFragmentShaderDefinition(desc, mat_def.fragment_def);
00114 
00115   mat_def.name = material_name_opaque;
00116   Ogre::MaterialPtr mat_opaque = generateOgreMaterial(mat_def);
00117   mat_opaque->setPointSize(5);
00118   mat_opaque->setCullingMode(Ogre::CULL_NONE);
00119 
00120   mat_def.name = material_name_alpha;
00121   mat_def.transparent = true;
00122   Ogre::MaterialPtr mat_alpha = generateOgreMaterial(mat_def);
00123   mat_alpha->setPointSize(5);
00124   mat_alpha->setCullingMode(Ogre::CULL_NONE);
00125 
00126   return std::make_pair(mat_opaque, mat_alpha);
00127 }
00128 
00129 } // namespace rve_render_server
00130 


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