CgNet / CgNet / GL / CgGLParameter.cs

  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 CgNet v1.0
 Copyright (c) 2010 Tobias Bohnen

 Permission is hereby granted, free of charge, to any person obtaining a copy of this
 software and associated documentation files (the "Software"), to deal in the Software
 without restriction, including without limitation the rights to use, copy, modify, merge,
 publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or
 substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.
 */
namespace CgNet.GL
{
    using System;
    using System.Runtime.InteropServices;

    using OpenTK;
    using OpenTK.Graphics.OpenGL;

    public static class CgGLParameter
    {
        #region Methods

        #region Public Static Methods

        public static void DisableClientState(this Parameter param)
        {
            NativeMethods.cgGLDisableClientState(param.Handle);
        }

        // TODO: TextureParameter
        public static void DisableTexture(this Parameter param)
        {
            NativeMethods.cgGLDisableTextureParameter(param.Handle);
        }

        public static void EnableClientState(this Parameter param)
        {
            NativeMethods.cgGLEnableClientState(param.Handle);
        }

        // TODO: TextureParameter
        public static void EnableTexture(this Parameter param)
        {
            NativeMethods.cgGLEnableTextureParameter(param.Handle);
        }

        public static T Get<T>(this Parameter param)
            where T : struct
        {
            IntPtr param1 = param.Handle;
            GCHandle handle = GCHandle.Alloc(new T(), GCHandleType.Pinned);
            try
            {
                if (typeof(T) == typeof(float))
                {
                    NativeMethods.cgGLGetParameter1f(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(double))
                {
                    NativeMethods.cgGLGetParameter1d(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(Vector2))
                {
                    NativeMethods.cgGLGetParameter2f(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(Vector2d))
                {
                    NativeMethods.cgGLGetParameter2d(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(Vector3))
                {
                    NativeMethods.cgGLGetParameter3f(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(Vector3d))
                {
                    NativeMethods.cgGLGetParameter3d(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(Vector4))
                {
                    NativeMethods.cgGLGetParameter4f(param1, handle.AddrOfPinnedObject());
                }
                else if (typeof(T) == typeof(Vector4d))
                {
                    NativeMethods.cgGLGetParameter4d(param1, handle.AddrOfPinnedObject());
                }
                else
                {
                    throw new ArgumentException();
                }

                return (T)handle.Target;
            }
            finally
            {
                handle.Free();
            }
        }

        public static void Get(this Parameter param, ref float[] values)
        {
            IntPtr param1 = param.Handle;
            GCHandle handle = GCHandle.Alloc(values, GCHandleType.Pinned);
            try
            {
                switch (values.Length)
                {
                    case 1:
                        NativeMethods.cgGLGetParameter1f(param1, handle.AddrOfPinnedObject());
                        break;
                    case 2:
                        NativeMethods.cgGLGetParameter2f(param1, handle.AddrOfPinnedObject());
                        break;
                    case 3:
                        NativeMethods.cgGLGetParameter3f(param1, handle.AddrOfPinnedObject());
                        break;
                    case 4:
                        NativeMethods.cgGLGetParameter4f(param1, handle.AddrOfPinnedObject());
                        break;
                }
            }
            finally
            {
                handle.Free();
            }
        }

        public static void Get(this Parameter param, ref double[] values)
        {
            IntPtr param1 = param.Handle;
            GCHandle handle = GCHandle.Alloc(values);
            try
            {
                switch (values.Length)
                {
                    case 1:
                        NativeMethods.cgGLGetParameter1d(param1, handle.AddrOfPinnedObject());
                        break;
                    case 2:
                        NativeMethods.cgGLGetParameter2d(param1, handle.AddrOfPinnedObject());
                        break;
                    case 3:
                        NativeMethods.cgGLGetParameter3d(param1, handle.AddrOfPinnedObject());
                        break;
                    case 4:
                        NativeMethods.cgGLGetParameter4d(param1, handle.AddrOfPinnedObject());
                        break;
                }
            }
            finally
            {
                handle.Free();
            }
        }

        public static void GetArray(this Parameter param, int offset, int nelements, ref float[] values)
        {
            IntPtr param1 = param.Handle;
            GCHandle g = GCHandle.Alloc(values, GCHandleType.Pinned);

            try
            {
                switch (values.Length / nelements)
                {
                    case 1:
                        NativeMethods.cgGLGetParameterArray1f(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                    case 2:
                        NativeMethods.cgGLGetParameterArray2f(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                    case 3:
                        NativeMethods.cgGLGetParameterArray3f(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                    case 4:
                        NativeMethods.cgGLGetParameterArray4f(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                }
            }
            finally
            {
                g.Free();
            }
        }

        public static void GetArray(this Parameter param, int offset, int nelements, ref double[] values)
        {
            IntPtr param1 = param.Handle;
            GCHandle g = GCHandle.Alloc(values, GCHandleType.Pinned);

            try
            {
                switch (values.Length / nelements)
                {
                    case 1:
                        NativeMethods.cgGLGetParameterArray1d(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                    case 2:
                        NativeMethods.cgGLGetParameterArray2d(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                    case 3:
                        NativeMethods.cgGLGetParameterArray3d(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                    case 4:
                        NativeMethods.cgGLGetParameterArray4d(param1, offset, nelements, g.AddrOfPinnedObject());
                        break;
                }
            }
            finally
            {
                g.Free();
            }
        }

        public static void GetMatrix(this Parameter param, out Matrix4 value)
        {
            value = CgGL.GetMatrixParameter<Matrix4>(param.Handle, Cg.DefaultMatrixOrder);
        }

        public static void GetMatrix(this Parameter param, out Matrix4d value)
        {
            value = CgGL.GetMatrixParameter<Matrix4d>(param.Handle, Cg.DefaultMatrixOrder);
        }

        public static T GetMatrix<T>(this Parameter param)
            where T : struct
        {
            return CgGL.GetMatrixParameter<T>(param.Handle, Cg.DefaultMatrixOrder);
        }

        public static T GetMatrix<T>(this Parameter param, MatrixOrder order)
            where T : struct
        {
            return CgGL.GetMatrixParameter<T>(param.Handle, order);
        }

        public static void GetMatrixArray(this Parameter param, int offset, int nelements, out Matrix4[] values)
        {
            values = CgGL.GetMatrixParameterArray<Matrix4>(param.Handle, offset, nelements, Cg.DefaultMatrixOrder);
        }

        public static void GetMatrixArray(this Parameter param, int offset, int nelements, out Matrix4d[] values)
        {
            values = CgGL.GetMatrixParameterArray<Matrix4d>(param.Handle, offset, nelements, Cg.DefaultMatrixOrder);
        }

        public static T[] GetMatrixArray<T>(this Parameter param, int offset, int nelements)
            where T : struct
        {
            return CgGL.GetMatrixParameterArray<T>(param.Handle, offset, nelements, Cg.DefaultMatrixOrder);
        }

        public static T[] GetMatrixArray<T>(this Parameter param, int offset, int nelements, MatrixOrder order)
            where T : struct
        {
            return CgGL.GetMatrixParameterArray<T>(param.Handle, offset, nelements, order);
        }

        public static int GetTextureEnum(this Parameter param)
        {
            return NativeMethods.cgGLGetTextureEnum(param.Handle);
        }

        public static int GetTextureParameter(this Parameter param)
        {
            return NativeMethods.cgGLGetTextureParameter(param.Handle);
        }

        public static void Set(this Parameter param, double x)
        {
            NativeMethods.cgGLSetParameter1d(param.Handle, x);
        }

        public static void Set(this Parameter param, double[] v)
        {
            IntPtr param1 = param.Handle;
            switch (v.Length)
            {
                case 1:
                    NativeMethods.cgGLSetParameter1dv(param1, v);
                    break;
                case 2:
                    NativeMethods.cgGLSetParameter2dv(param1, v);
                    break;
                case 3:
                    NativeMethods.cgGLSetParameter3dv(param1, v);
                    break;
                case 4:
                    NativeMethods.cgGLSetParameter4dv(param1, v);
                    break;
                default:
                    throw new ArgumentException();
            }
        }

        public static void Set(this Parameter param, float[] v)
        {
            IntPtr param1 = param.Handle;
            switch (v.Length)
            {
                case 1:
                    NativeMethods.cgGLSetParameter1fv(param1, v);
                    break;
                case 2:
                    NativeMethods.cgGLSetParameter2fv(param1, v);
                    break;
                case 3:
                    NativeMethods.cgGLSetParameter3fv(param1, v);
                    break;
                case 4:
                    NativeMethods.cgGLSetParameter4fv(param1, v);
                    break;
                default:
                    throw new ArgumentException();
            }
        }

        public static void Set(this Parameter param, float x)
        {
            NativeMethods.cgGLSetParameter1f(param.Handle, x);
        }

        public static void Set(this Parameter param, double x, double y)
        {
            NativeMethods.cgGLSetParameter2d(param.Handle, x, y);
        }

        public static void Set(this Parameter param, float x, float y)
        {
            NativeMethods.cgGLSetParameter2f(param.Handle, x, y);
        }

        public static void Set(this Parameter param, double x, double y, double z)
        {
            NativeMethods.cgGLSetParameter3d(param.Handle, x, y, z);
        }

        public static void Set(this Parameter param, float x, float y, float z)
        {
            NativeMethods.cgGLSetParameter3f(param.Handle, x, y, z);
        }

        public static void Set(this Parameter param, double x, double y, double z, double w)
        {
            NativeMethods.cgGLSetParameter4d(param.Handle, x, y, z, w);
        }

        public static void Set(this Parameter param, float x, float y, float z, float w)
        {
            NativeMethods.cgGLSetParameter4f(param.Handle, x, y, z, w);
        }

        public static void Set(this Parameter param, Vector2 v)
        {
            Vector2 v1 = v;
            NativeMethods.cgGLSetParameter2f(param.Handle, v1.X, v1.Y);
        }

        public static void Set(this Parameter param, Vector2d v)
        {
            Vector2d v1 = v;
            NativeMethods.cgGLSetParameter2d(param.Handle, v1.X, v1.Y);
        }

        public static void Set(this Parameter param, Vector3 v)
        {
            Vector3 v1 = v;
            NativeMethods.cgGLSetParameter3f(param.Handle, v1.X, v1.Y, v1.Z);
        }

        public static void Set(this Parameter param, Vector3d v)
        {
            Vector3d v1 = v;
            NativeMethods.cgGLSetParameter3d(param.Handle, v1.X, v1.Y, v1.Z);
        }

        public static void Set(this Parameter param, Vector4 v)
        {
            Vector4 v1 = v;
            NativeMethods.cgGLSetParameter4f(param.Handle, v1.X, v1.Y, v1.Z, v1.W);
        }

        public static void Set(this Parameter param, Vector4d v)
        {
            Vector4d v1 = v;
            NativeMethods.cgGLSetParameter4d(param.Handle, v1.X, v1.Y, v1.Z, v1.W);
        }

        public static void SetArray(this Parameter param, int offset, int nelements, float[] values)
        {
            IntPtr param1 = param.Handle;
            switch (values.Length / nelements)
            {
                case 1:
                    NativeMethods.cgGLSetParameterArray1f(param1, offset, nelements, values);
                    break;
                case 2:
                    NativeMethods.cgGLSetParameterArray2f(param1, offset, nelements, values);
                    break;
                case 3:
                    NativeMethods.cgGLSetParameterArray3f(param1, offset, nelements, values);
                    break;
                case 4:
                    NativeMethods.cgGLSetParameterArray4f(param1, offset, nelements, values);
                    break;
            }
        }

        public static void SetArray(this Parameter param, int offset, int nelements, double[] values)
        {
            IntPtr param1 = param.Handle;
            switch (values.Length / nelements)
            {
                case 1:
                    NativeMethods.cgGLSetParameterArray1d(param1, offset, nelements, values);
                    break;
                case 2:
                    NativeMethods.cgGLSetParameterArray2d(param1, offset, nelements, values);
                    break;
                case 3:
                    NativeMethods.cgGLSetParameterArray3d(param1, offset, nelements, values);
                    break;
                case 4:
                    NativeMethods.cgGLSetParameterArray4d(param1, offset, nelements, values);
                    break;
            }
        }

        public static void SetMatrix(this Parameter param, Matrix4 matrix)
        {
            SetMatrix(param, matrix, Cg.DefaultMatrixOrder);
        }

        public static void SetMatrix(this Parameter param, Matrix4 matrix, MatrixOrder order)
        {
            switch (order)
            {
                case MatrixOrder.ColumnMajor:
                    NativeMethods.cgGLSetMatrixParameterfc(param.Handle, ref matrix);
                    break;
                case MatrixOrder.RowMajor:
                    NativeMethods.cgGLSetMatrixParameterfr(param.Handle, ref matrix);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("order");
            }
        }

        public static void SetMatrix(this Parameter param, Matrix4d matrix)
        {
            CgGL.SetMatrixParameter(param.Handle, matrix, Cg.DefaultMatrixOrder);
        }

        public static void SetMatrix(this Parameter param, Matrix4d matrix, MatrixOrder order)
        {
            CgGL.SetMatrixParameter(param.Handle, matrix, order);
        }

        public static void SetMatrixArray(this Parameter param, int offset, int nelements, Matrix4d[] matrices)
        {
            CgGL.SetMatrixParameterArray(param.Handle, offset, nelements, matrices, Cg.DefaultMatrixOrder);
        }

        public static void SetMatrixArray(this Parameter param, int offset, int nelements, Matrix4[] matrices)
        {
            CgGL.SetMatrixParameterArray(param.Handle, offset, nelements, matrices, Cg.DefaultMatrixOrder);
        }

        public static void SetMatrixArray(this Parameter param, int offset, int nelements, Matrix4d[] matrices, MatrixOrder order)
        {
            CgGL.SetMatrixParameterArray(param.Handle, offset, nelements, matrices, order);
        }

        public static void SetMatrixArray(this Parameter param, int offset, int nelements, Matrix4[] matrices, MatrixOrder order)
        {
            CgGL.SetMatrixParameterArray(param.Handle, offset, nelements, matrices, order);
        }

        public static void SetPointer(this Parameter param, int fsize, DataType type, int stride, IntPtr pointer)
        {
            NativeMethods.cgGLSetParameterPointer(param.Handle, fsize, type, stride, pointer);
        }

        public static void SetStateMatrix(this Parameter param, MatrixType matrix, MatrixTransform transform)
        {
            NativeMethods.cgGLSetStateMatrixParameter(param.Handle, matrix, transform);
        }

        // TODO: TextureParameter
        public static void SetTexture(this Parameter param, int texobj)
        {
            NativeMethods.cgGLSetTextureParameter(param.Handle, texobj);
        }

        public static void SetupSampler(this Parameter param, int texobj)
        {
            NativeMethods.cgGLSetupSampler(param.Handle, texobj);
        }

        #endregion Public Static Methods

        #endregion Methods
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.