00001 #include <math.h>
00002 #include <assert.h>
00003 #include <sstream>
00004
00005 #include <vrs/callback.h>
00006 #include <vrs/material.h>
00007 #include <vrs/opengl/imagetexture2dgl.h>
00008 #include <vrs/opengl/texenvgl.h>
00009 #include <vrs/opengl/texture2dgl.h>
00010 #include <vrs/scaling.h>
00011 #include <vrs/sg/scenething.h>
00012 #include <vrs/facestyle.h>
00013
00014 #include "Console.h"
00015 #include "ResourceManager.h"
00016 #include "VRSTerrainLoader.h"
00017
00018 #include "MeshBeautifier.h"
00019
00020 using namespace VRS;
00021
00022
00023 namespace random_racer
00024 {
00025
00026 MeshBeautifier::MeshBeautifier(
00027 VRSTerrainLoader* p_loader, int p_minHeight, int p_maxHeight)
00028 {
00029 assert( p_minHeight < p_maxHeight );
00030
00031 m_minHeight = p_minHeight;
00032 m_maxHeight = p_maxHeight;
00033
00034 m_absHeight = calculateAbsHeight(p_minHeight, p_maxHeight);
00035
00036 m_loader = p_loader;
00037 m_newTexture = false;
00038 m_debug = false;
00039 m_removed = false;
00040 m_grid = false;
00041
00042 m_faceStyle = new FaceStyle(FaceStyle::Outlined, FaceStyle::Outlined);
00043
00044 m_gradientImage =
00045 ResourceManager::get()->loadImage("texture/gradient_grass.rgb");
00046 m_imageGradientWidth = m_gradientImage->width();
00047
00048 m_texture = ResourceManager::get()->loadTexture2D("texture/grass.jpg",
00049 true, false);
00050 m_texture->setWrap(TextureGL::REPEAT);
00051 m_texture->setFilter(TextureGL::TRILINEAR);
00052 m_texture->setMatrix(Scaling(Vector(0.2,0.2,1)).matrix());
00053
00054 m_textureTemp = ResourceManager::get()->loadTexture2D("texture/mud.jpg",
00055 true, false);
00056 m_textureTemp->setWrap(TextureGL::REPEAT);
00057 m_textureTemp->setFilter(TextureGL::TRILINEAR);
00058 m_textureTemp->setMatrix(Scaling(Vector(0.2,0.2,1)).matrix());
00059
00060 Console::get()->registerCommand("ter_texture",
00061 new MethodCallback1<MeshBeautifier, const std::string&>(
00062 this, &MeshBeautifier::terLoadTextureCommand));
00063 }
00064
00065 MeshBeautifier::~MeshBeautifier()
00066 {
00067 }
00068
00069 unsigned int
00070 MeshBeautifier::calculateAbsHeight(int p_minHeight, int p_maxHeight)
00071 {
00072 if(p_minHeight <= 0 && p_maxHeight > 0)
00073 return (p_maxHeight + abs(p_minHeight));
00074 if(p_minHeight > 0 && p_maxHeight > 0 || p_minHeight < 0 && p_maxHeight < 0)
00075 return (p_maxHeight - p_minHeight);
00076 else
00077 return 0;
00078 }
00079
00080 void
00081 MeshBeautifier::initializeSceneThing(SO<SceneThing> p_thing)
00082 {
00083
00084 m_thing = p_thing;
00085
00086
00087 p_thing->append(new Material(1.0, 1.0, 0.0));
00088
00089
00090 p_thing->append(new TexEnvGL(TexEnvGL::Modulate, Color(0)));
00091
00092
00093 p_thing->append(m_texture);
00094 }
00095
00096 void
00097 MeshBeautifier::terLoadTextureCommand(const std::string& p_string)
00098 {
00099 if(p_string.empty())
00100 {
00101 std::ostringstream s, t, u;
00102 s << "Try sand, grass and mud for some nice Textures";
00103 t << "To see the Grid of the Mesh type grid";
00104 u << "To see just the Mesh without any Textures try debug";
00105
00106 Console::get()->addLine(s.str());
00107 Console::get()->addLine(t.str());
00108 Console::get()->addLine(u.str());
00109 }
00110 else if(p_string != "debug" && p_string != "grid")
00111 {
00112 m_debug = false;
00113 m_grid = false;
00114
00115 m_textureTemp = ResourceManager::get()->loadTexture2D(
00116 "texture/" + p_string + ".jpg", true, true);
00117
00118 if(m_textureTemp != NULL)
00119 {
00120 m_textureTemp->setWrap(TextureGL::REPEAT);
00121 m_textureTemp->setFilter(TextureGL::TRILINEAR);
00122 m_textureTemp->setMatrix(Scaling(Vector(0.2,0.2,1)).matrix());
00123
00124 m_newTexture = true;
00125
00126 m_gradientImage = ResourceManager::get()->loadImage(
00127 "texture/gradient_"+p_string+".rgb");
00128 m_imageGradientWidth = m_gradientImage->width();
00129
00130 m_loader->beautifyMesh();
00131 }
00132 else
00133 Console::get()->addLine("Invalid Texturename");
00134 }
00135 else
00136 {
00137 m_grid = false;
00138
00139 if(p_string == "grid")
00140 m_grid = true;
00141
00142 m_removed = true;
00143 m_debug = true;
00144 m_loader->beautifyMesh();
00145 }
00146 }
00147
00148 }