I am trying to create some types/procs to be able to use OpenGL functionality 
in Nim. I started with the Opengl Program object. I created a proc that 
compiles a vertex and fragment shader, then links it into a program and stores 
the id in the Program object. This works.

But to perform some error checking (checking errors in shader code or in 
linker), you need to pass a pre-allocated cstring to a function. I don't know 
how to do that and I don't seem to be able to find it in the manual. I tried 
several things, and the last one was creating a sequence of chars and passing 
the addr but it doesn't work.

How do you do this?

Here's a part the code:
    
    
    import opengl
    type
      openglProgram = object
        id: GLuint
        ...
    
    proc createOpenglProgram(vert: string, frag: string) : openglProgram =
      result.id = glCreateProgram()
      
      var status: GLint
      let vs = glCreateShader(GL_VERTEX_SHADER)
      let verts = allocCStringArray([ vert ])
      glShaderSource(vs, 1.GLsizei, verts, nil)
      glCompileShader(vs)
      glGetShaderiv(vs, GL_COMPILE_STATUS, status.addr)
      if status != GL_TRUE.GLint:
        var length: GLint
        glGetShaderiv(vs, GL_INFO_LOG_LENGTH, length.addr)
        var msg = cstring # <= this is the one that should be preallocated to 
be of size 'length'
        glGetShaderInfoLog(vs, length, nil, msg) # <= this gives error because 
msg is a null pointer
        echo "Error in vertex shader: " & $msg
      glAttachShader(result.id, vs);
      ...
    
    
    Run

How?

Also: coding this is really a pain, it's ugly, and gives me a headache... I 
don't mind doing it once because after that, this code is never to be touched 
again, but if there is a way to do it better than this, please let me know. I'm 
still very new to Nim.

Also: when passing the cStringArray (in earlier call glShaderSource), I need to 
pass the size of that array. I _know_ it is 1 so I hard code it, but is there a 
way to get the size from the cStringArray?

Reply via email to