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/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
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 }
00147