Offset MemoryBlock Starting Position in Interleaved Array


I am working on OpenGL and would like to know how to change the starting position parameter of a memoryblock.

The interleaved array has a set of repeating data, 3 bytes for position (1,1,-1), 3 bytes for colour (0.18,0.31,0.30), and repeats for position and colour. Here is an example:

MyMBCube is an array with this interleaved data (1,1,-1, 0.18, 0.31, 0.30, -1,1,-1, 0.44, 0.86, 0.26, ... etc.)

The first line of OpenGL glVertexPointer code works well, which reads 3-bytes at a time, is a double value, and repeats every 48 bits. The glColorPointer must begin reading the MemoryBlock at an offset byte position 3 because of the interleaved data. I jokingly put MyMBCube(3) to show that the passed parameter of the MyMBCube would start at position 3 and not at position zero.

OpenGL.glVertexPointer(3, OpenGL.GL_DOUBLE, 48, MyMBCube) //load the data OpenGL.glColorPointer(3, OpenGL.GL_DOUBLE, 48, MyMBCube(3))

I can copy MyMBCube data into another array and pass the modified memoryblock, but then this defeats the purpose of saving memory and increased performance as two memoryblocks will be pushed to the videocard for writing, one for position and the other for colour. This memoryblock data is being sent at about 60 times per second.

How do I pass the memoryblock parameter to begin at position 3 when the glColorPointer parameter requires a memoryblock type?

Thanks :slight_smile:

Can you cast the memoryblock to ptr, then cast to integer, add the offset, then cast back to ptr and cast back to memoryblock?

HI Jim,

My initial thought after working with this is that when I cast the moved pointer back to the memoryblock, then the next time the image is drawn, I will need to undo the recasting when the next cycle is to use the glVertexPointer, as the memoryblock will be shifted.

I wasn’t able to convert to an integer, and this is the code that I have which symantically works, but there is a logical error that is causing an issue. It could be the pointer to integer step that you mentioned which I can’t figure out.

OpenGL.glVertexPointer(3, OpenGL.GL_DOUBLE, 48, MyMBCube) //load the data Dim MyP as Ptr = MyMBCube MyP = MyP + Ptr(24) MyMBCube = MyP OpenGL.glColorPointer(3, OpenGL.GL_DOUBLE, 48, MyMBCube)

You don’t want to use ptr(24) as that would get the pointer value stored at byte 24 rather than a ptr to the 24th byte. I’m not at my computer but you should be able to do something like

Dim myP as ptr= MyMBCube
Dim myI as integer=ctype(myP,Integer)
Dim newMB as memoryblock=myP

Don’t reuse myMBCube, to keep the original memoryblock intact. NewMB above is basically just a wrapper around the ptr. No copying is done.

This is what I use, basically what’s already said but on one line going from the Memoryblock to a Ptr pointing to 4 bytes past the start.

srcMem.Ptr(0) = Ptr(Integer(CType(srcMem, Ptr)) + 4)

Here’s some notes I’d made working out how to go from MemoryBlock <-> Ptr <-> Integer

[code] implicit explicit CType
MemoryBlock <-> Ptr yes no yes
Ptr <-> Integer no yes no

dim m As MemoryBlock, p As Ptr, i As Integer

m = p //implicit
m = Ptr(i) //explicit to Ptr then implicit to MemoryBlock

p = m //implicit
p = Ptr(i) //explicit

i = Integer(CType(m, Ptr)) //CType to Ptr then explicit to Integer
i = Integer§ //explicit

i = CType(p, Integer) //err
p = CType(i, Ptr) //err[/code]

Thank you to both Jim (for getting my head wrapped around the conversions steps) and Will (for the handy one-line of code).

This solution works great! I hope your having a good weekend.