OpenGL Demo in PureBasic

Advanced game related topics
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mark1Up.

I have converted a simple OpenGL C demo to PureBasic and uploaded it to the Resources site (hopefully it will be available soon). It includes the complete OpenGL header files (gl.h & glu.h) translated for PureBasic use (now called gl.pbh & glu.pbh) for easier OpenGL experimentation.

Note: This code would not run under PB 2.7 but works fine now with 2.8. I believe the Floating Point and Procedure improvements did the trick. Thanks Fred!

I did not write the original C code and I'm just a beginner in OpenGL so I won't be able to help with many questions, but the original well-commented C tutorial can be found at:

http://www.geocities.com/SiliconValley/ ... ngl32.html

I would be interested to see anything that other people do in OpenGL with PB.

Have fun,

Mark
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Argh, you beat me . I've also converted a spinning cube to openGL (to test the floats..) and I will update it to the site..

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Mark1Up this is a excellent job!!!

I started some times ago with OpenGL but I stocked with the port of C stuff (I'm not a C coder ) and PureBasic's floating point problem.

Your code runs excellent.

The following is the THREAD version of your code!
Now the object is rotating EVEN if you MOVE THE WINDOW!!!!

Code: Select all

; OpenGL Demo in PureBasic ([url]http://www.purebasic.com[/url])
;
; Converted from C example code ([url]http://www.geocities.com/SiliconValley/Code/1219/opengl32.html[/url])
;
; Goto C example For OpenGL comments.
;
; Include files are converted C headers for OpenGL
;
; This is the THREAD Version of Mark1Up's OpenGL example
; now the object is rotating even if you move the window!
;
; Great job Mark1Up. Franco :)
;
IncludeFile ".\pfd.pbh"
IncludeFile ".\gl.pbh"
IncludeFile ".\glu.pbh"

InitSprite() 
InitKeyboard() 

Structure WindowClass
  wcstyle.b
EndStructure

wc.WindowClass

wc\wcstyle = #CS_OWNDC

RegisterClass_(wc)

Procedure Go(hDC)
  Structure PixelFormatDescriptor
    nSize.w
    nVersion.w
    dwFlags.l 
    iPixelType.b 
    cColorBits.b 
    cDepthBits.b 
    iLayerType.b 
  EndStructure

  pfd.PixelFormatDescriptor

  pfd\nSize = SizeOf(PixelFormatDescriptor)
  pfd\nVersion = 1
  pfd\dwFlags = #PFD_DRAW_TO_WINDOW | #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER
  pfd\iPixelType = #PFD_TYPE_RGBA
  pfd\cColorBits = 24
  pfd\cDepthBits = 16
  pfd\iLayerType = #PFD_MAIN_PLANE

  iFormat = ChoosePixelFormat_ (hDC, pfd)
  SetPixelFormat_ (hDC, iFormat, pfd)

  hRC.l = wglCreateContext_(hDC)
  wglMakeCurrent_(hDC,hRC)

  Theta.f = 0

  WinEvt= WindowEvent() 
  While WinEvt  #PB_EventCloseWindow 
    WinEvt= WindowEvent()

    glClearColor_( 0.0, 0.0, 0.0, 0.0 )
    glClear_( #GL_COLOR_BUFFER_BIT )
   
    glPushMatrix_()
    glRotatef_( Theta, 0.0, 0.0, 1.0 )
    glBegin_( #GL_TRIANGLES )
    glColor3f_( 1.0, 0.0, 0.0 ) 
    glVertex2f_( 0.0, 1.0 );
    glColor3f_( 0.0, 1.0, 0.0 )
    glVertex2f_( 0.87, -0.5 )
    glColor3f_( 0.0, 0.0, 1.0 )
    glVertex2f_( -0.87, -0.5 )
    glEnd_()
    glPopMatrix_()
   
    SwapBuffers_( hDC )
   
    Theta = Theta + 1.0 
  Wend
EndProcedure

hWnd = OpenWindow(1, 20, 20, 600, 400, #PB_Window_SystemMenu, "OpenGL Demo") 
hDC.l = GetDC_(hWnd)

CreateThread(@Go(),hDC)

WinEvt= WindowEvent()
While WinEvt  #PB_EventCloseWindow 
  WinEvt= WindowEvent()
  Delay(10)
Wend

wglMakeCurrent_(#NULL,#NULL)
wglDeleteContext_(hRC)
ReleaseDC_(hWnd, hDC)
CloseWindow(1) 

End

 
Fred, thanks for the real floating point support with v2.8.
I hope we all will see more OpenGL demos soon...



Have a nice day...
Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by michaelj.

As per my thread in the Bugs section [v2.80 delay crash], the PixelFormatDescriptor structure is very wrong, and can cause corrupt data. It should be as follows:

Code: Select all

Structure PixelFormatDescriptor
  nSize.w
  nVersion.w
  dwFlags.l 
  iPixelType.b 
  cColorBits.b 
  cRedBits.b
  cRedShift.b
  cGreenBits.b
  cGreenShift.b
  cBlueBits.b
  cBlueShift.b
  cAlphaBits.b
  cAlphaShift.b
  cAccumBits.b
  cAccumRedBits.b
  cAccumGreenBits.b
  cAccumBlueBits.b
  cAccumAlpgaBits.b
  cDepthBits.b
  cStencilBits.b
  cAuxBuffers.b
  iLayerType.b
  bReserved.b
  dwLayerMask.l
  dwVisibleMask.l
  dwDamageMask.l
EndStructure
I don't believe that any of the other flags need to be set. They just need to be in the structure.

Michael.

(Registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by michaelj.

And it also occurred to me, as I woke up this morning, that the WindowClass (wndClass) structure is also far too small, and could likely cause problems.

I realise you were porting from existing code, which may well have the problem already.

[edit]

In fact, I'm now wondering if any of the wndClass stuff will affect the window created using OpenWindow(...). You can remove the code, and it all still works fine, and I can't see how the OpenWindow command could know to use the new class (it's usually specified within the win API).

HTH,

Michael.

(Registered PureBasic user)

Edited by - michaelj on 14 January 2002 10:05:29
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mark1Up.

Fred,

Did you ever finish your OpenGL Spinning Cube demo? I have recently started playing with the code I had converted, but am having trouble with the PixelFormatDescriptor structure, as Michaelj pointed out it did not have all the elements defined properly (but it ran). I have tried adding all the standard elements and it causes problems with the application (OpenGL does not seem to initialize properly and Windows reports 'Application Not Responding'). This seems to happen as I get past about 15 or 16 elements in the Structure definition (doesn't seem to matter which elements, just having that many). Is there a limit to the number of elements (or size) of a Structure in PureBasic?

Anyways if you could share your Spinning Cube demo, maybe that would shine some light on my problems and also give me some insight on defining OpenGL verticies in PB arrays.

Thanks,

Mark
Post Reply