mardi 12 avril 2016

How to Make a 3d Game with FreeBasic in 24 hours ....(OS Linux,win32)

I'd like to share with the beginner especially who cares about 3D games and never had the opportunity to build real 3d application of course a good knowledge of 3d math is necessary , if you follow every step I garantie you that you will create your owner game in 24 hours without including any external libraries or exporting 3d model from other application the 3d modelisation is made by code using basic geometry (box, sphere, ...) adding a few simples algorithms , you have just to need freebasic as a standard language which can be downloaded and installed without much efforts (< 20 mb of size), you can also learn from this simple tutorial step by step the technics of 3d object motion ,detecting collision etc ...and use it in your owner code...I hope this code below  would be enough and useful . 


REM SPACE3D Programmed by quantum 15 /03/2016
#include "GL/gl.bi" 'OpenGL funks and consts
#include "GL/glu.bi" 'GL standard utility lib
#include "GL/glut.bi" 'GL standard utility lib
#include "fbgfx.bi"
#include once "createtex.bi"
#include "xsound.bi"
 

#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif

const maxpic = 3

Dim shared mixer As xsound.mixer
Dim shared sound As xsound.sound
dim shared channel as xsound.channel

dim shared gbase as uinteger                      '' Base Display List For The Font
dim shared texture(0 to 6) as uinteger            '' Storage For Our  Texture
dim shared gloop as integer  

dim shared score as integer
Dim x As Single
Dim y As Single
Dim shared Wheel As Integer
Dim result As Integer
Dim a As Integer
dim shared move as integer
dim shared pause as integer
dim shared as integer mouse,key
dim shared st as string
dim shared collision as integer
dim shared pic as integer

Const JoystickID = 0
dim shared as single FogCol(0 to 3 )=> {0.9f,0.9f,0.9f}
dim shared as single LightPos(0 to 3) => {-1000.0, 0, 0.0, 1.0} '' Light Position
dim shared as single LightAmb(0 to 3) => {0.0, 0.0, 0.0, 1.0} '' Ambient Light Values
dim shared as single LightDif(0 to 3) => {1.0, 1.0, 1.0, 1.0} '' Diffuse Light Values
dim shared as single LightSpc(0 to 3) => {1.0, 1.0, 0.0, 1.0} '' Specular Light Values
dim shared as single MatAmb(0 to 3) => {0.0, 0.0, 0.0, 1.0} '' Material - Ambient Values
dim shared as single MatDif(0 to 3) => {0.9, 0.9, 0.9, 1.0} '' Material - Diffuse Values
dim shared as single MatSpc(0 to 3) => {0.0, 0.0, 0.0, 1.0} '' Material - Specular Values
dim shared as single MatShn(0) => {0.0}

type vec3
dim as single x,y,z
end type
dim shared as vec3 position,velocity,up
type box
dim as single x,y,z
dim as single lx,ly,lz
dim as single r,g,b
end type

dim shared as double xs,ys,zs,radius
Dim shared As Integer xmouse, ymouse, xmousep, ymousep,buttons
DIM SHARED as single teta, phi
dim shared as single phip,tetap,d
dim shared FOVy as double 'Field of view angle in Y
dim shared Aspect as double 'Aspect of screen
dim shared znear as double 'z-near clip distance
dim shared zfar as double
declare sub init_screen(w as integer, h as integer)
declare sub getxymouse(byref x as single,byref y as single,byref w as integer)
declare sub BuildFont()  
declare sub glPrint(byval x as integer, byval y as integer, byref glstring as string, byval gset as integer)
declare sub boom
declare sub init_box
declare sub draw_box
declare sub setcamera(p as vec3,v as vec3)
declare sub setcamera_plane(p as vec3,v as vec3)
declare Sub setcamera_plane_up(p as vec3,v as vec3)
declare sub setcamera0
declare sub  cokbit ( teta as single)
Declare sub  plane( teta as Single)
declare sub  picture ( teta as single, x as single,y as single, z as single)

dim shared as integer Xres, Yres
dim  shared as double  dt,start

windowtitle " family Space 3D programmed by Delta Fares..."
sound.load(1,"Electro_1.wav")
sound.load(2,"explode.wav")
sound.load(3,"FinalSound.wav")
mixer.start

print "welcome to space family 3d game  programmed by quantum  15/03/2015"
input "please choose mouse (1) or keyboard (2) or JoyStick (3)  control =  ",x
if x = 1 then mouse = 1:key = 0 endif
if x = 2 then mouse = 0:key = 1 endif
if x<>1 and  x<>2 then key =0 : mouse =1: print "mouse control by default "  else print "press any key to start": sleep endif

dim shared as integer nx,ny
input "please choose Difficulty level  0-1-2-3-4 =  ",x
if nx>4 then ny = 2*10*4+20 else ny = 2*10*x+20
print "press any key to start " :sleep

init_screen (Xres,Yres)

dim shared as single Lx,Ly,Lz
 nx =10
 ny =30
dim shared nbox(0 to nx-1, 0 to ny-1) as box
dim shared deltad as single
dim shared as integer i,j

BuildFont()

dim shared as GLUquadricObj ptr q 
q = gluNewQuadric()                          '' Initialize Quadratic
gluQuadricNormals(q, GL_SMOOTH)              '' Enable Smooth Normal Generation
gluQuadricTexture(q, GL_TRUE)    

mixer.start

start_game:                    'star game

sound.play(1,1,0,0)
'channel.vol(-10)

setmouse(Xres/2,Yres/2)
Lx = 1000
Ly = 2000
Lz = 200
position.x = -2000
position.y = 0
position.z = 60
velocity.x = 500
velocity.y = 0
velocity.z = 0
wheel =0
collision = 0
pic  =2*rnd+4

randomize
ys = 300*rnd-300 :xs = position.x+ 500*rnd+2000: zs = 30*rnd+30:radius = 30*rnd+50
score = 0
init_box
d = 0
pause =0

do
 start = timer   
   
'if Inkey="p" then pause = not(pause)
'id pause = 1
if position.y >= 1000 then position.y = 1000
if position.y <= -1000 then position.y = -1000
if key then
If MultiKey(SC_LEFT ) Then position.y = position.y + 0.5: d =d + 0.001
If MultiKey(SC_RIGHT) Then position.y = position.y - 0.5: d = d - 0.001
If MultiKey(SC_UP ) Then velocity.x = velocity.x + 1
If MultiKey(SC_DOWN ) Then velocity.x = velocity.x -1

if not(MultiKey(SC_LEFT )) and not(MultiKey(SC_RIGHT)) and d > 0 then d = d-0.005
if not(MultiKey(SC_LEFT )) and not(MultiKey(SC_RIGHT)) and d < 0 then d = d+0.005
if abs(d) < = 0.0005 then d = 0
endif

'result= GetJoystick(JoystickID,buttons,x,y)
'position.y = position.y - 5*x
'd=0.1*x
if mouse then
getxymouse(x,y,wheel)
if wheel < 0 then wheel = 0
velocity.x =  100*wheel+50
position.y = position.y - 0.5*x
d=.1*x
endif

if velocity.x >= 2000 then velocity.x = 2000
if velocity.x <= 50 then velocity.x = 10
'if buttons and 3 then velocity.x = velocity.x + 0.001

for j=0 to ny-1
for i = 0 to nx-1
if position.x > nbox(i,j).x  then
nbox(i,j).x = position.x+rnd*Lx/nx+2*Lx:nbox(i,j).lx = rnd*40+2
nbox(i,j).y = position.y+2*Ly/ny*j-Ly+rnd*Ly/ny:nbox(i,j).ly = rnd*20+2
nbox(i,j).z = 0:nbox(i,j).lz =20+rnd *Lz
endif
next i
next j
'd=d+deltad

for j=0 to ny-1
for i = 0 to nx-1
if abs(position.x - nbox(i,j).x)<=nbox(i,j).lx  and abs(position.y - nbox(i,j).y)<=nbox(i,j).ly  and position.z< nbox(i,j).lz then boom:goto start_game endif
next i
next j

if abs(position.x - xs)<=40  and abs(position.y - ys)<=radius then collision=1:channel.stop(3,0):sound.play(3,2):pic = 2*rnd+4 endif
if collision= 1 and abs(position.x - xs) >radius  and abs(position.y - ys) > radius then score=score+(x+1)*velocity.x/10:collision = 0 endif

up.x = 0
up.y = sin(d)
up.z = cos(d)
position.x = position.x+ velocity.x*dt


LightPos(0)=position.x
glLightfv(GL_LIGHT1, GL_POSITION, @LightPos(0))

'setcamera0
'setcamera(position,velocity)
setcamera_plane(position,velocity)
'setcamera_plane_up(position,velocity)
glclearcolor 0.1,0.0,0.3,1.0
glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

if position.x> Xs then ys = 800*rnd-800 :xs = position.x+ 500*rnd+1500:zs = 30*rnd+30: radius = 30*rnd+50 endif

glEnable GL_DEPTH_TEST

glcolor3f 1.0,1.0,1.0
glBindTexture GL_TEXTURE_2D, texture(3)
glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
glPushMatrix                                                '' Store The Modelview Matrix
glLoadIdentity
gltranslated xs,ys,zs+250
glrotatef timer*100,0,0,1

gluSphere(q, radius, 10, 10) 
glPopMatrix

glBindTexture GL_TEXTURE_2D, texture(pic)
picture(timer*0,xs,ys,zs)

if mouse then plane(200*d)
if key then plane(-120*d)

glEnable GL_COLOR_MATERIAL
glEnable(GL_FOG)
glDisable GL_BLEND
glBindTexture GL_TEXTURE_2D, texture(2)
glEnable GL_TEXTURE_2D
glcolor3f 0.7,0.6,0.4
draw_box 'Draw something

glDisable(GL_FOG)
glMatrixMode GL_MODELVIEW  
glLoadIdentity
glEnable GL_TEXTURE_2D
glShadeModel GL_SMOOTH
'glClearDepth 1.0                         '' Enables Clearing Of The Depth Buffer
glDepthFunc GL_LEQUAL  '' The Type Of Depth Test To Do
glEnable GL_BLEND
glBlendFunc GL_SRC_ALPHA, GL_ONE
glBindTexture GL_TEXTURE_2D, texture(1)                '' Select Our Second Texture
glColor3f 1.0,1.0,1.0                                  '' Set Color To White
glPrint 0,yres-20, "FREEBASIC OPENGL DEMONSTRATION!", 0

glMatrixMode GL_MODELVIEW  
glLoadIdentity
'glscale3f 1,1,1
st = str(score)
glColor3f 1.0,0.0,0.0 
glPrint xres-200,yres-20, "SCORE = "+st, 0
flip

dt = timer-start

loop until Inkey =chr(27)
end

sub init_box
dim as integer i,j
for j = 0 to ny-1
   for i = 0 to nx-1
  nbox(i,j).x = 2*Lx/nx*i-Lx+rnd*Lx/nx:nbox(i,j).lx = rnd*40+2
  nbox(i,j).y = 2*Ly/ny*j-Ly+rnd*Ly/ny:nbox(i,j).ly = rnd*20+2
  nbox(i,j).z = 0:nbox(i,j).lz =20+rnd *Lz
  nbox(i,j).r = rnd
  nbox(i,j).g = rnd
 nbox(i,j).b = rnd
 next i
next j

end sub

dim shared st1 as string
sub boom

channel.stop(1,0)
sound.play(2,1,0,0)

 start = timer
 do
    
'glDisable GL_BLEND     
glclearcolor rnd,0.0,0.0,1.0
glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
'glDisable GL_TEXTURE_2D
'glDisable GL_BLEND
'draw_box

'glEnable GL_BLEND
'glclearcolor 1.0,1.0,1.0,1.0
'glBindTexture GL_TEXTURE_2D, texture(1)                '' Select Our Second Texture
 'glscalef 4,4,4 
 '' Set Color To White
glBindTexture GL_TEXTURE_2D, texture(1)                '' Select Our Second Texture
glColor4f 1.0,1.0,1.0 ,1.0 
glPrint xres-580,yres-480, "GAME OVER ", 0
st1 = str(score)
glColor3f 1.0,1.0,1.0 
glPrint xres-600,yres-420, "YOUR SCORE = "+st1, 0
flip
loop until MultiKey(SC_SPACE )
' mixer.stop
 channel.stop(2,0)

' timer-start _game>= 1000

end sub   


sub draw_box
dim as integer i,j
glBindTexture GL_TEXTURE_2D, texture(2)
glBegin(GL_QUADS)
glNormal3f(0.0, 0.0, 1.0)
glTexCoord2f(1.0, 1.0) : glVertex3f(-100*Lx, -100*Ly,0)
glTexCoord2f(0.0, 1.0) : glVertex3f(-100*Lx, 100*Ly,0)
glTexCoord2f(0.0, 0.0) : glVertex3f(100*Lx, 100*Ly,0)
glTexCoord2f(1.0, 0.0) : glVertex3f(100*Lx, -100*Ly,0)
glend()
glBegin(GL_QUADS)
for j = 0 to ny-1
for i = 0 to nx-1
   
 glcolor3f nbox(i,j).r,nbox(i,j).g,nbox(i,j).b
glNormal3f(0.0, 0.0, 1.0)
glTexCoord2f(1.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz )
glTexCoord2f(0.0, 1.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(0.0, 0.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(1.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,nbox(i,j).lz)
'' Back Face
' glNormal3f(0.0, 0.0, - 1.0)
' glTexCoord2f(1.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
' glTexCoord2f(0.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
' glTexCoord2f(0.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
' glTexCoord2f(1.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
'' Top Face
glNormal3f(0.0, 1.0, 0.0)
glTexCoord2f(1.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,0)
glTexCoord2f(0.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(0.0, 0.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(1.0, 0.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,0)
'' Bottom Face
glNormal3f(0.0, - 1.0, 0.0)
glTexCoord2f(0.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,0)
glTexCoord2f(1.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(1.0, 1.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(0.0, 1.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,0)
'' Right Face
glNormal3f(1.0, 0.0, 0.0)
glTexCoord2f(1.0, 0.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,0)
glTexCoord2f(1.0, 1.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(0.0, 1.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(0.0, 0.0) : glVertex3f(nbox(i,j).x+nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,0)
'' Left Face
glNormal3f(- 1.0, 0.0, 0.0)
glTexCoord2f(0.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,0)
glTexCoord2f(1.0, 0.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y-nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(1.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,nbox(i,j).lz)
glTexCoord2f(0.0, 1.0) : glVertex3f(nbox(i,j).x-nbox(i,j).lx, nbox(i,j).y+nbox(i,j).ly,0)
' line(nbox(i,j).x-Lx/(n)+xres/2, nbox(i,j).y-Ly/(n)+yres/2)-(nbox(i,j).x+Lx/(n)+xres/2,nbox(i,j).y+Ly/(n)+yres/2),RGB(255,255,255),b
next i
next j
glEnd()
end sub
sub init_screen(w as integer, h as integer)

Dim w1 As Integer, h1 As Integer
Dim depth As Integer
Dim driver_name As String

' Obtain info about current mode
ScreenInfo w1, h1, depth,,,,driver_name
xres = w1: yres = h1
ScreenRes xres, yres, 16,2,&h2 Or GFX_NO_FRAME
glViewport 0, 0, w1, h1
glMatrixMode GL_PROJECTION
glLoadIdentity
FOVy = 60 'deg fovy
Aspect = w1 / h1 'aspect = x/y
znear = 1 'Near clip
zfar = 2400 'far clip


gluPerspective FOVy, aspect, znear, zfar
gluLookAt 100,100,100,0,0,0,0,0,1
'Modelview mode
glMatrixMode GL_MODELVIEW
glLoadIdentity
glShadeModel GL_SMOOTH 'set shading to smooth(try GL_FLAT)
glEnable GL_COLOR_MATERIAL
glEnable(GL_LIGHTING)
'Enable Texturing
'glEnable GL_TEXTURE_2D
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
'Tell openGL that we want the best possible perspective transform
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

glLightfv(GL_LIGHT1, GL_POSITION, @LightPos(0)) '' Set Light1 Position
glLightfv(GL_LIGHT1, GL_AMBIENT, @LightAmb(0)) '' Set Light1 Ambience
glLightfv(GL_LIGHT1, GL_DIFFUSE, @LightDif(0)) '' Set Light1 Diffuse
glLightfv(GL_LIGHT1, GL_SPECULAR, @LightSpc(0)) '' Set Light1 Specular
glEnable(GL_LIGHT1) '' Enable Light1
glEnable(GL_LIGHTING) '' Enable Lighting
glMaterialfv(GL_FRONT, GL_AMBIENT, @MatAmb(0)) '' Set Material Ambience
glMaterialfv(GL_FRONT, GL_DIFFUSE, @MatDif(0)) '' Set Material Diffuse
glMaterialfv(GL_FRONT, GL_SPECULAR, @MatSpc(0)) '' Set Material Specular
glMaterialfv(GL_FRONT, GL_SHININESS, @MatShn(0)) '' Set Material Shininess
'glCullFace(GL_BACK) '' Set Culling Face To Back Face
'glEnable(GL_CULL_FACE)

glEnable(GL_FOG)

glFogfv(GL_FOG_COLOR,@FogCol(0))
glFogi(GL_FOG_MODE, GL_LINEAR)

glFogf(GL_FOG_START, 50.f)
glFogf(GL_FOG_END, 1.f)
glFogf(GL_FOG_DENSITY,0.1f)
END SUB
sub setcamera(p as vec3,v as vec3)
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective FOVy, aspect, znear, zfar
gluLookAt p.x,p.y,p.z,p.x+v.x,p.y+v.y,p.z+v.z,up.x,up.y,up.z
end Sub

Sub setcamera_plane(p as vec3,v as vec3)
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 60, aspect, znear, zfar
gluLookAt p.x-9,p.y,p.z+2,p.x,p.y,p.z+3,up.x,up.y,up.z
end Sub

Sub setcamera_plane_up(p as vec3,v as vec3)
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 1, aspect, znear, zfar
gluLookAt p.x,p.y,p.z+2000,p.x,p.y,p.z,1,0,0
end Sub

sub setcamera0
GetMouse( xmouse, ymouse , , buttons)
If buttons And 1 Then phi =phip-0.01*(xmouse-xmousep):teta =tetap-0.01*(ymouse-ymousep) else tetap=teta:phip=phi:xmousep=xmouse:ymousep=ymouse end if
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective FOVy, aspect, znear, zfar
'gluLookAt 700,0,0,0,0,0,0,0,1
gluLookAt 400*sin(teta)*cos(phi),400*sin(teta)*sin(phi),400*cos(teta),0,0,0,0,0,1
end sub

sub getxymouse(byref x as single,byref y as single,byref w as integer )
GetMouse( xmouse, ymouse ,w, buttons)
'If buttons And 1 Then phi =phip-0.01*(xmouse-xmousep):teta =tetap-0.01*(ymouse-ymousep) else  tetap=teta:phip=phi:xmousep=xmouse:ymousep=ymouse endif
'if abs(xmouse-xmousep)< 1 then move = 0 else move = 1
'if move =0  then  xmousep= xmouse:ymousep = ymouse:tetap=teta:phip=phi: x = 0:y=0:setmouse(xres/2,yres/2) endif
'if move = 1 then phi =phip-0.01*(xmouse-xmousep-xres/2):teta =tetap-0.01*(ymouse-ymousep):x = -phi:y = teta endif
if abs(xmouse-xres/2) <=400 then x = (xmouse-xres/2)/30 'else x = (400-xres/2)/100

end sub

sub BuildFont()                                  '' Build Our Font Display List

 dim cx as single                             '' Holds Our X Character Coord
 dim cy as single                             '' Holds Our Y Character Coord
    redim buffer(256*256*4+4) as ubyte       '' Size = Width x Height x 4 bytes per pixel + 4 bytes for header
 bload "Font.bmp", @buffer(0)        '' BLOAD the bitmap
 texture(0) = CreateTexture(@buffer(0))   '' Linear Texture
 bload "Bumps.bmp", @buffer(0)       '' BLOAD the bitmap
 texture(1) = CreateTexture(@buffer(0))
   
    bload "concrete.bmp", @buffer(0)       '' BLOAD the bitmap
 texture(2) = CreateTexture(@buffer(0))
   
  redim buffer(512*512*4+4) as ubyte
    
 bload "sky.bmp", @buffer(0)       '' BLOAD the bitmap 
texture(3) = CreateTexture(@buffer(0))
   
    bload "image1.bmp", @buffer(0) ,     '' BLOAD the bitmap
 texture(4) = CreateTexture(@buffer(0))
   
    bload "image2.bmp", @buffer(0) ,     '' BLOAD the bitmap
 texture(5) = CreateTexture(@buffer(0))
   
    bload "image3.bmp", @buffer(0) ,     '' BLOAD the bitmap
 texture(6) = CreateTexture(@buffer(0))
   
    gbase = glGenLists(256)                      '' Creating 256 Display Lists
   glBindTexture GL_TEXTURE_2D, texture(0)      '' Select Our Font Texture
   
for gloop = 0 to 255                         '' Loop Through All 256 Lists
  cx = (gloop mod 16)/16.0                 '' X Position Of Current Character
  cy = (gloop\16)/16.0                     '' Y Position Of Current Character

  glNewList gbase+gloop, GL_COMPILE        '' Start Building A List
  glBegin GL_QUADS                         '' Use A Quad For Each Character
   glTexCoord2f cx, 1-cy-0.0625         '' Texture Coord (Bottom Left)
   glVertex2i 0, 0                      '' Vertex Coord (Bottom Left)
   glTexCoord2f cx+0.0625, 1-cy-0.0625  '' Texture Coord (Bottom Right)
   glVertex2i 16,0                      '' Vertex Coord (Bottom Right)
   glTexCoord2f cx+0.0625, 1-cy         '' Texture Coord (Top Right)
   glVertex2i 16, 16                    '' Vertex Coord (Top Right)
   glTexCoord2f cx,1-cy                 '' Texture Coord (Top Left)
   glVertex2i 0, 16                     '' Vertex Coord (Top Left)
  glEnd                                    '' Done Building Our Quad (Character)
  glTranslated 10, 0, 0                    '' Move To The Right Of The Character
  glEndList                                '' Done Building The Display List
 next                                         '' Loop Until All 256 Are Built
end sub

sub glPrint(byval x as integer, byval y as integer, byref glstring as string, byval gset as integer)

 if gset>1 then gset=1

 glBindTexture GL_TEXTURE_2D, texture(0)                         '' Select Our Font Texture
 glDisable GL_DEPTH_TEST                                         '' Disables Depth Testing
 glMatrixMode GL_PROJECTION                                      '' Select The Projection Matrix
 glPushMatrix                                                    '' Store The Projection Matrix
  glLoadIdentity                                              '' Reset The Projection Matrix
  glOrtho 0, Xres, 0, Yres,-1, 1                                '' Set Up An Ortho Screen
  glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
  glPushMatrix                                                '' Store The Modelview Matrix
   glLoadIdentity                                          '' Reset The Modelview Matrix
   glTranslated x, y, 0                                    '' Position The Text (0,0 - Bottom Left)
   glListBase gbase-32+(128*gset)                          '' Choose The Font Set (0 or 1)
   glCallLists len(glstring),GL_BYTE, strptr(glstring)     '' Write The Text To The Screen
   glMatrixMode GL_PROJECTION                              '' Select The Projection Matrix
  glPopMatrix                                                 '' Restore The Old Projection Matrix
  glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
 glPopMatrix                                                     '' Restore The Old Projection Matrix
 glEnable GL_DEPTH_TEST                                          '' Enables Depth Testing
end sub


sub  cokbit ( teta as single)
    glcolor3f 0.0,0.0,1.0
  
   glDISABLE(GL_FOG)
    glEnable GL_TEXTURE_2D 
   
    'glDisable GL_DEPTH_TEST  
    glBindTexture GL_TEXTURE_2D, texture(3)
   glMatrixMode GL_PROJECTION                                      '' Select The Projection Matrix
  glPushMatrix                                                    '' Store The Projection Matrix
  glLoadIdentity                                              '' Reset The Projection Matrix
  glOrtho 0, xres, 0, yres,-1, 1  
   glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
   glPushMatrix                                                '' Store The Modelview Matrix
   glLoadIdentity
            gltranslated xres/2,yres/2,0
            glrotatef teta,0,0,1
            glBegin GL_QUADS                         '' Use A Quad For Each Character
   glTexCoord2f 0,0         '' Texture Coord (Bottom Left)
   glVertex2f - 200, 200                      '' Vertex Coord (Bottom Left)
   glTexCoord2f 0,1  '' Texture Coord (Bottom Right)
   glVertex2f 200,200                      '' Vertex Coord (Bottom Right)
   glTexCoord2f 1, 1         '' Texture Coord (Top Right)
   glVertex2f 200, -200                    '' Vertex Coord (Top Right)
   glTexCoord2f 1.0,0                 '' Texture Coord (Top Left)
   glVertex2f -200, -200
            '' Vertex Coord (Top Left)
  glEnd 
         glMatrixMode GL_PROJECTION                              '' Select The Projection Matrix
  glPopMatrix                                                 '' Restore The Old Projection Matrix
  glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
 glPopMatrix     
end sub

sub  picture ( teta as single, x as single,y as single, z as single)
    glcolor3f 1.0,1.0,1.0
   glDisable GL_BLEND
   glDisable GL_COLOR_MATERIAL
   glDISABLE(GL_FOG)
    glEnable GL_TEXTURE_2D 
   
    'glDisable GL_DEPTH_TEST  
   ' glBindTexture GL_TEXTURE_2D, texture(3)
 
        glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
   glPushMatrix                                                '' Store The Modelview Matrix
   glLoadIdentity
            glrotatef teta,0,0,1
            gltranslated x,y,z
            glBegin GL_QUADS                         '' Use A Quad For Each Character
   glTexCoord2f 0.0,0.0 : glVertex3f 0,-radius/2, 0
   glTexCoord2f 1.0,0.0 :  glVertex3f 0,radius/2, 0
   glTexCoord2f 1.0, 1.0 : glVertex3f 0,radius/2,radius                   
            glTexCoord2f 0.0,1.0  : glVertex3f 0,-radius/2, radius
            '' Vertex Coord (Top Left)
  glEnd 
        glPushMatrix 
         glcolor3f 1,0,0
         gltranslated 0,0,radius/2
        glrotatef 90,0.0,1.0,0.0
        glutSolidTorus(10,radius,20,20)
         glPopMatrix
  glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
 glPopMatrix     
end sub

sub  plane( teta as Single)
    glcolor3f 0.5,0.5,0.5
   glDisable GL_BLEND
   glEnable GL_COLOR_MATERIAL
   glDISABLE(GL_FOG)
    glDisable GL_TEXTURE_2D 
  
    'glDisable GL_DEPTH_TEST  
   ' glBindTexture GL_TEXTURE_2D, texture(3)
 
        glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
   glPushMatrix                                                '' Store The Modelview Matrix
   glLoadIdentity
            gltranslatef position.x,position.y,position.z
            glrotatef teta,1,0,0
           
        '     glBegin GL_QUADS                         '' Use A Quad For Each Character
  ' glTexCoord2f 0.0,0.0 : glVertex3f 0,0, 0
 '  glTexCoord2f 1.0,0.0 :  glVertex3f 0,0, 0
 '  glTexCoord2f 1.0, 1.0 : glVertex3f -1,-1,0                  
     '       glTexCoord2f 0.0,1.0  : glVertex3f -1,+1,0
            '' Vertex Coord (Top Left)
  'glEnd 
        gluSphere(q, 0.3, 10, 10) 
        
        glPushMatrix                                                '' Store The Modelview Matrix
        glscalef 1,17,1
        glutSolidCube(0.1)
        glPopMatrix
      
        glPushMatrix   
         glrotatef 20,1,0,0
         gltranslatef 0,0.8,0.05
          glscalef 7,0.5,7
          glutSolidCube(0.1)
        glPopMatrix
       
        glPushMatrix                                                '' Store The Modelview Matrix
          glrotatef -20,1,0,0
          gltranslatef 0,-0.8,0.05
          glscalef 7,0.5,7
          glutSolidCube(0.1)
        glPopMatrix
       
         glPushMatrix   
         glrotatef 20+180,1,0,0
          gltranslatef 0,0.8,0.05
          glscalef 7,0.5,7
          glutSolidCube(0.1)
        glPopMatrix
       
        glPushMatrix                                                '' Store The Modelview Matrix
          glrotatef -20+180,1,0,0
          gltranslatef 0,-0.8,0.05
          glscalef 7,0.5,7
          glutSolidCube(0.1)
        glPopMatrix
       
        glPushMatrix  
        'glrotatef 90,0.0,1.0,0.0
        glutSolidTorus(0.05,0.4,20,20)
         glPopMatrix
  glMatrixMode GL_MODELVIEW                                   '' Select The Modelview Matrix
       
 glPopMatrix     
end sub






Aucun commentaire:

Enregistrer un commentaire

Remarque : Seul un membre de ce blog est autorisé à enregistrer un commentaire.