Re: Small cross-platform OpenGL demo
Posted: Sun Aug 12, 2012 8:17 pm
Because of difficulties in running my above cross-platform OpenGL example in the new PB 4.70 Beta 1 of the new cocoa-framework based Mac version I further simplified the example by rendering the OpenGL content into a WindowedScreen thus eliminating all the platform-specific code to obtain a rendering context of the window in order to let PureBasic do that work. Only in Windows this new simplified code requires the subsystem to be set to OpenGL in the IDE's menu (Compiler/Compiler Options.../Library Subsystem: OpenGL). This new example runs on Macs with PB 4.61 and PB 4.70 Beta 1.5 in 32 bit and 64 bit mode (cocoa framework) and 32 bit (Subsystem: Carbon) and should of course run in all listed OSes from my first post.
Update: I had to addso that this example also works in OpenSuSE 12.1. Thank you for your hint, Fred.
Code: Select all
EnableExplicit
#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
CompilerIf Subsystem("OpenGL") = #False
MessageRequester("Error", "Please set the subsystem to OpenGL")
End
CompilerEndIf
CompilerEndIf
If InitSprite() = 0
MessageRequester("Error", "Can't open screen & sprite environment!")
End
EndIf
OpenWindow(0, 270, 100, 640, 480, "Cross-platform OpenGL demo", #PB_Window_SystemMenu | #PB_Window_Invisible)
SetWindowColor(0, 0)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
ImportC "-lGL"
glBegin_(PrimitiveID.I) As "glBegin"
glClear_(BuffersToClear.I) As "glClear"
glColor3f_(Red.F, Green.F, Blue.F) As "glColor3f"
glEnd_() As "glEnd"
glLoadIdentity_() As "glLoadIdentity"
glMatrixMode_(Target.I) As "glMatrixMode"
glTranslatef_(x.F, y.F, z.F) As "glTranslatef"
glVertex3f_(x.F, y.F, z.F) As "glVertex3f"
glViewport_(x.I, y.I, Width.I, Height.I) As "glViewport"
glXChooseVisual(*Display, ScreenNumber.I, *AttributeArray)
glXCreateContext(*Display, *XVisualInfo, GLXContext.I, DirectGraphicsConnection.I)
glXDestroyContext(*Display, GLXContext.I)
glXMakeCurrent(*Display, GLXDrawable.I, GLXContext.I)
glXSwapBuffers(*Display, GLXDrawable.I)
EndImport
ImportC "-lGLU"
gluPerspective_(FieldOfViewAngle.D, AspectRatioWidthToHeight.D, DistanceFromViewerToNearPlane.D, DistanceFromViewerToFarPlane.D) As "gluPerspective"
EndImport
ImportC "-lX11" : EndImport
CompilerCase #PB_OS_MacOS
ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
glBegin_(PrimitiveID.I) As "_glBegin"
glClear_(BuffersToClear.I) As "_glClear"
glColor3f_(Red.F, Green.F, Blue.F) As "_glColor3f"
glEnd_() As "_glEnd"
glLoadIdentity_() As "_glLoadIdentity"
glMatrixMode_(Target.I) As "_glMatrixMode"
glTranslatef_(x.F, y.F, z.F) As "_glTranslatef"
gluPerspective_(FieldOfViewAngle.D, WidthToHeightRatio.D, DistanceToNearPlane.D, DistanceToFarPlane.D) As "_gluPerspective"
glVertex3f_(x.F, y.F, z.F) As "_glVertex3f"
glViewport_(x.I, y.I, Width.I, Height.I) As "_glViewport"
EndImport
CompilerEndSelect
HideWindow(0, #False)
Repeat
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)
glLoadIdentity_()
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glLoadIdentity_()
glTranslatef_(0.0, 0.0, -8.0)
glColor3f_(1.0, 1.0, 1.0)
glBegin_(#GL_TRIANGLES) ; Start drawing a triangle
glColor3f_ ( 1.0, 0.0, 0.0) ; Set top point of triangle to Red
glVertex3f_( 0.0, 1.0, 0.0) ; First point of the triangle
glColor3f_ ( 0.0, 1.0, 0.0) ; Set left point of triangle to Green
glVertex3f_(-1.0, -1.0, 0.0) ; Second point of the triangle
glColor3f_ ( 0.0, 0.0, 1.0) ; Set right point of triangle to Blue
glVertex3f_( 1.0, -1.0, 0.0) ; Third point of the triangle
glEnd_() ; Done drawing the triangle
; Swap buffers (double buffering)
FlipBuffers()
Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Select all
ImportC "-lX11" : EndImport