OpenGL and Vertex Arrays

Advanced game related topics
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

OpenGL and Vertex Arrays

Post by Polo »

I hear the Vertex Arrays were a lot much faster, so I want to use them, with Purebasic of course, but I don't know really how... Does someone know how to ?

PS : Please don't link me to a tutorial page with C++ code :)
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: OpenGL and Vertex Arrays

Post by traumatic »

Polo wrote:I hear the Vertex Arrays were a lot much faster[...]
IMHO that's not always the truth. Sometime displaylists win, sometimes vertexarrays are faster. I think it depends on what you're trying to do.
PS : Please don't link me to a tutorial page with C++ code :)
No tutorial? Fine...

You can do it exactly like in C, here's a small code example, called the obligatory "RenderMeACubeAndMakeMeHappyExample"

Code: Select all

Dim vertices.f(23)
Dim indices.b(23)

Restore vertices
For i=0 To 23
  Read vertex.f
  vertices(i) = vertex
Next

Restore indices
For i=0 To 23
  Read index.b
  indices(i) = index
Next

DataSection
  vertices:
    Data.f -1.0,  1.0,  1.0
    Data.f  1.0,  1.0,  1.0
    Data.f  1.0, -1.0,  1.0
    Data.f -1.0, -1.0,  1.0
    Data.f -1.0,  1.0, -1.0
    Data.f  1.0,  1.0, -1.0
    Data.f  1.0, -1.0, -1.0
    Data.f -1.0, -1.0, -1.0

  indices:
    Data.b 0, 1, 2, 3
    Data.b 4, 5, 1, 0
    Data.b 3, 2, 6, 7
    Data.b 5, 4, 7, 6
    Data.b 1, 5, 6, 2
    Data.b 4, 0, 3, 7
EndDataSection
Later, you can render it like this:

Code: Select all

glEnableClientState_(#GL_VERTEX_ARRAY)
glVertexPointer_(3, #GL_FLOAT, 0, vertices())
glDrawElements_(#GL_QUADS, 24, #GL_UNSIGNED_BYTE, indices())
Voilá... no tutorial :wink:
Good programmers don't comment their code. It was hard to write, should be hard to read.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

lol @ "renderMeACubeAndMakeMeHappy" :D

What values are #GL_VERTEX_ARRAY, #GL_FLOAT, #GL_QUADS, #GL_UNSIGNED_BYTE ?

And is there a tutorial for beginners explaining vertices and indices? Or life, the universe and everything?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

Dare2 wrote:And is there a tutorial for beginners explaining vertices and indices? Or life, the universe and everything?
42
SPAMINATOR NR.1
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

What values are #GL_VERTEX_ARRAY, #GL_FLOAT, #GL_QUADS, #GL_UNSIGNED_BYTE ?
They are defined in the opengl include.
And is there a tutorial for beginners explaining vertices and indices? Or life, the universe and everything?
Well, just take your favourite searchengine. There are a lot of tutorials on this.
OpenGL-related I can't recommend http://nehe.gamedev.net often enough. The OGL-Redbook (http://www.opengl.org)
also explains some basics of 3d, like the things you were asking for.

Other than that, I guess the best way to learn this kind of stuff is to read lots of books and to try.

Feel free to contact me on further questions.

And yes, 42...
Good programmers don't comment their code. It was hard to write, should be hard to read.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Ah 42!

Well, so long, and thanks for all the fishes.

:D
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

instead of putting the vertices in arrays, can we put the vertices in memory ? If yes, how ?
pg
User
User
Posts: 75
Joined: Wed Jun 18, 2003 3:31 pm
Location: Swiss
Contact:

Post by pg »

If they are in arrays they are allready in memory, you could access the memory location like this

address.l = @myarray(0).

or you can reserve memory

memaddress.l = AllocateMemory(0, size, 0)

and write to memory

PokeF(memaddress, vertex) // float values

and read
value.f=PeekF(memaddress)

for each new value you must increment the memoryaddress


you can test it with this code:

memaddress.l = AllocateMemory(0, 100, 0)
PokeF(memaddress, 125.23)
value.f=PeekF(memaddress)
MessageRequester("value read from memory: ", StrF(value), 0);
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Thanks for the reply !
But that's not what I mean, I know how to use the memory, I mean, instead of putting :
glVertexPointer_(3, #GL_FLOAT, 0, vertices())
vertices is a Dim array, and I want to know if we can use the memory for that, if I use your example :
memaddress.l = AllocateMemory(0, 100, 0)
PokeF(memaddress, 125.23)
PokeF(memaddress+4, 125.23)
PokeF(memaddress+8, 125.23)

[...]

;and....

glVertexPointer_(3, #GL_FLOAT, 0, memaddress.l)
Well, perhaps not like that, but you see what I mean ?
pg
User
User
Posts: 75
Joined: Wed Jun 18, 2003 3:31 pm
Location: Swiss
Contact:

Post by pg »

sorry, I see....

I think this must work, it's just a memory address



void glVertexPointer(GLint size,
GLenum type,
GLsizei stride,
const GLvoid *pointer)

pointer Specifies a pointer to the first coordinate of the first vertex in the array
pg
User
User
Posts: 75
Joined: Wed Jun 18, 2003 3:31 pm
Location: Swiss
Contact:

Post by pg »

something like this if the first data is at 0...

glVertexPointer_(3, #GL_FLOAT, 0, @vertices(0))
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

I can't put @vertices(0) if I'm using memory...
pg
User
User
Posts: 75
Joined: Wed Jun 18, 2003 3:31 pm
Location: Swiss
Contact:

Post by pg »

Yes, I did reboot my brain

that how I would do it, maybe you too?

Restore vertices
add.w=0
For i=0 To 23
Read vertex.f
PokeF(memaddress+add, vertex)
add=add+4
Next
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

I do that : I have one memory with the vertices, one with the indexes. The fact is I want to use these stuffs which are in memory, and that I don't know how to do...
RonStyle
User
User
Posts: 10
Joined: Sat Jan 17, 2004 11:35 pm

Post by RonStyle »

Display lists will probably be alot faster than anything held in local sys-mem.(Depending on your card, display lists are usually stored within AGP memory, which is ddr on decent cards, and much closer to the gpu)

not to mention encapsulating everything needed to render the model in one ultra-tiday call.(Obviously not the reason to use it, but a nice side-effect ;) )
Post Reply