Debugged exe with freeglut issue

Everything related to 3D programming
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Debugged exe with freeglut issue

Post by Samuel »

I've been fiddling with freeglut for a couple opengl windows and everything seems to run fine except when I close out of the freeglut window.
I end up with a "The debugged executable quit unexpectedly" message.

I think this might be related to how glutMainLoop() works, but I'm not sure if I need to resolve this or if it's OK to overlook.
I'm hoping someone with freeglut/glut experience can point me in the right direction.

This download contains all the necessary files. Also this won't run on the x64 compiler because this is the x86 version of freeglut.
Freeglut Window Test.zip


For those interested here's the Window.pb file from the download above, but it won't run without the includes and lib file from it.

Code: Select all

IncludeFile "freeglut_ext.pb"
IncludeFile "freeglut_std.pb"

Import "freeglut.lib"
  
  glutCreateWindow(Title.s)
  glutDisplayFunc(*Proc)
  glutGetWindow()
  glutInit(argc.i, argv.b)
  glutInitDisplayMode(Flags.i)
  glutInitWindowSize(Width.i, Height.i)
  glutInitWindowPosition(StartX, StartY.i)
  glutMainLoop()
  glutMainLoopEvent()
  glutSetOption(Action.i, Option.i)
  glutSwapBuffers()

EndImport


Declare RenderLoop()


Define.i argc = 0

Dim argv.s(0)
argv(0) = ""

glutInit(@argc, @argv)
glutInitDisplayMode(#GLUT_DOUBLE | #GLUT_RGBA)
glutInitWindowSize(800, 600)
glutInitWindowPosition(200, 200)
glutCreateWindow("Freeglut Window")

glutDisplayFunc(@RenderLoop())

glClearColor_(1.0, 0.0, 0.0, 1.0)

glutMainLoop()

End

Procedure RenderLoop()
  
  Debug "At Render"
  
  glClear_(#GL_COLOR_BUFFER_BIT)
  
  glutSwapBuffers()

EndProcedure
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Debugged exe with freeglut issue

Post by Danilo »

glutMainLoop() does never return by default, and directly exits the application.

This means PB's Debugger does not shutdown correctly, and also PB's libraries are not correctly closed/shutdown at end.
To prevent this, you can use:

Code: Select all

glutSetOption(#GLUT_ACTION_ON_WINDOW_CLOSE,#GLUT_ACTION_GLUTMAINLOOP_RETURNS)
right before calling glutMainLoop().

Code: Select all

IncludeFile "freeglut_ext.pb"
IncludeFile "freeglut_std.pb"

Import "freeglut.lib"
  
  glutCreateWindow(Title.p-ascii)           ; changed for UNICODE mode
  glutDisplayFunc(*Proc)                    
  glutIdleFunc(*func)
  glutGetWindow()                           
  glutInit(argc.i, argv.b)                  
  glutInitDisplayMode(Flags.i)              
  glutInitWindowSize(Width.i, Height.i)     
  glutInitWindowPosition(StartX, StartY.i)  
  glutMainLoop()                            
  glutMainLoopEvent()                       
  glutSetOption(Action.i, Option.i)         
  glutSwapBuffers()                         
  glutSetWindowTitle(title.p-ascii)         
EndImport


Declare RenderLoop()
Declare IdleLoop()

Define.i argc = 0

Dim argv.s(0)
argv(0) = ""

glutInit(@argc, @argv)
glutInitDisplayMode(#GLUT_DOUBLE | #GLUT_RGBA)
glutInitWindowSize(800, 600)
glutInitWindowPosition(200, 200)
glutCreateWindow("Freeglut Window")

glutDisplayFunc(@RenderLoop())
glutIdleFunc(@IdleLoop())

glClearColor_(1.0, 0.0, 0.0, 1.0)

glutSetOption(#GLUT_ACTION_ON_WINDOW_CLOSE,#GLUT_ACTION_GLUTMAINLOOP_RETURNS)

glutMainLoop()

End

Procedure RenderLoop()
  Debug "At Render"
  
  #GL_COLOR_BUFFER_BIT = 16384
  glClear_(#GL_COLOR_BUFFER_BIT)
  
  glutSwapBuffers()

EndProcedure

Procedure IdleLoop()
    ;Debug "Idle"
EndProcedure
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Debugged exe with freeglut issue

Post by applePi »

why i can't make the shapes to rotate ? even the yrot are global, i miss something.
when we call glutWireTeapot_(0.5) it seems to call the glut32 and it works, but when we call glutWireTeapot(0.5) there is a polink error.
the glutWireRhombicDodecahedron() ; are from freeglut only and it is displayed okay
also glutWireCylinder is from the freeglut only and it is okay, but the glutWireSierpinskiSponge from freeglut only but it gives error
and thanks for Samuel and Danilo for the posts.

Code: Select all

IncludeFile "freeglut_ext.pb"
IncludeFile "freeglut_std.pb"

Import "freeglut.lib"
  
  glutCreateWindow(Title.p-ascii)           ; changed for UNICODE mode
  glutDisplayFunc(*Proc)                    
  glutIdleFunc(*func)
  glutGetWindow()                           
  glutInit(argc.i, argv.b)                  
  glutInitDisplayMode(Flags.i)              
  glutInitWindowSize(Width.i, Height.i)     
  glutInitWindowPosition(StartX, StartY.i)  
  glutMainLoop()                            
  glutMainLoopEvent()                       
  glutSetOption(Action.i, Option.i)         
  glutSwapBuffers()                         
  glutSetWindowTitle(title.p-ascii)  
  glutWireTeapot(lenght.f)
  glutWireDodecahedron()
  glutWireRhombicDodecahedron()
  glutWireSierpinskiSponge(a.i, b.d, c.d)
  glutWireCylinder(radius.d, height.d, slices.i,stacks.i);
EndImport

Global yrot.f
Declare RenderLoop()
Declare IdleLoop()

Define.i argc = 0

Dim argv.s(0)
argv(0) = ""

glutInit(@argc, @argv)
glutInitDisplayMode(#GLUT_DOUBLE | #GLUT_RGBA)
glutInitWindowSize(800, 600)
glutInitWindowPosition(0, 0)
glutCreateWindow("Freeglut Window")

glutDisplayFunc(@RenderLoop())
glutIdleFunc(@IdleLoop())

glClearColor_(0.9, 0.9, 0.9, 1.0)

glutSetOption(#GLUT_ACTION_ON_WINDOW_CLOSE,#GLUT_ACTION_GLUTMAINLOOP_RETURNS)

glutMainLoop()

End

Procedure RenderLoop()
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) ;Clear The Screen And The Depth Buffer
  glLoadIdentity_() 
  
  #GL_COLOR_BUFFER_BIT = 16384
  glClear_(#GL_COLOR_BUFFER_BIT)
  
  glScalef_(1,1,1)
  ;gluLookAt_( 0, 0, 0,
   ;        0,  0, 0,
    ;        0, 1,  0 )
  ;glDisable_(#GL_LIGHTING)
 
 glClearColor_(0, 0, 0, 0)
  yrot.f+1
  glRotatef_(yrot,0.0,1.0,0.0) ;Rotate On The Y Axis By yrot
  
  glPushMatrix_() ; Save the original Matrix coordinates
  glMatrixMode_(#GL_MODELVIEW)
  ;glTranslatef_(0.0, 0.0, 0.0);
  
  glColor3f_(0.0, 0.6, 0.0)  
  glutWireTeapot_(0.5)
  glColor3f_(1.0, 1.0, 0.0)
  ;glutWireTorus_(0.3,0.4,12,12)
  glScalef_(0.2,0.2,0.2)
  glutWireDodecahedron_() 
  glScalef_(1,1,1)
  ;glutWireRhombicDodecahedron() ; 
  ;glutWireSierpinskiSponge(int num_levels, const GLdouble offset[ 3 ], GLdouble scale);
  ;glutWireSierpinskiSponge(3, 4, 1)
  glutWireCylinder(2, 2, 8,5);
  glPopMatrix_()
  glFinish_()
  
  
  glutSwapBuffers()

EndProcedure

Procedure IdleLoop()
   
    ;Debug "Idle"
EndProcedure
this is why i prefer glut since its functions works natively even in openGLgadget (needs glut32.dll look the file links here http://purebasic.fr/english/viewtopic.p ... 23#p463756).

Code: Select all

OpenWindow(0, 0, 0, 800, 600, "Glut Shapes")
SetWindowColor(0, 0)
OpenGLGadget(0, 0, 0, WindowWidth(0) , WindowHeight(0), #PB_OpenGL_Keyboard )

SetActiveGadget(0) 

SetWindowColor(0, 0)

HideWindow(0, #False)
Global yrot.f ;Y Rotation
glClearColor_ (0.0, 0.0, 0.0, 0.0);
glShadeModel_ (#GL_SMOOTH)

glEnable_(#GL_LIGHTING);
glEnable_(#GL_LIGHT0);
glEnable_(#GL_DEPTH_TEST);


Procedure.l DrawGLScene()
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) ;Clear The Screen And The Depth Buffer
 glLoadIdentity_() ;Reset The View
 gluLookAt_( 0, 10, 1, ;great explanation for gluLookAt http://www.gamedev.net/topic/211831-can-some-one-post-a-example-of-glulookat/
                0,  0, -10,
                0, 1,  0 )
  glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
  glMatrixMode_(#GL_PROJECTION)
  glLoadIdentity_()
  gluPerspective_(30.0, Abs(WindowWidth(0) / WindowHeight(0)), 0.1, 500.0)
  glMatrixMode_(#GL_MODELVIEW)
  
   ; viewing transformation  
  glTranslatef_(0.0, 0.0, -10.0); 
       
  glPushMatrix_()                  ; Save the original Matrix coordinates
  ;glMatrixMode_(#GL_MODELVIEW)
  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glDisable_(#GL_LIGHTING)
  glRotatef_(angle, 0.0, 1.0, 0.0)

glScalef_(0.4,0.4,0.4)  
glBegin_(#GL_TRIANGLES) ; start drawing a triangle
  glColor3f_(1.0, 0.0, 0.0);
  glVertex3f_( 0.0, 1.0, 0.0) ; top
  glColor3f_(0.0, 1.0, 0.0);
  glVertex3f_(-1.0,-1.0, 0.0) ; bottom left
  glColor3f_(0.0, 0.0, 1.0);
  glVertex3f_( 1.0,-1.0, 0.0) ; bottom right
glEnd_() ; finished 
  
glPopMatrix_() 
 
 yrot+0.5
 glRotatef_(yrot,0.0,1.0,0.0) ;Rotate On The Y Axis By yrot
 glColor3f_(1.0, 0.0, 1.0)
 glutWireDodecahedron_()
 glColor3f_(0.0, 0.6, 0.0)
 glutWireIcosahedron_()
 
 glPushMatrix_() 
 glTranslatef_(2.0, 0.0, -2.0); 
 glutWireTeapot_(1)
 glPopMatrix_() 

 glPushMatrix_() 
 glColor3f_(0.6, 0.6, 0.0)
 glTranslatef_(-3.0, 0.0, 0.0);
 glutWireTorus_(0.3,0.8,16,16);
 glPopMatrix_() 
 
  
 ProcedureReturn #True ;Keep Going
 
EndProcedure


  Repeat 
    Event = WindowEvent()
    If Event = #PB_Event_Gadget And EventGadget() = 0 
      If EventType() = #PB_EventType_KeyDown
      
        key = GetGadgetAttribute(0,#PB_OpenGL_Key )
        Select key
          
          Case #PB_Shortcut_Up 
          ;some sode  
            
          Case #PB_Shortcut_Escape
            quit = 1
        EndSelect
           
      EndIf
   EndIf   
    
    
    DrawGLScene()
    SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
  
  Delay(2)
  
Until quit = 1 Or Event = #PB_Event_CloseWindow

End
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Debugged exe with freeglut issue

Post by applePi »

i have found the reason that it does not rotate in the first code
the glutIdleFunc(@RenderLoop()) should refer also to the RenderLoop procedure and not to other one.
i got the idea while looking at this code : http://www.perlmonks.org/?node_id=921909
now the glutWireTeapot_, glutWireDodecahedron, glutWireCylinder. rotate
save the file to the folder of Samuel files above

Code: Select all

Global yrot.f
Global tot
IncludeFile "freeglut_ext.pb"
IncludeFile "freeglut_std.pb"

Import "freeglut.lib"
  
  glutCreateWindow(Title.p-ascii)           ; changed for UNICODE mode
  glutDisplayFunc(*Proc)                    
  glutIdleFunc(*func)
  glutGetWindow()                           
  glutInit(argc.i, argv.b)                  
  glutInitDisplayMode(Flags.i)              
  glutInitWindowSize(Width.i, Height.i)     
  glutInitWindowPosition(StartX, StartY.i)  
  glutMainLoop()                            
  glutMainLoopEvent()                       
  glutSetOption(Action.i, Option.i)         
  glutSwapBuffers()                         
  glutSetWindowTitle(title.p-ascii)  
  glutWireTeapot(lenght.f)
  glutWireDodecahedron()
  glutWireRhombicDodecahedron()
  glutWireSierpinskiSponge(a.i, b.d, c.d)
  glutWireCylinder(radius.d, height.d, slices.i,stacks.i);
  glutReshapeFunc(*Proc)
  glutKeyboardFunc(*Proc)
  glutTimerFunc(refreshMills.f, *Proc, q.i)
  glutPostRedisplay()
    
EndImport

Procedure key()
   yrot.f + 0.1
    ;Debug yrot
 EndProcedure
 
Procedure RenderLoop()
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) ;Clear The Screen And The Depth Buffer
  glLoadIdentity_() 
  
  #GL_COLOR_BUFFER_BIT = 16384
  glClear_(#GL_COLOR_BUFFER_BIT)
  
  glScalef_(1,1,1)
  ;gluLookAt_( 0, 0, 0,
   ;        0,  0, 0,
    ;        0, 1,  0 )
  ;glDisable_(#GL_LIGHTING)
 
 glClearColor_(0, 0, 0, 0)
  yrot.f+1
  glRotatef_(yrot,0,1,0) ;Rotate On The Y Axis By yrot
  
  glPushMatrix_() ; Save the original Matrix coordinates
  glMatrixMode_(#GL_MODELVIEW)
  ;glTranslatef_(0.0, 0.0, 0.0);
  
  glColor3f_(0.0, 0.6, 0.0)  
  glutWireTeapot_(0.5)
  glColor3f_(1.0, 1.0, 0.0)
  ;glutWireTorus_(0.3,0.4,12,12)
  glScalef_(0.2,0.2,0.2)
  glutWireDodecahedron() 
  glScalef_(1,1,1)
  ;glutWireRhombicDodecahedron() ; 
  ;glutWireSierpinskiSponge(int num_levels, const GLdouble offset[ 3 ], GLdouble scale);
  ;glutWireSierpinskiSponge(3, 4, 1)
  glutWireCylinder(2, 2, 8,5);
  glPopMatrix_()
  glFinish_()
  
  
  glutSwapBuffers()
  ;tot+1
  ;Debug tot
EndProcedure

Procedure IdleLoop()
   
    ;Debug "Idle"
 EndProcedure
 

;Declare RenderLoop()
;Declare IdleLoop()
Global tot
Define.i argc = 0

Dim argv.s(0)
argv(0) = ""


glutInit(@argc, @argv)
glutInitDisplayMode(#GLUT_DOUBLE | #GLUT_RGBA)
glutInitWindowSize(800, 600)
glutInitWindowPosition(0, 0)
glutCreateWindow("Freeglut Window")

glutDisplayFunc_(@RenderLoop())
glutKeyboardFunc_(@key())
;glutReshapeFunc(@RenderLoop())
glutIdleFunc(@RenderLoop())
glutInitDisplayMode(#GLUT_DOUBLE)

glClearColor_(0.9, 0.9, 0.9, 1.0)

glutSetOption(#GLUT_ACTION_ON_WINDOW_CLOSE,#GLUT_ACTION_GLUTMAINLOOP_RETURNS)

;glutTimerFunc(15, @timer, 0)
glutMainLoop()
;glutMainLoopEvent()

End

  

 
   
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Debugged exe with freeglut issue

Post by Samuel »

Thank you, Danilo. Your solution and explanation are very much appreciated.

@applePi
I'm getting the same error when using freeglut's glutWireTeapot(). Don't now why it's happening, but luckily we still have the original gluts teapot.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Debugged exe with freeglut issue

Post by Danilo »

applePi wrote:[...]
but when we call glutWireTeapot(0.5) there is a polink error.
[...]
but the glutWireSierpinskiSponge from freeglut only but it gives error
Your imports had wrong parameters. Added some primitives and updated freeglut.dll (for Teacup & Teaspoon): Freeglut_Test_applePi.zip (x86 only, freeglut 3.0.0)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Debugged exe with freeglut issue

Post by applePi »

Literally "Incredible" demo with a source code, thank you Danilo , from the primitives i like mostly the Teaspoon and Teacup , i have not noticed or read about before.
regards
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Debugged exe with freeglut issue

Post by Danilo »

freeglut is very easy to use and powerful. Thanks to Samuel for pointing it out.

BTW, Windows x86 and x64 binary version can be found at Martin Payne's Windows binaries (MSVC and MinGW) (MSVC Package for PB)

Small effect:

Add

Code: Select all

#GLUT_GEOMETRY_VISUALIZE_NORMALS = $205
glutSetOption(#GLUT_GEOMETRY_VISUALIZE_NORMALS,#True)
before

Code: Select all

glutMainLoop()
Updated Freeglut_Test_applePi.zip
Post Reply