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 char *shadowMaskBuf;
136 
137 		bool operator==(const RasterizationState &other) const;
138 	};
139 
140 	RasterizationState _state;
141 
142 	RasterizationState captureState() const;
143 	void applyState(const RasterizationState &state) const;
144 };
145 
146 // Encapsulate a blit call: it might execute either a color buffer or z buffer blit.
147 class BlittingDrawCall : public DrawCall {
148 public:
149 	enum BlittingMode {
150 		BlitMode_Regular,
151 		BlitMode_NoBlend,
152 		BlitMode_Fast,
153 		BlitMode_ZBuffer
154 	};
155 
156 	BlittingDrawCall(BlitImage *image, const BlitTransform &transform, BlittingMode blittingMode);
157 	virtual ~BlittingDrawCall();
158 	bool operator==(const BlittingDrawCall &other) const;
159 	virtual void execute(bool restoreState) const;
160 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const;
161 
getBlittingMode()162 	BlittingMode getBlittingMode() const { return _mode; }
163 
new(size_t size)164 	void *operator new(size_t size) {
165 		return ::Internal::allocateFrame(size);
166 	}
167 
delete(void * p)168 	void operator delete(void *p) { }
169 private:
170 	void computeDirtyRegion();
171 	BlitImage *_image;
172 	BlitTransform _transform;
173 	BlittingMode _mode;
174 	int _imageVersion;
175 
176 	struct BlittingState {
177 		bool enableBlending;
178 		int sfactor, dfactor;
179 		bool alphaTest;
180 		int alphaFunc, alphaRefValue;
181 		int depthTestEnabled;
182 
183 		bool operator==(const BlittingState &other) const {
184 			return	enableBlending == other.enableBlending &&
185 					sfactor == other.sfactor &&
186 					dfactor == other.dfactor &&
187 					alphaTest == other.alphaTest &&
188 					alphaFunc == other.alphaFunc &&
189 					alphaRefValue == other.alphaRefValue &&
190 					depthTestEnabled == other.depthTestEnabled;
191 		}
192 	};
193 
194 	BlittingState captureState() const;
195 	void applyState(const BlittingState &state) const;
196 
197 	BlittingState _blitState;
198 };
199 
200 } // end of namespace Graphics
201 
202 #endif
203