PushGLStates() публичный Метод

Save the current OpenGL render states and matrices.

This function can be used when you mix SFML drawing and direct OpenGL rendering. Combined with PopGLStates, it ensures that:

SFML's internal states are not messed up by your OpenGL code

Your OpenGL states are not modified by a call to a SFML function

More specifically, it must be used around code that calls Draw functions.

Note that this function is quite expensive: it saves all the possible OpenGL states and matrices, even the ones you don't care about. Therefore it should be used wisely. It is provided for convenience, but the best results will be achieved if you handle OpenGL states yourself (because you know which states have really changed, and need to be saved and restored). Take a look at the ResetGLStates function if you do so.

public PushGLStates ( ) : void
Результат void
Пример #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            // Request a 24-bits depth buffer when creating the window
            ContextSettings contextSettings = new ContextSettings();
            contextSettings.DepthBits = 24;

            // Create the main window
            RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML graphics with OpenGL", Styles.Default, contextSettings);
            window.SetVerticalSyncEnabled(true);

            // Initialize OpenTK
            Toolkit.Init();
            GraphicsContext context = new GraphicsContext(new ContextHandle(IntPtr.Zero), null);

            // Setup event handlers
            window.Closed     += new EventHandler(OnClosed);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
            window.Resized    += new EventHandler<SizeEventArgs>(OnResized);

            // Create a sprite for the background
            Sprite background = new Sprite(new Texture("resources/background.jpg"));

            // Create a text to display on top of the OpenGL object
            Text text = new Text("SFML / OpenGL demo", new Font("resources/sansation.ttf"));
            text.Position = new Vector2f(250, 450);
            text.Color = new Color(255, 255, 255, 170);

            // Make the window the active target for OpenGL calls
            window.SetActive();

            // Load an OpenGL texture.
            // We could directly use a SFML.Graphics.Texture as an OpenGL texture (with its Bind() member function),
            // but here we want more control on it (generate mipmaps, ...) so we create a new one
            int texture = 0;
            using (Image image = new Image("resources/texture.jpg"))
            {
                GL.GenTextures(1, out texture);
                GL.BindTexture(TextureTarget.Texture2D, texture);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)image.Size.X, (int)image.Size.Y, 0, PixelFormat.Rgba, PixelType.UnsignedByte, image.Pixels);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            }

            // Enable Z-buffer read and write
            GL.Enable(EnableCap.DepthTest);
            GL.DepthMask(true);
            GL.ClearDepth(1);

            // Disable lighting
            GL.Disable(EnableCap.Lighting);

            // Configure the viewport (the same size as the window)
            GL.Viewport(0, 0, (int)window.Size.X, (int)window.Size.Y);

            // Setup a perspective projection
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            float ratio = (float)(window.Size.X) / window.Size.Y;
            GL.Frustum(-ratio, ratio, -1, 1, 1, 500);

            // Bind the texture
            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, texture);

            // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
            float[] cube = new float[]
            {
                // positions    // texture coordinates
                -20, -20, -20,  0, 0,
                -20,  20, -20,  1, 0,
                -20, -20,  20,  0, 1,
                -20, -20,  20,  0, 1,
                -20,  20, -20,  1, 0,
                -20,  20,  20,  1, 1,

                 20, -20, -20,  0, 0,
                 20,  20, -20,  1, 0,
                 20, -20,  20,  0, 1,
                 20, -20,  20,  0, 1,
                 20,  20, -20,  1, 0,
                 20,  20,  20,  1, 1,

                -20, -20, -20,  0, 0,
                 20, -20, -20,  1, 0,
                -20, -20,  20,  0, 1,
                -20, -20,  20,  0, 1,
                 20, -20, -20,  1, 0,
                 20, -20,  20,  1, 1,

                -20,  20, -20,  0, 0,
                 20,  20, -20,  1, 0,
                -20,  20,  20,  0, 1,
                -20,  20,  20,  0, 1,
                 20,  20, -20,  1, 0,
                 20,  20,  20,  1, 1,

                -20, -20, -20,  0, 0,
                 20, -20, -20,  1, 0,
                -20,  20, -20,  0, 1,
                -20,  20, -20,  0, 1,
                 20, -20, -20,  1, 0,
                 20,  20, -20,  1, 1,

                -20, -20,  20,  0, 0,
                 20, -20,  20,  1, 0,
                -20,  20,  20,  0, 1,
                -20,  20,  20,  0, 1,
                 20, -20,  20,  1, 0,
                 20,  20,  20,  1, 1
            };

            // Enable position and texture coordinates vertex components
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            GL.VertexPointer(3, VertexPointerType.Float, 5 * sizeof(float), Marshal.UnsafeAddrOfPinnedArrayElement(cube, 0));
            GL.TexCoordPointer(2, TexCoordPointerType.Float, 5 * sizeof(float), Marshal.UnsafeAddrOfPinnedArrayElement(cube, 3));

            // Disable normal and color vertex components
            GL.DisableClientState(ArrayCap.NormalArray);
            GL.DisableClientState(ArrayCap.ColorArray);

            Clock clock = new Clock();

            // Start game loop
            while (window.IsOpen)
            {
                // Process events
                window.DispatchEvents();

                // Clear the window
                GL.Clear(ClearBufferMask.DepthBufferBit);

                // Draw background
                window.PushGLStates();
                window.Draw(background);
                window.PopGLStates();

                // Clear the depth buffer
                GL.Clear(ClearBufferMask.DepthBufferBit);

                // We get the position of the mouse cursor, so that we can move the box accordingly
                float x =  Mouse.GetPosition(window).X * 200.0F / window.Size.X - 100.0F;
                float y = -Mouse.GetPosition(window).Y * 200.0F / window.Size.Y + 100.0F;

                // Apply some transformations
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
                GL.Translate(x, y, -100.0F);
                GL.Rotate(clock.ElapsedTime.AsSeconds() * 50, 1.0F, 0.0F, 0.0F);
                GL.Rotate(clock.ElapsedTime.AsSeconds() * 30, 0.0F, 1.0F, 0.0F);
                GL.Rotate(clock.ElapsedTime.AsSeconds() * 90, 0.0F, 0.0F, 1.0F);

                // Draw the cube
                GL.DrawArrays(OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, 0, 36);

                // Draw some text on top of our OpenGL object
                window.PushGLStates();
                window.Draw(text);
                window.PopGLStates();

                // Finally, display the rendered frame on screen
                window.Display();
            }

            // Don't forget to destroy our texture
            GL.DeleteTextures(1, ref texture);
        }
Пример #2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            // Create main window
            RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML.Net OpenGL", Styles.Default, new ContextSettings(32, 0));
            window.SetVerticalSyncEnabled(true);

            // Setup event handlers
            window.Closed     += new EventHandler(OnClosed);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
            window.Resized    += new EventHandler<SizeEventArgs>(OnResized);

            // Create a sprite for the background
            Sprite background = new Sprite(new Texture("resources/background.jpg"));

            // Create a text to display
            Text text = new Text("SFML / OpenGL demo", new Font("resources/sansation.ttf"));
            text.Position = new Vector2f(250.0F, 450.0F);
            text.Color = new Color(255, 255, 255, 170);

            // Load an OpenGL texture.
            // We could directly use a sf::Texture as an OpenGL texture (with its Bind() member function),
            // but here we want more control on it (generate mipmaps, ...) so we create a new one
            int texture = 0;
            using (Image image = new Image("resources/texture.jpg"))
            {
                Gl.glGenTextures(1, out texture);
                Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
                Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGBA, (int)image.Size.X, (int)image.Size.Y, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, image.Pixels);
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
            }

            // Enable Z-buffer read and write
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glDepthMask(Gl.GL_TRUE);
            Gl.glClearDepth(1.0F);

            // Setup a perspective projection
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(90.0F, 1.0F, 1.0F, 500.0F);

            // Bind our texture
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
            Gl.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

            int startTime = Environment.TickCount;

            // Start game loop
            while (window.IsOpen())
            {
                // Process events
                window.DispatchEvents();

                // Clear the window
                window.Clear();

                // Draw background
                window.PushGLStates();
                window.Draw(background);
                window.PopGLStates();

                // Activate the window before using OpenGL commands.
                // This is useless here because we have only one window which is
                // always the active one, but don't forget it if you use multiple windows
                window.SetActive();

                // Clear depth buffer
                Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT);

                // We get the position of the mouse cursor, so that we can move the box accordingly
                float x =  Mouse.GetPosition(window).X * 200.0F / window.Size.X - 100.0F;
                float y = -Mouse.GetPosition(window).Y * 200.0F / window.Size.Y + 100.0F;

                // Apply some transformations
                float time = (Environment.TickCount - startTime) / 1000.0F;
                Gl.glMatrixMode(Gl.GL_MODELVIEW);
                Gl.glLoadIdentity();
                Gl.glTranslatef(x, y, -100.0F);
                Gl.glRotatef(time * 50, 1.0F, 0.0F, 0.0F);
                Gl.glRotatef(time * 30, 0.0F, 1.0F, 0.0F);
                Gl.glRotatef(time * 90, 0.0F, 0.0F, 1.0F);

                // Draw a cube
                float size = 20.0F;
                Gl.glBegin(Gl.GL_QUADS);

                    Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, -size);
                    Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size,  size, -size);
                    Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size,  size, -size);
                    Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, -size, -size);

                    Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, size);
                    Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size,  size, size);
                    Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size,  size, size);
                    Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, -size, size);

                    Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, -size);
                    Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size,  size, -size);
                    Gl.glTexCoord2f(1, 1); Gl.glVertex3f(-size,  size,  size);
                    Gl.glTexCoord2f(1, 0); Gl.glVertex3f(-size, -size,  size);

                    Gl.glTexCoord2f(0, 0); Gl.glVertex3f(size, -size, -size);
                    Gl.glTexCoord2f(0, 1); Gl.glVertex3f(size,  size, -size);
                    Gl.glTexCoord2f(1, 1); Gl.glVertex3f(size,  size,  size);
                    Gl.glTexCoord2f(1, 0); Gl.glVertex3f(size, -size,  size);

                    Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, -size,  size);
                    Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, -size);
                    Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, -size, -size);
                    Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size, -size,  size);

                    Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, size,  size);
                    Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, size, -size);
                    Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, size, -size);
                    Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size, size,  size);

                Gl.glEnd();

                // Draw some text on top of our OpenGL object
                window.PushGLStates();
                window.Draw(text);
                window.PopGLStates();

                // Finally, display the rendered frame on screen
                window.Display();
            }

            // Don't forget to destroy our texture
            Gl.glDeleteTextures(1, ref texture);
        }
Пример #3
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            // Request a 32-bits depth buffer when creating the window
            ContextSettings contextSettings = new ContextSettings();
            contextSettings.DepthBits = 32;

            // Create the main window
            RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML graphics with OpenGL", Styles.Default, contextSettings);
            window.SetVerticalSyncEnabled(true);

            // Make it the active window for OpenGL calls
            window.SetActive();

            // Setup event handlers
            window.Closed     += new EventHandler(OnClosed);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
            window.Resized    += new EventHandler<SizeEventArgs>(OnResized);

            // Create a sprite for the background
            Sprite background = new Sprite(new Texture("resources/background.jpg"));

            // Create a text to display on top of the OpenGL object
            Text text = new Text("SFML / OpenGL demo", new Font("resources/sansation.ttf"));
            text.Position = new Vector2f(250, 450);
            text.Color = new Color(255, 255, 255, 170);

            // Load an OpenGL texture.
            // We could directly use a SFML.Graphics.Texture as an OpenGL texture (with its Bind() member function),
            // but here we want more control on it (generate mipmaps, ...) so we create a new one
            int texture = 0;
            using (Image image = new Image("resources/texture.jpg"))
            {
                Gl.glGenTextures(1, out texture);
                Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
                Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGBA, (int)image.Size.X, (int)image.Size.Y, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, image.Pixels);
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
            }

            // Enable Z-buffer read and write
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glDepthMask(Gl.GL_TRUE);
            Gl.glClearDepth(1);

            // Disable lighting
            Gl.glDisable(Gl.GL_LIGHTING);

            // Configure the viewport (the same size as the window)
            Gl.glViewport(0, 0, (int)window.Size.X, (int)window.Size.Y);

            // Setup a perspective projection
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            float ratio = (float)(window.Size.X) / window.Size.Y;
            Gl.glFrustum(-ratio, ratio, -1, 1, 1, 500);

            // Bind the texture
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);

            // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
            float[] cube = new float[]
            {
                // positions    // texture coordinates
                -20, -20, -20,  0, 0,
                -20,  20, -20,  1, 0,
                -20, -20,  20,  0, 1,
                -20, -20,  20,  0, 1,
                -20,  20, -20,  1, 0,
                -20,  20,  20,  1, 1,

                 20, -20, -20,  0, 0,
                 20,  20, -20,  1, 0,
                 20, -20,  20,  0, 1,
                 20, -20,  20,  0, 1,
                 20,  20, -20,  1, 0,
                 20,  20,  20,  1, 1,

                -20, -20, -20,  0, 0,
                 20, -20, -20,  1, 0,
                -20, -20,  20,  0, 1,
                -20, -20,  20,  0, 1,
                 20, -20, -20,  1, 0,
                 20, -20,  20,  1, 1,

                -20,  20, -20,  0, 0,
                 20,  20, -20,  1, 0,
                -20,  20,  20,  0, 1,
                -20,  20,  20,  0, 1,
                 20,  20, -20,  1, 0,
                 20,  20,  20,  1, 1,

                -20, -20, -20,  0, 0,
                 20, -20, -20,  1, 0,
                -20,  20, -20,  0, 1,
                -20,  20, -20,  0, 1,
                 20, -20, -20,  1, 0,
                 20,  20, -20,  1, 1,

                -20, -20,  20,  0, 0,
                 20, -20,  20,  1, 0,
                -20,  20,  20,  0, 1,
                -20,  20,  20,  0, 1,
                 20, -20,  20,  1, 0,
                 20,  20,  20,  1, 1
            };

            // Enable position and texture coordinates vertex components
            Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
            Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
            Gl.glVertexPointer(3, Gl.GL_FLOAT, 5 * sizeof(float), Marshal.UnsafeAddrOfPinnedArrayElement(cube, 0));
            Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 5 * sizeof(float), Marshal.UnsafeAddrOfPinnedArrayElement(cube, 3));

            // Disable normal and color vertex components
            Gl.glDisableClientState(Gl.GL_NORMAL_ARRAY);
            Gl.glDisableClientState(Gl.GL_COLOR_ARRAY);

            int startTime = Environment.TickCount;

            // Start game loop
            while (window.IsOpen())
            {
                // Process events
                window.DispatchEvents();

                // Clear the window
                window.Clear();

                // Draw background
                window.PushGLStates();
                window.Draw(background);
                window.PopGLStates();

                // Clear the depth buffer
                Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT);

                // We get the position of the mouse cursor, so that we can move the box accordingly
                float x =  Mouse.GetPosition(window).X * 200.0F / window.Size.X - 100.0F;
                float y = -Mouse.GetPosition(window).Y * 200.0F / window.Size.Y + 100.0F;

                // Apply some transformations
                float time = (Environment.TickCount - startTime) / 1000.0F;
                Gl.glMatrixMode(Gl.GL_MODELVIEW);
                Gl.glLoadIdentity();
                Gl.glTranslatef(x, y, -100.0F);
                Gl.glRotatef(time * 50, 1.0F, 0.0F, 0.0F);
                Gl.glRotatef(time * 30, 0.0F, 1.0F, 0.0F);
                Gl.glRotatef(time * 90, 0.0F, 0.0F, 1.0F);

                // Draw the cube
                Gl.glDrawArrays(Gl.GL_TRIANGLES, 0, 36);

                // Draw some text on top of our OpenGL object
                window.PushGLStates();
                window.Draw(text);
                window.PopGLStates();

                // Finally, display the rendered frame on screen
                window.Display();
            }

            // Don't forget to destroy our texture
            Gl.glDeleteTextures(1, ref texture);
        }