I use table in type like:
type
RenderableObject* = object
vertices_tbl*: Table[int, seq[float32]]
indices_tbl*: Table[int, seq[uint32]]
# init
var FACE : RenderableObject
FACE.vertices_tbl = initTable[int, seq[float32]]()
FACE.indices_tbl = initTable[int, seq[uint32]]()
#init the seq
proc CreateFace(f: int, pos: int) =
if not hasKey(FACE.vertices_tbl, pos):
FACE.vertices_tbl[pos] = @[]
FACE.indices_tbl[pos] = @[]
but in OpenGL function where i specify the container
proc CreateBuffers*(obj: RenderableObject) {.inline.} =
# # #
glBufferData(GL_ARRAY_BUFFER, obj.vertices_tbl[f].len*sizeof(GLfloat), obj.vertices_tbl[f][0].unsafeAddr, GL_STATIC_DRAW)
# # #
It says it cant get the address.
Is it because its not yet initialized? if I pass it with var like obj: var RenderableObject it works, but that kinda defeats the purpose and its very slow
From: https://nim-lang.org/docs/manual.html#statements-and-expressions-the-addr-operator
The addr operator returns the address of an l-value.
From: https://nim-lang.org/docs/manual.html#procedures-var-return-type
A proc, converter or iterator may return a var type which means that the returned value is an l-value and can be modified by the caller:
In other words, when a return type is not a var type, it is not an l-value.
From: https://nim-lang.org/docs/tables.html#%5B%5D%2CTable%5BA%2CB%5D%2CA https://nim-lang.org/docs/tables.html#%5B%5D%2CTable%5BA%2CB%5D%2CA_2
proc `[]`A, B: B
proc `[]`A, B: var B
So in your CreateBuffers proc, proc `[]`[A, B](t: Table[A, B]; key: A): B is called and it doesn't return var type. You can get an address only from an l-value. That is why you cannt use unsafeAddr in your code.
You can not access seq[0] when the lenght of the sequence is 0.
I think your code shoulde be like:
proc CreateBuffers*(obj: var RenderableObject) {.inline.} =
# # #
glBufferData(GL_ARRAY_BUFFER, obj.vertices_tbl[f].len*sizeof(GLfloat), if obj.vertices_tbl[f].len == 0: nil else: obj.vertices_tbl[f][0].unsafeAddr, GL_STATIC_DRAW)
if I pass it with var like obj: var RenderableObject it works, but that kinda defeats the purpose of using tables and its very slow
I don't understand neither why you are using tables in your code nor why var defeats the purpose of using tables only from your code.
When im creating the faces, i know their exact number, but to speedup the rendering, I want to merge them by their common texture. I dont know the number of the merged faces, so I have to make one loop trough all the faces and find the number of unique texture/lightmap combinations.
Then from this number i have the size of the render data (number of merged faces) Then I do another loop to fill the data.
With tables I dont have to do that, I can do that in the first loop