3D Lines using GL_LINE?

BlitzMax Forums/MiniB3D Module/3D Lines using GL_LINE?

Uncle(Posted 2009) [#1]
Hello,

I've searched the forums and havent found a good solution for drawing 3D lines. Can anyone give me any pointers on how to do this? I know how to draw 2D lines based on a cameraprojection, but I would really like to have true 3D lines.

I see there is an Open GL command called GL_Line, and I see you feed it with two 3D vectors. However Im not sure how these vectors are work as they do not appear to be the same as a 3D position in blitz space? Can anyone point me in the right direction please?

Cheers,


Unc


Uncle(Posted 2009) [#2]
Sorry to bump, but I'm still stuck with this. Basically I would like to create a 3D line using a (startx,starty,startz,endx,endy,endz) set of arguments. I think this would be possible with GL_Line, however I cant figure out how GL co-ordinates work with minib3d co-ordinates.

There was an old post from Klepto saying adding 3D Lines would be easy and that it would be added, but I guess it hasn't yet.

http://www.blitzbasic.com/Community/posts.php?topic=74501#832693

Of course I'm open to any other ideas as well. Ideally though the lines need to be a fixed with and not effected by scale i.e. one of the line should not appear thicker than the other end due to perspective.

Cheers,

Unc


simonh(Posted 2009) [#3]
Yep, you can use GL_LINE (just invert the Z value), or you can create your own line using a custom mesh - two triangles between each point will do it.


Uncle(Posted 2009) [#4]
Simon you're the best. Here's the code for anyone else who may want to do this.

SuperStrict

Import "minib3d.bmx"

Graphics3D 1024, 768, 32, 2, 0
SetGraphicsDriver GLMax2DDriver()
SetBlend ALPHABLEND
SetBlend LIGHTBLEND
glEnable(GL_LINE_SMOOTH)
HideMouse()

Global cam:TCamera = CreateCamera()
CameraRange cam, 0.01, 1000
MoveEntity cam, 0, 0, - 10

'/////////////// CREATE 2 POINTS WHICH WE WANT TO CONNECT
Global startPivot:tmesh = CreateSphere()
ScaleEntity startPivot, 0.2, 0.2, 0.2
EntityColor startPivot, 255, 0, 0
MoveEntity startPivot, 5, 0, 0

Global endPivot:tmesh = CreateSphere()
ScaleEntity endPivot, 0.2, 0.2, 0.2
EntityColor endPivot, 0, 255, 0
MoveEntity endPivot, - 5, 0, 0

Global centerPivot:tpivot = CreatePivot()
EntityParent startPivot, centerPivot
EntityParent endPivot, centerPivot
TurnEntity centerPivot, 0, 0, 20

While Not KeyDown(KEY_ESCAPE)
	
	TurnEntity centerPivot, 0, 1, 0
	
	RenderWorld
	glBegin(GL_LINES)
	    glVertex3f(EntityX(startPivot, 1), EntityY(startPivot, 1), - EntityZ(startPivot, 1))
    	glVertex3f(EntityX(endPivot, 1), EntityY(endPivot, 1), - EntityZ(endPivot, 1))
  	glEnd()

	If KeyDown(KEY_Q) Then MoveEntity cam, 0, 0, 0.1
	If KeyDown(KEY_A) Then MoveEntity cam, 0, 0, - 0.1
	Flip 1
	
Wend


Be careful though. I had this code written last week, but there was one mistake. I was using glBegin(GL_LINE) and not glBegin(GL_LINES). That S makes all the difference.

Cheers,


Unc


jkrankie(Posted 2009) [#5]
Sample code should be prettier ;)

SuperStrict

Import sidesign.minib3d

Graphics3D 1024, 768, 32, 2, 0
SetGraphicsDriver GLMax2DDriver()
SetBlend ALPHABLEND
SetBlend LIGHTBLEND
glEnable(GL_LINE_SMOOTH)
glEnable(GL_LINE_STIPPLE)
HideMouse()

Global cam:TCamera = CreateCamera()
CameraRange cam, 0.01, 1000
MoveEntity cam, 0, 0, - Pi*20
camerazoom cam,Pi

Global light:tlight=createlight()
positionentity light,0,0,-10

Global startPivot:tmesh[50]
Global endPivot:tmesh[50]
Global centerPivot:tpivot[50]

For Local i:Int=0 To 49
startpivot[i]= createsphere()
ScaleEntity startPivot[i], 0.2, 0.2, 0.2
EntityColor startPivot[i], 255, 255-(i*5.1), i*5.1
MoveEntity startPivot[i], 5, i*0.22, Float(i)/10
entityblend startpivot[i],3

endpivot[i]=createsphere()
ScaleEntity endPivot[i], 0.2, 0.2, 0.2
EntityColor endPivot[i], 255, 255-(i*5.1), i*5.1
MoveEntity endPivot[i], - 5, i*0.22, Float(i)/10
entityblend endPivot[i],3

centerPivot[i]=createpivot()
EntityParent startPivot[i], centerPivot[i]
EntityParent endPivot[i], centerPivot[i]
TurnEntity centerPivot[i], 0, i*7.2, 0

Next

While Not KeyDown(KEY_ESCAPE)
	
	For Local i:Int=0 To 49
	TurnEntity centerPivot[i], -2, Pi, -1
	Next
	
	RenderWorld
	
	For Local i:Int=0 To 49
		
		gllinewidth(2)
		gllinestipple(3,$0A0C0F)
		
		glBegin(GL_LINES)
			
		    glVertex3f(EntityX(startPivot[i], 1), EntityY(startPivot[i], 1), - EntityZ(startPivot[i], 1))
	    	glVertex3f(EntityX(endPivot[i], 1), EntityY(endPivot[i], 1), - EntityZ(endPivot[i], 1))
	  	glEnd()
	Next
	If KeyDown(KEY_Q) Then MoveEntity cam, 0, 0, 0.1
	If KeyDown(KEY_A) Then MoveEntity cam, 0, 0, - 0.1
	Flip 1
	
Wend


Cheers
Charlie


jkrankie(Posted 2009) [#6]
I've been looking at this again, and i can't work out how to set the lines color or alpha values. I've tried putting a glColor4f() in there, but it doesn't work. Do i need to enable or disable something?

Cheers
Charlie


Uncle(Posted 2009) [#7]
Hi Charlie,

Have you enabled color_material and disable lighting?

    glEnable(GL_COLOR_MATERIAL)
    glDisable(GL_LIGHTING)


Try this before before drawing your lines.

Cheers,


Unc


jkrankie(Posted 2009) [#8]
No, i hadn't tried them, thanks for pointing them out :) It seems i only need to enable the color materials though, disabling the lighting doesn't appear to have any effect on the lines.

Thanks very much, despite having two large OpenGL books next to me in my office, i still quite often find myself stuck.

Cheers
Charlie