Based on this post at stackoverflow i'm wondering if this reason is why i'm getting flickering vertices in opengl.
basically i think the post suggests that u cannot call a swapbuffer or glclear proc or function in a seperate object? So basically i'm wondering if this is an issue in my program. here is the main.nim file
import os, glfw/glfw3 as glfw
import "opengl-1.0/opengl"
import model , matrix_loop
enableAutoGLErrorCheck(true)
proc stencil_on() =
glEnable(GL_STENCIL_TEST.GLenum)
glStencilFunc(GL_ALWAYS.GLenum,1.GLint,GLuint(0xFF))
glStencilOp(GL_KEEP.GLenum,GL_KEEP.GLenum,GL_REPLACE.GLenum)
glStencilMask(GLuint(0xFF))
glDepthMask(GL_FALSE.GLBoolean)
glClear(GL_STENCIL_BUFFER_BIT.GLenum)
#glColorMask(GL_FALSE.GLboolean,GL_FALSE.GLboolean,GL_FALSE.GLboolean,GL_FALSE.GLboolean)
#model.draw(Win)
glStencilFunc(GL_EQUAL.GLenum,1.GLint,0xFF)
glStencilMask(GLuint(0x00))
glDepthMask(GL_TRUE.GLBoolean)
proc stencil_off() =
glDepthMask(GL_TRUE.GLboolean)
glDisable(GL_STENCIL_TEST.GLenum)
proc main() =
var Win: glfw.Window
if glfw.Init() == cint(0):
echo(" glfw did not init/n")
quit()
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR,2)
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR,0)
Win = glfw.CreateWindow(800.cint,600.cint,"Opengl Window",nil,nil)
glfw.MakeContextCurrent(Win)
loadExtensions()
# when defined(useGlew):
# if glewInit() == 0:
# echo(" glew did not init/n")
# quit()
model.load("frag.glsl","vert.glsl","u.obj")
#glEnable(GL_TEXTURE_2D.GLenum)
#glEnable(GL_CULL_FACE.GLenum)
glEnable(GL_DEPTH_TEST.GLenum)
#glfw.SetCursorPosCallback()
while glfw.WindowShouldClose(Win) == 0:
#glDepthRange(-0.01.GLfloat,1000.0.GLfloat)
glViewport(0,0,800,600)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glClearColor(0.1,0.1,0.1,1.0)
#glStencilMask(GLuint(0xFF))
model.draw(Win)
glfw.SwapBuffers(Win)
glfw.PollEvents()
glfw.Terminate()
main()
But based of the post the gist of the idea is that u will have flickering vertices if u call glfw.SwapBuffers(Win) or glClear() in different objects so i'm wondering if maybe Garbage Collection is an issue? Or if there is a problem with calling main() like this or is it something else :)
And i don't have a swapbuffer call or a glClear call anywhere else. To be clear!
edit: this is a opengl problem i think now, i'll keep looking for different solutions
i think i tracked down where the problem is occuring it seems that opengl does like seq's
for instance this works but if i change it..
var
data_t: array[18,GLfloat] = [-1.0.GLfloat,1.0.GLfloat,1.5.GLfloat,1.0.GLfloat,1.0.GLfloat,1.5.GLfloat,-1.0.GLfloat,-1.0.GLfloat,1.5.GLfloat,1.0.GLfloat,1.0.GLfloat,1.0.GLfloat,1.0.GLfloat,-1.0.GLfloat,1.0.GLfloat,-1.0.GLfloat,-1.0.GLfloat,1.0.GLfloat]
id : array[5,GLuint]
size_a*: int
size_e*: int
proc input_3fa*[T](a:ptr seq[T]): GLuint =
#echo ( "a[].len ", a[].len)
glGenBuffers(GLint(1),id[0].addr)
glBindBuffer(GL_ARRAY_BUFFER.GLenum,id[0])
#echo(a[])
var data : ptr = data_t.addr
#var data : ptr seq[T] = a
size_a = a[].len
glBufferData(GL_ARRAY_BUFFER.GLenum, GLsizeiptr(len(data[])) * sizeof(T).GLsizeiptr, data, GL_STATIC_DRAW.GLenum)
glEnableVertexAttribArray(vert_atr.GLuint)
glVertexAttribPointer(vert_atr.GLuint,GLint(3),cGL_FLOAT.GLenum,GL_FALSE.GLboolean,GLsizei(0),nil)
return id[0]
to this
var
data_t: array[18,GLfloat] = [-1.0.GLfloat,1.0.GLfloat,1.5.GLfloat,1.0.GLfloat,1.0.GLfloat,1.5.GLfloat,-1.0.GLfloat,-1.0.GLfloat,1.5.GLfloat,1.0.GLfloat,1.0.GLfloat,1.0.GLfloat,1.0.GLfloat,-1.0.GLfloat,1.0.GLfloat,-1.0.GLfloat,-1.0.GLfloat,1.0.GLfloat]
id : array[5,GLuint]
size_a*: int
size_e*: int
proc input_3fa*[T](a:ptr seq[T]): GLuint =
#echo ( "a[].len ", a[].len)
glGenBuffers(GLint(1),id[0].addr)
glBindBuffer(GL_ARRAY_BUFFER.GLenum,id[0])
#echo(a[])
var data : ptr seq[T] = a
size_a = a[].len
glBufferData(GL_ARRAY_BUFFER.GLenum, GLsizeiptr(len(data[])) * sizeof(T).GLsizeiptr, data, GL_STATIC_DRAW.GLenum)
glEnableVertexAttribArray(vert_atr.GLuint)
glVertexAttribPointer(vert_atr.GLuint,GLint(3),cGL_FLOAT.GLenum,GL_FALSE.GLboolean,GLsizei(0),nil)
return id[0]
it does not like my seq[T], i dunno why but the array data_t works perfectly
thanks yglukhov that did the trick, i still needed to change the order i bound my buffers in the draw loop too.
many thanks!
And for objects, is there addr obj the address of the first element? I think so, but now I am not really sure
Addr of object is the addr of its first field if the object is not inheritable. Inheritable objects contain hidden isa field in the beginning.
Addr of seq is easily comprehendable if you think c++. seqs have pretty much the same semantics as c++ std::vector. It is dynamically sized, but it has copy semantics. So internally its a struct with pointer to data. Nim arrays are a lot like C arrays, except they too have copy semantics, which is kinda emulated in terms of C. Openarrays are C arrays as well.
Please note, that addr/unsafeAddr/cast/ptr semantics is backend specific. Compiling to C you get the C behavior. Compiling to JS you get something else. @Kerp, you can have a look at partable_gl.nim module in nimx lib. It abstracts away the difference between JS and C opengl, having no runtime cost for the abstraction. https://github.com/yglukhov/nimx/blob/master/nimx/portable_gl.nim