Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 }
00130