Sorry, due to a server error, these pages are currently not working. I'll fix it as soon as possible.

OpenGL for .NET

CZ EN
Tato stránka není k dipozici v českém jazyce ...

Code Snippets

Shortest OpenGL program

This is one of the shortest examples of OpenGL program with working loop. It creates a form with OpenGL context and change the background to red color in 5 second interval.

more/less...
using System;
using System.Windows.Forms;
using OpenGL;

namespace ShortestOpenGL
{
    class Program : Form
    {
        RenderingContext rc;

        void Init()
        {
            rc = RenderingContext.CreateContext(this);
        }

        void Render()
        {
            gl.ClearColor((float)Math.Sin(DateTime.Now.Ticks/5000000.0), 0, 0, 0);
            gl.Clear(GL.COLOR_BUFFER_BIT);
            rc.SwapBuffers();
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case Windows.WM_PAINT: Render(); break;
                default: base.WndProc(ref m); break;
            }
        }

        static void Main(string[] args)
        {
            Program program = new Program();
            program.Init();
            Application.Run(program);
        }
    }
}

Vertex Array Object

Vertex Array Object (VAO) are OpenGL objects that can store a set of bindings between user data and vertex attributes. The typical way to use it, is create a VAO object (glGenVertexArrays), bind it (glBindVertexArray), then bind and copy vertex and index data (glBindBuffer, glBufferData) and finally specify the location of vertex attrbibutes and eneable them (glEnableVertexAttribArray, glVertexAttrbiPointer).

more/less...

Structures

It is possible to send several different arrays for each attribute or you can use interleaved data, e.g.:
	struct VertexPositionColorRG
        {
            public float Px, Py, Pz;
            public float R, G;

            public static int size
            {
                get { return Marshal.SizeOf(typeof(VertexPositionColorRG)); } 
            }
        }

Initialization


            vdataQuad = new VertexPositionColorRG[] { 
                new VertexPositionColorRG() { Px = -.9f, Py=-.9f, R = 0, G = 0 },
                new VertexPositionColorRG() { Px =  .9f, Py=-.9f, R = 1, G = 0 },
                new VertexPositionColorRG() { Px =  .9f, Py= .9f, R = 1, G = 1 },
                new VertexPositionColorRG() { Px = -.9f, Py= .9f, R = 0, G = 1 }
            };

            idataQuad = new byte[] { 0, 1, 2, 3 };

            // Create and bind vertex array object
            vaoQuad = gl.GenVertexArray();
            gl.BindVertexArray(vaoQuad);

            // Create and bind a buffer object for vertex data
            vbQuad = gl.GenBuffer();
            gl.BindBuffer(GL.ARRAY_BUFFER, vbQuad);

            // Copy data into the buffer object
            gl.BufferData(GL.ARRAY_BUFFER, vdataQuad.Length * VertexPositionColorRG.size, vdataQuad, GL.STATIC_DRAW);

            // Set up vertex attributes. Use glGetAttribLocation to get the attribute location in shader.
            // In this example fixed pipeline is used; 0 is the index of vertex position 
            // and 3 should be the index of color attribute.
            gl.EnableVertexAttribArray(0);
            gl.VertexAttribPointer(0, 3, GL.FLOAT, false, VertexPositionColorRG.size, (IntPtr)0);
            gl.EnableVertexAttribArray(3);
            gl.VertexAttribPointer(3, 2, GL.FLOAT, false, VertexPositionColorRG.size, (IntPtr)12);

            // Create and bind a BO for index data           
            ibQuad = gl.GenBuffer();
            gl.BindBuffer(GL.ELEMENT_ARRAY_BUFFER, ibQuad);

            // Copy data into the buffer object
            gl.BufferData(GL.ELEMENT_ARRAY_BUFFER, idataQuad.Length * sizeof(byte), idataQuad, GL.STATIC_DRAW);

            // Unbind the ready to use vertex array object
            gl.BindVertexArray(0);        

Rendering

All you need to do is bind the VAO and draw all elements:
            gl.BindVertexArray(vaoQuad);
            gl.DrawElements(GL.QUADS, idataQuad.Length , GL.UNSIGNED_BYTE, IntPtr.Zero);

GUI integration

It is very simple to integrate an OpenGL context into .NET Windows form. This snippet is created from standard WinForm application. For the snippet purpose, the buttons are created by hand, but you can simply use the designer to make a rich GUI.

more/less...
public partial class Form1 : Form
    {
        RenderingContext rc;
        Button red;
        Button green;
        Panel glview;

        public Form1()
        {
            InitializeComponent();
            // Create red button, dock it and register the click event
            red = new Button();
            red.Parent = this;
            red.Text = "red";
            red.Dock = DockStyle.Top;
            red.Click += new EventHandler(red_Click);
            // Create green button, dock it and register the click event
            green = new Button();
            green.Parent = this;
            green.Text = "green";
            green.Dock = DockStyle.Top;
            green.Click += new EventHandler(green_Click);
            // Create panel for OpenGL and use it for RC creation
            glview = new Panel();
            glview.Parent = this;
            glview.Dock = DockStyle.Fill;
            rc = RenderingContextWin.CreateContext(glview);
        }

        void green_Click(object sender, EventArgs e)
        {
            gl.ClearColor(0, 1, 0, 0);
            Render();
        }

        void red_Click(object sender, EventArgs e)
        {
            gl.ClearColor(1, 0, 0, 0);
            Render();
        }

        void Render()
        {
            gl.Clear(GL.COLOR_BUFFER_BIT);
            rc.SwapBuffers();
        }
    }

Poslední úprava: 23.07.2010