Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: ninogenio on September 28, 2006
-
i posted this on my old forum and thought someone here might find it usefull so brought it across.
' OPENGL AABB TEST BY NINO
'---------------------------------------------------
'
' use keys ws for z position
' LEFT RIGHT UP DOWN ARROWS TO alter x,y positions
'
' if your yousing gltranslatef then you also need
' to call the update bonding box function to adjust
' the bounding box accordingly also if you wish to
' youse glscalef then call the scale bounding box
' function accordingly
'
' if you dont have your vertices stored in my_vector
' type then you will have to set bounding min/max
' values manually or else you can just run the
' my_vector type through the set_min_max_box_vertex
' function
'
'---------------------------------------------------
#include once "GL/gl.bi"
#include once "GL/glu.bi"
#include once "fbgfx.bi"
'------------------------------------------------
' do gl initalization stuff here
'------------------------------------------------
screen 19, 32, ,3
glViewport 0, 0, 800, 600
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective(45,800/600,1,100)
glMatrixMode GL_MODELVIEW
glLoadIdentity
glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 1.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
glBlendFunc(GL_SRC_ALPHA,GL_ONE)
glEnable GL_TEXTURE_2D
'-------------------------------------------------
type vertex
x as double
y as double
z as double
end type
type faces
a as integer
b as integer
c as integer
end type
type my_vector
obj_x as double
obj_y as double
obj_z as double
obj_xscale as double
obj_yscale as double
obj_zscale as double
bou_min as vertex
bou_max as vertex
vertex(8) as vertex
face(6) as faces
end type
declare function update_bounding_box_pos(byval xamount as double , byval yamount as double , zamount as double , bbox as my_vector)
declare function set_min_max_box_vertex(byval min_x_amount as double , byval min_y_amount as double , byval min_z_amount as double , bbox as my_vector)
declare function hard_set_bounding_min( byval xamount as double , byval yamount as double , byval zamount as double , bbox as my_vector )
declare function hard_set_bounding_max( byval xamount as double , byval yamount as double , byval zamount as double , bbox as my_vector )
declare function draw_box()
declare function move_box(box as my_vector)
declare function bounding_box_collision(box1 as my_vector,box2 as my_vector) as integer
declare function scale_bounding_box(byval xamount as double , byval yamount as double , zamount as double , bbox as my_vector)
dim shared box1 as my_vector
dim shared box2 as my_vector
hard_set_bounding_min -1 , -1 , -1 , box1
hard_set_bounding_max 1 , 1 , 1 , box1
box1.obj_x = 0
box1.obj_y = 0
box1.obj_z = -20
update_bounding_box_pos box1.obj_x , box1.obj_y , box1.obj_z , box1
hard_set_bounding_min -1 , -1 , -1 , box2
hard_set_bounding_max 1 , 1 , 1 , box2
box2.obj_x = -2
box2.obj_y = -2
box2.obj_z = -20
update_bounding_box_pos box2.obj_x , box2.obj_y , box2.obj_z , box2
do
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
move_box(box1)
glloadidentity
gltranslatef box1.obj_x ,box1.obj_y ,box1.obj_z
if bounding_box_collision(box1,box2) then
glcolor3f 0.0,1.0,0.0
else
glcolor3f 1.0,1.0,1.0
endif
draw_box()
glloadidentity
gltranslatef box2.obj_x , box2.obj_y , box2.obj_z
glcolor3f 1,0,0
draw_box()
flip
loop while not multikey(SC_ESCAPE)
while INKEY$ <> "": wend
end
function bounding_box_collision(box1 as my_vector,box2 as my_vector) as integer
if (box1.bou_min.y>box2.bou_min.y and box1.bou_max.y<box2.bou_max.y) or (box1.bou_max.y>box2.bou_min.y and box1.bou_min.y<box2.bou_max.y) then
if (box1.bou_min.x>box2.bou_min.x and box1.bou_max.x<box2.bou_max.x) or (box1.bou_max.x>box2.bou_min.x and box1.bou_min.x<box2.bou_max.x) then
if (box1.bou_min.z>box2.bou_min.z and box1.bou_max.z<box2.bou_max.z) or (box1.bou_max.z>box2.bou_min.z and box1.bou_min.z<box2.bou_max.z) then
return 1
endif
endif
endif
return 0
end function
function move_box(box as my_vector)
if multikey(SC_UP) then
box.obj_y += 0.1
update_bounding_box_pos 0 , +0.1 , 0 , box
end if
if multikey(SC_DOWN) then
box.obj_y -= 0.1
update_bounding_box_pos 0 , -0.1 , 0 , box
endif
if multikey(SC_RIGHT) then
box.obj_x += 0.1
update_bounding_box_pos +0.1 , 0 , 0 , box
endif
if multikey(SC_LEFT) then
box.obj_x -= 0.1
update_bounding_box_pos -0.1 , 0 , 0 , box
endif
if multikey(SC_W) then
box.obj_z += 0.1
update_bounding_box_pos 0 , 0 , +0.1 , box
endif
if multikey(SC_S) then
box.obj_z -= 0.1
update_bounding_box_pos 0 , 0 , -0.1 , box
endif
return 0
end function
function update_bounding_box_pos(byval xamount as double , byval yamount as double , zamount as double , bbox as my_vector)
bbox.bou_min.x += xamount
bbox.bou_min.y += yamount
bbox.bou_min.z += zamount
bbox.bou_max.x += xamount
bbox.bou_max.y += yamount
bbox.bou_max.z += zamount
return 0
end function
function scale_bounding_box(byval xamount as double , byval yamount as double , zamount as double , bbox as my_vector)
bbox.bou_min.x = bbox.bou_min.x * xamount
bbox.bou_min.y = bbox.bou_min.y * yamount
bbox.bou_min.z = bbox.bou_min.z * zamount
bbox.bou_max.x = bbox.bou_max.x * xamount
bbox.bou_max.y = bbox.bou_max.y * yamount
bbox.bou_max.z = bbox.bou_max.z * zamount
return 0
end function
'set min max values based on object
function set_min_max_bbox_vertex(bbox as my_vector)
dim as double min_x , min_y , min_z
dim as double max_x , max_y , max_z
'find the min & max xyz`s of the object
min_x = bbox.vertex(1).x
min_y = bbox.vertex(1).y
min_z = bbox.vertex(1).z
max_x = min_x
max_y = min_y
max_z = min_z
for x=2 to ubound(bbox.vertex)
if bbox.vertex(x).x<min_x then
min_x = bbox.vertex(x).x
endif
if bbox.vertex(x).x>max_x then
max_x = bbox.vertex(x).x
endif
if bbox.vertex(x).y<min_y then
min_y = bbox.vertex(x).y
endif
if bbox.vertex(x).y>max_y then
max_y = bbox.vertex(x).y
endif
if bbox.vertex(x).z<min_z then
min_x = bbox.vertex(x).z
endif
if bbox.vertex(x).z>max_z then
max_x = bbox.vertex(x).x
endif
next
bbox.bou_min.x = min_x
bbox.bou_min.y = min_y
bbox.bou_min.z = min_z
bbox.bou_max.x = max_x
bbox.bou_max.y = max_y
bbox.bou_max.z = max_z
return 0
end function
function hard_set_bounding_min( byval xamount as double , byval yamount as double , byval zamount as double , bbox as my_vector )
bbox.bou_min.x = xamount
bbox.bou_min.y = yamount
bbox.bou_min.z = zamount
return 0
end function
function hard_set_bounding_max( byval xamount as double , byval yamount as double , byval zamount as double , bbox as my_vector )
bbox.bou_max.x = xamount
bbox.bou_max.y = yamount
bbox.bou_max.z = zamount
return 0
end function
function draw_box()
glBegin GL_QUADS
glVertex3f -1.0, -1.0, 1.0
glVertex3f 1.0, -1.0, 1.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f 1.0, -1.0, -1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f 1.0, -1.0, -1.0
glVertex3f 1.0, -1.0, 1.0
glVertex3f -1.0, -1.0, 1.0
glVertex3f 1.0, -1.0, -1.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f 1.0, -1.0, 1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f -1.0, -1.0, 1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f -1.0, 1.0, -1.0
glEnd
return 0
end function
-
Sweet... Must have a play with this opengl stuff someday...
-
Really useful code Nino, thanks for posting it mate :)
It's just this sort of thing that will help me get started with gl.
-
cool as longs its usefull to some others then im a happy bunny :)
-
Nice one. Works fine here. Thanks for sharing !!!
And btw ...
'------------------------------------------------
' do gl initalization stuff here
'------------------------------------------------
screen 19, 32, ,3
glViewport 0, 0, 800, 600
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective(45,800/600,1,100)
glMatrixMode GL_MODELVIEW
glLoadIdentity
glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 1.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
glBlendFunc(GL_SRC_ALPHA,GL_ONE)
glEnable GL_TEXTURE_2D
'-------------------------------------------------
Never saw someone formatting is code centered :D
-
Yeah, that's a different way of indenting isn't it :) I hadn't noticed that.
-
lol, ohh yeah i forgot about that. i was bored and thought what the hell im a bit sad like that :)
-
Actually I saw this and thought, what a brilliant idea, that way irrelevent parts of the code are there for completeness but you dont bother with them and skipp straight to the meat. I think its cool: intentional or not.
-
cheers taj,
i guess i better give the tabbing a name like ninos formatting or something ;D