Advertisement
Guest User

MainWindow.xaml.cs

a guest
Mar 23rd, 2012
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using SharpGL.SceneGraph;
  15. using SharpGL;
  16.  
  17. namespace SimpleVboSample
  18. {
  19.     /// <summary>
  20.     /// Interaction logic for MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.         float[] vertices;
  25.         uint[] indices;
  26.         // used for storing the id of the vbo
  27.         uint[] vertexBufferObjectIds = new uint[1];
  28.  
  29.         public MainWindow()
  30.         {
  31.             InitializeComponent();
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Handles the OpenGLDraw event of the OpenGLControl control.
  36.         /// </summary>
  37.         /// <param name="sender">The source of the event.</param>
  38.         /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
  39.         private void OpenGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
  40.         {
  41.             OpenGL gl = args.OpenGL;
  42.  
  43.             //CORNFLOWER BLUE
  44.             gl.ClearColor(0.39f, 0.53f, 0.92f, 1);
  45.             gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
  46.             gl.LoadIdentity();
  47.  
  48.             // point the camera at the center of the world 0,0,0 and move back 2, up 3 and over 2
  49.             gl.LookAt(3, 0.5f, 3, 0, 0.5f, 0, 0, 1, 0);
  50.             // Set the color to
  51.             gl.Color(0.85f, 0.41f, 0, 1f);
  52.  
  53.             gl.Rotate(0,rotation++,rotation/2);
  54.  
  55.  
  56.             gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBufferObjectIds[0]);
  57.             gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
  58.             gl.EnableVertexAttribArray(0);
  59.             gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));
  60.  
  61.             gl.DrawElements(OpenGL.GL_TRIANGLES, indices.Length, indices);
  62.         }
  63.  
  64.         float rotation = 0;    
  65.  
  66.         /// <summary>
  67.         /// Handles the OpenGLInitialized event of the OpenGLControl control.
  68.         /// </summary>
  69.         /// <param name="sender">The source of the event.</param>
  70.         /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
  71.         private void OpenGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
  72.         {
  73.             OpenGL gl = args.OpenGL;
  74.  
  75.             gl.Enable(OpenGL.GL_DEPTH_TEST);
  76.             gl.ShadeModel(OpenGL.GL_SMOOTH);
  77.  
  78.             gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);
  79.  
  80.             vertices = new float[]
  81.             {                
  82.                 0, 0, 0, // 0 bottom back left
  83.                 1, 0, 0, // 1 bottom front left
  84.                 1, 1, 0, // 2 top front left
  85.                 0, 1, 0, // 3 top back left
  86.                 1, 0, 1, // 4 bottom front right
  87.                 1, 1, 1, // 5 top front right
  88.             };
  89.  
  90.             indices = new uint[]
  91.             {
  92.                 0, 1, 2, // left bottom triangle
  93.                 2, 3, 0,  // left top triangle
  94.                 1, 4, 5, // front bottom triangle
  95.                 5, 1, 2, // front top triangle
  96.             };
  97.  
  98.             gl.GenBuffers(1, vertexBufferObjectIds);
  99.             gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBufferObjectIds[0]);
  100.  
  101.             unsafe
  102.             {
  103.                 fixed (float* verts = vertices)
  104.                 {
  105.                     var ptr = new IntPtr(verts);
  106.                     gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices.Length * sizeof(float), ptr, OpenGL.GL_STATIC_DRAW);
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement