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 <vector>
24 
25 #include "GLModel.h"
26 #include "IGLDevice.h"
27 #include <Core/VoxelModel.h>
28 
29 namespace spades {
30 	namespace draw {
31 		class GLRenderer;
32 		class GLProgram;
33 		class GLImage;
34 		class GLVoxelModel : public GLModel {
35 			struct Vertex {
36 				uint8_t x, y, z;
37 				uint8_t aoID;
38 
39 				// texture coord
40 				uint16_t u, v;
41 
42 				// color
43 				uint8_t red, green, blue;
44 				uint8_t diffuse;
45 
46 				// normal
47 				uint8_t nx, ny, nz;
48 			};
49 
50 			GLRenderer *renderer;
51 			IGLDevice *device;
52 			GLProgram *program;
53 			GLProgram *dlightProgram;
54 			GLProgram *shadowMapProgram;
55 			GLImage *aoImage;
56 
57 			IGLDevice::UInteger buffer;
58 			IGLDevice::UInteger idxBuffer;
59 			std::vector<Vertex> vertices;
60 			std::vector<uint32_t> indices;
61 			unsigned int numIndices;
62 
63 			Vector3 origin;
64 			float radius;
65 
66 			AABB3 boundingBox;
67 
68 			uint8_t calcAOID(VoxelModel *, int x, int y, int z, int ux, int uy, int uz, int vx,
69 			                 int vy, int vz);
70 			void EmitFace(VoxelModel *, int x, int y, int z, int nx, int ny, int nz,
71 			              uint32_t color);
72 			void BuildVertices(VoxelModel *);
73 
74 		protected:
75 			~GLVoxelModel();
76 
77 		public:
78 			GLVoxelModel(VoxelModel *, GLRenderer *r);
79 
80 			static void PreloadShaders(GLRenderer *);
81 
82 			void Prerender(std::vector<client::ModelRenderParam> params) override;
83 
84 			void RenderShadowMapPass(std::vector<client::ModelRenderParam> params) override;
85 
86 			void RenderSunlightPass(std::vector<client::ModelRenderParam> params) override;
87 
88 			void RenderDynamicLightPass(std::vector<client::ModelRenderParam> params,
89 			                            std::vector<GLDynamicLight> lights) override;
90 
GetBoundingBox()91 			AABB3 GetBoundingBox() override { return boundingBox; }
92 		};
93 	}
94 }
95