Author Topic: [Axe] - GLib TUTO  (Read 12449 times)

0 Members and 1 Guest are viewing this topic.

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #15 on: December 08, 2013, 12:44:13 pm »
Well well well I may have release the GPOLYGON library  ;)
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: [Axe] - GLib TUTO
« Reply #16 on: December 27, 2013, 12:57:17 pm »
I have created a simple example program to show how you can use GLib in a game.
It handles basic wireframe rendering, as you can learn from the tutorials, but it also shows you how to make a dynamic level, a way to use very large levels without getting accuracy problems, and a way to move the camera in an other way than by GLib's GGetKey() function.

This is a screenshot of how it looks:


You can find the partially commented 8xp file in the attachement. The full source code in ASCII format with more comments can be found in the spoiler below.
Spoiler For "Source code":
.TUNNEL3D

.INCLUDE GLIB
prgmGCORE

.Add a world view camera, and set the coordinates to (0,0,0)
GNewCam(^^oGWORLDVIEW)
0->GCPosX->GCPosY->GCPosZ

.Set up the vertex buffers. VBuff is for the world space vertices, VBuff2 is for the screen space (transformed) vertices
L2->^^oVBuff->GVAdr
L3->^^oVBuff2->GVStr

.The sprites for the player and their masks
[0000007E997E0000]->GDB1
[00007EFFFFFF7E00]
[00183C7E997E0000]
[183C7EFFFFFF7E00]
[00007EFFFFFF7E3C]
[0000007EFF7E3C18]
[00003E2E1E0A0600]
[003E7F7F3F1F0F06]
[00007C7478506000]
[007CFEFEFCF8F060]

.Generate a straight tunnel in which the player will start
^^oVBuff->A
5->B
For(15)
~20->{A}^^r
B->{A+2}^^r
~20->{A+4}^^r

20->{A+6}^^r
B->{A+8}^^r
~20->{A+10}^^r

20->{A+12}^^r
B->{A+14}^^r
20->{A+16}^^r

~20->{A+18}^^r
B->{A+20}^^r
20->{A+22}^^r

A+24->A
B+10->B
End

.Set up the variables
.Used for the level generator: F and G represent the X and Y coordinates of the center of the last tunnel frame
.J and K represent the angle of the tunnel segment that needs to be generated
.H and I are counters used to determine the length of the tunnel piece being generated (because of this, the tunnel is a series of bents instead of randomly displaced squares
0->F->J
0->G->K
0->H
0->I
.S is the score (number of frames since the start of the game)
0->S

.SETUP OTHER STUFF (makes the text being drawn on the buffer, and inverted)
Fix 3
Fix 5

.MAIN GAME LOOP
Repeat getKey(15)

.MOVE THE CHARACTER
.The +3 is the speed of the player in the Y-direction.
GCPosY+3->GCPosY
    .If the character's Y-position is greater than 10, then the world has to be moved backwards by 10 units (this way, the player never gets away too far from the orgin, so that there won't be problems with overflows)
If GCPosY>10
GCPosY-10->GCPosY
.SHIFT WORLD
Copy(^^oVBuff+24,^^oVBuff,336)
^^oVBuff+2->A
For(60)
{A}^^r-10->{A}^^r
A+6->A
End

.Generate a new tunnel piece. This is done by adding random offsets to the center of the squares, but it adds the same offset multiple times to make it look more like a neat tunnel instead of a random mess.

If H=0
rand^17-8->J
rand^(20-abs(J))+1->H
End
If I=0
rand^17-8->K
rand^(20-abs(K))+1->I
End

H--
I--

S/256->B

F+J->F
G+K->G

        .Add the vertices for the new tunnel piece

^^oVBuff+336->A

F-20+B->{A}^^r
145->{A+2}^^r
G-20+B->{A+4}^^r

F+20-B->{A+6}^^r
145->{A+8}^^r
G-20+B->{A+10}^^r

F+20-B->{A+12}^^r
145->{A+14}^^r
G+20-B->{A+16}^^r

F-20+B->{A+18}^^r
145->{A+20}^^r
G+20-B->{A+22}^^r

End

.CONTROLS: move the player based on what kays are pressed. The *2 determines the speed at which the player can move in the XZ plane
getKey(3)-getKey(2)*2+GCPosX->GCPosX
getKey(4)-getKey(1)*2+GCPosZ->GCPosZ

.CHECK IF PLAYER DIED

GCPosX->A
GCPosZ->B
^^oVBuff+48->C

If {C}^^r+2>>A or ({C+4}^^r+2>>B) or ({C+12}^^r-2<<A) or ({C+16}^^r-2<<B)
        .If the player is within 2 units from an edge of the third frame, then the player died and the program is terminated (we use the third frame because the camera appears behind the player)
Goto END
End

.TRANSFORM THE VERTICES (This basically does the same as the tutorial)
^^oVBuff->A
^^oVBuff2->B
For(60)
GVertex({A}^^r,{A+4}^^r,{A+2}^^r)
Copy(^^oGVertex,B,6)
A+6->A
B+6->B
End

.DRAW THE TUNNEL
ClrDraw
    .Because the tunnel consists of simple squares, the vertices can be connected by a very simple algorithm, so that we don't need to read them from a link list
0->E
For(15)
For(3)
GClipLine(E,E+1)
E++
End
GClipLine(E,E-3)
E++
End

.Draw the score
Text(0,0,S>Dec)
S++

.Draw the correct player sprite, based on the keys being pressed
If getKey(3)
Pt-Mask(44,28,GDB1+48)^^r
ElseIf getKey(2)
Pt-Mask(44,28,GDB1+64)^^r
ElseIf getKey(1)
Pt-Mask(44,28,GDB1+32)^^r
ElseIf getKey(4)
Pt-Mask(44,28,GDB1+16)^^r
Else
Pt-Mask(44,28,GDB1)^^r
End

DispGraph
End

Lbl END

.Restore the text flags
Fix 2
Fix 4

Ps: do you want a challenge? Put a Full at the start, and/or increase the Y and XZ speeds by the same factor (example of a doubled speed + 15MHZ (=5x normal speed)).
« Last Edit: December 27, 2013, 05:31:27 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #17 on: December 28, 2013, 05:10:45 am »
Wow I love this. I think it will have his place in the GLib's example folder  :P


EDIT ; by the way, you don't need to set cam coordinate to 0', cause GNewCam already do that normally.

EDIT2 : after verif, only GCPosZ should be set to 0 . In fact,  °GWORLDVIEW implement an eye offset, wich is 10 by default.
I you want to have an optimized thing, you can set the constant °GEyeOffset to 0 (before GCORE as usual) , and there will be no need to set camera's coordinate to 0.
« Last Edit: December 28, 2013, 11:00:44 am by TheMachine02 »
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline pimathbrainiac

  • Occasionally I make projects
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1731
  • Rating: +136/-23
  • dagaem
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #18 on: December 28, 2013, 11:56:12 am »
Great job ben_g

* pimathbrainiac is expecting a Starfox 64-like sometime soon.
I am Bach.

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #19 on: December 28, 2013, 12:55:26 pm »
Maybe a very simple one is possible  :P . Anyway, I am really impressed that it run so fast (60 vertices are rotated and projected each frame) .... and it's me who say that  :P

* TheMachine02 hides
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [Axe] - GLib TUTO
« Reply #20 on: January 04, 2014, 05:31:04 am »
That tunnel is just epic o.o

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #21 on: January 14, 2014, 01:44:17 pm »
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 BASIS

Let's create a rotating cube !

First of all, include the GCORE prog inside of your axe source :

Code: [Select]
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 :
Code: [Select]
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 :

Code: [Select]
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 :

Code: [Select]
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 :
Code: [Select]
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 :

Code: [Select]
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 :

Code: [Select]
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
Code: [Select]
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 :
Code: [Select]
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  :P)

It's the GGetkey function.
The call is:
Code: [Select]
GGetkey()

End of the code :

Code: [Select]
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 ! :D
« Last Edit: January 15, 2014, 02:01:41 am by TheMachine02 »
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline TheCoder1998

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 434
  • Rating: +20/-2
  • my art is written in code, not in graphite
    • View Profile
    • My website :D
Re: [Axe] - GLib TUTO
« Reply #22 on: January 14, 2014, 01:45:09 pm »
wow, this looks awesome :D
great job on the tutorial!

EDIT: Since when is 6400 bytes pretty huge? :P (i've got an SE)
« Last Edit: January 14, 2014, 01:46:08 pm by TheCoder1998 »
my ticalc acc:

http://www.ticalc.org/archives/files/authors/113/11365.html

Spoiler For The Best Song Ever:


follow me on tumblr :)
www.rickdepizza.tumblr.com

check out my anilist :D
http://anilist.co/animelist/29701/Rickdepizza

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #23 on: January 14, 2014, 01:45:51 pm »
thanks  :D
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Axe] - GLib TUTO
« Reply #24 on: January 14, 2014, 02:17:36 pm »
Nice explanation :) will you include a specific chapter about VBOs too ?

IMO you should just erase the whole previous chapter 1 and replace it by this one.

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #25 on: January 14, 2014, 02:19:17 pm »
yep, I will explain VBO - especially for the structure. So you can still generate Vertex on fly  :P


EDIT : tuto have been placed on the first post, with warning for those unupdated.
« Last Edit: January 14, 2014, 02:24:13 pm by TheMachine02 »
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: [Axe] - GLib TUTO
« Reply #26 on: January 14, 2014, 05:01:28 pm »
I've downloaded the GCORE version of the tutorial post (edit: it says "Version 2.0 omega" in the program editor), and when I try to use it, it thrown an undefined error at GGenBuffer and GUpdateBuffer.
I've checked them for typing mistakes, but they looked correct.
What am I doing wrong?

EDIT2: I've looked at the source code, and it looks like it should be GGenVBO and GUpdateVBO instead of GGenBuffer and GUpdateBuffer. It works with those.
« Last Edit: January 14, 2014, 05:07:18 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #27 on: January 15, 2014, 02:00:40 am »
O_o I guess I've change the syntax, and I forget that I've chnage it. Thanks for the report  ;D . The tuto have been updated.
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: [Axe] - GLib TUTO
« Reply #28 on: February 05, 2014, 12:29:28 pm »
.....soooooo I have finally updated the tuto talking about lines and shader  ;D
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Axe] - GLib TUTO
« Reply #29 on: February 05, 2014, 02:49:45 pm »
Cool o/

Can you give an example of what the vertex shader could be used for ? I can't really think of any use for it, but it's only because I lack imagination.