AntTweakBar tricky GUI for OpenGL and D3D.

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

AntTweakBar tricky GUI for OpenGL and D3D.

Post by D.J.Peters »

AntTweakBar 1.16 a tricky GUI for OpenGL, GLFW, freeGLUT
homepage: https://anttweakbar.sourceforge.net

download: fbAntTweakBar.zip

Joshy
Last edited by D.J.Peters on Oct 12, 2022 17:48, edited 11 times in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by counting_pine »

Cool,
D.J.Peters wrote:' @doc http:' anttweakbar.sourceforge.net/doc
Something tells me this may be a find/replace error?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by D.J.Peters »

First FBGFX OpenGL test:

Joshy
Image
Last edited by D.J.Peters on Oct 12, 2022 17:49, edited 6 times in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by D.J.Peters »

First GLFW 3.0.1 test.

Joshy
Image
Last edited by D.J.Peters on Oct 12, 2022 17:49, edited 3 times in total.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by VANYA »

Very cool Joshy!
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by VANYA »

Hi Joshy!

I'm having to translate the example TwSimpleGLUT.
You will not prompt what is wrong in the code?

Code: Select all

''  ---------------------------------------------------------------------------
''
''  @file       TwSimpleGLUT.c
''  @brief      A simple example that uses AntTweakBar with OpenGL and GLUT.
''
''              AntTweakBar: http:'anttweakbar.sourceforge.net/doc
''              OpenGL:      http:'www.opengl.org
''              GLUT:        http:'opengl.org/resources/libraries/glut
''
''  @author     Philippe Decaudin
''  @date       2006/05/20
''
''  ---------------------------------------------------------------------------


#Include Once "GL/glu.bi"
#Include Once "GL/glut.bi"
#Include Once "AntTweakBar.bi"
#Include Once "crt.bi"

#If Defined(__FB_WIN32__)
	#Include "windows.bi"
	#Define USE_MINI_GLUT
#EndIf


' This example displays one of the following shapes
Enum Shape
	SHAPE_TEAPOT=1
	SHAPE_TORUS
	SHAPE_CONE
End Enum

#define NUM_SHAPES 3


Type float As Single


Dim Shared As Shape g_CurrentShape = SHAPE_TORUS
' Shapes scale
Dim Shared As float g_Zoom = 1.0f
' Shape orientation (stored as a quaternion)
Dim Shared As float g_Rotation(...) = { 0.0f, 0.0f, 0.0f, 1.0f }
' Auto rotate
Dim Shared As Integer g_AutoRotate
Dim Shared As Integer g_RotateTime
Dim Shared As float g_RotateStart(...) = { 0.0f, 0.0f, 0.0f, 1.0f }
' Shapes material
Dim Shared As float g_MatAmbient(...) = { 0.5f, 0.0f, 0.0f, 1.0f }
Dim Shared As float g_MatDiffuse(...) = { 1.0f, 1.0f, 0.0f, 1.0f }
' Light parameter
Dim Shared As float g_LightMultiplier = 1.0f
Dim Shared As float g_LightDirection(...) = { -0.57735f, -0.57735f, -0.57735f }


' Routine to set a quaternion from a rotation axis and angle
' ( input axis = float[3] angle = float  output: quat = float[4] )
Sub SetQuaternionFromAxisAngle(axis As float Ptr ,angle As float , quat As float Ptr)
	Dim As float sina2, norm
	sina2 = Sin(0.5f * angle)
	norm = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2])
	quat[0] = sina2 * axis[0] / norm
	quat[1] = sina2 * axis[1] / norm
	quat[2] = sina2 * axis[2] / norm
	quat[3] = Cos(0.5f * angle)
End Sub


' Routine to convert a quaternion to a 4x4 matrix
' ( input: quat = float[4]  output: mat = float[4*4] )
Sub ConvertQuaternionToMatrix(quat As float Ptr , mat As float Ptr )
	Dim As float yy2 = 2.0f * quat[1] * quat[1]
	Dim As float xy2 = 2.0f * quat[0] * quat[1]
	Dim As float xz2 = 2.0f * quat[0] * quat[2]
	Dim As float yz2 = 2.0f * quat[1] * quat[2]
	Dim As float zz2 = 2.0f * quat[2] * quat[2]
	Dim As float wz2 = 2.0f * quat[3] * quat[2]
	Dim As float wy2 = 2.0f * quat[3] * quat[1]
	Dim As float wx2 = 2.0f * quat[3] * quat[0]
	Dim As float xx2 = 2.0f * quat[0] * quat[0]
	mat[0*4+0] = - yy2 - zz2 + 1.0f
	mat[0*4+1] = xy2 + wz2
	mat[0*4+2] = xz2 - wy2
	mat[0*4+3] = 0
	mat[1*4+0] = xy2 - wz2
	mat[1*4+1] = - xx2 - zz2 + 1.0f
	mat[1*4+2] = yz2 + wx2
	mat[1*4+3] = 0
	mat[2*4+0] = xz2 + wy2
	mat[2*4+1] = yz2 - wx2
	mat[2*4+2] = - xx2 - yy2 + 1.0f
	mat[2*4+3] = 0
	mat[3*4+0] = mat[3*4+1] = mat[3*4+2] = 0
	mat[3*4+3] = 1
End Sub


' Routine to multiply 2 quaternions (ie, compose rotations)
' ( input q1 = float[4] q2 = float[4]  output: qout = float[4] )
Sub MultiplyQuaternions(q1 As float Ptr , q2 As float Ptr , qout As float Ptr )
	Dim As float qr(4)
	qr(0) = q1[3]*q2[0] + q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1]
	qr(1) = q1[3]*q2[1] + q1[1]*q2[3] + q1[2]*q2[0] - q1[0]*q2[2]
	qr(2) = q1[3]*q2[2] + q1[2]*q2[3] + q1[0]*q2[1] - q1[1]*q2[0]
	qr(3)  = q1[3]*q2[3] - (q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2])
	qout[0] = qr(0): qout[1] = qr(1): qout[2] = qr(2): qout[3] = qr(3)
End Sub


' Return elapsed time in milliseconds
Function GetTimeMs() As Integer
	#If Not Defined(__FB_WIN32__)
		Return glutGet(GLUT_ELAPSED_TIME)
	#Else
		' glutGet(GLUT_ELAPSED_TIME) seems buggy on Windows
		Return GetTickCount()
	#EndIf
End Function





' Callback function called by GLUT to render screen
Sub Display Cdecl()

	Dim As float v(4) ' will be used to set light parameters
	Dim As float mat(4*4) ' rotation matrix

	' Clear frame buffer
	glClearColor(0, 0, 0, 1)
	glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)

	glEnable(GL_DEPTH_TEST)
	glDisable(GL_CULL_FACE)
	glEnable(GL_NORMALIZE)

	' Set light
	glEnable(GL_LIGHTING)
	glEnable(GL_LIGHT0)
	v(2) = g_LightMultiplier*0.4f
	v(2) = v(1) : v(0) = v(1): v(3) = 1.0f
	glLightfv(GL_LIGHT0, GL_AMBIENT, @v(0))
	v(2) = g_LightMultiplier*0.8f
	v(0) = v(2): v(1) = v(2):v(3) = 1.0f
	glLightfv(GL_LIGHT0, GL_DIFFUSE, @v(0))
	v(0) = -g_LightDirection(0): v(1) = -g_LightDirection(1)
	v(2) = -g_LightDirection(2): v(3) = 0.0f
	glLightfv(GL_LIGHT0, GL_POSITION, @v(0))

	' Set material
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, @g_MatAmbient(0))
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, @g_MatDiffuse(0))

	' Rotate and draw shape
	glPushMatrix()
	glTranslatef(0.5f, -0.3f, 0.0f)
	If g_AutoRotate   Then
		Dim As float axis(3) = { 0, 1, 0 }
		Dim As float angle = (GetTimeMs()-g_RotateTime)/1000.0f
		Dim As float quat(4)
		SetQuaternionFromAxisAngle(@axis(0), angle, @quat(0))
		MultiplyQuaternions(@g_RotateStart(0), @quat(0), @g_Rotation(0))
	EndIf
	ConvertQuaternionToMatrix(@g_Rotation(0), @mat(0))
	glMultMatrixf(@mat(0))
	glScalef(g_Zoom, g_Zoom, g_Zoom)
	glCallList(g_CurrentShape)
	glPopMatrix()

	' Draw tweak bars
	TwDraw()

	' Present frame buffer
	glutSwapBuffers()

	' Recall Display at next frame
	glutPostRedisplay()
End Sub


' Callback function called by GLUT when window size changes
Sub Reshape Cdecl(width_ As Integer,height As Integer)

	' Set OpenGL viewport and camera
	glViewport(0, 0, width_, height)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(40, width_/height, 1, 10)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	gluLookAt(0,0,5, 0,0,0, 0,1,0)
	glTranslatef(0, 0.6f, -1)

	' Send the new window size to AntTweakBar
	TwWindowSize(width_, height)
End Sub


' Function called at exit
Sub Terminate Cdecl()
	glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES)

	TwTerminate()
End Sub


'  Callback function called when the 'AutoRotate' variable value of the tweak bar has changed
Sub SetAutoRotateCB TW_CALL(value As Any Ptr, clientData As Any Ptr)

	'(void)clientData; ' unused

	g_AutoRotate = *Cast(Integer Ptr,value) ' copy value to g_AutoRotate
	If( g_AutoRotate<>0 ) Then
		' init rotation
		g_RotateTime = GetTimeMs()
		g_RotateStart(0) = g_Rotation(0)
		g_RotateStart(1) = g_Rotation(1)
		g_RotateStart(2) = g_Rotation(2)
		g_RotateStart(3) = g_Rotation(3)

		' make Rotation variable read-only
		TwDefine(" TweakBar/ObjRotation readonly ")
	Else
		' make Rotation variable read-write
		TwDefine(" TweakBar/ObjRotation readwrite ")
	EndIf
End Sub


'  Callback function called by the tweak bar to get the 'AutoRotate' value
Sub GetAutoRotateCB TW_CALL(value As Any Ptr, clientData As Any Ptr)
	'(void)clientData; ' unused
	*Cast(Integer Ptr,value) = g_AutoRotate ' copy g_AutoRotate to value
End Sub



Dim As TwBar Ptr bar ' Pointer to the tweak bar
Dim As float axis(...) = { 0.7f, 0.7f, 0.0f } ' initial model rotation
Dim As float angle = 0.8f

' Initialize GLUT
glutInit(1, StrPtr( " " ))
glutInitDisplayMode(GLUT_DOUBLE + GLUT_RGB + GLUT_DEPTH)
glutInitWindowSize(640, 480)
glutCreateWindow("AntTweakBar simple example using GLUT")
glutCreateMenu(NULL)

' Set GLUT callbacks
glutDisplayFunc(@Display)
glutReshapeFunc(@Reshape)
atexit(@Terminate)  ' Called after glutMainLoop ends

' Initialize AntTweakBar
TwInit(TW_OPENGL, NULL)

' Set GLUT event callbacks
' - Directly redirect GLUT mouse button events to AntTweakBar
glutMouseFunc(@TwEventMouseButtonGLUT)
'' - Directly redirect GLUT mouse motion events to AntTweakBar
glutMotionFunc(@TwEventMouseMotionGLUT)
'' - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion)
glutPassiveMotionFunc(@TwEventMouseMotionGLUT)
'' - Directly redirect GLUT key events to AntTweakBar
glutKeyboardFunc(@TwEventKeyboardGLUT)
'' - Directly redirect GLUT special key events to AntTweakBar
glutSpecialFunc(@TwEventSpecialGLUT)
'' - Send 'glutGetModifers' function pointer to AntTweakBar;
''   required because the GLUT key event functions do not report key modifiers states.
TwGLUTModifiersFunc(glutGetModifiers)

' Create some 3D objects (stored in display lists)
glNewList(SHAPE_TEAPOT, GL_COMPILE)
glutSolidTeapot(1.0)
glEndList()
glNewList(SHAPE_TORUS, GL_COMPILE)
glutSolidTorus(0.3, 1.0, 16, 32)
glEndList()
glNewList(SHAPE_CONE, GL_COMPILE)
glutSolidCone(1.0, 1.5, 64, 4)
glEndList()

' Create a tweak bar
bar = TwNewBar("TweakBar")
TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLUT and OpenGL.' ") ' Message added to the help bar.
TwDefine(" TweakBar size='200 400' color='96 216 224' ") ' change default tweak bar size and color

' Add 'g_Zoom' to 'bar': this is a modifable (RW) variable of type TW_TYPE_FLOAT. Its key shortcuts are [z] and [Z].
TwAddVarRW(bar, "Zoom", TW_TYPE_FLOAT, @g_Zoom, _
" min=0.01 max=2.5 step=0.01 keyIncr=z keyDecr=Z help='Scale the object (1=original size).' ")

' Add 'g_Rotation' to 'bar': this is a variable of type TW_TYPE_QUAT4F which defines the object's orientation
TwAddVarRW(bar, "ObjRotation", TW_TYPE_QUAT4F, @g_Rotation(0), _
" label='Object rotation' opened=true help='Change the object orientation.' ")

' Add callback to toggle auto-rotate mode (callback functions are defined above).
TwAddVarCB(bar, "AutoRotate", TW_TYPE_BOOL32, Cast(TwSetVarCallback,@SetAutoRotateCB), @GetAutoRotateCB, NULL, _
" label='Auto-rotate' key=space help='Toggle auto-rotate mode.' ")

' Add 'g_LightMultiplier' to 'bar': this is a variable of type TW_TYPE_FLOAT. Its key shortcuts are [+] and [-].
TwAddVarRW(bar, "Multiplier", TW_TYPE_FLOAT, @g_LightMultiplier, _
" label='Light booster' min=0.1 max=4 step=0.02 keyIncr='+' keyDecr='-' help='Increase/decrease the light power.' ")

' Add 'g_LightDirection' to 'bar': this is a variable of type TW_TYPE_DIR3F which defines the light direction
TwAddVarRW(bar, "LightDir", TW_TYPE_DIR3F, @g_LightDirection(0), _
" label='Light direction' opened=true help='Change the light direction.' ")

' Add 'g_MatAmbient' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored)
' and is inserted into a group named 'Material'.
TwAddVarRW(bar, "Ambient", TW_TYPE_COLOR3F, @g_MatAmbient(0), " group='Material' ")

' Add 'g_MatDiffuse' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored)
' and is inserted into group 'Material'.
TwAddVarRW(bar, "Diffuse", TW_TYPE_COLOR3F, @g_MatDiffuse(0), " group='Material' ")

' Add the enum variable 'g_CurrentShape' to 'bar'
' (before adding an enum variable, its enum type must be declared to AntTweakBar as follow)
Scope
	' ShapeEV associates Shape enum values with labels that will be displayed instead of enum values
	Dim As TwEnumVal shapeEV(NUM_SHAPES) = { (SHAPE_TEAPOT, @"Teapot"), (SHAPE_TORUS, @"Torus"), (SHAPE_CONE, @"Cone") }
	' Create a type for the enum shapeEV
	Dim As TwType shapeType = TwDefineEnum("ShapeType", @shapeEV(0), NUM_SHAPES)
	' add 'g_CurrentShape' to 'bar': this is a variable of type ShapeType. Its key shortcuts are [<] and [>].
	TwAddVarRW(bar, "Shape", shapeType, @g_CurrentShape, " keyIncr='<' keyDecr='>' help='Change object shape.' ")
End Scope

' Store time
g_RotateTime = GetTimeMs()
' Init rotation
SetQuaternionFromAxisAngle(@axis(0), angle, @g_Rotation(0))
SetQuaternionFromAxisAngle(@axis(0), angle, @g_RotateStart(0))

' Call the GLUT main loop
glutMainLoop()
FbTemp.o:fake:(.text+0xa14): undefined reference to `TWEVENTMOUSEBUTTONGLUT'
FbTemp.o:fake:(.text+0xa1e): undefined reference to `TWEVENTMOUSEMOTIONGLUT'
FbTemp.o:fake:(.text+0xa28): undefined reference to `TWEVENTMOUSEMOTIONGLUT'
FbTemp.o:fake:(.text+0xa32): undefined reference to `TWEVENTKEYBOARDGLUT'
FbTemp.o:fake:(.text+0xa3c): undefined reference to `TWEVENTSPECIALGLUT'
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by TJF »

It may need aliases in the header like (just guessing)
  • ... ALIAS "_TwEventSpecialGlut" ...
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by VANYA »

TJF wrote:It may need aliases in the header like (just guessing)
  • ... ALIAS "_TwEventSpecialGlut" ...
Thank TJF! Yes indeed incorrectly describes the functions in the header. Although in the example, and a lot of my mistakes, I will continue to look for, why does not work properly :)))
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by VANYA »

Corrected the errors in the code which found, but it still does not work properly. :(
Crashes on exit and rotate by hand is incorrect
Who knows, tell me what I did wrong there

Code: Select all

''  ---------------------------------------------------------------------------
''
''  @file       TwSimpleGLUT.c
''  @brief      A simple example that uses AntTweakBar with OpenGL and GLUT.
''
''              AntTweakBar: http:'anttweakbar.sourceforge.net/doc
''              OpenGL:      http:'www.opengl.org
''              GLUT:        http:'opengl.org/resources/libraries/glut
''
''  @author     Philippe Decaudin
''  @date       2006/05/20
''
''  ---------------------------------------------------------------------------


#Include Once "GL/glu.bi"
#Include Once "GL/glut.bi"
#Include Once "AntTweakBar.bi"
#Include Once "crt.bi"

#If Defined(__FB_WIN32__)
	#Include "windows.bi"
	#Define USE_MINI_GLUT
#EndIf


' This example displays one of the following shapes
Enum Shape
	SHAPE_TEAPOT=1
	SHAPE_TORUS
	SHAPE_CONE
End Enum

#define NUM_SHAPES 3


Type float As Single


Dim Shared As Shape g_CurrentShape = SHAPE_TORUS
' Shapes scale
Dim Shared As float g_Zoom = 1.0f
' Shape orientation (stored as a quaternion)
Dim Shared As float g_Rotation(...) = { 0.0f, 0.0f, 0.0f, 1.0f }
' Auto rotate
Dim Shared As Integer g_AutoRotate
Dim Shared As Integer g_RotateTime
Dim Shared As float g_RotateStart(...) = { 0.0f, 0.0f, 0.0f, 1.0f }
' Shapes material
Dim Shared As float g_MatAmbient(...) = { 0.5f, 0.0f, 0.0f, 1.0f }
Dim Shared As float g_MatDiffuse(...) = { 1.0f, 1.0f, 0.0f, 1.0f }
' Light parameter
Dim Shared As float g_LightMultiplier = 1.0f
Dim Shared As float g_LightDirection(...) = { -0.57735f, -0.57735f, -0.57735f }


' Routine to set a quaternion from a rotation axis and angle
' ( input axis = float[3] angle = float  output: quat = float[4] )
Sub SetQuaternionFromAxisAngle(axis As float Ptr ,angle As float , quat As float Ptr)
	Dim As float sina2, norm
	sina2 = Sin(0.5f * angle)
	norm = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2])
	quat[0] = sina2 * axis[0] / norm
	quat[1] = sina2 * axis[1] / norm
	quat[2] = sina2 * axis[2] / norm
	quat[3] = Cos(0.5f * angle)
End Sub


' Routine to convert a quaternion to a 4x4 matrix
' ( input: quat = float[4]  output: mat = float[4*4] )
Sub ConvertQuaternionToMatrix(quat As float Ptr , mat As float Ptr )
	Dim As float yy2 = 2.0f * quat[1] * quat[1]
	Dim As float xy2 = 2.0f * quat[0] * quat[1]
	Dim As float xz2 = 2.0f * quat[0] * quat[2]
	Dim As float yz2 = 2.0f * quat[1] * quat[2]
	Dim As float zz2 = 2.0f * quat[2] * quat[2]
	Dim As float wz2 = 2.0f * quat[3] * quat[2]
	Dim As float wy2 = 2.0f * quat[3] * quat[1]
	Dim As float wx2 = 2.0f * quat[3] * quat[0]
	Dim As float xx2 = 2.0f * quat[0] * quat[0]
	mat[0*4+0] = - yy2 - zz2 + 1.0f
	mat[0*4+1] = xy2 + wz2
	mat[0*4+2] = xz2 - wy2
	mat[0*4+3] = 0
	mat[1*4+0] = xy2 - wz2
	mat[1*4+1] = - xx2 - zz2 + 1.0f
	mat[1*4+2] = yz2 + wx2
	mat[1*4+3] = 0
	mat[2*4+0] = xz2 + wy2
	mat[2*4+1] = yz2 - wx2
	mat[2*4+2] = - xx2 - yy2 + 1.0f
	mat[2*4+3] = 0
	
	mat[3*4+0] = 0: mat[3*4+1] =0: mat[3*4+2] = 0
	mat[3*4+3] = 1
End Sub


' Routine to multiply 2 quaternions (ie, compose rotations)
' ( input q1 = float[4] q2 = float[4]  output: qout = float[4] )
Sub MultiplyQuaternions(q1 As float Ptr , q2 As float Ptr , qout As float Ptr )
	Dim As float qr(4)
	qr(0) = q1[3]*q2[0] + q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1]
	qr(1) = q1[3]*q2[1] + q1[1]*q2[3] + q1[2]*q2[0] - q1[0]*q2[2]
	qr(2) = q1[3]*q2[2] + q1[2]*q2[3] + q1[0]*q2[1] - q1[1]*q2[0]
	qr(3)  = q1[3]*q2[3] - (q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2])
	qout[0] = qr(0): qout[1] = qr(1): qout[2] = qr(2): qout[3] = qr(3)
End Sub


' Return elapsed time in milliseconds
Function GetTimeMs() As Integer
	#If Not Defined(__FB_WIN32__)
		Return glutGet(GLUT_ELAPSED_TIME)
	#Else
		' glutGet(GLUT_ELAPSED_TIME) seems buggy on Windows
		Return GetTickCount()
	#EndIf
End Function





' Callback function called by GLUT to render screen
Sub Display Cdecl()

	Dim As float v(4) ' will be used to set light parameters
	Dim As float mat(4*4) ' rotation matrix

	' Clear frame buffer
	glClearColor(0, 0, 0, 1)
	glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)

	glEnable(GL_DEPTH_TEST)
	glDisable(GL_CULL_FACE)
	glEnable(GL_NORMALIZE)

	' Set light
	glEnable(GL_LIGHTING)
	glEnable(GL_LIGHT0)
	v(2) = g_LightMultiplier*0.4f
	v(1) = v(2) : v(0) = v(2): v(3) = 1.0f
	glLightfv(GL_LIGHT0, GL_AMBIENT, @v(0))
	v(2) = g_LightMultiplier*0.8f
	v(0) = v(2): v(1) = v(2):v(3) = 1.0f
	glLightfv(GL_LIGHT0, GL_DIFFUSE, @v(0))
	v(0) = -g_LightDirection(0): v(1) = -g_LightDirection(1)
	v(2) = -g_LightDirection(2): v(3) = 0.0f
	glLightfv(GL_LIGHT0, GL_POSITION, @v(0))

	' Set material
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, @g_MatAmbient(0))
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, @g_MatDiffuse(0))

	' Rotate and draw shape
	glPushMatrix()
	glTranslatef(0.5f, -0.3f, 0.0f)
	If g_AutoRotate   Then
		Dim As float axis(3) = { 0, 1, 0 }
		Dim As float angle = (GetTimeMs()-g_RotateTime)/1000.0f
		Dim As float quat(4)
		SetQuaternionFromAxisAngle(@axis(0), angle, @quat(0))
		MultiplyQuaternions(@g_RotateStart(0), @quat(0), @g_Rotation(0))
	EndIf
	ConvertQuaternionToMatrix(@g_Rotation(0), @mat(0))
	glMultMatrixf(@mat(0))
	glScalef(g_Zoom, g_Zoom, g_Zoom)
	glCallList(g_CurrentShape)
	glPopMatrix()

	' Draw tweak bars
	TwDraw()

	' Present frame buffer
	glutSwapBuffers()

	' Recall Display at next frame
	glutPostRedisplay()
End Sub


' Callback function called by GLUT when window size changes
Sub Reshape Cdecl(width_ As Integer,height As Integer)

	' Set OpenGL viewport and camera
	glViewport(0, 0, width_, height)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(40, width_/height, 1, 10)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	gluLookAt(0,0,5, 0,0,0, 0,1,0)
	glTranslatef(0, 0.6f, -1)

	' Send the new window size to AntTweakBar
	TwWindowSize(width_, height)
End Sub


' Function called at exit
Sub Terminate Cdecl()
	glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES)

	TwTerminate()
End Sub


'  Callback function called when the 'AutoRotate' variable value of the tweak bar has changed
Sub SetAutoRotateCB TW_CALL(value As Any Ptr, clientData As Any Ptr)

	'(void)clientData; ' unused
	g_AutoRotate = *Cast(Integer Ptr,value) ' copy value to g_AutoRotate
	If( g_AutoRotate<>0 ) Then
		' init rotation
		g_RotateTime = GetTimeMs()
		g_RotateStart(0) = g_Rotation(0)
		g_RotateStart(1) = g_Rotation(1)
		g_RotateStart(2) = g_Rotation(2)
		g_RotateStart(3) = g_Rotation(3)

		' make Rotation variable read-only
		TwDefine(" TweakBar/ObjRotation readonly ")
	Else
		' make Rotation variable read-write
		TwDefine(" TweakBar/ObjRotation readwrite ")
	EndIf
End Sub


'  Callback function called by the tweak bar to get the 'AutoRotate' value
Sub GetAutoRotateCB TW_CALL(value As Any Ptr, clientData As Any Ptr)
	'(void)clientData; ' unused
	*Cast(Integer Ptr,value) = g_AutoRotate ' copy g_AutoRotate to value
End Sub



Dim As TwBar Ptr bar ' Pointer to the tweak bar
Dim As float axis(...) = { 0.7f, 0.7f, 0.0f } ' initial model rotation
Dim As float angle = 0.8f

' Initialize GLUT
glutInit(1, StrPtr( " " ))
glutInitDisplayMode(GLUT_DOUBLE + GLUT_RGB + GLUT_DEPTH)
glutInitWindowSize(640, 480)
glutCreateWindow("AntTweakBar simple example using GLUT")
glutCreateMenu(NULL)

' Set GLUT callbacks
glutDisplayFunc(@Display)
glutReshapeFunc(@Reshape)
atexit(@Terminate)  ' Called after glutMainLoop ends

' Initialize AntTweakBar
TwInit(TW_OPENGL, NULL)

' Set GLUT event callbacks
' - Directly redirect GLUT mouse button events to AntTweakBar
glutMouseFunc(@TwEventMouseButtonGLUT)
'' - Directly redirect GLUT mouse motion events to AntTweakBar
glutMotionFunc(@TwEventMouseMotionGLUT)
'' - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion)
glutPassiveMotionFunc(@TwEventMouseMotionGLUT)
'' - Directly redirect GLUT key events to AntTweakBar
glutKeyboardFunc(@TwEventKeyboardGLUT)
'' - Directly redirect GLUT special key events to AntTweakBar
glutSpecialFunc(@TwEventSpecialGLUT)
'' - Send 'glutGetModifers' function pointer to AntTweakBar;
''   required because the GLUT key event functions do not report key modifiers states.
TwGLUTModifiersFunc(@glutGetModifiers)

' Create some 3D objects (stored in display lists)
glNewList(SHAPE_TEAPOT, GL_COMPILE)
glutSolidTeapot(1.0F)
glEndList()
glNewList(SHAPE_TORUS, GL_COMPILE)
glutSolidTorus(0.3F, 1.0F, 16F, 32F)
glEndList()
glNewList(SHAPE_CONE, GL_COMPILE)
glutSolidCone(1.0F, 1.5F, 64F, 4F)
glEndList()

' Create a tweak bar
bar = TwNewBar(!"TweakBar")
TwDefine(!" GLOBAL help='This example shows how to integrate AntTweakBar with GLUT and OpenGL.' ") ' Message added to the help bar.
TwDefine(!" TweakBar size='200 400' color='96 216 224' ") ' change default tweak bar size and color

' Add 'g_Zoom' to 'bar': this is a modifable (RW) variable of type TW_TYPE_FLOAT. Its key shortcuts are [z] and [Z].
TwAddVarRW(bar, !"Zoom", TW_TYPE_FLOAT, @g_Zoom, _
!" min=0.01 max=2.5 step=0.01 keyIncr=z keyDecr=Z help='Scale the object (1=original size).' ")

' Add 'g_Rotation' to 'bar': this is a variable of type TW_TYPE_QUAT4F which defines the object's orientation
TwAddVarRW(bar, "ObjRotation", TW_TYPE_QUAT4F, @g_Rotation(0), _
!" label='Object rotation' opened=true help='Change the object orientation.' ")

' Add callback to toggle auto-rotate mode (callback functions are defined above).
TwAddVarCB(bar, "AutoRotate", TW_TYPE_BOOL32, Cast(TwSetVarCallback,@SetAutoRotateCB), @GetAutoRotateCB, NULL, _
!" label='Auto-rotate' key=space help='Toggle auto-rotate mode.' ")

' Add 'g_LightMultiplier' to 'bar': this is a variable of type TW_TYPE_FLOAT. Its key shortcuts are [+] and [-].
TwAddVarRW(bar, "Multiplier", TW_TYPE_FLOAT, @g_LightMultiplier, _
!" label='Light booster' min=0.1 max=4 step=0.02 keyIncr='+' keyDecr='-' help='Increase/decrease the light power.' ")

' Add 'g_LightDirection' to 'bar': this is a variable of type TW_TYPE_DIR3F which defines the light direction
TwAddVarRW(bar, "LightDir", TW_TYPE_DIR3F, @g_LightDirection(0), _
!" label='Light direction' opened=true help='Change the light direction.' ")

' Add 'g_MatAmbient' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored)
' and is inserted into a group named 'Material'.
TwAddVarRW(bar, "Ambient", TW_TYPE_COLOR3F, @g_MatAmbient(0), !" group='Material' ")

' Add 'g_MatDiffuse' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored)
' and is inserted into group 'Material'.
TwAddVarRW(bar, "Diffuse", TW_TYPE_COLOR3F, @g_MatDiffuse(0), !" group='Material' ")

' Add the enum variable 'g_CurrentShape' to 'bar'
' (before adding an enum variable, its enum type must be declared to AntTweakBar as follow)
Scope
	' ShapeEV associates Shape enum values with labels that will be displayed instead of enum values
	Dim As TwEnumVal shapeEV(NUM_SHAPES) = { (SHAPE_TEAPOT, @"Teapot"), (SHAPE_TORUS, @"Torus"), (SHAPE_CONE, @"Cone") }
	' Create a type for the enum shapeEV
	Dim As TwType shapeType = TwDefineEnum("ShapeType", @shapeEV(0), NUM_SHAPES)
	' add 'g_CurrentShape' to 'bar': this is a variable of type ShapeType. Its key shortcuts are [<] and [>].
	TwAddVarRW(bar, "Shape", shapeType, @g_CurrentShape, !" keyIncr='<' keyDecr='>' help='Change object shape.' ")
End Scope

' Store time
g_RotateTime = GetTimeMs()
' Init rotation
SetQuaternionFromAxisAngle(@axis(0), angle, @g_Rotation(0))
SetQuaternionFromAxisAngle(@axis(0), angle, @g_RotateStart(0))

' Call the GLUT main loop
glutMainLoop()
Last edited by VANYA on Jun 19, 2013 14:31, edited 1 time in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by counting_pine »

(Note: I'm still seeing references to http:' in the comments...)
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by D.J.Peters »

Hello VANYA
remove your posted header please my is up to date now.
(it's better to have only one include file in this thread)

Joshy
Last edited by D.J.Peters on Oct 01, 2022 6:00, edited 1 time in total.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by VANYA »

I deleted the header of the post, as you asked.

The example works better than me, but still if you close the window, the process is not closed. And if you click on the closure of the console, then program crashed.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by D.J.Peters »

VANYA
you have to use FreeGLUT
GLUT is out of date 2001,2003
there is no glutLeaveMainLoop()
FreeBASIC can't handle the C function atexit
May be it's own module destructor is never called. (I don't know)

Test it self FreeBASIC\examples\graphics\OpenGL\gl_test.bas

Joshy

By the way i wrote all twBar and twVar properties in the include file:

Code: Select all

' TwHelpBar:
'-----------
' visible=false / true eg. TwDefine(" TW_HELP visible=false ")

' TwBar parameters syntax:
'---------------------------
' label=string
' help=string
' color=‘red green blue‘
' alpha=a
' text=dark
' text=light
' position=‘x y‘
' size=‘sx sy‘
' valueswidth=w or = fit
' refresh=float in seconds
' visible=true / false
' iconified=true / false
' iconpos= bottomleft | bl / bottomright | br  / topleft | tl  / topright | tr
' iconalign=vertical / horizontal
' iconmargin=’x y’
' iconifiable=true / false
' movable=true / false
' resizable=true / false
' fontsize=value (value is 1 for small font, 2 for medium font, or 3 for large font.)
' fontstyle=default / fixed
' fontresizable=true / false
' fontscaling=s
' alwaystop=true / false
' alwaysbottom=true / false
' contained=true / false
' overlap=true / false
' buttonalign=left / center / right
' 

' Variable parameters syntax:
'-----------------------------
' label=string
' help=string
' group=groupname
' visible=true / false
' readonly=true / false
'
' for numeric values int/float
' min=value
' max=value
' step=value
'
' float only
' precision=value
'
' integer only
' hexa=true / false
'
' key=keyshortcut
' keyincr=keyshortcut
' keydecr=keyshortcut
'
' boolean only
' true=string
' false=string
'
' for groups only
' opened=true / false
'
' For enum's variables only.
' enum=’const1 {label1} , const2 {label2} , ...’
'
' showval=true / false
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by VANYA »

Joshy!

Which version are you using? I downloaded the source code Freeglut 2.8.1, but the compile fails.
VC 6 - does not load the project.
visual studio express 10 - also a problem.

I downloaded two files ready dll file from here: http://www.transmissionzero.co.uk/softw ... lut-devel/

1) normally shuts down, but distorts the picture by rotating
2) normally rotates, but the error at the end of

If you do not throw off difficult me your version of the dll
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: AntTweakBar tricky GUI for OpenGL and D3D.

Post by angros47 »

I compiled AntTweakBar in Linux, and tried the example.

When I compiled the BAS file (using the .bi file provided by D.J. Peters), I got a lot of errors:

Code: Select all

demo.o: In function `main':
(.text+0xa3): undefined reference to `_TwInit'
demo.o: In function `main':
(.text+0x10b): undefined reference to `_TwWindowSize'
demo.o: In function `main':
(.text+0x118): undefined reference to `_TwNewBar'
demo.o: In function `main':
(.text+0x128): undefined reference to `_TwDefine'
demo.o: In function `main':
(.text+0x19d): undefined reference to `_TwAddVarRW'
demo.o: In function `main':
(.text+0x1bf): undefined reference to `_TwAddVarRO'
demo.o: In function `main':
(.text+0x1ec): undefined reference to `_TwAddVarRO'
demo.o: In function `main':
(.text+0x20e): undefined reference to `_TwAddVarRO'
demo.o: In function `main':
(.text+0x22c): undefined reference to `_TwAddButton'
demo.o: In function `main':
(.text+0x5e2): undefined reference to `_TwKeyPressed'
demo.o: In function `main':
(.text+0x5fb): undefined reference to `_TwMouseMotion'
demo.o: In function `main':
(.text+0x69a): undefined reference to `_TwMouseButton'
demo.o: In function `main':
(.text+0x6ad): undefined reference to `_TwMouseWheel'
demo.o: In function `main':
(.text+0x6ff): undefined reference to `_TwRefreshBar'
demo.o: In function `main':
(.text+0x73a): undefined reference to `_TwDraw'
demo.o: In function `main':
(.text+0x75a): undefined reference to `_TwTerminate'
To get it working, I had to remove the underscore in all aliases; so the line:

Code: Select all

    declare function TwNewBar                   alias "_TwNewBar" (barName as const zstring ptr) as TwBar ptr
must become;

Code: Select all

    declare function TwNewBar                   alias "TwNewBar" (barName as const zstring ptr) as TwBar ptr
and so on. With this change, it can be used in linux. Anybody else tried it?
Post Reply