Latest way of using OpenGL

BlitzMax Forums/OpenGL Module/Latest way of using OpenGL

USNavyFish(Posted 2008) [#1]
Any help getting OpenGL into BMax would be greatly appreciated (I am not new to GL, just new to BMax)

Where does the command "GLGraphics 800, 600" come from? Glew?

Basically I just want the quickest way to access OGL 1.3

Thanks!


USNavyFish(Posted 2008) [#2]
Another Question,

I'd trying to have a MaxGUI Canvas object handle OpenGL buffers. Is this even possible?


kfprimm(Posted 2008) [#3]
It comes from BRL.GLGraphics. It is just a module which creates/handles an OpenGL context.

Currently, OpenGL 1.1 is used. So I don't know how you would go about doing that.

To use OpenGL in a canvas call,
SetGraphicsDriver GLGraphicsDriver()

at the beginning of your program.

Then when you go to render call,
SetGraphics CanvasGraphics(canvas)

After that, you can use OpenGL like you normally would.


tonyg(Posted 2008) [#4]
You can access OGL 1.3 (1.34 i fact) using pub.glew


USNavyFish(Posted 2008) [#5]
I've patched together this extremely messy app by looking through a number of tutorials. The implementation is embedded in the 'gui', which is horrible practice, I know.. but right now it's simply to learn the commands.

Import maxgui.drivers

SuperStrict


SetGraphicsDriver(GLGraphicsDriver())

Local style:Int = WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS | WINDOW_RESIZABLE | WINDOW_STATUS
'DebugLog "Stye Value: "+style

Global MyWindow:TGadget=CreateWindow("OpenGL in a MaxGUI Canvas Gadget", 400,400,640,480,Null,style)
Global MyCanvas:TGadget=CreateCanvas(0,0,GadgetWidth(MyWindow)-200,GadgetHeight(MyWindow),MyWindow)
Local GraphicContext:TGraphics=CanvasGraphics(MyCanvas)
SetGraphics GraphicContext


glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0,GadgetWidth(mycanvas),GadgetHeight(mycanvas),0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() 
glClearColor(0 , 0 , 0 , 1) 


Global Vertices:Float[] = [-20.0, -20.0, 0.0, -20.0, 20.0, 0.0, 20.0, 20.0, 0.0, 20.0, -20.0, 0.0]
Global Colors:Float[] = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0]


Global x:Int=0
Global y:Int=0

ActivateGadget(MyCanvas)

AddHook EmitEventHook , HookManager


Repeat
	WaitEvent()
Forever


Function HookManager:Object(id:Int , data:Object , context:Object)
	
	Local event:TEvent = TEvent(data) 
	
	If event = Null Then Return Null
	
	Select event.source
	
	Case MyCanvas
		
		Select event.id
	  	
		Case EVENT_MOUSEENTER
			HideMouse
		
		Case EVENT_MOUSELEAVE
			ShowMouse
		
		Case EVENT_MOUSEMOVE
			x=event.x
			y=event.y
	     	RedrawGadget(MyCanvas)
	 	
		Case EVENT_GADGETPAINT
			drawcanvas()    
	 	
		End Select
	
	Case MyWindow
		
		Select event.id
		
		Case EVENT_WINDOWCLOSE
			End
		
		Case EVENT_WINDOWSIZE
			SetGadgetShape(MyCanvas,0,0,event.x-200,event.y )
			
		End Select	
	
	End Select

End Function


Function drawcanvas() 
	
	SetGraphics CanvasGraphics (MyCanvas)
    	
	glLoadIdentity()
	glClear(GL_COLOR_BUFFER_BIT)
	
	glTranslatef(x,y,0)
	
	glEnableClientState(GL_VERTEX_ARRAY)
	glEnableClientState(GL_COLOR_ARRAY)
	
	glColorPointer(3, GL_FLOAT, 0, Colors)
	glVertexPointer(3, GL_FLOAT, 0, Vertices)
	glDrawArrays(GL_QUADS, 0, 4)
	
	glDisableClientState(GL_VERTEX_ARRAY)
	glDisableClientState(GL_COLOR_ARRAY) 
	
	Flip
	
End Function



It works, but for all I know I'm doing things more complex than they need to be. what modifications (other than making it clean and object-oriented) would you present to the above methods? Or do I have it somewhat right?

Lastly, I also noticed that if there is no "waitevent()" between the repeat and forever, then my program freezes upon execution. Can anyone tell me why that is, and how to live without it?


USNavyFish(Posted 2008) [#6]
On a side note, what does Glew actually do? I've never used OGL extensions before.


USNavyFish(Posted 2008) [#7]
More stuff.. upon resizing my canvas with:

Case EVENT_WINDOWSIZE
			SetGadgetShape(MyCanvas , 0 , 0 , ClientWidth(MyWindow) - 200 , ClientHeight(myWindow) ) 


I then call my projection matrix resize function:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0,ClientWidth(mycanvas),ClientHeight(mycanvas),0)
glMatrixMode(GL_MODELVIEW) 


But the behavior is not as expected. I simply want the opengl projection matrix to stretch/skew with the canvas. Throw aspect ration right out the window (I'll lock the window/canvas sizes to a fixed 4:3 ratio later on)

Any Ideas?