flopoloco's picture

VBO: How to draw a simple triangle

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace OpenTKVBOTriangle
	class Program : GameWindow
		int vbo;
		void CreateVertexBuffer()
			Vector3[] vertices = new Vector3[3];
			vertices[0] = new Vector3(-1f, -1f, 0f);
			vertices[1] = new Vector3( 1f, -1f, 0f);
			vertices[2] = new Vector3( 0f,  1f, 0f);
			GL.GenBuffers(1, out vbo);
			GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
			                       new IntPtr(vertices.Length * Vector3.SizeInBytes),
			                       vertices, BufferUsageHint.StaticDraw);
		protected override void OnLoad(EventArgs e)
		protected override void OnRenderFrame(FrameEventArgs e)
			GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
			GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
			GL.DrawArrays(BeginMode.Triangles, 0, 3);
		public static void Main(string[] args)
			using (Program p = new Program())
Inline Images


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
texted's picture

Could we have a detailed explanation on this code? It's very good for the beginner.

flopoloco's picture

I am not very experienced in teaching OpenGL (I could sound faulty), but I have learned the commands technically. When I want to get more information about a command then I will visit the OpenGL manual and have a look into a bit explanations.


flopoloco's picture

I have also found about WebGL tutorials, there are plenty of people working there and making tutorials and it seems that the scene is blooming.

It's better to swap knowledge (javascript to C#) this way, because javascript is more readable and of higher level than C++. Tutorials follow a more logical approach that is similar to C# and OpenTK.

P.S. If you have Firefox 4 type in the address bar...
then switch webgl.force-enabled to true (because it's WebGL is deactivated by default on Firefox 4), if you notice instability then better use Google Chrome for WebGL :P

puklaus's picture

There is plenty of webGL engines. but, javascript, I tried, not liked. C# some reason I like the most.
webGL is the most crossplatform , but I just hate script languages (like lua, python, go, etc, (YÖK) well, javascript is best of those)

izrik's picture

I'm having trouble with this example. Can someone help me out? At first I tried just pasting the code into a new C# console project and running it. The window appeared and had the red background, but no triangle was visible. I then tried replacing the call to DrawArrays with calls to GL.Begin, GL.Vertex, and Gl.End, ignoring the VBO completely. That got the triangle to show up.

I thought maybe it's poor support for VBO's on Mac hardware. To compare, I downloaded the GLEssentials sample from Apple's website (see here). That sample uses VBO's and renders correctly. The monster shows up and everything. So clearly, Mac can theoretically support VBO's.

I tried a few other OpenTK VBO samples I found on the internet, but none of them work correctly. It's like the calls to GL.DrawArrays or GL.DrawElements are being ignored. Any idea what's going on? Am I doing something wrong? Does Apple not play well with VBO's? with OpenTK? Have I unwittingly stumbled across a bug?

My system is a 13" MacBook Pro, with the built-in Intel HD Graphics 3000 chipset.
I'm using the OpenTK NuGet package, version 1.1.1589.5942 ( see here )
I'm running Xamarin Studio on Mac OSX 10.7.5. Here's my full system setup, from "About Xamarin Studio":

Xamarin Studio
Version 5.2 (build 386)
Installation UUID: 2559daa3-52ff-48dd-af35-58382d139e83
Mono 3.6.0 ((no/f540f8a)
GTK+ 2.24.23 (Raleigh theme)

Package version: 306000039

Apple Developer Tools
Xcode 4.6 (2066)
Build 4H127

Not Installed

Not Installed


Build Information
Release ID: 502000386
Git revision: e6a54dee5376e6e7a2d9982695b060fddc09e65d
Build date: 2014-08-04 14:03:28-04
Xamarin addins: 2b5a5c26ac2ee74c6e91a8d24ef44d0ca9cb74d0

Operating System
Mac OS X 10.7.5
Darwin MH39L1DV13 11.4.2 Darwin Kernel Version 11.4.2
Thu Aug 23 16:25:48 PDT 2012
root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

flopoloco's picture

Hi izrik.

As I can see in this example of Xamarin
and also as well by looking at the MonoGame source code. It looks like you could use OpenGL only through Monomac library for now. using MonoMac.OpenGL;

It might take a little time for the issue to be investigated and a solution is found.
You might need to catch up with "Fiddler" in P.M. if there's a case he won't spot this message here.

the Fiddler's picture

@flopoloco: MonoMac.OpenGL is a terribly outdated copy of OpenTK bindings without support for OpenGL 3.x/4.x or OpenGL extensions. I would recommend against using that.

You can combine MonoMac with OpenTK or you can use OpenTK on its own. The issue described here is unlikely to have anything to do with this.

The VBO example that comes with OpenTK works correctly on my Mac.

If you still have trouble, try downloading the latest version from sourceforge, which is slightly newer than the one in NuGet.

izrik's picture

Thanks! I tried the example you linked and it worked correctly. I also tried the 3.x example and it worked too. I don't know what I was doing wrong.