Advertisement
ZeronSix

OpenGL SFML .NET

Jul 30th, 2013
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1.  using System;
  2. using System.Runtime.InteropServices;
  3. using SFML;
  4. using SFML.Graphics;
  5. using SFML.Window;
  6. using Tao.OpenGl;
  7.  
  8. namespace opengl
  9. {
  10.     static class Program
  11.     {
  12.         /// <summary>
  13.         /// The main entry point for the application.
  14.         /// </summary>
  15.         static void Main()
  16.         {
  17.             // Request a 32-bits depth buffer when creating the window
  18.             ContextSettings contextSettings = new ContextSettings();
  19.             contextSettings.DepthBits = 32;
  20.  
  21.             // Create the main window
  22.             RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML graphcis with OpenGL", Styles.Default, contextSettings);
  23.             window.SetVerticalSyncEnabled(true);
  24.  
  25.             // Make it the active window for OpenGL calls
  26.             window.SetActive();
  27.  
  28.             // Setup event handlers
  29.             window.Closed     += new EventHandler(OnClosed);
  30.             window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
  31.             window.Resized    += new EventHandler<SizeEventArgs>(OnResized);
  32.  
  33.             // Create a sprite for the background
  34.             Sprite background = new Sprite(new Texture("resources/background.jpg"));
  35.  
  36.             // Create a text to display on top of the OpenGL object
  37.             Text text = new Text("SFML / OpenGL demo", new Font("resources/sansation.ttf"));
  38.             text.Position = new Vector2f(250, 450);
  39.             text.Color = new Color(255, 255, 255, 170);
  40.  
  41.             // Load an OpenGL texture.
  42.             // We could directly use a SFML.Graphics.Texture as an OpenGL texture (with its Bind() member function),
  43.             // but here we want more control on it (generate mipmaps, ...) so we create a new one
  44.             int texture = 0;
  45.             using (Image image = new Image("resources/texture.jpg"))
  46.             {
  47.                 Gl.glGenTextures(1, out texture);
  48.                 Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
  49.                 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);
  50.                 Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
  51.                 Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
  52.             }
  53.  
  54.             // Enable Z-buffer read and write
  55.             Gl.glEnable(Gl.GL_DEPTH_TEST);
  56.             Gl.glDepthMask(Gl.GL_TRUE);
  57.             Gl.glClearDepth(1);
  58.  
  59.             // Disable lighting
  60.             Gl.glDisable(Gl.GL_LIGHTING);
  61.  
  62.             // Configure the viewport (the same size as the window)
  63.             Gl.glViewport(0, 0, (int)window.Size.X, (int)window.Size.Y);
  64.  
  65.             // Setup a perspective projection
  66.             Gl.glMatrixMode(Gl.GL_PROJECTION);
  67.             Gl.glLoadIdentity();
  68.             float ratio = (float)(window.Size.X) / window.Size.Y;
  69.             Gl.glFrustum(-ratio, ratio, -1, 1, 1, 500);
  70.  
  71.             // Bind the texture
  72.             Gl.glEnable(Gl.GL_TEXTURE_2D);
  73.             Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
  74.  
  75.             // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
  76.             float[] cube = new float[]
  77.             {
  78.                 // positions    // texture coordinates
  79.                 -20, -20, -20,  0, 0,
  80.                 -20,  20, -20,  1, 0,
  81.                 -20, -20,  20,  0, 1,
  82.                 -20, -20,  20,  0, 1,
  83.                 -20,  20, -20,  1, 0,
  84.                 -20,  20,  20,  1, 1,
  85.  
  86.                  20, -20, -20,  0, 0,
  87.                  20,  20, -20,  1, 0,
  88.                  20, -20,  20,  0, 1,
  89.                  20, -20,  20,  0, 1,
  90.                  20,  20, -20,  1, 0,
  91.                  20,  20,  20,  1, 1,
  92.  
  93.                 -20, -20, -20,  0, 0,
  94.                  20, -20, -20,  1, 0,
  95.                 -20, -20,  20,  0, 1,
  96.                 -20, -20,  20,  0, 1,
  97.                  20, -20, -20,  1, 0,
  98.                  20, -20,  20,  1, 1,
  99.  
  100.                 -20,  20, -20,  0, 0,
  101.                  20,  20, -20,  1, 0,
  102.                 -20,  20,  20,  0, 1,
  103.                 -20,  20,  20,  0, 1,
  104.                  20,  20, -20,  1, 0,
  105.                  20,  20,  20,  1, 1,
  106.  
  107.                 -20, -20, -20,  0, 0,
  108.                  20, -20, -20,  1, 0,
  109.                 -20,  20, -20,  0, 1,
  110.                 -20,  20, -20,  0, 1,
  111.                  20, -20, -20,  1, 0,
  112.                  20,  20, -20,  1, 1,
  113.  
  114.                 -20, -20,  20,  0, 0,
  115.                  20, -20,  20,  1, 0,
  116.                 -20,  20,  20,  0, 1,
  117.                 -20,  20,  20,  0, 1,
  118.                  20, -20,  20,  1, 0,
  119.                  20,  20,  20,  1, 1
  120.             };
  121.  
  122.             // Enable position and texture coordinates vertex components
  123.             Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
  124.             Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
  125.             Gl.glVertexPointer(3, Gl.GL_FLOAT, 5 * sizeof(float), Marshal.UnsafeAddrOfPinnedArrayElement(cube, 0));
  126.             Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 5 * sizeof(float), Marshal.UnsafeAddrOfPinnedArrayElement(cube, 3));
  127.  
  128.             // Disable normal and color vertex components
  129.             Gl.glDisableClientState(Gl.GL_NORMAL_ARRAY);
  130.             Gl.glDisableClientState(Gl.GL_COLOR_ARRAY);
  131.  
  132.             int startTime = Environment.TickCount;
  133.  
  134.             // Start game loop
  135.             while (window.IsOpen())
  136.             {
  137.                 // Process events
  138.                 window.DispatchEvents();
  139.  
  140.                 // Clear the window
  141.                 window.Clear();
  142.  
  143.                 // Draw background
  144.                 window.PushGLStates();
  145.                 window.Draw(background);
  146.                 window.PopGLStates();
  147.  
  148.                 // Clear the depth buffer
  149.                 Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT);
  150.  
  151.                 // We get the position of the mouse cursor, so that we can move the box accordingly
  152.                 float x =  Mouse.GetPosition(window).X * 200.0F / window.Size.X - 100.0F;
  153.                 float y = -Mouse.GetPosition(window).Y * 200.0F / window.Size.Y + 100.0F;
  154.  
  155.                 // Apply some transformations
  156.                 float time = (Environment.TickCount - startTime) / 1000.0F;
  157.                 Gl.glMatrixMode(Gl.GL_MODELVIEW);
  158.                 Gl.glLoadIdentity();
  159.                 Gl.glTranslatef(x, y, -100.0F);
  160.                 Gl.glRotatef(time * 50, 1.0F, 0.0F, 0.0F);
  161.                 Gl.glRotatef(time * 30, 0.0F, 1.0F, 0.0F);
  162.                 Gl.glRotatef(time * 90, 0.0F, 0.0F, 1.0F);
  163.  
  164.                 // Draw the cube
  165.                 Gl.glDrawArrays(Gl.GL_TRIANGLES, 0, 36);
  166.  
  167.                 // Draw some text on top of our OpenGL object
  168.                 window.PushGLStates();
  169.                 window.Draw(text);
  170.                 window.PopGLStates();
  171.  
  172.                 // Finally, display the rendered frame on screen
  173.                 window.Display();
  174.             }
  175.  
  176.             // Don't forget to destroy our texture
  177.             Gl.glDeleteTextures(1, ref texture);
  178.         }
  179.  
  180.         /// <summary>
  181.         /// Function called when the window is closed
  182.         /// </summary>
  183.         static void OnClosed(object sender, EventArgs e)
  184.         {
  185.             RenderWindow window = (RenderWindow)sender;
  186.             window.Close();
  187.         }
  188.  
  189.         /// <summary>
  190.         /// Function called when a key is pressed
  191.         /// </summary>
  192.         static void OnKeyPressed(object sender, KeyEventArgs e)
  193.         {
  194.             RenderWindow window = (RenderWindow)sender;
  195.             if (e.Code == Keyboard.Key.Escape)
  196.                 window.Close();
  197.         }
  198.  
  199.         /// <summary>
  200.         /// Function called when the window is resized
  201.         /// </summary>
  202.         static void OnResized(object sender, SizeEventArgs e)
  203.         {
  204.             Gl.glViewport(0, 0, (int)e.Width, (int)e.Height);
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement