-1

I wrote an N-body code in C# which outputs an array with x,y,z positions of N objects. I want to render it on a screen frame by frame using OpenTK.

Basically, I need to figure out how to create an array of vertices using VBO-s, show it on a screen, and update it periodically. Since I have zero experience in graphics, I tried to modify an example I found on the web, it works for one vertex but not an array.

Here's the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
using System.Drawing;

namespace OpenTK
{
class Program : GameWindow

{

    int vbo;

    void CreateVertexBuffer()
    {
        Vector3[] vertices = new Vector3[2];
        vertices[0] = new Vector3(0f, 0f, 0f);
        vertices[1] = new Vector3(1f, 7f, 6f);

        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(0,0,0,0);
        GL.PointSize(5f);
        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.Points, 0, 1);

        GL.DisableVertexAttribArray(0);

        SwapBuffers();
    }


    public static void Main(string[] args)
    {
        using (Program p = new Program())
        {
            p.Run(60);
        }
    }
}
}
fbartolic
  • 403
  • 1
  • 5
  • 18

1 Answers1

1

There are a few things that looks off:

  • The second vertex coordinate is wrong. It must be normalized in [-1,1 ] range. Either you do that when you create the array or you do transformation in your vertex shader
  • It appears that you don't have a shader
  • You are drawing only one point, the first one.

I recommend going through this tutorial series, it is an excellent learning resource. Though it uses C++, the concepts are exactly the same, and the function names are pretty much the same too