1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "terrain.h" |
---|
18 | |
---|
19 | #include "model.h" |
---|
20 | #include "vector.h" |
---|
21 | #include "glincl.h" |
---|
22 | |
---|
23 | #include "factory.h" |
---|
24 | #include "load_param.h" |
---|
25 | #include "resource_manager.h" |
---|
26 | #include "spatial_separation.h" |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | CREATE_FACTORY(Terrain); |
---|
31 | |
---|
32 | /** |
---|
33 | * standard constructor |
---|
34 | |
---|
35 | */ |
---|
36 | Terrain::Terrain (const TiXmlElement* root) |
---|
37 | { |
---|
38 | this->init(); |
---|
39 | this->loadParams(root); |
---|
40 | |
---|
41 | this->ssp = new SpatialSeparation((AbstractModel*)this->model, 10.0f); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /** |
---|
46 | * Constructor for loading a Terrain out of a file |
---|
47 | * @param fileName The file to load data from. |
---|
48 | |
---|
49 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
50 | */ |
---|
51 | Terrain::Terrain(const char* fileName) |
---|
52 | { |
---|
53 | this->init(); |
---|
54 | |
---|
55 | if (strstr(fileName, ".obj") || strstr(fileName, ".OBJ")) |
---|
56 | { |
---|
57 | this->model = (Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_LEVEL); |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | // load the hightMap here. |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * a Constructor for the Debug-Worlds |
---|
67 | |
---|
68 | @todo make it not compileable when not in debug-mode |
---|
69 | */ |
---|
70 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
71 | { |
---|
72 | this->init(); |
---|
73 | this->buildDebugTerrain(debugTerrain); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * standard deconstructor |
---|
78 | |
---|
79 | */ |
---|
80 | Terrain::~Terrain () |
---|
81 | { |
---|
82 | if (objectList) |
---|
83 | glDeleteLists(this->objectList, 1); |
---|
84 | if( this->ssp) |
---|
85 | delete ssp; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void Terrain::init() |
---|
90 | { |
---|
91 | this->setClassID(CL_TERRAIN, "Terrain"); |
---|
92 | |
---|
93 | this->objectList = 0; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | void Terrain::loadParams(const TiXmlElement* root) |
---|
98 | { |
---|
99 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
100 | |
---|
101 | //LoadParam<Terrain>(root, "DebugTerrain", ); |
---|
102 | } |
---|
103 | |
---|
104 | void Terrain::draw () |
---|
105 | { |
---|
106 | glMatrixMode(GL_MODELVIEW); |
---|
107 | glPushMatrix(); |
---|
108 | |
---|
109 | /* translate */ |
---|
110 | glTranslatef (this->getAbsCoor ().x, |
---|
111 | this->getAbsCoor ().y, |
---|
112 | this->getAbsCoor ().z); |
---|
113 | /* rotate */ |
---|
114 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
115 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
116 | |
---|
117 | if (this->objectList) |
---|
118 | glCallList(this->objectList); |
---|
119 | else if (this->model) |
---|
120 | this->model->draw(); |
---|
121 | glPopMatrix(); |
---|
122 | |
---|
123 | /* THIS IS ONLY FOR DEBUGGING INFORMATION */ |
---|
124 | this->ssp->drawQuadtree(); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
129 | { |
---|
130 | // if the terrain is the Terrain of Dave |
---|
131 | if (debugTerrain == TERRAIN_DAVE) |
---|
132 | { |
---|
133 | objectList = glGenLists(1); |
---|
134 | glNewList (objectList, GL_COMPILE); |
---|
135 | |
---|
136 | glColor3f(1.0,0,0); |
---|
137 | |
---|
138 | int sizeX = 100; |
---|
139 | int sizeZ = 80; |
---|
140 | float length = 1000; |
---|
141 | float width = 200; |
---|
142 | float widthX = float (length /sizeX); |
---|
143 | float widthZ = float (width /sizeZ); |
---|
144 | |
---|
145 | float height [sizeX][sizeZ]; |
---|
146 | Vector normal_vectors[sizeX][sizeZ]; |
---|
147 | |
---|
148 | |
---|
149 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
150 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
151 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
152 | #ifdef __WIN32__ |
---|
153 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
154 | #else |
---|
155 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
156 | #endif |
---|
157 | |
---|
158 | //Die Huegel ein wenig glaetten |
---|
159 | for (int h=1; h<2;h++) |
---|
160 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
161 | for(int j=1;j<sizeZ-2;j+=1) |
---|
162 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
163 | |
---|
164 | //Berechnung von normalen Vektoren |
---|
165 | for(int i=1;i<sizeX-2;i+=1) |
---|
166 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
167 | { |
---|
168 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
169 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
170 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
171 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
172 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
173 | |
---|
174 | Vector c1 = v2 - v1; |
---|
175 | Vector c2 = v3 - v1; |
---|
176 | Vector c3= v4 - v1; |
---|
177 | Vector c4 = v5 - v1; |
---|
178 | Vector zero = Vector (0,0,0); |
---|
179 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
180 | normal_vectors[i][j].normalize(); |
---|
181 | } |
---|
182 | |
---|
183 | glBegin(GL_QUADS); |
---|
184 | int snowheight=3; |
---|
185 | for ( int i = 0; i<sizeX; i+=1) |
---|
186 | for (int j = 0; j<sizeZ;j+=1) |
---|
187 | { |
---|
188 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
189 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
190 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
191 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
192 | float a[3]; |
---|
193 | if(height[i][j]<snowheight){ |
---|
194 | a[0]=0; |
---|
195 | a[1]=1.0-height[i][j]/10-.3; |
---|
196 | a[2]=0; |
---|
197 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
198 | } |
---|
199 | else{ |
---|
200 | a[0]=1.0; |
---|
201 | a[1]=1.0; |
---|
202 | a[2]=1.0; |
---|
203 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
204 | |
---|
205 | } |
---|
206 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
207 | glVertex3f(v1.x, v1.y, v1.z); |
---|
208 | if(height[i+1][j]<snowheight){ |
---|
209 | a[0]=0; |
---|
210 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
211 | a[2]=0; |
---|
212 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
213 | } |
---|
214 | else{ |
---|
215 | a[0]=1.0; |
---|
216 | a[1]=1.0; |
---|
217 | a[2]=1.0; |
---|
218 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
219 | |
---|
220 | } |
---|
221 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
222 | glVertex3f(v2.x, v2.y, v2.z); |
---|
223 | if(height[i+1][j+1]<snowheight){ |
---|
224 | a[0]=0; |
---|
225 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
226 | a[2]=0; |
---|
227 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
228 | } |
---|
229 | else{ |
---|
230 | a[0]=1.0; |
---|
231 | a[1]=1.0; |
---|
232 | a[2]=1.0; |
---|
233 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
234 | |
---|
235 | |
---|
236 | } |
---|
237 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
238 | glVertex3f(v3.x, v3.y, v3.z); |
---|
239 | if(height[i][j+1]<snowheight){ |
---|
240 | a[0]=0; |
---|
241 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
242 | a[2]=0; |
---|
243 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
244 | } |
---|
245 | else{ |
---|
246 | a[0]=1.0; |
---|
247 | a[1]=1.0; |
---|
248 | a[2]=1.0; |
---|
249 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
250 | } |
---|
251 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
252 | glVertex3f(v4.x, v4.y, v4.z); |
---|
253 | |
---|
254 | } |
---|
255 | glEnd(); |
---|
256 | glEndList(); |
---|
257 | } |
---|
258 | |
---|
259 | if (debugTerrain == TERRAIN_BENSCH) |
---|
260 | { |
---|
261 | /* |
---|
262 | this->model = (OBJModel*) new Model(); |
---|
263 | this->model->setName("CUBE"); |
---|
264 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
265 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
266 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
267 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
268 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
269 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
270 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
271 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
272 | |
---|
273 | this->model->addVertexTexture (0.0, 0.0); |
---|
274 | this->model->addVertexTexture (1.0, 0.0); |
---|
275 | this->model->addVertexTexture (0.0, 1.0); |
---|
276 | this->model->addVertexTexture (1.0, 1.0); |
---|
277 | this->model->addVertexTexture (0.0, 2.0); |
---|
278 | this->model->addVertexTexture (1.0, 2.0); |
---|
279 | this->model->addVertexTexture (0.0, 3.0); |
---|
280 | this->model->addVertexTexture (1.0, 3.0); |
---|
281 | this->model->addVertexTexture (0.0, 4.0); |
---|
282 | this->model->addVertexTexture (1.0, 4.0); |
---|
283 | this->model->addVertexTexture (2.0, 0.0); |
---|
284 | this->model->addVertexTexture (2.0, 1.0); |
---|
285 | this->model->addVertexTexture (-1.0, 0.0); |
---|
286 | this->model->addVertexTexture (-1.0, 1.0); |
---|
287 | |
---|
288 | this->model->finalize(); |
---|
289 | */ |
---|
290 | } |
---|
291 | |
---|
292 | } |
---|