New tuto using the new GCORE and final version !
I puting this here, to don't make confusion with other.
First of all, please download the file attached to this post, (not in the GLib main thread, 'cause I didn't update it) (GCORE).
Last version is 2.0 omega
Place it preferably in archive : warning ! this file is pretty huge (6400 bytes !!).
I THE BASISLet's create a rotating cube !
First of all, include the GCORE prog inside of your axe source :
prgmGCORE
Once you did this you have access to all the GLib functions.
We need to define the 3D coordinates of each point of the cube, so we can rotate and display them afterwards.
Those points are just a bunch of data, so no real explanations are needed here :
Data(40r,-40r,40r)->GDB1
Data(40r,40r,40r
Data(-40r,-40r,40r
Data(-40r,40r,40r
Data(40r,-40r,-40r
Data(40r,40r,-40r
Data(-40r,-40r,-40r
Data(-40r,40r,-40r
Just remember that a 3D point is always defined as X,Z,Y using the standard 3D axis (X goes from left to right, Y from down to up and Z from near to far). Each coordinate takes 2 bytes, so don't forget the
r.
By default GLib has no camera defined. So we must specify what type of camera we want to use.
In this example, I want to be able to rotate my cube : this is a GMODELVIEW camera (I don't want the camera to move, only the model to rotate).
The function to define a camera is :
GNewCam(pointer to camera)
.Destroys r1
**But but but .... I don't know how to create my camera, and I need a pointer to it !
STOP don't worry, GLib provides two pointers to some really basic cameras ! These are the °GMODELVIEW and °GWORLDVIEW cameras.
So I just give one of those pointers to the function and my new camera is created :
GNewCam(°GMODELVIEW)
Glib also need a special structure, allowing easy vertex processing/clipping. It's called a VBO : vertex buffer object. Basically, it's just a ram area.
You need at the begining of your program to generate such structure :
This is the command used :
GGenVBO(size,data_to_match)
.destroy r1,r2
your size is the number of vertices you want to have.
data_to_match is the raw (not processed) vertices coordinates (here it's GDB1)
It also set the current VBO to the one created , so it will be used by all other command.
The function return a special value, that you have to store somewhere : it's the VBO id.
We now have this :
GGenVBO(8,GDB1)->G
.I used G, but you can used any way to save this value
Now we can start the main loop, with 3D calculations :
While 1
GUpdateVBO(G,0,7)
this piece of code "update" (transform the vertices) for the specified VBO. You can see that we are passing the id we have previously store.
0 and 7 are the start and the end of vertices we should transform. Here it transform from the vertx 0,...., to the vertx 7 (the last one)
And now we want display our points.
GLib provide the
GVBOPoint(id_vertex)
.destroy r1
function to retrieve a point from VBO. It's store the x-coord and y-coord on screen to GScreenX and GSreenY var (alias OP1 and OP1+2)
It also return in hl a clipping information : 0 if the point is visible, anything else (=/= from 0)
We can now write this :
For(M,0,7)
GVBOPoint(M)
!If
Rect(GScreenX,GScreenY,2,2
End
End
the code loop from the first vertice, to the last one, retrieve on screen coordinate and then disp it if it is visible.
We need now to handle the arrow keys, so we can rotate our cube. GLib provides an in-built function, which handles everything (life is beautiful isn't it
)
It's the GGetkey function.
The call is:
GGetkey()
End of the code :
GGetkey()
DispgraphClrdraw // we want to have something on screen !
EndIf getkey(15) // stop the While loop if [clear] is pressed
Now time to compile it ... aaaaaaaaaand ...
It works !