00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <GLEXT/GLState.h>
00022 #include <GLEXT/GLStateExtension.h>
00023 #include <GLEXT/GLInfo.h>
00024 #include <client/ScorchedClient.h>
00025 #include <graph/OptionsDisplay.h>
00026 #include <landscape/Landscape.h>
00027 #include <landscapemap/LandscapeMaps.h>
00028 #include <landscapedef/LandscapeDefinition.h>
00029 #include <landscapedef/LandscapeTex.h>
00030 #include <land/LandSurround.h>
00031 #include <sky/Sky.h>
00032
00033 LandSurround::LandSurround() :
00034 listNo_(0)
00035 {
00036
00037 }
00038
00039 LandSurround::~LandSurround()
00040 {
00041
00042 }
00043
00044 void LandSurround::draw(HeightMap &map, bool detail, bool lightMap)
00045 {
00046 if (!listNo_)
00047 {
00048 glNewList(listNo_ = glGenLists(1), GL_COMPILE);
00049 generateList(map,detail, lightMap);
00050 glEndList();
00051 }
00052
00053 glCallList(listNo_);
00054
00055 GLInfo::addNoTriangles(8);
00056 }
00057
00058 void LandSurround::generate()
00059 {
00060 if (listNo_) glDeleteLists(listNo_, 1);
00061 listNo_ = 0;
00062 }
00063
00064 void LandSurround::generateVerts(HeightMap &map)
00065 {
00066 int width = 1536 + map.getMapWidth();
00067 int height = 1536 + map.getMapHeight();
00068 Vector centre(map.getMapWidth() / 2, map.getMapHeight() / 2, 0);
00069 Vector offset(map.getMapWidth() + width, map.getMapHeight() + height, 0);
00070
00071 Vector offset2(map.getMapWidth() / 2, map.getMapHeight() / 2, 0);
00072 hMapBoxVerts_[0] = Vector(centre[0] - offset2[0], centre[1] - offset2[1], 0.0f);
00073 hMapBoxVerts_[1] = Vector(centre[0] - offset2[0], centre[1] + offset2[1], 0.0f);
00074 hMapBoxVerts_[2] = Vector(centre[0] + offset2[0], centre[1] + offset2[1], 0.0f);
00075 hMapBoxVerts_[3] = Vector(centre[0] + offset2[0], centre[1] - offset2[1], 0.0f);
00076
00077 Vector offset3(width + map.getMapWidth() * 2, height + map.getMapWidth() * 2, 0);
00078 hMapBoxVerts_[4] = Vector(centre[0] - offset3[0], centre[1] - offset3[1], 0.0f);
00079 hMapBoxVerts_[5] = Vector(centre[0] - offset3[0], centre[1] + offset3[1], 0.0f);
00080 hMapBoxVerts_[6] = Vector(centre[0] + offset3[0], centre[1] + offset3[1], 0.0f);
00081 hMapBoxVerts_[7] = Vector(centre[0] + offset3[0], centre[1] - offset3[1], 0.0f);
00082
00083 hMapBoxVerts_[8] = Vector(centre[0] - offset2[0], centre[1] - offset3[1], 0.0f);
00084 hMapBoxVerts_[9] = Vector(centre[0] - offset2[0], centre[1] + offset3[1], 0.0f);
00085 hMapBoxVerts_[10] = Vector(centre[0] + offset2[0], centre[1] + offset3[1], 0.0f);
00086 hMapBoxVerts_[11] = Vector(centre[0] + offset2[0], centre[1] - offset3[1], 0.0f);
00087
00088 hMapBoxVerts_[12] = Vector(centre[0] - offset3[0], centre[1] - offset2[1], 0.0f);
00089 hMapBoxVerts_[13] = Vector(centre[0] - offset3[0], centre[1] + offset2[1], 0.0f);
00090 hMapBoxVerts_[14] = Vector(centre[0] + offset3[0], centre[1] + offset2[1], 0.0f);
00091 hMapBoxVerts_[15] = Vector(centre[0] + offset3[0], centre[1] - offset2[1], 0.0f);
00092 }
00093
00094 void LandSurround::generateList(HeightMap &map, bool detail, bool lightMap)
00095 {
00096 generateVerts(map);
00097
00098 const int dataOfs[8][4] = {
00099 {8,11,3,0},
00100 {1,2,10,9},
00101 {4,8,0,12},
00102 {11,7,15,3},
00103 {3,15,14,2},
00104 {2,14,6,10},
00105 {13,1,9,5},
00106 {12,0,1,13}
00107 };
00108
00109 LandscapeTex &tex =
00110 *ScorchedClient::instance()->getLandscapeMaps().getDefinitions().getTex();
00111 Vector &ambient = tex.skyambience;
00112 Vector &diffuse = tex.skydiffuse;
00113 Vector &sunPos =
00114 Landscape::instance()->getSky().getSun().getPosition();
00115 Vector normal(0.0f, 0.0f, 1.0f);
00116
00117 float width = float(Landscape::instance()->getGroundTexture().getWidth());
00118 float height = float(Landscape::instance()->getGroundTexture().getHeight());
00119 float texTileX = width / 16.0f;
00120 float texTileY = height / 16.0f;
00121
00122 glBegin(GL_QUADS);
00123 for (int i=0; i<8; i++)
00124 {
00125 for (int j=0; j<4; j++)
00126 {
00127 Vector pos = hMapBoxVerts_[dataOfs[i][j]];
00128 Vector sunDirection = (sunPos - pos).Normalize();
00129 float diffuseLightMult =
00130 (((normal.dotP(sunDirection)) / 2.0f) + 0.5f);
00131 Vector light = diffuse * diffuseLightMult + ambient;
00132 light[0] = MIN(1.0f, light[0]);
00133 light[1] = MIN(1.0f, light[1]);
00134 light[2] = MIN(1.0f, light[2]);
00135
00136 float texX = hMapBoxVerts_[dataOfs[i][j]][0] / width;
00137 float texY = hMapBoxVerts_[dataOfs[i][j]][1] / height;
00138
00139 #ifdef SPLAT_MAP
00140 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, texX, texY);
00141 #else
00142 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, texX * texTileX / 4.0f, texY * texTileY / 4.0f);
00143 #endif
00144 if (GLStateExtension::hasMultiTex())
00145 {
00146 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, texX, texY);
00147 if (GLStateExtension::getTextureUnits() > 2)
00148 {
00149 glMultiTexCoord2fARB(GL_TEXTURE2_ARB, texX * texTileX, texY * texTileY);
00150 }
00151 }
00152
00153 if (lightMap) glColor3fv(light);
00154 else glColor3f(1.0f, 1.0f, 1.0f);
00155 glNormal3f(0.0f, 0.0f, 1.0f);
00156 glVertex3fv(pos);
00157 }
00158 }
00159 glEnd();
00160 }