File: One.cs

package info (click to toggle)
taoframework 2.1.svn20090801-15.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,312 kB
  • sloc: cs: 113,093; makefile: 908; sh: 107; ansic: 7
file content (355 lines) | stat: -rw-r--r-- 11,717 bytes parent folder | download | duplicates (6)
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
#region License
/*
MIT License
Copyright 2003-2006 Tao Framework Team
http://www.taoframework.com
All rights reserved.

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.
*/
#endregion License

#region Original License
/*
Copyright (c) 1999 by Pawel W. Olszta
Written by Pawel W. Olszta, <olszta@sourceforge.net>
Creation date: czw gru  2 11:58:41 CET 1999
*/
#endregion Original License

using System;
using System.IO;
using System.Text;

using Tao.FreeGlut;
using Tao.OpenGl;

namespace FreeGlutExamples
{
    /// <summary>
    /// FreeGlut One Example
    /// </summary>
    /// <remarks>
    ///     Hey! This was the original file where freeglut development started. Just
    ///     note what I have written here at the time. And see the creation date :)
    /// </remarks>
    public class One
    {
        static int g_LeaveGameMode = 0;
        static int g_InGameMode = 1;

        static void PrintText(int nX, int nY, string pszText)
        {
            int lines = 0;

            /*
             * Prepare the OpenGL state
             */
            Gl.glDisable(Gl.GL_LIGHTING);
            Gl.glDisable(Gl.GL_DEPTH_TEST);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPushMatrix();
            Gl.glLoadIdentity();

            /*
             * Have an orthogonal projection matrix set
             */
            Gl.glOrtho(0, Glut.glutGet(Glut.GLUT_WINDOW_WIDTH),
                     0, Glut.glutGet(Glut.GLUT_WINDOW_HEIGHT),
                     -1, +1
            );

            /*
             * Now the matrix mode
             */
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glPushMatrix();
            Gl.glLoadIdentity();

            /*
             * Now the main text
             */
            Gl.glColor3ub(0, 0, 0);
            Gl.glRasterPos2i(nX, nY);

            for (int i = 0; i < pszText.Length; i++)
            {
                char p = pszText[i];
                if (p == '\n')
                {
                    lines++;
                    Gl.glRasterPos2i(nX, nY - (lines * 18));
                }

                Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_HELVETICA_18, (int)p);
            }

            /*
             * Revert to the old matrix modes
             */
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPopMatrix();

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glPopMatrix();

            /*
             * Restore the old OpenGL states
             */
            Gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_LIGHTING);
        }

        /*
         * This is the display routine for our sample FreeGLUT windows
         */
        static float g_fTime = 0.0f;

        public static void SampleDisplay()
        {
            /*
             * Clear the screen
             */
            Gl.glClearColor(0f, 0.5f, 1f, 1f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

            /*
             * Have the cube rotated
             */
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glPushMatrix();

            Gl.glRotatef(g_fTime, 0, 0, 1);
            Gl.glRotatef(g_fTime, 0, 1, 0);
            Gl.glRotatef(g_fTime, 1, 0, 0);

            /*
             * And then drawn...
             */
            Gl.glColor3f(1, 1, 0);
            /* glutWireCube( 20.0 ); */
            Glut.glutWireTeapot(20.0);
            /* glutWireSpher( 15.0, 15, 15 ); */
            /* glColor3f( 0, 1, 0 ); */
            /* glutWireCube( 30.0 ); */
            /* glutSolidCone( 10, 20, 10, 2 ); */

            /*
             * Don't forget about the model-view matrix
             */
            Gl.glPopMatrix();

            /*
             * Draw a silly text
             */
            if (g_InGameMode == 0)
                PrintText(20, 20, "Hello there cruel world!");
            else
                PrintText(20, 20, "Press ESC to leave the game mode!");

            /*
             * And swap this context's buffers
             */
            Glut.glutSwapBuffers();
        }

        /*
         * This is a sample idle function
         */
        static void SampleIdle()
        {
            g_fTime += 0.5f;

            if (g_LeaveGameMode == 1)
            {
                Glut.glutLeaveGameMode();
                g_LeaveGameMode = 0;
                g_InGameMode = 0;
            }
        }

        /*
         * The reshape function
         */
        static void SampleReshape(int nWidth, int nHeight)
        {
            float fAspect = (float)nHeight / (float)nWidth;
            float[] fPos = new float[4] { 0.0f, 0.0f, 10.0f, 0.0f };
            float[] fCol = new float[4] { 0.5f, 1.0f, 0.0f, 1.0f };

            /*
             * Update the viewport first
             */
            Gl.glViewport(0, 0, nWidth, nHeight);

            /*
             * Then the projection matrix
             */
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glFrustum(-1.0, 1.0, -fAspect, fAspect, 1.0, 80.0);

            /*
             * Move back the camera a bit
             */
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Gl.glTranslatef(0.0f, 0.0f, -40.0f);

            /*
             * Enable some features...
             */
            Gl.glEnable(Gl.GL_CULL_FACE);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_NORMALIZE);

            /*
             * Set up some lighting
             */
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, fPos);
            Gl.glEnable(Gl.GL_LIGHTING);
            Gl.glEnable(Gl.GL_LIGHT0);

            /*
             * Set up a sample material
             */
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT_AND_DIFFUSE, fCol);
        }

        /*
         * A sample keyboard callback
         */
        static void SampleKeyboard(byte cChar, int nMouseX, int nMouseY)
        {
            Console.WriteLine("SampleKeyboard(): keypress '%c' at (%i,%i)\n",
                    cChar, nMouseX, nMouseY);
        }

        /*
         * A sample keyboard callback (for game mode window)
         */
        static void SampleGameModeKeyboard(byte cChar, int nMouseX, int nMouseY)
        {
            if (cChar == 27)
                g_LeaveGameMode = 1;
        }


        /*
        * A sample special callback
        */
        static void SampleSpecial(int nSpecial, int nMouseX, int nMouseY)
        {
            Console.WriteLine("SampleSpecial(): special keypress %i at (%i,%i)\n",
                    nSpecial, nMouseX, nMouseY);
        }

        /*
         * A sample menu callback
         */
        static void SampleMenu(int menuID)
        {
            /*
             * Just print something funny
             */
            Console.WriteLine("SampleMenu() callback executed, menuID is %i\n", menuID);
        }


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main(string[] args)
        {
            int menuID, subMenuA, subMenuB;

            Glut.glutInitDisplayString("stencil~2 rgb double depth>=16 samples");
            Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowPosition(100, 100);

            Glut.glutInit();

            subMenuA = Glut.glutCreateMenu(SampleMenu);
            Glut.glutAddMenuEntry("Sub menu A1 (01)", 1);
            Glut.glutAddMenuEntry("Sub menu A2 (02)", 2);
            Glut.glutAddMenuEntry("Sub menu A3 (03)", 3);

            subMenuB = Glut.glutCreateMenu(SampleMenu);
            Glut.glutAddMenuEntry("Sub menu B1 (04)", 4);
            Glut.glutAddMenuEntry("Sub menu B2 (05)", 5);
            Glut.glutAddMenuEntry("Sub menu B3 (06)", 6);
            Glut.glutAddSubMenu("Going to sub menu A", subMenuA);

            menuID = Glut.glutCreateMenu(SampleMenu);
            Glut.glutAddMenuEntry("Entry one", 1);
            Glut.glutAddMenuEntry("Entry two", 2);
            Glut.glutAddMenuEntry("Entry three", 3);
            Glut.glutAddMenuEntry("Entry four", 4);
            Glut.glutAddMenuEntry("Entry five", 5);
            Glut.glutAddSubMenu("Enter sub menu A", subMenuA);
            Glut.glutAddSubMenu("Enter sub menu B", subMenuB);

            Glut.glutCreateWindow("Hello world!");
            Glut.glutDisplayFunc(new Glut.DisplayCallback(SampleDisplay));
            Glut.glutReshapeFunc(new Glut.ReshapeCallback(SampleReshape));
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(SampleKeyboard));
            Glut.glutSpecialFunc(new Glut.SpecialCallback(SampleSpecial));
            Glut.glutIdleFunc(new Glut.IdleCallback(SampleIdle));
            Glut.glutAttachMenu(Glut.GLUT_LEFT_BUTTON);

            Glut.glutInitWindowPosition(200, 200);
            Glut.glutCreateWindow("I am not Jan B.");
            Glut.glutDisplayFunc(new Glut.DisplayCallback(SampleDisplay));
            Glut.glutReshapeFunc(new Glut.ReshapeCallback(SampleReshape));
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(SampleKeyboard));
            Glut.glutSpecialFunc(new Glut.SpecialCallback(SampleSpecial));
            Glut.glutIdleFunc(new Glut.IdleCallback(SampleIdle));
            Glut.glutAttachMenu(Glut.GLUT_LEFT_BUTTON);

            Console.WriteLine("Testing game mode string parsing, don't panic!\n");
            Glut.glutGameModeString("320x240:32@100");
            Glut.glutGameModeString("640x480:16@72");
            Glut.glutGameModeString("1024x768");
            Glut.glutGameModeString(":32@120");
            Glut.glutGameModeString("Toudi glupcze, Danwin bedzie moj!");
            Glut.glutGameModeString("640x480:16@72");

            Glut.glutEnterGameMode();
            Glut.glutDisplayFunc(new Glut.DisplayCallback(SampleDisplay));
            Glut.glutReshapeFunc(new Glut.ReshapeCallback(SampleReshape));
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(SampleGameModeKeyboard));
            Glut.glutIdleFunc(new Glut.IdleCallback(SampleIdle));
            Glut.glutAttachMenu(Glut.GLUT_LEFT_BUTTON);

            Console.WriteLine("current window is %ix%i+%i+%i",
                    Glut.glutGet(Glut.GLUT_WINDOW_X), Glut.glutGet(Glut.GLUT_WINDOW_Y),
                    Glut.glutGet(Glut.GLUT_WINDOW_WIDTH), Glut.glutGet(Glut.GLUT_WINDOW_HEIGHT)
            );

            /*
             * Enter the main FreeGLUT processing loop
             */
            Glut.glutMainLoop();

            Console.WriteLine("glutMainLoop() termination works fine!\n");
        }
    }
}