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);
			GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
			                       new IntPtr(vertices.Length * Vector3.SizeInBytes),
			                       vertices, BufferUsageHint.StaticDraw);
		}
 
		protected override void OnLoad(EventArgs e)
		{
			GL.ClearColor(Color.Brown);
			CreateVertexBuffer();
		}
 
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			GL.Clear(ClearBufferMask.ColorBufferBit);
 
			GL.EnableVertexAttribArray(0);
			GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
			GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
 
			GL.DrawArrays(BeginMode.Triangles, 0, 3);
 
			GL.DisableVertexAttribArray(0);
 
			SwapBuffers();
		}
 
 
		public static void Main(string[] args)
		{
			using (Program p = new Program())
			{
				p.Run(60);
			}
		}
	}
}
Inline Images

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
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)

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.
http://learningwebgl.com/blog/?page_id=1217

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...
about:config
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

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.

http://www.opengl.org/sdk/docs/man4/

texted's picture

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