Initial revision
[0ad:0ad.git] / terrain / terrainMain.cpp
1 #include <windows.h>
2
3 #include "Matrix3D.H"
4 #include "Renderer.H"
5 #include "Terrain.H"
6
7 #include "time.h"
8 #include "wsdl.h"
9 #include "tex.h"
10
11
12 HWND InitializeGame (HINSTANCE hInstance);
13 void DestroyGame();
14
15 void InitScene ();
16 void InitResources ();
17 void RenderScene ();
18
19 extern bool keys[256];
20
21
22 HWND                            GameWindow;
23
24 CMatrix3D                       g_WorldMat;
25 CRenderer                       g_Renderer;
26 CTerrain                        g_Terrain;
27 CCamera                         g_Camera;
28
29 int                                     SelPX, SelPY, SelTX, SelTY;
30 int                                     g_BaseTexCounter = 0;
31 int                                     g_SecTexCounter = 1;
32 int                                     g_TransTexCounter = 0;
33
34 int                                     g_TickCounter = 0;
35 double                          g_LastTime;
36
37
38 const int NUM_ALPHA_MAPS = 13;
39
40 //CTexture                      g_BaseTexture[5];
41 Handle BaseTexs[5];
42
43 Handle AlphaMaps[NUM_ALPHA_MAPS];
44 //CTexture                      g_TransitionTexture[NUM_ALPHA_MAPS];
45
46 int mouse_x=50, mouse_y=50;
47
48 extern int xres, yres;
49
50
51 void terr_init()
52 {
53         g_Renderer.Initialize (GameWindow, 1280, 1024, 32);
54
55         InitResources ();
56         InitScene ();
57 }
58
59 void terr_update()
60 {
61 g_FrameCounter++;
62
63         /////////////////////////////////////////////
64                 POINT MousePos;
65
66                 GetCursorPos (&MousePos);
67                 CVector3D right(1,0,1);
68                 CVector3D up(1,0,-1);
69                 right.Normalize ();
70                 up.Normalize ();
71                 
72                 if (mouse_x >= xres-2)
73                         g_Camera.m_Orientation.Translate (right);
74                 if (mouse_x <= 3)
75                         g_Camera.m_Orientation.Translate (right*-1);
76
77                 if (mouse_y >= yres-2)
78                         g_Camera.m_Orientation.Translate (up);
79                 if (mouse_y <= 3)
80                         g_Camera.m_Orientation.Translate (up*-1);
81
82
83
84                 float fov = g_Camera.GetFOV();
85                 float d = DEGTORAD(0.4f);
86                 if(keys[SDLK_KP_MINUS])
87                         if (fov+d < DEGTORAD(90))
88                                 g_Camera.SetProjection (1, 1000, fov + d);
89                 if(keys[SDLK_KP_ADD])
90                         if (fov-d > DEGTORAD(20))
91                         g_Camera.SetProjection (1, 1000, fov - d);
92
93                 g_Camera.UpdateFrustum ();
94 /////////////////////////////////////////////
95
96
97         g_Renderer.RenderTerrain (&g_Terrain, &g_Camera);
98         g_Renderer.RenderTileOutline (&(g_Terrain.m_Patches[SelPY][SelPX].m_MiniPatches[SelTY][SelTX]));
99
100
101
102 }
103
104
105
106
107
108
109 bool terr_handler(const SDL_Event& ev)
110 {
111         switch(ev.type)
112         {
113         case SDL_MOUSEMOTION:
114                 mouse_x = ev.motion.x;
115                 mouse_y = ev.motion.y;
116                 break;
117
118         case SDL_KEYDOWN:
119                 switch(ev.key.keysym.sym)
120                 {
121                 case 'W':
122                         g_WireFrame = !g_WireFrame;
123                         break;
124
125                 case 'H':
126                         // quick hack to return camera home, for screenshots (after alt+tabbing)
127                         g_Camera.SetProjection (1, 1000, DEGTORAD(20));
128                         g_Camera.m_Orientation.SetXRotation(DEGTORAD(30));
129                         g_Camera.m_Orientation.RotateY(DEGTORAD(-45));
130                         g_Camera.m_Orientation.Translate (100, 150, -100);
131                         break;
132
133                 case 'L':
134                         g_HillShading = !g_HillShading;
135                         break;
136
137 // tile selection
138                 case SDLK_DOWN:
139                         if(++SelTX > 15)
140                                 if(SelPX == NUM_PATCHES_PER_SIDE-1)
141                                         SelTX = 15;
142                                 else
143                                         SelTX = 0, SelPX++;
144                         break;
145
146                 case SDLK_UP:
147                         if(--SelTX < 0)
148                                 if(SelPX == 0)
149                                         SelTX = 0;
150                                 else
151                                         SelTX = 15, SelPX--;
152                         break;
153                 case SDLK_RIGHT:
154                         if(++SelTY > 15)
155                                 if(SelPY == NUM_PATCHES_PER_SIDE-1)
156                                         SelTY = 15;
157                                 else
158                                         SelTY = 0, SelPY++;
159                         break;
160
161                 case SDLK_LEFT:
162                         if(--SelTY < 0)
163                                 if(SelPY == 0)
164                                         SelTY = 0;
165                                 else
166                                         SelTY = 15, SelPY--;
167                         break;
168
169
170                 case SDLK_KP0:
171                                 {
172                                         CMiniPatch *MPatch = &g_Terrain.m_Patches[SelPY][SelPX].m_MiniPatches[SelTY][SelTX];
173                                         if (!MPatch->Tex2)
174                                         {
175                                                 MPatch->m_AlphaMap = AlphaMaps[g_TransTexCounter];
176                                                 MPatch->Tex2 = BaseTexs[g_SecTexCounter];
177                                         }
178                                         else
179                                         {
180                                                 MPatch->Tex2 = 0;
181                                                 MPatch->m_AlphaMap = 0;
182                                         }
183                                         break;
184                                 }
185
186                 case SDLK_KP1:
187                                 {
188                                         CMiniPatch *MPatch = &g_Terrain.m_Patches[SelPY][SelPX].m_MiniPatches[SelTY][SelTX];
189
190                                         g_BaseTexCounter++;
191                                         if (g_BaseTexCounter > 4)
192                                                 g_BaseTexCounter = 0;
193                                         
194                                         MPatch->Tex1 = BaseTexs[g_BaseTexCounter];
195                                         break;
196                                 }
197
198                 case SDLK_KP2:
199                                 {
200                                         CMiniPatch *MPatch = &g_Terrain.m_Patches[SelPY][SelPX].m_MiniPatches[SelTY][SelTX];
201                                         
202                                         if (MPatch->Tex2)
203                                         {
204                                                 g_SecTexCounter++;
205                                                 if (g_SecTexCounter > 4)
206                                                         g_SecTexCounter = 0;
207
208                                                 MPatch->Tex2 = BaseTexs[g_SecTexCounter];
209                                         }
210
211                                         break;
212                                 }
213                                                 
214                 case SDLK_KP3:
215                                 {
216                                         CMiniPatch *MPatch = &g_Terrain.m_Patches[SelPY][SelPX].m_MiniPatches[SelTY][SelTX];
217                                         
218                                         if (MPatch->/*m_pTransitionTexture*/m_AlphaMap)
219                                         {
220                                                 g_TransTexCounter++;
221                                                 if (g_TransTexCounter >= NUM_ALPHA_MAPS)
222                                                         g_TransTexCounter = 0;
223
224                                                 MPatch->m_AlphaMap = AlphaMaps[g_TransTexCounter];
225                                         }
226
227                                         break;
228                                 }
229
230                 }
231         }
232
233         return false;
234 }
235
236
237
238
239 void InitScene ()
240 {
241         g_Terrain.Initalize ("terrain.raw");
242
243         for (int pj=0; pj<NUM_PATCHES_PER_SIDE; pj++)
244         {
245                 for (int pi=0; pi<NUM_PATCHES_PER_SIDE; pi++)
246                 {
247                         for (int tj=0; tj<16; tj++)
248                         {
249                                 for (int ti=0; ti<16; ti++)
250                                 {
251                                         g_Terrain.m_Patches[pj][pi].m_MiniPatches[tj][ti].Tex1 = BaseTexs[0];//rand()%5];
252                                         g_Terrain.m_Patches[pj][pi].m_MiniPatches[tj][ti].Tex2 = NULL;//&g_BaseTexture[rand()%5];
253                                         g_Terrain.m_Patches[pj][pi].m_MiniPatches[tj][ti].m_AlphaMap = 0;//&g_TransitionTexture[rand()%5];
254                                 }
255                         }
256                 }
257         }
258
259         g_Camera.SetProjection (1, 1000, DEGTORAD(20));
260         g_Camera.m_Orientation.SetXRotation(DEGTORAD(30));
261         g_Camera.m_Orientation.RotateY(DEGTORAD(-45));
262
263         g_Camera.m_Orientation.Translate (100, 150, -100);
264
265         SelPX = SelPY = SelTX = SelTY = 0;
266 }
267
268 void InitResources()
269 {
270         int i;
271         char* base_fns[] =
272         {
273         "Base1.bmp",
274         "Base2.bmp",
275         "Base3.bmp",
276         "Base4.bmp",
277         "Base5.bmp"
278         };
279
280         for(i = 0; i < 5; i++)
281         {
282                 BaseTexs[i] = tex_load(base_fns[i]);
283                 tex_upload(BaseTexs[i], GL_LINEAR_MIPMAP_LINEAR);
284         }
285
286
287 int cnt;
288 #if 1
289
290         char* fns[NUM_ALPHA_MAPS] = {
291 "blendcircle.raw",
292 "blendcorner.raw",
293 "blendedge.raw",
294 "blendedgecorner.raw",
295 "blendedgetwocorners.raw",
296 "blendfourcorners.raw",
297 "blendlshape.raw",
298 "blendlshapecorner.raw",
299 "blendthreecorners.raw",
300 "blendtwocorners.raw",
301 "blendtwoedges.raw",
302 "blendtwooppositecorners.raw",
303 "blendushape.raw"
304         };
305 /*
306 //for(i = 0; i < NUM_ALPHA_MAPS;i++)
307 i=5;
308 {
309 FILE* f = fopen(fns[i],"rb");
310 u8 buf[5000],buf2[5000];
311 fread(buf,5000,1,f);
312 fclose(f);
313 for(int j = 0; j < 1024; j++)
314 buf2[2*j] = buf2[2*j+1] = buf[j];
315 f=fopen(fns[i],"wb");
316 fwrite(buf2,2048,1,f);
317 fclose(f);
318 }
319 /**/
320 cnt=13;
321 #else
322
323         char* fns[NUM_ALPHA_MAPS] = {
324 "Transition1.bmp",
325 "Transition2.bmp",
326 "Transition3.bmp",
327 "Transition4.bmp",
328 "Transition5.bmp",
329         };
330 cnt=5;
331 #endif
332
333 for(i = 0; i < cnt; i++)
334 {
335         AlphaMaps[i] = tex_load(fns[i]);
336         tex_upload(AlphaMaps[i], GL_LINEAR, GL_INTENSITY4);
337 }
338
339 }
340