Math Voxel demo

BlitzMax Forums/BlitzMax OpenGL Programming/Math Voxel demo

I had this idea a while ago that voxels would be a great method for graphing 3d inequalities. Anyway, i did it pretty quick with bmax and ogl:

'Not very optimized voxel demo

Strict
framework brl.blitzgl
Import brl.system

bglCreateContext(800,600)

Const mode=5

Local cells:Byte[,,,]

Select mode
	Case 5
		cells=New Byte[90,100,100,100]
	Default
		cells=New Byte[1,100,100,100]
End Select

For Local ax=0 Until 100
	For Local ay=0 Until 100
		For Local az=0 Until 100
			Local x#=ax-50
			Local y#=ay-50
			Local z#=az-50
			Select mode
			Case 1
				cells[0,ax,ay,az]=(Sqr(x*x+y*y+z*z)<25)
			Case 2
				cells[0,ax,ay,az]=((Sin(x*2)+Sin(y*2)-Sin(z*2))<.1)
			Case 3
				x=x/6
				y=y/6
				z=z/6
				cells[0,ax,ay,az]=((X^2 + Y^2 + Z^2 <= 9) & (Z^2 + (Sqr(X^2 + Y^2) - 2.5)^2 >= 2))
			Case 4
				cells[0,ax,ay,az]=25-Sqr(x*x+y*y+z*z)
			Case 5
				For Local i=0 Until 90
					cells[i,ax,ay,az]=(X*X+Y*Y+Sin(i*2)*180-50-Z*Z<2)
				Next
			End Select
		Next
	Next
Next

Local temp:Byte[,,]=New Byte[100,100,100]
If mode=5 Then
	For Local i=0 Until 90
		For Local ax=1 Until 99
			For Local ay=1 Until 99
				For Local az=1 Until 99
					temp[ax,ay,az]=cells[i,ax,ay,az]
				Next
			Next
		Next
		For Local ax=1 Until 99
			For Local ay=1 Until 99
				For Local az=1 Until 99
					If temp[ax,ay,az]>0 Then
						If temp[ax-1,ay-1,az-1]>0 And..
						   temp[ax,ay-1,az-1]>0 And..
						   temp[ax+1,ay-1,az-1]>0 And..
						   temp[ax-1,ay,az-1]>0 And..
						   temp[ax,ay,az-1]>0 And..
						   temp[ax+1,ay,az-1]>0 And..
						   temp[ax,ay+1,az-1]>0 And..
						   temp[ax+1,ay+1,az-1]>0 And..
						   temp[ax-1,ay-1,az]>0 And..
						   temp[ax,ay-1,az]>0 And..
						   temp[ax+1,ay-1,az]>0 And..
						   temp[ax-1,ay,az]>0 And..
						   temp[ax+1,ay,az]>0 And..
						   temp[ax,ay+1,az]>0 And..
						   temp[ax+1,ay+1,az]>0 And..
						   temp[ax-1,ay-1,az+1]>0 And..
						   temp[ax,ay-1,az+1]>0 And..
						   temp[ax+1,ay-1,az+1]>0 And..
						   temp[ax-1,ay,az+1]>0 And..
						   temp[ax,ay,az+1]>0 And..
						   temp[ax+1,ay,az+1]>0 And..
						   temp[ax,ay+1,az+1]>0 And..
						   temp[ax+1,ay+1,az+1]>0 Then
							cells[i,ax,ay,az]=0
						EndIf
					EndIf
				Next
			Next
		Next
	Next
Else
EndIf

glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, Float(800)/Float(600), 10, 300.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glEnable(GL_LIGHTING)
Local lite:Float Ptr=Float Ptr(GCMalloc(16))
lite[0]=.7
lite[1]=.2
lite[2]=.2
lite[3]=1
Local pos:Float Ptr=Float Ptr(GCMalloc(16))
pos[0]=50
pos[1]=50
pos[2]=50
pos[3]=1
glLightfv(GL_LIGHT0,GL_DIFFUSE,lite)
gllightfv(GL_LIGHT0,GL_POSITION,pos)
lite[0]=.2
lite[1]=.5
lite[2]=.2
lite[3]=1
pos[0]=-50
pos[1]=-50
pos[2]=50
pos[3]=1
glLightfv(GL_LIGHT1,GL_DIFFUSE,lite)
gllightfv(GL_LIGHT1,GL_POSITION,pos)
lite[0]=.2
lite[1]=.2
lite[2]=.5
lite[3]=1
pos[0]=50
pos[1]=-50
pos[2]=50
pos[3]=1
glLightfv(GL_LIGHT2,GL_DIFFUSE,lite)
gllightfv(GL_LIGHT2,GL_POSITION,pos)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
glEnable(GL_LIGHT2)
glEnable(GL_COLOR_MATERIAL)
'glpolygonmode(GL_FRONT,GL_LINE)

Local t=0
While Not KeyHit(1)
	t:+1	
	glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	glLoadIdentity()
	Local z=MouseZ()-200
	gltranslatef(0,0,z)
	glrotatef(MouseX(),0,1,0)
	glrotatef(MouseY(),1,0,0)
	If mode<>5 Then DrawCells(cells) Else DrawCells(cells,t Mod 90)
	bglSwapBuffers
Wend

Function DrawCells(a:Byte[,,,],t=0)
	glbegin(GL_QUADS)
		For Local x=0 Until 100
			For Local y=0 Until 100
				For Local z=0 Until 100
					If a[t,x,y,z]>0 Then
						'glcolor3ub(a[t,x,y,z],a[t,x,y,z],a[t,x,y,z])
						'glcolor3f(1,0,0)
						glnormal3f(0,1,0)
						glvertex3f(x-50.5,y-49.5,z-50.5)
						glvertex3f(x-50.5,y-49.5,z-49.5)
						glvertex3f(x-49.5,y-49.5,z-49.5)
						glvertex3f(x-49.5,y-49.5,z-50.5)	'bottom
						
						'glcolor3f(0,1,0)
						glnormal3f(0,-1,0)
						glvertex3f(x-49.5,y-50.5,z-50.5)
						glvertex3f(x-49.5,y-50.5,z-49.5)
						glvertex3f(x-50.5,y-50.5,z-49.5)
						glvertex3f(x-50.5,y-50.5,z-50.5)	'top
						
						'glcolor3f(1,1,0)
						glnormal3f(0,0,1)
						glvertex3f(x-49.5,y-49.5,z-49.5)
						glvertex3f(x-50.5,y-49.5,z-49.5)
						glvertex3f(x-50.5,y-50.5,z-49.5)
						glvertex3f(x-49.5,y-50.5,z-49.5)	'Front
						
						'glcolor3f(0,0,1)
						glnormal3f(0,0,-1)
						glvertex3f(x-49.5,y-50.5,z-50.5)
						glvertex3f(x-50.5,y-50.5,z-50.5)
						glvertex3f(x-50.5,y-49.5,z-50.5)
						glvertex3f(x-49.5,y-49.5,z-50.5)	'Back
						
						'glcolor3f(1,0,1)
						glnormal3f(-1,0,0)
						glvertex3f(x-50.5,y-50.5,z-50.5)
						glvertex3f(x-50.5,y-50.5,z-49.5)
						glvertex3f(x-50.5,y-49.5,z-49.5)
						glvertex3f(x-50.5,y-49.5,z-50.5)	'Right
						
						'glcolor3f(1,1,1)
						glnormal3f(1,0,0)
						glvertex3f(x-49.5,y-49.5,z-50.5)
						glvertex3f(x-49.5,y-49.5,z-49.5)
						glvertex3f(x-49.5,y-50.5,z-49.5)
						glvertex3f(x-49.5,y-50.5,z-50.5)	'Left
					EndIf
				Next
			Next
		Next
	glend()
EndFunction


I have it set on the coolest one, which will take 85mb of ram for the data since it is a 100x100x100 grid with 90 frames of animation. The others are lots smaller and preprocess faster.

Could prolly optimize it by storing the voxels as types rather than a big array of ones and zeros. If someone could point me to how to do transperancy in ogl, it wouldn't be too hard to make some neat graphs where the transperancy is the distance from the center, etc.

looks cool :D

there are quite a few gl examples around doing that stuff, one plots an entire planet using height data etc, its mad:P

This hangs for me :(
Strict

Framework brl.blitzgl
Import brl.system

bglCreateContext(800,600)

Const mode=5

Local cells:Byte[,,,]

Select mode
	Case 5
		cells=New Byte[90,100,100,100]
	Default
		cells=New Byte[1,100,100,100]
End Select

For Local ax=0 Until 100
	For Local ay=0 Until 100
		For Local az=0 Until 100
			Local x#=ax-50
			Local y#=ay-50
			Local z#=az-50
			Select mode
			Case 1
				cells[0,ax,ay,az]=(Sqr(x*x+y*y+z*z)<25)
			Case 2
				cells[0,ax,ay,az]=((Sin(x*2)+Sin(y*2)-Sin(z*2))<.1)
			Case 3
				x=x/6
				y=y/6
				z=z/6
				cells[0,ax,ay,az]=((X^2 + Y^2 + Z^2 <= 9) & (Z^2 + (Sqr(X^2 + Y^2) - 2.5)^2 >= 2))
			Case 4
				cells[0,ax,ay,az]=25-Sqr(x*x+y*y+z*z)
			Case 5
				For Local i=0 Until 90
					cells[i,ax,ay,az]=(X*X+Y*Y+Sin(i*2)*180-50-Z*Z<2)
				Next
			End Select
		Next
	Next
Next

Local temp:Byte[,,]=New Byte[100,100,100]
If mode=5 Then
	For Local i=0 Until 90
		For Local ax=1 Until 99
			For Local ay=1 Until 99
				For Local az=1 Until 99
					temp[ax,ay,az]=cells[i,ax,ay,az]
				Next
			Next
		Next
		For Local ax=1 Until 99
			For Local ay=1 Until 99
				For Local az=1 Until 99
					If temp[ax,ay,az]>0 Then
						If temp[ax-1,ay-1,az-1]>0 And..
						   temp[ax,ay-1,az-1]>0 And..
						   temp[ax+1,ay-1,az-1]>0 And..
						   temp[ax-1,ay,az-1]>0 And..
						   temp[ax,ay,az-1]>0 And..
						   temp[ax+1,ay,az-1]>0 And..
						   temp[ax,ay+1,az-1]>0 And..
						   temp[ax+1,ay+1,az-1]>0 And..
						   temp[ax-1,ay-1,az]>0 And..
						   temp[ax,ay-1,az]>0 And..
						   temp[ax+1,ay-1,az]>0 And..
						   temp[ax-1,ay,az]>0 And..
						   temp[ax+1,ay,az]>0 And..
						   temp[ax,ay+1,az]>0 And..
						   temp[ax+1,ay+1,az]>0 And..
						   temp[ax-1,ay-1,az+1]>0 And..
						   temp[ax,ay-1,az+1]>0 And..
						   temp[ax+1,ay-1,az+1]>0 And..
						   temp[ax-1,ay,az+1]>0 And..
						   temp[ax,ay,az+1]>0 And..
						   temp[ax+1,ay,az+1]>0 And..
						   temp[ax,ay+1,az+1]>0 And..
						   temp[ax+1,ay+1,az+1]>0 Then
							cells[i,ax,ay,az]=0
						EndIf
					EndIf
				Next
			Next
		Next
	Next
Else
EndIf

glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, Float(800)/Float(600), 10, 300.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glEnable(GL_LIGHTING)
Local lite:Float Ptr=Float Ptr(GCMalloc(16))
lite[0]=.7
lite[1]=.2
lite[2]=.2
lite[3]=1
Local pos:Float Ptr=Float Ptr(GCMalloc(16))
pos[0]=50
pos[1]=50
pos[2]=50
pos[3]=1
glLightfv(GL_LIGHT0,GL_DIFFUSE,lite)
gllightfv(GL_LIGHT0,GL_POSITION,pos)
lite[0]=.2
lite[1]=.5
lite[2]=.2
lite[3]=1
pos[0]=-50
pos[1]=-50
pos[2]=50
pos[3]=1
glLightfv(GL_LIGHT1,GL_DIFFUSE,lite)
gllightfv(GL_LIGHT1,GL_POSITION,pos)
lite[0]=.2
lite[1]=.2
lite[2]=.5
lite[3]=1
pos[0]=50
pos[1]=-50
pos[2]=50
pos[3]=1
glLightfv(GL_LIGHT2,GL_DIFFUSE,lite)
gllightfv(GL_LIGHT2,GL_POSITION,pos)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
glEnable(GL_LIGHT2)
glEnable(GL_COLOR_MATERIAL)
'glpolygonmode(GL_FRONT,GL_LINE)

Local t=0
While Not KeyHit(1)
	t:+1	
	glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	glLoadIdentity()
	Local z=MouseZ()-200
	gltranslatef(0,0,z)
	glrotatef(MouseX(),0,1,0)
	glrotatef(MouseY(),1,0,0)
	If mode<>5 Then DrawCells(cells) Else DrawCells(cells,t Mod 90)
	bglSwapBuffers
Wend

Function DrawCells(a:Byte[,,,],t=0)
	glbegin(GL_QUADS)
		For Local x=0 Until 100
			For Local y=0 Until 100
				For Local z=0 Until 100
					If a[t,x,y,z]>0 Then
						'glcolor3ub(a[t,x,y,z],a[t,x,y,z],a[t,x,y,z])
						'glcolor3f(1,0,0)
						glnormal3f(0,1,0)
						glvertex3f(x-50.5,y-49.5,z-50.5)
						glvertex3f(x-50.5,y-49.5,z-49.5)
						glvertex3f(x-49.5,y-49.5,z-49.5)
						glvertex3f(x-49.5,y-49.5,z-50.5)	'bottom
						
						'glcolor3f(0,1,0)
						glnormal3f(0,-1,0)
						glvertex3f(x-49.5,y-50.5,z-50.5)
						glvertex3f(x-49.5,y-50.5,z-49.5)
						glvertex3f(x-50.5,y-50.5,z-49.5)
						glvertex3f(x-50.5,y-50.5,z-50.5)	'top
						
						'glcolor3f(1,1,0)
						glnormal3f(0,0,1)
						glvertex3f(x-49.5,y-49.5,z-49.5)
						glvertex3f(x-50.5,y-49.5,z-49.5)
						glvertex3f(x-50.5,y-50.5,z-49.5)
						glvertex3f(x-49.5,y-50.5,z-49.5)	'Front
						
						'glcolor3f(0,0,1)
						glnormal3f(0,0,-1)
						glvertex3f(x-49.5,y-50.5,z-50.5)
						glvertex3f(x-50.5,y-50.5,z-50.5)
						glvertex3f(x-50.5,y-49.5,z-50.5)
						glvertex3f(x-49.5,y-49.5,z-50.5)	'Back
						
						'glcolor3f(1,0,1)
						glnormal3f(-1,0,0)
						glvertex3f(x-50.5,y-50.5,z-50.5)
						glvertex3f(x-50.5,y-50.5,z-49.5)
						glvertex3f(x-50.5,y-49.5,z-49.5)
						glvertex3f(x-50.5,y-49.5,z-50.5)	'Right
						
						'glcolor3f(1,1,1)
						glnormal3f(1,0,0)
						glvertex3f(x-49.5,y-49.5,z-50.5)
						glvertex3f(x-49.5,y-49.5,z-49.5)
						glvertex3f(x-49.5,y-50.5,z-49.5)
						glvertex3f(x-49.5,y-50.5,z-50.5)	'Left
					EndIf
				Next
			Next
		Next
	glend()
EndFunction


"DannyD's" name has been on the main page for 4 days now - not any more.

"puki" takes his place on the top spot.