* some small cleanup
[LazOpenGLCore.git] / uglcTypes.pas
1 unit uglcTypes;
2
3 { Package:      OpenGLCore
4   Prefix:       glc - OpenGL Core
5   Beschreibung: diese Unit definiert Enum-Typen die OpenGL Konstanten wrappen und stellt zusätzlich
6                 Funktions-Wrapper zur verfügung die diese Enum-Typen als Parameter entgegen nehmen }
7
8 {$mode objfpc}{$H+}
9 {$MACRO ON}
10 {$SCOPEDENUMS ON}
11
12 interface
13
14 uses
15   dglOpenGL, sysutils;
16
17 type
18   TglcFace = (
19     faFront = GL_FRONT,
20     faBack  = GL_BACK,
21     faBoth  = GL_FRONT_AND_BACK);
22
23   TglcPolygonMode = (
24     pmPoint = GL_POINT,
25     pmLine  = GL_LINE,
26     pmFill  = GL_FILL);
27
28   TglcDepthFunc = (
29     dfNever         = GL_NEVER,
30     dfLess          = GL_LESS,
31     dfEqual         = GL_EQUAL,
32     dfLessEqual     = GL_LEQUAL,
33     dfGreater       = GL_GREATER,
34     dfNotEqual      = GL_NOTEQUAL,
35     dfGreaterEqual  = GL_GEQUAL,
36     dfAlways        = GL_ALWAYS);
37
38   TglcClearBuffer = (
39     cbDepthBuffer   = GL_DEPTH_BUFFER_BIT,
40     cbAccumBuffer   = GL_ACCUM_BUFFER_BIT,
41     cbStencilBuffer = GL_STENCIL_BUFFER_BIT,
42     cbColorBuffer   = GL_COLOR_BUFFER_BIT);
43
44   TglcTextureMinFilter = (
45     mfNearest              = GL_NEAREST,
46     mfLinear               = GL_LINEAR,
47     mfNearestMipmapNearest = GL_NEAREST_MIPMAP_NEAREST,
48     mfLinearMipmapNearest  = GL_LINEAR_MIPMAP_NEAREST,
49     mfNearestMipmapLinear  = GL_NEAREST_MIPMAP_LINEAR,
50     mfLinearMipmapLinear   = GL_LINEAR_MIPMAP_LINEAR);
51
52   TglcTextureMagFilter = (
53     mfNearest = GL_NEAREST,
54     mfLinear  = GL_LINEAR);
55
56   TglcTextureWrap = (
57     twClamp          = GL_CLAMP,
58     twRepeat         = GL_REPEAT,
59     twClampToBorder  = GL_CLAMP_TO_BORDER,
60     twClampToEdge    = GL_CLAMP_TO_EDGE,
61     twMirroredRepeat = GL_MIRRORED_REPEAT);
62
63   TglcBlendFactor = (
64     bfZero               = GL_ZERO,
65     bfOne                = GL_ONE,
66     bfSrcColor           = GL_SRC_COLOR,
67     bfOneMinusSrcColor   = GL_ONE_MINUS_SRC_COLOR,
68     bfSrcAlpha           = GL_SRC_ALPHA,
69     bfOneMinusSrcAlpha   = GL_ONE_MINUS_SRC_ALPHA,
70     bfDstAlpha           = GL_DST_ALPHA,
71     bfOneMinusDstAlpha   = GL_ONE_MINUS_DST_ALPHA,
72     bfDstColor           = GL_DST_COLOR,
73     bfOneMinusDstColor   = GL_ONE_MINUS_DST_COLOR,
74     bfSrcAlphaSaturate   = GL_SRC_ALPHA_SATURATE,
75     bgConstColor         = GL_CONSTANT_COLOR,
76     bfOneMinusConstColor = GL_ONE_MINUS_CONSTANT_COLOR,
77     bfConstAlpha         = GL_CONSTANT_ALPHA,
78     bfOneMinusConstAlpha = GL_ONE_MINUS_CONSTANT_ALPHA);
79
80   TglcBlendMode = (
81     bmNone,
82     bmAlphaBlend,
83     bmAdditiveAlphaBlend,
84     bmAdditiveBlend);
85
86   TglcFormat = (
87     fmUnknown           = 0,
88     fmColorIndex        = GL_COLOR_INDEX,
89     fmDepthComponent    = GL_DEPTH_COMPONENT,
90     fmRed               = GL_RED,
91     fmGreen             = GL_GREEN,
92     fmBlue              = GL_BLUE,
93     fmAlpha             = GL_ALPHA,
94     fmRGB               = GL_RGB,
95     fmRGBA              = GL_RGBA,
96     fmLuminance         = GL_LUMINANCE,
97     fmLuminanceAlpha    = GL_LUMINANCE_ALPHA,
98     fmBGR               = GL_BGR,
99     fmBGRA              = GL_BGRA,
100     fmDepthStencil      = GL_DEPTH_STENCIL);
101
102   TglcInternalFormat = (
103     ifUnknown                   = 0,
104     ifDepthComponent            = GL_DEPTH_COMPONENT,
105     ifAlpha                     = GL_ALPHA,
106     ifRGB                       = GL_RGB,
107     ifRGBA                      = GL_RGBA,
108     ifLuminance                 = GL_LUMINANCE,
109     ifLuminanceAlpha            = GL_LUMINANCE_ALPHA,
110     ifR3G3B2                    = GL_R3_G3_B2,
111     ifAlpha4                    = GL_ALPHA4,
112     ifAlpha8                    = GL_ALPHA8,
113     ifAlpha12                   = GL_ALPHA12,
114     ifAlpha16                   = GL_ALPHA16,
115     ifLuminance4                = GL_LUMINANCE4,
116     ifLuminance8                = GL_LUMINANCE8,
117     ifLuminance12               = GL_LUMINANCE12,
118     ifLuminance16               = GL_LUMINANCE16,
119     ifLuminance4Alpha4          = GL_LUMINANCE4_ALPHA4,
120     ifLuminance6Alpha2          = GL_LUMINANCE6_ALPHA2,
121     ifLuminance8Alpha8          = GL_LUMINANCE8_ALPHA8,
122     ifLuminance12Alpha4         = GL_LUMINANCE12_ALPHA4,
123     ifLuminance12Alpha12        = GL_LUMINANCE12_ALPHA12,
124     ifLuminance16Alpha16        = GL_LUMINANCE16_ALPHA16,
125     ifIntensity                 = GL_INTENSITY,
126     ifIntensity4                = GL_INTENSITY4,
127     ifIntensity8                = GL_INTENSITY8,
128     ifIntensity12               = GL_INTENSITY12,
129     ifIntensity16               = GL_INTENSITY16,
130     ifRGB4                      = GL_RGB4,
131     ifRGB5                      = GL_RGB5,
132     ifRGB8                      = GL_RGB8,
133     ifRGB10                     = GL_RGB10,
134     ifRGB12                     = GL_RGB12,
135     ifRGB16                     = GL_RGB16,
136     ifRGBA2                     = GL_RGBA2,
137     ifRGBA4                     = GL_RGBA4,
138     ifRGB5A1                    = GL_RGB5_A1,
139     ifRGBA8                     = GL_RGBA8,
140     ifRGB10A2                   = GL_RGB10_A2,
141     ifRGBA12                    = GL_RGBA12,
142     ifRGBA16                    = GL_RGBA16,
143     ifDepthComponent16          = GL_DEPTH_COMPONENT16,
144     ifDepthComponent24          = GL_DEPTH_COMPONENT24,
145     ifDepthComponent32          = GL_DEPTH_COMPONENT32,
146     ifCompressedAlpha           = GL_COMPRESSED_ALPHA,
147     ifCompressedLuminance       = GL_COMPRESSED_LUMINANCE,
148     ifCompressedLuminanceAlpha  = GL_COMPRESSED_LUMINANCE_ALPHA,
149     ifCompressedIntensity       = GL_COMPRESSED_INTENSITY,
150     ifCompressedRGB             = GL_COMPRESSED_RGB,
151     ifCompressedRGBA            = GL_COMPRESSED_RGBA,
152     ifRGBA32f                   = GL_RGBA32F,
153     ifRGB32f                    = GL_RGB32F,
154     ifRGBA16F                   = GL_RGBA16F,
155     ifRGB16F                    = GL_RGB16F,
156     ifDepth24Stencil8           = GL_DEPTH24_STENCIL8,
157     ifSRGB                      = GL_SRGB,
158     ifSRGB8                     = GL_SRGB8,
159     ifSRGBA                     = GL_SRGB_ALPHA,
160     ifSRGBA8                    = GL_SRGB8_ALPHA8,
161     ifSLuminanceAlpha           = GL_SLUMINANCE_ALPHA,
162     ifSLuminance8Alpha8         = GL_SLUMINANCE8_ALPHA8,
163     ifSLuminance                = GL_SLUMINANCE,
164     ifSLuminance8               = GL_SLUMINANCE8,
165     ifDepth32fStencil8          = GL_DEPTH32F_STENCIL8,
166     ifStencil1                  = GL_STENCIL_INDEX1,
167     ifStencil4                  = GL_STENCIL_INDEX4,
168     ifStencil8                  = GL_STENCIL_INDEX8,
169     ifStencil16                 = GL_STENCIL_INDEX16);
170
171   TglcAttachment = (
172     atDepthStencil = GL_DEPTH_STENCIL_ATTACHMENT,
173     atColor0       = GL_COLOR_ATTACHMENT0,
174     atColor1       = GL_COLOR_ATTACHMENT1,
175     atColor2       = GL_COLOR_ATTACHMENT2,
176     atColor3       = GL_COLOR_ATTACHMENT3,
177     atColor4       = GL_COLOR_ATTACHMENT4,
178     atColor5       = GL_COLOR_ATTACHMENT5,
179     atColor6       = GL_COLOR_ATTACHMENT6,
180     atColor7       = GL_COLOR_ATTACHMENT7,
181     atColor8       = GL_COLOR_ATTACHMENT8,
182     atColor9       = GL_COLOR_ATTACHMENT9,
183     atColor10      = GL_COLOR_ATTACHMENT10,
184     atColor11      = GL_COLOR_ATTACHMENT11,
185     atColor12      = GL_COLOR_ATTACHMENT12,
186     atColor13      = GL_COLOR_ATTACHMENT13,
187     atColor14      = GL_COLOR_ATTACHMENT14,
188     atColor15      = GL_COLOR_ATTACHMENT15,
189     atDepth        = GL_DEPTH_ATTACHMENT,
190     atStencil      = GL_STENCIL_ATTACHMENT);
191
192   TglcShaderType = (
193     stFragment        = GL_FRAGMENT_SHADER,
194     stVertex          = GL_VERTEX_SHADER,
195     stGeometry        = GL_GEOMETRY_SHADER,
196     stTessEvaluation  = GL_TESS_EVALUATION_SHADER,
197     stTessControl     = GL_TESS_CONTROL_SHADER);
198
199   TglcBufferTarget = (
200     btArrayBuffer        = GL_ARRAY_BUFFER,
201     btElementArrayBuffer = GL_ELEMENT_ARRAY_BUFFER);
202
203   TglcBufferUsage = (
204     buStreamDraw  = GL_STREAM_DRAW,
205     buStreamRead  = GL_STREAM_READ,
206     buStreamCopy  = GL_STREAM_COPY,
207     buStaticDraw  = GL_STATIC_DRAW,
208     buStaticRead  = GL_STATIC_READ,
209     buStaticCopy  = GL_STATIC_COPY,
210     buDynamicDraw = GL_DYNAMIC_DRAW,
211     buDynamicRead = GL_DYNAMIC_READ,
212     buDynamicCopy = GL_DYNAMIC_COPY);
213
214   TglcBufferAccess = (
215     baReadOnly  = GL_READ_ONLY,
216     baWriteOnly = GL_WRITE_ONLY,
217     baReadWrite = GL_READ_WRITE);
218
219   EOpenGL = class(Exception)
220   private
221     fErrorCode: GLenum;
222   public
223     property ErrorCode: GLenum read fErrorCode;
224     constructor Create(const aErrorCode: GLenum);
225     constructor Create(const aMsg: String; const aErrorCode: GLenum);
226   end;
227
228 procedure glcRenderFace(const aValue: TglcFace); inline;
229 procedure glcPolygonMode(const aFace: TglcFace; const aValue: TglcPolygonMode); inline;
230 procedure glcDepthFunc(const aValue: TglcDepthFunc); inline;
231 procedure glcBlendFunc(const aSource, aDest: TglcBlendFactor); inline; overload;
232 procedure glcBlendFunc(const aMode: TglcBlendMode); inline; overload;
233 procedure glcCheckAndRaiseError;
234
235 implementation
236
237 type
238   TglcBlendModeValue = packed record
239     src, dst: TglcBlendFactor;
240   end;
241
242 const
243   BLEND_MODE_VALUES: array[TglcBlendMode] of TglcBlendModeValue = (
244     (src: TglcBlendFactor.bfOne;         dst: TglcBlendFactor.bfZero),                        //bmNone
245     (src: TglcBlendFactor.bfSrcAlpha;    dst: TglcBlendFactor.bfOneMinusSrcAlpha),            //bmAlphaBlend
246     (src: TglcBlendFactor.bfSrcAlpha;    dst: TglcBlendFactor.bfOne),                         //bmAdditiveAlphaBlend
247     (src: TglcBlendFactor.bfOne;         dst: TglcBlendFactor.bfOne));                        //bmAdditiveBlend
248
249 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
250 procedure glcRenderFace(const aValue: TglcFace);
251 begin
252   case aValue of
253     TglcFace.faBoth: begin
254       glDisable(GL_CULL_FACE);
255     end;
256     TglcFace.faFront: begin
257       glEnable(GL_CULL_FACE);
258       glCullFace(GL_BACK);
259     end;
260     TglcFace.faBack: begin
261       glEnable(GL_CULL_FACE);
262       glCullFace(GL_FRONT);
263     end;
264   end;
265 end;
266
267 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
268 procedure glcPolygonMode(const aFace: TglcFace; const aValue: TglcPolygonMode);
269 begin
270   glPolygonMode(GLenum(aFace), GLenum(aValue));
271 end;
272
273 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
274 procedure glcDepthFunc(const aValue: TglcDepthFunc);
275 begin
276   glDepthFunc(GLenum(aValue));
277 end;
278
279 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
280 procedure glcBlendFunc(const aSource, aDest: TglcBlendFactor);
281 begin
282   glBlendFunc(GLenum(aSource), GLenum(aDest));
283 end;
284
285 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
286 procedure glcBlendFunc(const aMode: TglcBlendMode); overload;
287 begin
288   glBlendFunc(GLenum(BLEND_MODE_VALUES[aMode].src), GLenum(BLEND_MODE_VALUES[aMode].dst));
289 end;
290
291 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
292 procedure glcCheckAndRaiseError;
293 var
294   e: GLenum;
295 begin
296   e := glGetError();
297   if (e <> GL_NO_ERROR) then
298     raise EOpenGL.Create(e);
299 end;
300
301 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
302 //EOpenGL///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
303 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
304 constructor EOpenGL.Create(const aErrorCode: GLenum);
305 begin
306   fErrorCode := aErrorCode;
307   inherited Create(gluErrorString(fErrorCode));
308 end;
309
310 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
311 constructor EOpenGL.Create(const aMsg: String; const aErrorCode: GLenum);
312 begin
313   fErrorCode := aErrorCode;
314   inherited Create(aMsg + ': ' + gluErrorString(fErrorCode))
315 end;
316
317 end.
318