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 | #include "stdincl.h" |
---|
19 | #include "model.h" |
---|
20 | #include "vector.h" |
---|
21 | #include "glincl.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | |
---|
26 | /** |
---|
27 | \brief standard constructor |
---|
28 | |
---|
29 | */ |
---|
30 | Terrain::Terrain () |
---|
31 | { |
---|
32 | this->init(); |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | \brief Constructor for loading a Terrain out of a file |
---|
37 | \param fileName The file to load data from. |
---|
38 | |
---|
39 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
40 | */ |
---|
41 | Terrain::Terrain(char* fileName) |
---|
42 | { |
---|
43 | this->init(); |
---|
44 | |
---|
45 | if (strstr(fileName, ".obj") || strstr(fileName, ".OBJ")) |
---|
46 | { |
---|
47 | this->model = (Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_LEVEL); |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | // load the hightMap here. |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | \brief a Constructor for the Debug-Worlds |
---|
57 | |
---|
58 | \todo make it not compileable when not in debug-mode |
---|
59 | */ |
---|
60 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
61 | { |
---|
62 | this->init(); |
---|
63 | this->buildDebugTerrain(debugTerrain); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | \brief standard deconstructor |
---|
68 | |
---|
69 | */ |
---|
70 | Terrain::~Terrain () |
---|
71 | { |
---|
72 | if (objectList) |
---|
73 | glDeleteLists(this->objectList, 1); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | void Terrain::init(void) |
---|
78 | { |
---|
79 | this->setClassID(CL_TERRAIN, "Terrain"); |
---|
80 | this->objectList = 0; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | void Terrain::draw () |
---|
86 | { |
---|
87 | glMatrixMode(GL_MODELVIEW); |
---|
88 | glPushMatrix(); |
---|
89 | float matrix[4][4]; |
---|
90 | |
---|
91 | /* translate */ |
---|
92 | glTranslatef (this->getAbsCoor ().x, |
---|
93 | this->getAbsCoor ().y, |
---|
94 | this->getAbsCoor ().z); |
---|
95 | /* rotate */ |
---|
96 | this->getAbsDir ().matrix (matrix); |
---|
97 | glMultMatrixf((float*)matrix); |
---|
98 | |
---|
99 | if (this->objectList) |
---|
100 | glCallList(this->objectList); |
---|
101 | else if (this->model) |
---|
102 | this->model->draw(); |
---|
103 | glPopMatrix(); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
108 | { |
---|
109 | // if the terrain is the Terrain of Dave |
---|
110 | if (debugTerrain == TERRAIN_DAVE) |
---|
111 | { |
---|
112 | objectList = glGenLists(1); |
---|
113 | glNewList (objectList, GL_COMPILE); |
---|
114 | |
---|
115 | glColor3f(1.0,0,0); |
---|
116 | |
---|
117 | int sizeX = 100; |
---|
118 | int sizeZ = 80; |
---|
119 | float length = 1000; |
---|
120 | float width = 200; |
---|
121 | float widthX = float (length /sizeX); |
---|
122 | float widthZ = float (width /sizeZ); |
---|
123 | |
---|
124 | float height [sizeX][sizeZ]; |
---|
125 | Vector normal_vectors[sizeX][sizeZ]; |
---|
126 | |
---|
127 | |
---|
128 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
129 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
130 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
131 | #ifdef __WIN32__ |
---|
132 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
133 | #else |
---|
134 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
135 | #endif |
---|
136 | |
---|
137 | //Die Huegel ein wenig glaetten |
---|
138 | for (int h=1; h<2;h++) |
---|
139 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
140 | for(int j=1;j<sizeZ-2;j+=1) |
---|
141 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
142 | |
---|
143 | //Berechnung von normalen Vektoren |
---|
144 | for(int i=1;i<sizeX-2;i+=1) |
---|
145 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
146 | { |
---|
147 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
148 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
149 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
150 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
151 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
152 | |
---|
153 | Vector c1 = v2 - v1; |
---|
154 | Vector c2 = v3 - v1; |
---|
155 | Vector c3= v4 - v1; |
---|
156 | Vector c4 = v5 - v1; |
---|
157 | Vector zero = Vector (0,0,0); |
---|
158 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
159 | normal_vectors[i][j].normalize(); |
---|
160 | } |
---|
161 | |
---|
162 | glBegin(GL_QUADS); |
---|
163 | int snowheight=3; |
---|
164 | for ( int i = 0; i<sizeX; i+=1) |
---|
165 | for (int j = 0; j<sizeZ;j+=1) |
---|
166 | { |
---|
167 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
168 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
169 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
170 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
171 | float a[3]; |
---|
172 | if(height[i][j]<snowheight){ |
---|
173 | a[0]=0; |
---|
174 | a[1]=1.0-height[i][j]/10-.3; |
---|
175 | a[2]=0; |
---|
176 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
177 | } |
---|
178 | else{ |
---|
179 | a[0]=1.0; |
---|
180 | a[1]=1.0; |
---|
181 | a[2]=1.0; |
---|
182 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
183 | |
---|
184 | } |
---|
185 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
186 | glVertex3f(v1.x, v1.y, v1.z); |
---|
187 | if(height[i+1][j]<snowheight){ |
---|
188 | a[0]=0; |
---|
189 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
190 | a[2]=0; |
---|
191 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
192 | } |
---|
193 | else{ |
---|
194 | a[0]=1.0; |
---|
195 | a[1]=1.0; |
---|
196 | a[2]=1.0; |
---|
197 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
198 | |
---|
199 | } |
---|
200 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
201 | glVertex3f(v2.x, v2.y, v2.z); |
---|
202 | if(height[i+1][j+1]<snowheight){ |
---|
203 | a[0]=0; |
---|
204 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
205 | a[2]=0; |
---|
206 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
207 | } |
---|
208 | else{ |
---|
209 | a[0]=1.0; |
---|
210 | a[1]=1.0; |
---|
211 | a[2]=1.0; |
---|
212 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
213 | |
---|
214 | |
---|
215 | } |
---|
216 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
217 | glVertex3f(v3.x, v3.y, v3.z); |
---|
218 | if(height[i][j+1]<snowheight){ |
---|
219 | a[0]=0; |
---|
220 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
221 | a[2]=0; |
---|
222 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
223 | } |
---|
224 | else{ |
---|
225 | a[0]=1.0; |
---|
226 | a[1]=1.0; |
---|
227 | a[2]=1.0; |
---|
228 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
229 | } |
---|
230 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
231 | glVertex3f(v4.x, v4.y, v4.z); |
---|
232 | |
---|
233 | } |
---|
234 | glEnd(); |
---|
235 | glEndList(); |
---|
236 | } |
---|
237 | |
---|
238 | if (debugTerrain == TERRAIN_BENSCH) |
---|
239 | { |
---|
240 | /* |
---|
241 | this->model = (OBJModel*) new Model(); |
---|
242 | this->model->setName("CUBE"); |
---|
243 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
244 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
245 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
246 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
247 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
248 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
249 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
250 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
251 | |
---|
252 | this->model->addVertexTexture (0.0, 0.0); |
---|
253 | this->model->addVertexTexture (1.0, 0.0); |
---|
254 | this->model->addVertexTexture (0.0, 1.0); |
---|
255 | this->model->addVertexTexture (1.0, 1.0); |
---|
256 | this->model->addVertexTexture (0.0, 2.0); |
---|
257 | this->model->addVertexTexture (1.0, 2.0); |
---|
258 | this->model->addVertexTexture (0.0, 3.0); |
---|
259 | this->model->addVertexTexture (1.0, 3.0); |
---|
260 | this->model->addVertexTexture (0.0, 4.0); |
---|
261 | this->model->addVertexTexture (1.0, 4.0); |
---|
262 | this->model->addVertexTexture (2.0, 0.0); |
---|
263 | this->model->addVertexTexture (2.0, 1.0); |
---|
264 | this->model->addVertexTexture (-1.0, 0.0); |
---|
265 | this->model->addVertexTexture (-1.0, 1.0); |
---|
266 | |
---|
267 | this->model->finalize(); |
---|
268 | */ |
---|
269 | } |
---|
270 | |
---|
271 | } |
---|