1 /*
2  Copyright (c) 2013 yvt
3 
4  This file is part of OpenSpades.
5 
6  OpenSpades 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 3 of the License, or
9  (at your option) any later version.
10 
11  OpenSpades is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OpenSpades.  If not, see <http://www.gnu.org/licenses/>.
18 
19  */
20 
21 #pragma once
22 
23 #include <Client/IGameMapListener.h>
24 #include <Client/IRenderer.h>
25 #include <Core/Math.h>
26 #include "GLDynamicLight.h"
27 #include "IGLDevice.h"
28 
29 namespace spades {
30 	namespace draw {
31 		class GLRenderer;
32 		class GLMapChunk;
33 		class GLProgram;
34 		class GLImage;
35 		class GLMapRenderer {
36 
37 			friend class GLMapChunk;
38 
39 		protected:
40 			GLRenderer *renderer;
41 			IGLDevice *device;
42 
43 			GLProgram *depthonlyProgram;
44 			GLProgram *basicProgram;
45 			GLProgram *dlightProgram;
46 			GLProgram *backfaceProgram;
47 			GLImage *aoImage;
48 
49 			IGLDevice::UInteger squareVertexBuffer;
50 
51 			struct ChunkRenderInfo {
52 				bool rendered;
53 				float distance;
54 			};
55 			GLMapChunk **chunks;
56 			ChunkRenderInfo *chunkInfos;
57 
58 			client::GameMap *gameMap;
59 
60 			int numChunkWidth, numChunkHeight;
61 			int numChunkDepth, numChunks;
62 
GetChunkIndex(int x,int y,int z)63 			inline int GetChunkIndex(int x, int y, int z) {
64 				return (x * numChunkHeight + y) * numChunkDepth + z;
65 			}
66 
GetChunk(int x,int y,int z)67 			inline GLMapChunk *GetChunk(int x, int y, int z) {
68 				return chunks[GetChunkIndex(x, y, z)];
69 			}
70 
71 			void RealizeChunks(Vector3 eye);
72 
73 			void DrawColumnDepth(int cx, int cy, int cz, Vector3 eye);
74 			void DrawColumnSunlight(int cx, int cy, int cz, Vector3 eye);
75 			void DrawColumnDLight(int cx, int cy, int cz, Vector3 eye,
76 			                      const std::vector<GLDynamicLight> &lights);
77 
78 			void RenderBackface();
79 
80 		public:
81 			GLMapRenderer(client::GameMap *, GLRenderer *);
82 			virtual ~GLMapRenderer();
83 
84 			static void PreloadShaders(GLRenderer *);
85 
86 			void GameMapChanged(int x, int y, int z, client::GameMap *);
87 
GetMap()88 			client::GameMap *GetMap() { return gameMap; }
89 
90 			void Realize();
91 			void Prerender();
92 			void RenderSunlightPass();
93 			void RenderDynamicLightPass(std::vector<GLDynamicLight> lights);
94 		};
95 	}
96 }
97