File: GrassDrawer.h

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (104 lines) | stat: -rw-r--r-- 2,532 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef GRASSDRAWER_H
#define GRASSDRAWER_H

#include <array>
#include <vector>

#include "Rendering/GL/RenderDataBuffer.hpp"
#include "System/float3.h"
#include "System/type2.h"
#include "System/EventClient.h"

namespace Shader {
	struct IProgramObject;
}

class CCamera;
class CGrassDrawer : public CEventClient
{
public:
	CGrassDrawer();
	~CGrassDrawer();

	void Draw();
	void DrawShadow();
	void AddGrass(const float3& pos);
	void RemoveGrass(const float3& pos);
	uint8_t GetGrass(const float3& pos);

	float IncrDrawDistance();
	float DecrDrawDistance();
	void ChangeDetail(int detail);

	/// @see ConfigHandler::ConfigNotifyCallback
	void ConfigNotify(const std::string& key, const std::string& value);

	void HandleAction(int arg) {
		switch (arg) {
			case -1: { defDrawGrass = !defDrawGrass; luaDrawGrass = !luaDrawGrass; } break;
			case  0: { defDrawGrass =         false; luaDrawGrass =         false; } break;
			case  1: { defDrawGrass =          true; luaDrawGrass =         false; } break;
			case  2: { defDrawGrass =         false; luaDrawGrass =          true; } break;
			default: {                                                             } break;
		}
	}

	bool DefDrawGrass() const { return defDrawGrass; }
	bool LuaDrawGrass() const { return luaDrawGrass; }

public:
	// EventClient
	void UnsyncedHeightMapUpdate(const SRectangle& rect) override {}
	void Update() override;

protected:
	enum GrassShaderProgram {
		GRASS_PROGRAM_OPAQUE = 0,
		GRASS_PROGRAM_SHADOW = 1,
		GRASS_PROGRAM_CURR   = 2,
		GRASS_PROGRAM_LAST   = 3,
	};

	bool LoadGrassShaders();
	void CreateGrassBladeTex(uint8_t* buf);
	void CreateGrassBuffer();

	void EnableShader(const GrassShaderProgram type);
	void SetupStateOpaque();
	void ResetStateOpaque();
	void SetupStateShadow();
	void ResetStateShadow();

	unsigned int DrawBlock(const float3& camPos, const int2& blockPos, unsigned int turfMatIndex);
	void DrawBlocks(const CCamera* cam);

protected:
	int2 blockCount;
	int2 turfDetail;

	unsigned int grassBladeTex = 0;

	std::vector<int2> grassBlocks;
	std::vector<uint8_t> grassMap;

	std::array<Shader::IProgramObject*, GRASS_PROGRAM_LAST> grassShaders;

	GL::RenderDataBuffer grassBuffer;

	float grassDrawDist;
	float maxDetailedDist;

	float3 prvUpdateCamPos;
	float3 prvUpdateCamDir;

	bool luaDrawGrass = false;
	bool defDrawGrass = false;
	bool updateVisibility = false;
};

extern CGrassDrawer* grassDrawer;


#endif /* GRASSDRAWER_H */