1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the AUTHORS
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * This file is based on, or a modified version of code from TinyGL (C) 1997-1998 Fabrice Bellard,
25  * which is licensed under the zlib-license (see LICENSE).
26  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
27  */
28 
29 #ifndef GRAPHICS_TINYGL_ZRECT_H_
30 #define GRAPHICS_TINYGL_ZRECT_H_
31 
32 #include "common/rect.h"
33 #include "graphics/tinygl/zblit.h"
34 #include "common/array.h"
35 
36 namespace TinyGL {
37 	struct GLContext;
38 	struct GLVertex;
39 	struct GLTexture;
40 }
41 
42 namespace Internal {
43 	void *allocateFrame(int size);
44 }
45 
46 namespace Graphics {
47 
48 class DrawCall {
49 public:
50 
51 	enum DrawCallType {
52 		DrawCall_Rasterization,
53 		DrawCall_Blitting,
54 		DrawCall_Clear
55 	};
56 
DrawCall(DrawCallType type)57 	DrawCall(DrawCallType type) : _type(type) { }
~DrawCall()58 	virtual ~DrawCall() { }
59 	bool operator==(const DrawCall &other) const;
60 	bool operator!=(const DrawCall &other) const {
61 		return !(*this == other);
62 	}
63 	virtual void execute(bool restoreState) const = 0;
64 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const = 0;
getType()65 	DrawCallType getType() const { return _type; }
getDirtyRegion()66 	virtual const Common::Rect getDirtyRegion() const { return _dirtyRegion; }
67 protected:
68 	Common::Rect _dirtyRegion;
69 private:
70 	DrawCallType _type;
71 };
72 
73 class ClearBufferDrawCall : public DrawCall {
74 public:
75 	ClearBufferDrawCall(bool clearZBuffer, int zValue, bool clearColorBuffer, int rValue, int gValue, int bValue);
~ClearBufferDrawCall()76 	virtual ~ClearBufferDrawCall() { }
77 	bool operator==(const ClearBufferDrawCall &other) const;
78 	virtual void execute(bool restoreState) const;
79 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const;
80 
new(size_t size)81 	void *operator new(size_t size) {
82 		return ::Internal::allocateFrame(size);
83 	}
84 
delete(void * p)85 	void operator delete(void *p) { }
86 private:
87 	bool _clearZBuffer, _clearColorBuffer;
88 	int _rValue, _gValue, _bValue, _zValue;
89 };
90 
91 // Encapsulate a rasterization call: it might execute either a triangle or line rasterization.
92 class RasterizationDrawCall : public DrawCall {
93 public:
94 	RasterizationDrawCall();
~RasterizationDrawCall()95 	virtual ~RasterizationDrawCall() { }
96 	bool operator==(const RasterizationDrawCall &other) const;
97 	virtual void execute(bool restoreState) const;
98 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const;
99 
new(size_t size)100 	void *operator new(size_t size) {
101 		return ::Internal::allocateFrame(size);
102 	}
103 
delete(void * p)104 	void operator delete(void *p) { }
105 private:
106 	void computeDirtyRegion();
107 	typedef void (*gl_draw_triangle_func_ptr)(TinyGL::GLContext *c, TinyGL::GLVertex *p0, TinyGL::GLVertex *p1, TinyGL::GLVertex *p2);
108 	int _vertexCount;
109 	TinyGL::GLVertex *_vertex;
110 	gl_draw_triangle_func_ptr _drawTriangleFront, _drawTriangleBack;
111 
112 	struct RasterizationState {
113 		int beginType;
114 		int currentFrontFace;
115 		int cullFaceEnabled;
116 		int colorMask;
117 		int depthTest;
118 		int depthFunction;
119 		int depthWrite;
120 		int shadowMode;
121 		int texture2DEnabled;
122 		int currentShadeModel;
123 		int polygonModeBack;
124 		int polygonModeFront;
125 		int lightingEnabled;
126 		bool enableBlending;
127 		int sfactor, dfactor;
128 		int textureVersion;
129 		int depthTestEnabled;
130 		float viewportTranslation[3];
131 		float viewportScaling[3];
132 		bool alphaTest;
133 		int alphaFunc, alphaRefValue;
134 		TinyGL::GLTexture *texture;
135 		unsigned int wrapS, wrapT;
136 		unsigned char *shadowMaskBuf;
137 
138 		bool operator==(const RasterizationState &other) const;
139 	};
140 
141 	RasterizationState _state;
142 
143 	RasterizationState captureState() const;
144 	void applyState(const RasterizationState &state) const;
145 };
146 
147 // Encapsulate a blit call: it might execute either a color buffer or z buffer blit.
148 class BlittingDrawCall : public DrawCall {
149 public:
150 	enum BlittingMode {
151 		BlitMode_Regular,
152 		BlitMode_NoBlend,
153 		BlitMode_Fast,
154 		BlitMode_ZBuffer
155 	};
156 
157 	BlittingDrawCall(BlitImage *image, const BlitTransform &transform, BlittingMode blittingMode);
158 	virtual ~BlittingDrawCall();
159 	bool operator==(const BlittingDrawCall &other) const;
160 	virtual void execute(bool restoreState) const;
161 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const;
162 
getBlittingMode()163 	BlittingMode getBlittingMode() const { return _mode; }
164 
new(size_t size)165 	void *operator new(size_t size) {
166 		return ::Internal::allocateFrame(size);
167 	}
168 
delete(void * p)169 	void operator delete(void *p) { }
170 private:
171 	void computeDirtyRegion();
172 	BlitImage *_image;
173 	BlitTransform _transform;
174 	BlittingMode _mode;
175 	int _imageVersion;
176 
177 	struct BlittingState {
178 		bool enableBlending;
179 		int sfactor, dfactor;
180 		bool alphaTest;
181 		int alphaFunc, alphaRefValue;
182 		int depthTestEnabled;
183 
184 		bool operator==(const BlittingState &other) const {
185 			return	enableBlending == other.enableBlending &&
186 					sfactor == other.sfactor &&
187 					dfactor == other.dfactor &&
188 					alphaTest == other.alphaTest &&
189 					alphaFunc == other.alphaFunc &&
190 					alphaRefValue == other.alphaRefValue &&
191 					depthTestEnabled == other.depthTestEnabled;
192 		}
193 	};
194 
195 	BlittingState captureState() const;
196 	void applyState(const BlittingState &state) const;
197 
198 	BlittingState _blitState;
199 };
200 
201 } // end of namespace Graphics
202 
203 #endif
204