File: MatrixStateTracker.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 (165 lines) | stat: -rw-r--r-- 3,692 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef GL_MATRIX_STATE_TRACKER_H
#define GL_MATRIX_STATE_TRACKER_H

#include "Rendering/GL/myGL.h"
#include "System/Log/ILog.h"

struct SMatrixStateData {
	SMatrixStateData(): mode(GL_MODELVIEW), 
						modelView(0), 
						projection(0),
						texture(0) {}
	int mode;
	int modelView;
	int projection;
	int texture;
};

struct GLMatrixStateTracker {
public:
	SMatrixStateData matrixData; // [>0] = stack depth for mode, [0] = matrix mode
	bool listMode; // if creating display list

public:
	GLMatrixStateTracker() : listMode(false) {}

	SMatrixStateData PushMatrixState() {
		SMatrixStateData md;
		std::swap(matrixData, md);
		return md;
	}

	SMatrixStateData PushMatrixState(bool lm) {
		listMode = lm;
		return PushMatrixState();
	}

	void PopMatrixState(SMatrixStateData& md) {
		std::swap(matrixData, md);
	}

	void PopMatrixState(SMatrixStateData& md, bool lm) {
		listMode = lm;
		PopMatrixState(md);
	}

	unsigned int GetMode() const {
		return matrixData.mode;
	}

	int& GetDepth(unsigned int mode) {
		switch (mode) {
			case GL_MODELVIEW: return matrixData.modelView;
			case GL_PROJECTION: return matrixData.projection;
			case GL_TEXTURE: return matrixData.texture;
			default:
				LOG_L(L_ERROR, "unknown matrix mode = %u", mode);
				abort();
				break;
		}
	}

	bool PushMatrix() {
		unsigned int mode = GetMode();
		int& depth = GetDepth(mode);
		if (!listMode && depth >= 255)
			return false;
		depth += 1;
		return true;
	}

	bool PopMatrix() {
		unsigned int mode = GetMode();
		int& depth = GetDepth(mode);
		if (listMode) {
			depth -= 1;
			return true;
		}
		if (depth == 0)
			return false;
		depth -= 1;
		return true;
	}

	bool SetMatrixMode(int mode) {
		if (mode == GL_MODELVIEW || mode == GL_PROJECTION || mode == GL_TEXTURE) {
			matrixData.mode = mode;
			return true;
		}
		return false;
	}


	int ApplyMatrixState(SMatrixStateData& m) {
		// validate
#define VALIDATE(modeName) \
		{ \
			int newDepth = m.modeName + matrixData.modeName; \
			if (newDepth < 0) \
				return -1; \
			if (newDepth >= 255) \
				return 1; \
		}

		VALIDATE(modelView)
		VALIDATE(projection)
		VALIDATE(texture)

#undef VALIDATE

		// apply
		matrixData.mode = m.mode;
		matrixData.modelView += m.modelView;
		matrixData.projection += m.projection;
		matrixData.texture += m.texture;

		return 0;
	}

	const SMatrixStateData& GetMatrixState() const {
		return matrixData;
	}

	bool HasMatrixStateError() const {
		return matrixData.mode != GL_MODELVIEW ||
			matrixData.modelView != 0 ||
			matrixData.projection != 0 ||
			matrixData.texture != 0;
	}

	void HandleMatrixStateError(int error, const char* errsrc) {
		unsigned int mode = GetMode();

		// dont complain about stack/mode issues if some other error occurred
		// check if the lua code did not restore the matrix mode
		if (error == 0 && mode != GL_MODELVIEW)
			LOG_L(L_ERROR, "%s: OpenGL state check error, matrix mode = %d, please restore mode to GL.MODELVIEW before end", errsrc, mode);


#define CHECK_MODE(modeName, glMode) \
		assert(matrixData.modeName >= 0); \
		if (matrixData.modeName != 0) {\
			if (error == 0){ \
				LOG_L(L_ERROR, "%s: OpenGL stack check error, matrix mode = %s, depth = %d, please make sure to pop all matrices before end", errsrc, #glMode, matrixData.modeName); \
			} \
			GL::MatrixMode(glMode); \
			for (int p = 0; p < matrixData.modeName; ++p) { \
				GL::PopMatrix(); \
			} \
			matrixData.modeName = 0;\
		}

		CHECK_MODE(modelView, GL_MODELVIEW)
		CHECK_MODE(projection, GL_PROJECTION)
		CHECK_MODE(texture, GL_TEXTURE)

#undef CHECK_MODE

		GL::MatrixMode(GL_MODELVIEW);
	}
};

#endif