Initial revision
[0ad:0ad.git] / terrain / bak / 0 / Renderer.cpp
1 #include "Renderer.H"
2 #include "Matrix3D.H"
3 #include "Camera.H"
4
5 #include "ogl.h"
6 #include "tex.h"
7
8 #define         RENDER_STAGE_BASE               (1)
9 #define         RENDER_STAGE_TRANS              (2)
10
11 bool g_WireFrame = false;
12 unsigned int g_FrameCounter = 0;
13
14 CRenderer::CRenderer ()
15 {
16         m_Timer = 0;
17         m_CurrentSeason = 0;
18 }
19
20 CRenderer::~CRenderer ()
21 {
22 }
23
24         
25 bool CRenderer::Initialize (HWND hwnd, int width, int height, int depth)
26 {
27         m_Width = width;
28         m_Height = height;
29         m_Depth = depth;
30         return true;
31 }
32
33 void CRenderer::Shutdown ()
34 {
35
36 }
37
38 void CRenderer::RenderTerrain (CTerrain *terrain, CCamera *camera)
39 {
40 //      m_Timer += 0.001f;
41
42         if (m_Timer > 1.0f)
43         {
44                 m_Timer = 0;
45                 
46                 if (m_CurrentSeason == 0)
47                         m_CurrentSeason = 1;
48                 else
49                         m_CurrentSeason = 0;
50         }
51
52         CMatrix3D view = camera->m_Orientation.GetTranspose();
53         CMatrix3D proj = camera->GetProjection();
54
55         float gl_view[16] = {view._11, view._21, view._31, view._41,
56                                                  view._12, view._22, view._32, view._42,
57                                                  view._13, view._23, view._33, view._43,
58                                                  view._14, view._24, view._34, view._44};
59
60         float gl_proj[16] = {proj._11, proj._21, proj._31, proj._41,
61                                                  proj._12, proj._22, proj._32, proj._42,
62                                                  proj._13, proj._23, proj._33, proj._43,
63                                                  proj._14, proj._24, proj._34, proj._44};
64
65         glMatrixMode (GL_MODELVIEW);
66         glLoadMatrixf (gl_view);
67
68         glMatrixMode (GL_PROJECTION);
69         glLoadMatrixf (gl_proj);
70
71         SViewPort vp = camera->GetViewPort();
72         glViewport (vp.m_X, vp.m_Y, vp.m_Width, vp.m_Height);
73
74         if (g_WireFrame)
75                 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
76         else
77                 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
78
79
80         for (int j=0; j<NUM_PATCHES_PER_SIDE; j++)
81         {
82                 for (int i=0; i<NUM_PATCHES_PER_SIDE; i++)
83                 {
84                         if (camera->GetFustum().IsBoxVisible (CVector3D(0,0,0), terrain->m_Patches[j][i].m_Bounds))
85                                 terrain->m_Patches[j][i].m_LastVisFrame = g_FrameCounter;
86                 }
87         }
88
89         for (j=0; j<NUM_PATCHES_PER_SIDE; j++)
90         {
91                 for (int i=0; i<NUM_PATCHES_PER_SIDE; i++)
92                 {
93                         if (terrain->m_Patches[j][i].m_LastVisFrame == g_FrameCounter)
94                                 RenderPatchBase (&terrain->m_Patches[j][i]);
95                 }
96         }
97
98         for (j=0; j<NUM_PATCHES_PER_SIDE; j++)
99         {
100                 for (int i=0; i<NUM_PATCHES_PER_SIDE; i++)
101                 {
102                         if (terrain->m_Patches[j][i].m_LastVisFrame == g_FrameCounter)
103                                 RenderPatchTrans (&terrain->m_Patches[j][i]);
104                 }
105         }
106 }
107
108 void CRenderer::RenderPatchBase (CPatch *patch)
109 {
110         CMiniPatch *MPatch, *MPCurrent;
111
112         float StartU, StartV;
113         
114
115         for (int j=0; j<4; j++)
116         {
117                 for (int i=0; i<4; i++)
118                 {
119                         MPatch = &(patch->m_MiniPatches[j][i]);
120
121                         if (MPatch->m_LastRenderedFrame == g_FrameCounter)
122                                 continue;
123                         else
124                         {
125                                 MPatch->m_LastRenderedFrame = g_FrameCounter;
126                                 MPatch->m_RenderStage = RENDER_STAGE_BASE;
127                         }
128
129
130
131                         glActiveTexture (GL_TEXTURE0);
132                         glEnable(GL_TEXTURE_2D);
133
134 tex_bind(MPatch->Tex1);
135
136                         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
137                         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
138
139 /////////////////////////////////////
140                         glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
141
142                         glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
143                         glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
144                         glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
145                         glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR);
146                         glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
147                                 
148                         glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
149                         glTexEnvf (GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
150                         glTexEnvf (GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
151 /////////////////////////////////////
152
153                         StartU = 0.5f * (float)(i%2);
154                         StartV = 0.5f * (float)(j%2);
155
156                         float tu[2], tv[2];
157                         tu[0] = tu[1] = StartU;
158                         tv[0] = StartV+0.125f;
159                         tv[1] = StartV;
160
161                         for (int row=0; row<4; row++)
162                         {
163                                 MPCurrent = MPatch;
164                                 glBegin (GL_TRIANGLE_STRIP);
165
166                                 int start = 0;
167
168                                 while (MPCurrent)
169                                 {
170                                         bool ExitFlag;
171
172                                         if (!MPCurrent->m_pRightNeighbor)
173                                                 ExitFlag = true;
174                                         else
175                                         {
176                                                 if (MPCurrent->m_pRightNeighbor->Tex1 == MPCurrent->Tex1 &&
177                                                         MPCurrent->m_pRightNeighbor->m_pParrent->m_LastVisFrame == g_FrameCounter)
178                                                         ExitFlag = false;
179                                                 else
180                                                         ExitFlag = true;
181                                         }
182
183                                         for (int x=start; x<5; x++)
184                                         {
185                                                 int v1 = ((row+1)*MAP_SIZE) + x;
186                                                 int v2 = (row*MAP_SIZE) + x;
187
188                                                 float factor = m_Timer;
189                                                 if (m_CurrentSeason == 1)
190                                                         factor = 1.0f - factor;
191
192                                                 float color1[3] = {MPCurrent->m_pVertices[v1].m_Color[0][0]*factor + MPCurrent->m_pVertices[v1].m_Color[1][0]*(1.0f-factor),
193                                                                                    MPCurrent->m_pVertices[v1].m_Color[0][1]*factor + MPCurrent->m_pVertices[v1].m_Color[1][1]*(1.0f-factor),
194                                                                                    MPCurrent->m_pVertices[v1].m_Color[0][2]*factor + MPCurrent->m_pVertices[v1].m_Color[1][2]*(1.0f-factor)};
195
196                                                 float color2[3] = {MPCurrent->m_pVertices[v2].m_Color[0][0]*factor + MPCurrent->m_pVertices[v2].m_Color[1][0]*(1.0f-factor),
197                                                                                    MPCurrent->m_pVertices[v2].m_Color[0][1]*factor + MPCurrent->m_pVertices[v2].m_Color[1][1]*(1.0f-factor),
198                                                                                    MPCurrent->m_pVertices[v2].m_Color[0][2]*factor + MPCurrent->m_pVertices[v2].m_Color[1][2]*(1.0f-factor)};
199
200                                                 glTexCoord2f (tu[0], tv[0]);
201
202                                                 if (g_HillShading)
203                                                         glColor3f (color1[0],color1[1],color1[2]);
204                                                 else
205                                                         glColor3f (1,1,1);
206
207                                                 glVertex3f (MPCurrent->m_pVertices[v1].m_Position.X,
208                                                                         MPCurrent->m_pVertices[v1].m_Position.Y,
209                                                                         MPCurrent->m_pVertices[v1].m_Position.Z);
210
211                                                 glTexCoord2f (tu[1], tv[1]);
212
213                                                 if (g_HillShading)
214                                                         glColor3f (color2[0],color2[1],color2[2]);
215                                                 else
216                                                         glColor3f (1,1,1);
217                                         
218                                                 glVertex3f (MPCurrent->m_pVertices[v2].m_Position.X,
219                                                                         MPCurrent->m_pVertices[v2].m_Position.Y,
220                                                                         MPCurrent->m_pVertices[v2].m_Position.Z);
221
222                                                 tu[0]+=0.125f;
223                                                 tu[1]+=0.125f;
224                                         }
225
226                                         if (ExitFlag)
227                                                 break;
228
229                                         MPCurrent = MPCurrent->m_pRightNeighbor;
230                                         MPCurrent->m_LastRenderedFrame = g_FrameCounter;
231                                         MPCurrent->m_RenderStage = RENDER_STAGE_BASE;
232                                         start = 1;
233                                 }
234
235                                 tv[0]+=0.125f;
236                                 tv[1]+=0.125f;
237         
238                                 tu[0] = StartU;
239                                 tu[1] = StartU;
240
241                                 glEnd ();
242                         }
243                 }
244         }
245 }
246
247 void CRenderer::RenderPatchTrans (CPatch *patch)
248 {
249         CMiniPatch *MPatch, *MPCurrent;
250
251         float StartU, StartV;
252         
253         glEnable (GL_BLEND);
254         glDepthFunc (GL_EQUAL);
255         glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
256
257         for (int j=0; j<4; j++)
258         {
259                 for (int i=0; i<4; i++)
260                 {
261                         MPatch = &(patch->m_MiniPatches[j][i]);
262
263                         if (MPatch->m_LastRenderedFrame == g_FrameCounter &&
264                                 MPatch->m_RenderStage == RENDER_STAGE_TRANS)
265                                 continue;
266                         else
267                         {
268                                 MPatch->m_LastRenderedFrame = g_FrameCounter;
269                                 MPatch->m_RenderStage = RENDER_STAGE_TRANS;
270                         }
271
272                         //now for transition
273                         if (MPatch->Tex2 && MPatch->m_AlphaMap)
274                         {
275
276                                 glActiveTexture (GL_TEXTURE0);
277                                 glEnable(GL_TEXTURE_2D);
278
279 tex_bind(MPatch->Tex2);
280
281                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
282                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
283
284 /////////////////////////////////////
285                                 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
286
287                                 glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
288                                 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
289                                 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
290                                 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR);
291                                 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
292                                         
293                                 glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
294                                 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
295                                 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
296 /////////////////////////////////////
297                                 
298
299                                 glActiveTexture (GL_TEXTURE1);
300                                 glEnable(GL_TEXTURE_2D);
301 tex_bind(MPatch->m_AlphaMap);                           
302                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
303                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
304 /////////////////////////////////////
305                                 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
306
307                                 glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
308                                 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS);
309                                 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
310                                 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE);
311                                 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
312                                 
313                                 glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
314                                 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
315                                 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
316 /////////////////////////////////////
317
318                                 StartU = 0.5f * (float)(i%2);
319                                 StartV = 0.5f * (float)(j%2);
320
321                                 float tu[2], tv[2];
322                                 tu[0] = tu[1] = StartU;
323                                 tv[0] = StartV+0.125f;
324                                 tv[1] = StartV;
325                                 
326                                 for (int row=0; row<4; row++)
327                                 {
328                                         glBegin (GL_TRIANGLE_STRIP);
329                                         MPCurrent = MPatch;
330
331                                         while (MPCurrent)
332                                         {
333                                                 int count;
334                                                 bool ExitFlag;
335
336                                                 if (!MPCurrent->m_pRightNeighbor)
337                                                 {
338                                                         count = 5;
339                                                         ExitFlag = true;
340                                                 }
341                                                 else
342                                                 {
343                                                         if (MPCurrent->m_pRightNeighbor->Tex2 == MPCurrent->Tex2 &&
344                                                             MPCurrent->m_pRightNeighbor->m_AlphaMap == MPCurrent->m_AlphaMap &&
345                                                                 MPCurrent->m_pRightNeighbor->m_pParrent->m_LastVisFrame == g_FrameCounter)
346                                                         {
347                                                                 count = 4;
348                                                                 ExitFlag = false;
349                                                         }
350                                                         else
351                                                         {
352                                                                 count = 5;
353                                                                 ExitFlag = true;
354                                                         }
355                                                 }
356
357                                                 for (int x=0; x<count; x++)
358                                                 {
359                                                         int v1 = ((row+1)*MAP_SIZE) + x;
360                                                         int v2 = (row*MAP_SIZE) + x;
361
362                                                         float factor = m_Timer;
363                                                         if (m_CurrentSeason == 1)
364                                                                 factor = 1.0f - factor;
365                                                         
366                                                         float color1[3] = {MPCurrent->m_pVertices[v1].m_Color[0][0]*factor + MPCurrent->m_pVertices[v1].m_Color[1][0]*(1.0f-factor),
367                                                                                            MPCurrent->m_pVertices[v1].m_Color[0][1]*factor + MPCurrent->m_pVertices[v1].m_Color[1][1]*(1.0f-factor),
368                                                                                            MPCurrent->m_pVertices[v1].m_Color[0][2]*factor + MPCurrent->m_pVertices[v1].m_Color[1][2]*(1.0f-factor)};
369
370                                                         float color2[3] = {MPCurrent->m_pVertices[v2].m_Color[0][0]*factor + MPCurrent->m_pVertices[v2].m_Color[1][0]*(1.0f-factor),
371                                                                                            MPCurrent->m_pVertices[v2].m_Color[0][1]*factor + MPCurrent->m_pVertices[v2].m_Color[1][1]*(1.0f-factor),
372                                                                                            MPCurrent->m_pVertices[v2].m_Color[0][2]*factor + MPCurrent->m_pVertices[v2].m_Color[1][2]*(1.0f-factor)};
373
374                                                         glMultiTexCoord2f (GL_TEXTURE0_ARB, tu[0], tv[0]);
375                                                         glMultiTexCoord2f (GL_TEXTURE1_ARB, tu[0]*2, tv[0]*2);
376                                                 
377                                                         if (g_HillShading)
378                                                                 glColor3f (color1[0],color1[1],color1[2]);
379                                                         else
380                                                                 glColor3f (1,1,1);
381
382                                                         glVertex3f (MPCurrent->m_pVertices[v1].m_Position.X,
383                                                                                 MPCurrent->m_pVertices[v1].m_Position.Y,
384                                                                                 MPCurrent->m_pVertices[v1].m_Position.Z);
385
386                                                         glMultiTexCoord2f (GL_TEXTURE0_ARB, tu[1], tv[1]);
387                                                         glMultiTexCoord2f (GL_TEXTURE1_ARB, tu[1]*2, tv[1]*2);
388                                                 
389                                                         if (g_HillShading)
390                                                                 glColor3f (color2[0],color2[1],color2[2]);
391                                                         else
392                                                                 glColor3f (1,1,1);
393                                                 
394                                                         glVertex3f (MPCurrent->m_pVertices[v2].m_Position.X,
395                                                                                 MPCurrent->m_pVertices[v2].m_Position.Y,
396                                                                                 MPCurrent->m_pVertices[v2].m_Position.Z);
397
398                                                         tu[0]+=0.125f;
399                                                         tu[1]+=0.125f;
400                                                 }
401
402                                                 if (ExitFlag)
403                                                         break;
404
405                                                 MPCurrent = MPCurrent->m_pRightNeighbor;
406                                                 MPCurrent->m_LastRenderedFrame = g_FrameCounter;
407                                                 MPCurrent->m_RenderStage = RENDER_STAGE_TRANS;
408                                         }
409
410                                         tv[0]+=0.125f;
411                                         tv[1]+=0.125f;
412                                         tu[0] = tu[1] = StartU;
413
414                                         glEnd ();
415                                 }
416                         }
417                 }
418         }
419
420         glDepthFunc (GL_LEQUAL);
421         glDisable (GL_BLEND);
422         glActiveTexture (GL_TEXTURE1);
423 glDisable(GL_TEXTURE_2D);
424 }
425
426 void CRenderer::RenderTileOutline (CMiniPatch *mpatch)
427 {
428         glActiveTexture (GL_TEXTURE0);
429         glDisable (GL_DEPTH_TEST);
430         glDisable (GL_TEXTURE_2D);
431         glLineWidth (4);
432
433         STerrainVertex V[4];
434         V[0] = mpatch->m_pVertices[0];
435         V[1] = mpatch->m_pVertices[4];
436         V[2] = mpatch->m_pVertices[MAP_SIZE*4 + 4];
437         V[3] = mpatch->m_pVertices[MAP_SIZE*4];
438         
439         glBegin (GL_LINE_STRIP);
440
441                 glColor3f (0,1.0f,0);
442                 
443                 glVertex3f (V[0].m_Position.X, V[0].m_Position.Y, V[0].m_Position.Z);
444                 glVertex3f (V[1].m_Position.X, V[1].m_Position.Y, V[1].m_Position.Z);
445                 glVertex3f (V[2].m_Position.X, V[2].m_Position.Y, V[2].m_Position.Z);
446                 glVertex3f (V[3].m_Position.X, V[3].m_Position.Y, V[3].m_Position.Z);
447                 glVertex3f (V[0].m_Position.X, V[0].m_Position.Y, V[0].m_Position.Z);
448
449         glEnd ();
450
451         glEnable (GL_DEPTH_TEST);
452         glEnable (GL_TEXTURE_2D);
453 }