Source

CgNet / ExampleBrowser / Examples / OpenTK / Basic / ProjectiveTexturing.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
namespace ExampleBrowser.Examples.OpenTK.Basic
{
    using System;

    using CgNet;
    using CgNet.GL;

    using ExampleBrowser.Data;

    using global::OpenTK;
    using global::OpenTK.Graphics.OpenGL;
    using global::OpenTK.Input;

    using OpenTK = global::OpenTK;

    [ExampleDescription(NodePath = "OpenTK/Basic/27 Projective Texturing")]
    public class ProjectiveTexturing : Example
    {
        #region Fields

        private const float EyeAngle = 0.53f; /* Angle in radians eye rotates around scene. */
        private const float EyeHeight = 5.0f; /* Vertical height of light. */

        /* Page 254 */
        private const string FragmentProgramFileName = "Data/C9E6f_projTex.cg";

        /* Page 254 */
        /* Page 254 */
        private const string FragmentProgramName = "C9E6f_projTex";
        private const float LightHeight = 2.0f; /* Vertical height of light. */
        private const string VertexProgramFileName = "Data/C9E5v_projTex.cg";

        /* Page 254 */
        private const string VertexProgramName = "C9E5v_projTex";

        private readonly float[] kd = { 1, 1, 1 }; /* White. */
        private readonly float[] projectionMatrix = new float[16];

        private float lightAngle = -0.4f; /* Angle light rotates around scene. */
        private Parameter myCgVertexParamModelViewProj, myCgVertexParamKd, myCgVertexParamLightPosition, myCgVertexParamTextureMatrix;
        private ProfileType vertexProfile, fragmentProfile;
        private Program vertexProgram, fragmentProgram;

        #endregion Fields

        #region Constructors

        public ProjectiveTexturing()
            : base("27_projective_texturing", 600, 400)
        {
        }

        #endregion Constructors

        #region Methods

        #region Public Static Methods

        public static void LoadMvp(float[] modelView)
        {
            var transpose = new float[16];
            int i;

            for (i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    transpose[i * 4 + j] = modelView[j * 4 + i];
                }
            }
            GL.LoadMatrix(transpose);
        }

        #endregion Public Static Methods

        #region Protected Methods

        /// <summary>
        /// Add your game rendering code here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        /// <remarks>There is no need to call the base implementation.</remarks>
        protected override void DoRender(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            /* World-space positions for light and eye. */
            float[] eyePosition = {
                                      10*(float)Math.Sin(EyeAngle),
                                      EyeHeight,
                                      10*(float)Math.Cos(EyeAngle),
                                      1
                                  };
            float[] lightPosition = {
                                        4.5f * (float)Math.Sin(this.lightAngle),
                                        LightHeight,
                                        4.5f * (float)Math.Cos(this.lightAngle),
                                        1
                                    };
            float[] center = { 0, 0, 0 };  /* eye and light point at the origin */
            float[] up = { 0, 1, 0 };      /* up is positive Y direction */

            float[] translateMatrix = new float[16],
                rotateMatrix = new float[16],
                modelMatrix = new float[16],
                invModelMatrix = new float[16],
                    eyeViewMatrix = new float[16],
                    modelViewMatrix = new float[16],
                    modelViewProjMatrix = new float[16],
                    textureMatrix = new float[16],
                    lightViewMatrix = new float[16];
            float[] objSpaceLightPosition = new float[4];

            BuildLookAtMatrix(eyePosition[0], eyePosition[1], eyePosition[2],
                              center[0], center[1], center[2],
                              up[0], up[1], up[2],
                              eyeViewMatrix);

            BuildLookAtMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
                  center[0], center[1], center[2],
                  up[0], -up[1], up[2],
                  lightViewMatrix);

            this.vertexProgram.Bind();
            CgGL.EnableProfile(this.vertexProfile);

            this.fragmentProgram.Bind();
            CgGL.EnableProfile(this.fragmentProfile);

            /* modelView = rotateMatrix * translateMatrix */
            MakeRotateMatrix(70, 1, 1, 1, rotateMatrix);
            MakeTranslateMatrix(2, 0, 0, translateMatrix);
            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);

            /* invModelMatrix = inverse(modelMatrix) */
            InvertMatrix(invModelMatrix, modelMatrix);

            /* Transform world-space light positions to sphere's object-space. */
            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
            this.myCgVertexParamLightPosition.Set3(objSpaceLightPosition);

            /* modelViewMatrix = viewMatrix * modelMatrix */
            MultMatrix(modelViewMatrix, eyeViewMatrix, modelMatrix);

            /* modelViewProj = projectionMatrix * modelViewMatrix */
            MultMatrix(modelViewProjMatrix, projectionMatrix, modelViewMatrix);

            BuildTextureMatrix(lightViewMatrix, modelMatrix, textureMatrix);

            /* Set matrix parameter with row-major matrix. */
            this.myCgVertexParamModelViewProj.SetMatrix(modelViewProjMatrix);
            this.myCgVertexParamTextureMatrix.SetMatrix(textureMatrix);
            this.vertexProgram.UpdateParameters();
            this.fragmentProgram.UpdateParameters();
            NativeMethods.glutSolidSphere(2.0, 40, 40);

            /*** Render solid cube ***/

            /* modelView = viewMatrix * translateMatrix */
            MakeTranslateMatrix(-2, -1.0f, 0, translateMatrix);
            MakeRotateMatrix(90, 1, 0, 0, rotateMatrix);
            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);

            /* invModelMatrix = inverse(modelMatrix) */
            InvertMatrix(invModelMatrix, modelMatrix);

            /* Transform world-space eye and light positions to sphere's object-space. */
            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
            this.myCgVertexParamLightPosition.Set3(objSpaceLightPosition);

            /* modelViewMatrix = viewMatrix * modelMatrix */
            MultMatrix(modelViewMatrix, eyeViewMatrix, modelMatrix);

            /* modelViewProj = projectionMatrix * modelViewMatrix */
            MultMatrix(modelViewProjMatrix, this.projectionMatrix, modelViewMatrix);

            BuildTextureMatrix(lightViewMatrix, modelMatrix, textureMatrix);

            /* Set matrix parameter with row-major matrix. */
            this.myCgVertexParamModelViewProj.SetMatrix(modelViewProjMatrix);
            this.myCgVertexParamTextureMatrix.SetMatrix(textureMatrix);
            this.vertexProgram.UpdateParameters();
            this.fragmentProgram.UpdateParameters();
            NativeMethods.glutSolidCube(2.5);

            /*** Render floor ***/

            /* modelView = translateMatrix */
            MultMatrix(modelViewProjMatrix, projectionMatrix, eyeViewMatrix);
            MakeTranslateMatrix(0, 0, 0, modelMatrix);
            BuildTextureMatrix(lightViewMatrix, modelMatrix, textureMatrix);

            this.myCgVertexParamLightPosition.Set3(lightPosition);
            this.myCgVertexParamModelViewProj.SetMatrix(modelViewProjMatrix);
            this.myCgVertexParamTextureMatrix.SetMatrix(textureMatrix);

            vertexProgram.UpdateParameters();
            fragmentProgram.UpdateParameters();

            GL.Enable(EnableCap.CullFace);

            GL.Begin(BeginMode.Quads);
            {
                GL.Normal3(0f, 1f, 0f);
                GL.Vertex3(12f, -2, -12);
                GL.Vertex3(-12f, -2, -12);
                GL.Vertex3(-12f, -2, 12);
                GL.Vertex3(12f, -2, 12);

                GL.Normal3(0f, 0, 1);
                GL.Vertex3(-12f, -2, -12);
                GL.Vertex3(12f, -2, -12);
                GL.Vertex3(12f, 10, -12);
                GL.Vertex3(-12f, 10, -12);

                GL.Normal3(0f, 0, -1);
                GL.Vertex3(12f, -2, 12);
                GL.Vertex3(-12f, -2, 12);
                GL.Vertex3(-12f, 10, 12);
                GL.Vertex3(12f, 10, 12);

                GL.Normal3(0f, -1, 0);
                GL.Vertex3(-12f, 10, -12);
                GL.Vertex3(12f, 10, -12);
                GL.Vertex3(12f, 10, 12);
                GL.Vertex3(-12f, 10, 12);

                GL.Normal3(1f, 0, 0);
                GL.Vertex3(-12f, -2, 12);
                GL.Vertex3(-12f, -2, -12);
                GL.Vertex3(-12f, 10, -12);
                GL.Vertex3(-12f, 10, 12);

                GL.Normal3(-1f, 0, 0);
                GL.Vertex3(12f, -2, -12);
                GL.Vertex3(12f, -2, 12);
                GL.Vertex3(12f, 10, 12);
                GL.Vertex3(12f, 10, -12);
            }
            GL.End();

            GL.Disable(EnableCap.CullFace);

            CgGL.DisableProfile(vertexProfile);

            CgGL.DisableProfile(fragmentProfile);

            /*** Render light as white cone ***/

            /* modelView = translateMatrix */
            MakeTranslateMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
              modelMatrix);

            /* modelViewMatrix = eyeViewMatrix * modelMatrix */
            MultMatrix(modelViewMatrix, eyeViewMatrix, modelMatrix);

            GL.PushMatrix();
            /* glLoadMatrixf expects a column-major matrix but Cg matrices are
               row-major (matching C/C++ arrays) so used loadMVP to transpose
               the Cg version. */
            LoadMvp(modelViewMatrix);
            GL.Color3(1f, 1, 1); /* make sure current color is white */
            NativeMethods.glutSolidSphere(0.15, 30, 30);
            GL.PopMatrix();

            this.SwapBuffers();
        }

        /// <summary>
        /// Setup OpenGL and load resources here.
        /// </summary>
        /// <param name="e">Not used.</param>
        protected override void OnLoad(EventArgs e)
        {
            GL.ClearColor(0.1f, 0.1f, 0.1f, 0.0f);
            GL.Enable(EnableCap.DepthTest);
            this.CgContext = CgNet.Context.Create();

            CgGL.SetDebugMode(false);
            this.CgContext.ParameterSettingMode = ParameterSettingMode.Deferred;
            this.CgContext.SetManageTextureParameters(true);

            this.vertexProfile = CgGL.GetLatestProfile(ProfileClass.Vertex);
            CgGL.SetOptimalOptions(this.vertexProfile);

            this.vertexProgram =
                this.CgContext.CreateProgramFromFile(
                    ProgramType.Source, /* Program in human-readable form */
                    VertexProgramFileName, /* Name of file containing program */
                    this.vertexProfile, /* Profile: OpenGL ARB vertex program */
                    VertexProgramName, /* Entry function name */
                    null); /* No extra compiler options */
            this.vertexProgram.Load();

            this.myCgVertexParamModelViewProj = this.vertexProgram.GetNamedParameter("modelViewProj");
            this.myCgVertexParamLightPosition = this.vertexProgram.GetNamedParameter("lightPosition");
            this.myCgVertexParamKd = this.vertexProgram.GetNamedParameter("Kd");
            this.myCgVertexParamTextureMatrix = this.vertexProgram.GetNamedParameter("textureMatrix");

            /* Set light source color parameters once. */
            this.myCgVertexParamKd.Set(this.kd);

            this.fragmentProfile = CgGL.GetLatestProfile(ProfileClass.Fragment);
            CgGL.SetOptimalOptions(this.fragmentProfile);

            /* Specify "color passthrough" fragment program with a string. */
            this.fragmentProgram =
                this.CgContext.CreateProgramFromFile(
                    ProgramType.Source, /* Program in human-readable form */
                    FragmentProgramFileName,
                    this.fragmentProfile, /* Profile: latest fragment profile */
                    FragmentProgramName, /* Entry function name */
                    null); /* No extra compiler options */
            this.fragmentProgram.Load();
            this.SetupDemonSampler();
        }

        /// <summary>
        /// Respond to resize events here.
        /// </summary>
        /// <param name="e">Contains information on the new GameWindow size.</param>
        /// <remarks>There is no need to call the base implementation.</remarks>
        protected override void OnResize(EventArgs e)
        {
            this.Reshape(this.Width, this.Height);
            GL.Viewport(0, 0, this.Width, this.Height);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.vertexProgram.Dispose();
            this.fragmentProgram.Dispose();
            this.CgContext.Dispose();
        }

        /// <summary>
        /// Add your game logic here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        /// <remarks>There is no need to call the base implementation.</remarks>
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            this.lightAngle += 0.008f; /* Add a small angle (in radians). */
            if (this.lightAngle > 2 * Pi)
            {
                this.lightAngle -= 2 * Pi;
            }

            if (this.Keyboard[Key.Escape])
                this.Exit();
        }

        #endregion Protected Methods

        #region Private Static Methods

        private static void BuildTextureMatrix(float[] viewMatrix, float[] modelMatrix, float[] textureMatrix)
        {
            float[] eyeToClipMatrix = new float[16];
            float[] modelViewMatrix = new float[16];

            const float FieldOfView = 50.0f;
            const float AspectRatio = 1;
            float[] textureProjectionMatrix = new float[16];
            float[] clipToTextureMatrix = new float[16];

            /* Build texture projection matrix once. */
            BuildPerspectiveMatrix(FieldOfView, AspectRatio,
                                   0.25, 20.0,  /* Znear and Zfar */
                                   textureProjectionMatrix);

            MakeClipToTextureMatrix(clipToTextureMatrix);

            /* eyeToClip = clipToTexture * textureProjection */
            MultMatrix(eyeToClipMatrix,
               clipToTextureMatrix, textureProjectionMatrix);

            /* modelView = view * model */
            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
            /* texture = eyeToClip * modelView */
            MultMatrix(textureMatrix, eyeToClipMatrix, modelViewMatrix);
        }

        private static void MakeClipToTextureMatrix(float[] m)
        {
            m[0] = 0.5f; m[1] = 0; m[2] = 0; m[3] = 0.5f;
            m[4] = 0; m[5] = 0.5f; m[6] = 0; m[7] = 0.5f;
            m[8] = 0; m[9] = 0; m[10] = 0.5f; m[11] = 0.5f;
            m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;
        }

        #endregion Private Static Methods

        #region Private Methods

        private void Reshape(int width, int height)
        {
            double aspectRatio = (float)width / height;
            const double FieldOfView = 40.0;

            /* Build projection matrix once. */
            BuildPerspectiveMatrix(FieldOfView, aspectRatio,
                                   1.0, 50.0, /* Znear and Zfar */
                                   this.projectionMatrix);

            /* Light drawn with fixed-function transformation so also load same
               projection matrix in fixed-function projection matrix. */
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            global::OpenTK.Graphics.Glu.Perspective(FieldOfView, aspectRatio,
                           1.0, 50.0);  /* Znear and Zfar */
            GL.MatrixMode(MatrixMode.Modelview);
        }

        private void SetupDemonSampler()
        {
            const int Texobj = 666;

            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); /* Tightly packed texture data. */

            GL.BindTexture(TextureTarget.Texture2D, Texobj);
            /* Load demon decal texture with mipmaps. */

            OpenTK.Graphics.Glu.Build2DMipmap(global::OpenTK.Graphics.TextureTarget.Texture2D, (int)All.Rgb8,
              128, 128, global::OpenTK.Graphics.PixelFormat.Rgb, global::OpenTK.Graphics.PixelType.UnsignedByte, ImageDemon.Array);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, this.kd);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, 0x812D);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, 0x812D);

            Parameter sampler = this.fragmentProgram.GetNamedParameter("projectiveMap");
            sampler.SetTexture(Texobj);
        }

        #endregion Private Methods

        #endregion Methods
    }
}