Using Vertex Arrays with OpenTK

Using Vertex Arrays with OpenTK

Content Index :

Using Vertex Arrays with OpenTK
Tag : chash , By : John Tate
Date : November 29 2020, 09:01 AM

I wish this help you The unsafe route didn't work because new IntPtr(data + 8) will point 8 vertices ahead instead of pointing 8 bytes ahead. You can fix this by using IntPtr.Add method:
fixed (Vertex* data = vertices)
  var stride = Marshal.SizeOf<Vertex>();
  GL.VertexPointer(2, VertexPointerType.Float, stride, 
      new IntPtr.Add(new IntPtr(data), 0));
  GL.TexCoordPointer(2, TexCoordPointerType.Float, stride, 
      new IntPtr.Add(new IntPtr(data), 8));
  GL.ColorPointer(4, ColorPointerType.Float, stride,
      new IntPtr.Add(new IntPtr(data), 16));

  GL.DrawArrays(PrimitiveType.Quads, 0, vertices.Length);

fixed (void* posPtr = &vertices[0].x, texPtr = &vertices[0].u, colorPtr = &vertices[0].r)
  var stride = Marshal.SizeOf<Vertex>();
  GL.VertexPointer(2, VertexPointerType.Float, stride, 
      new IntPtr(posPtr));
  GL.TexCoordPointer(2, TexCoordPointerType.Float, stride, 
      new IntPtr(texPtr))
  GL.ColorPointer(4, ColorPointerType.Float, stride,
      new IntPtr(colorPtr))

  GL.DrawArrays(PrimitiveType.Quads, 0, vertices.Length);

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Vertex
   public float x, y;
   public float u, v;
   public float r, g, b, a;
public struct Vertex
    public Vector2<float> Pos;        
    public Vector2<float> Texcoord;        
    public Vector4<byte> Color;

public struct Vector2<T>
    public T x, y;

public struct Vector3<T>
    public T x, y, z;

public struct Vector4<T>
    public T x, y, z, w;
fixed (void* posPtr = &vertices[0].Pos.x, 
    texPtr = &vertices[0].Texcoord.x, 
    colorPtr = &vertices[0].Color.x)

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

For a 3d CAD program, should I remove my use of Vertex Arrays/Display lists in favor of Vertex buffer objects?

Tag : java , By : hellboy32
Date : March 29 2020, 07:55 AM
hop of those help? Vertex Buffer Objects are essentially Vertex Arrays, where instead of pointing to a address in your program's process address space, OpenGL gives you a handle to OpenGL managed memory and the Vertex Array pointers are offsets into the memory given out by that handle.
It is actually very easy to add VBO support to programs that already make use of Vertex Arrays. It's as easy to conditionally use VBOs if they are available and fall back to client addess space Vertex Arrays if not.

how to convert OpenGL code using vertex arrays into code using vertex buffer objects?

Tag : c , By : hellboy32
Date : March 29 2020, 07:55 AM
To fix the issue you can do Using GLEW for extension wrangling:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>

typedef struct vector /*dynamic vector of void* pointers. This one is used only by the deflate compressor*/
  void* data;
  size_t size; /*in groups of bytes depending on type*/
  size_t allocsize; /*in bytes*/
  unsigned typesize; /*sizeof the type you store in data*/
} vector;

static unsigned vector_resize(vector* p, size_t size) /*returns 1 if success, 0 if failure ==> nothing done*/
  if(size * p->typesize > p->allocsize)
    size_t newsize = size * p->typesize * 2;
    void* data = realloc(p->data, newsize);
      p->allocsize = newsize;
      p->data = data;
      p->size = size;
    else return 0;
  else p->size = size;
  return 1;

static void vector_cleanup(void* p)
  ((vector*)p)->size = ((vector*)p)->allocsize = 0;
  ((vector*)p)->data = NULL;

static void vector_init(vector* p, unsigned typesize)
  p->data = NULL;
  p->size = p->allocsize = 0;
  p->typesize = typesize;

static void* vector_get(vector* p, size_t index)
  return &((char*)p->data)[index * p->typesize];

/* function to calculate each data point */
float func(float x)
    return (float)sin(x);

GLuint vbo = 0;
GLsizei vertcount = 0;
void update(float (* func)(float x), float x1, float x2, int N)
    float x, dx = 1.0f/N;
    vector pts;

    vector_init( &pts, sizeof( float ) );
    for(x = x1; x < x2; x += dx)
        vector_resize( &pts, pts.size + 2 );
        *(float*)vector_get( &pts, pts.size-2 ) = x;
        *(float*)vector_get( &pts, pts.size-1 ) = func(x);

    vertcount = (GLsizei)( pts.size / 2 );
    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glBufferData( GL_ARRAY_BUFFER, pts.size * pts.typesize, pts.data, GL_DYNAMIC_DRAW );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );

    vector_cleanup( &pts );

/* plotting function - very slow */
void draw(float x1, float x2, float y1, float y2)

    glScalef( 1.0f / (x2 - x1), 1.0f / (y2 - y1), 1.0f );
    glTranslatef( -x1, -y1, 0.0f );
    glColor3f( 1.0f, 1.0f, 1.0f );

    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 2, GL_FLOAT, 0, 0 );
    glDrawArrays( GL_LINE_STRIP, 0, vertcount );
    glDisableClientState( GL_VERTEX_ARRAY );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );


/* Redrawing func */
float xmin = -10, xmax = 10, ymin = -5, ymax = 5;
void redraw(void)
    glClearColor(0, 0, 0, 0);

    // -x, +x, -y, +y, number points
    draw(xmin, xmax, ymin, ymax);


/* Idle proc. Redisplays, if called. */
int nPoints = 3000;
void idle(void)
    // shift 'xmin' & 'xmax' by one.

    update(func, xmin, xmax, nPoints);


/* Key press processing */
void key(unsigned char c, int x, int y)
    if(c == 27) exit(0);

/* Window reashape */
void reshape(int w, int h)
    glViewport(0, 0, w, h);
    glOrtho(0, 1, 0, 1, -1, 1);

/* Main function */
int main(int argc, char **argv)
    GLenum err;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("Graph plotter");
    glutReshapeWindow(1024, 800);

    // init GLEW and output some GL info
    err = glewInit();
    printf("GL_VERSION  : %s\n", glGetString(GL_VERSION)  );
    printf("GL_VENDOR   : %s\n", glGetString(GL_VENDOR)   );
    printf("GL_RENDERER : %s\n", glGetString(GL_RENDERER) );
    if( GLEW_OK != err )
        printf("glewInit failed: %s", glewGetErrorString(err));
        return EXIT_FAILURE;

    if( !glewIsSupported("GL_VERSION_1_5") )
        printf("OpenGL version 1.5 or greater required.\n");
        return EXIT_FAILURE;

    glGenBuffers( 1, &vbo );

    /* Register GLUT callbacks. */

    /* Init the GL state */

    /* Main loop */
    return 0;

Swizzling Vertex Arrays, Vertex Buffer Objects and Shaders

Tag : opengl , By : Giles
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have a basic understanding of Vertex Arrays, Vertex Buffer Objects and Shaders. However, I am not sure if I can reuse them or whether the association is bijective. , Yes you can :)
VBO - controls where your data is stored

can i use opentk and xna in the same project and showing the data on the same window? Does OpenTk Support Xna?

Tag : opengl , By : THD
Date : March 29 2020, 07:55 AM
wish helps you XNA is using Direct3d, whereas OpenTK is OpenGL. In the general case, you cannot draw using OpenGL into a Direct3d window.[1]
What you can do is switch from XNA to MonoGame. It is an open-source implementation of XNA and supports both Direct3d and OpenGL. If you use the OpenGL version, you will be able to draw into the same window using both XNA and OpenTK.

Whats the sizeof C# structs for vertex representation in OpenGL (OpenTK)?

Tag : chash , By : rixtertech
Date : March 29 2020, 07:55 AM
hope this fix your issue Apply the [StructLayout(LayoutKind.Explicit)] attribute to the struct and then apply [FieldOffset(n)] to each field to position it at your selected byte position, n.
This gives you fine-grained control of position, and since the sizes of primitive types in C# do not depend on the platform (64 vs 32 bit) that also tells you the size and complete layout.
Related Posts Related QUESTIONS :
  • Using Attributes to Override Data Model Conventions
  • Basic OOP console calculator, result Error
  • Compositon and Repository pattern
  • Multiple using statements with if condition
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • How to add a column in an existing AspNetUsers table
  • Order a list of elements with another list of doubles
  • How to setup a NuGet package to copy content files to output build directory?
  • In SignalR Core using ChannelWriter: Do I need to call TryComplete twice if there's an exception?
  • C# GetProcessesByName: issue with colon
  • c# wpf | create complex object with user-defined name to Serialize into JSON
  • How can I get a instance of a generic list with reflection?
  • WPF XAML - Design time and visibility of textbox
  • EF Core and MySql query is too slow
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • .Net Core Binding
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • Loading a pop up page in ASP.net through a js file
  • How to pass alert or notification message from controller to View?
  • C# to pause, turn on ssas server, backup cube.... how to?
  • How to execute DataTable.Select() for a column of custom class type for a particular element in that C#
  • how to connect mysql8.0 with C#
  • Passing incorrect values into MultiValueConverter by MultiBinding
  • Can i use IEnumerator as Update func?
  • How to convert API Json response to C# Array?
  • Blazor Textfield Oninput User Typing Delay
  • Performing both layout and render transform results in wrong output
  • uwp beforetextchanged cursor moving in front of text
  • How to keep duplicates from a string[] exclude words from a List and print them out
  • .Net Core Strings.Asc/Mid/Chr/Len missing even after importing Microsoft.VisualBasic
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com