David Nikdel & NeHe's Bezier Tutorial (Lesson 28)

Share your advanced PureBasic knowledge/code with the community.
hagibaba
Enthusiast
Enthusiast
Posts: 170
Joined: Fri Mar 05, 2004 2:55 am
Location: UK
Contact:

David Nikdel & NeHe's Bezier Tutorial (Lesson 28)

Post by hagibaba »

Code updated for 5.20+

This shows how to create bezier patches. It also has a fullscreen fix for some cards.
Press Left/Right Arrows to rotate object, Up/Down Arrows to set resolution, Spacebar to hide control lines.
You can get the "NeHe.bmp" from the "lesson28.zip" here:
http://nehe.gamedev.net/data/lessons/vc/lesson28.zip

Last edited on 20 Feb 2007.

Code: Select all

;David Nikdel & NeHe's Bezier Tutorial (Lesson 28)
;http://nehe.gamedev.net
;Credits: Nico Gruener, Dreglor, traumatic
;Author: hagibaba
;Date: 15 Jan 2007
;Note: up-to-date with PB v4.02 (Windows)
;Note: requires a bitmap in path "Data/NeHe.bmp"

;Section for standard constants, structures, macros and declarations

XIncludeFile #PB_Compiler_Home+"Examples\Sources - Advanced\OpenGL Cube\OpenGL.pbi" ;include the gl.h constants

;wingdi.h constants
#DM_BITSPERPEL=$40000
#DM_PELSWIDTH=$80000
#DM_PELSHEIGHT=$100000

;winuser.h constants
#ENUM_CURRENT_SETTINGS=-1
#CDS_TEST=2
#CDS_FULLSCREEN=4
#CDS_RESET=$40000000
#DISP_CHANGE_SUCCESSFUL=0
#SC_MONITORPOWER=$F170

Structure AUX_RGBImageRec ;glaux.h structure
 sizeX.l : sizeY.l
 Data.l
EndStructure

Procedure.w LoWord(value.l) ;windef.h macro
 ProcedureReturn (value & $FFFF)
EndProcedure

Procedure.w HiWord(value.l) ;windef.h macro
 ProcedureReturn ((value >> 16) & $FFFF)
EndProcedure

;glaux.lib symbols
!public ___ftoll
!___ftoll dw 0
!public __imp__wsprintfA
!__imp__wsprintfA dw 0

Import "glaux.lib"
  CompilerIf #PB_Compiler_Unicode
    auxDIBImageLoad.l(filename.s) As "_auxDIBImageLoadW@4" ;loads a 24-bit Windows DIB
  CompilerElse
    auxDIBImageLoad.l(filename.s) As "_auxDIBImageLoadA@4" ;loads a 24-bit Windows DIB
  CompilerEndIf
EndImport

Import "glu32.lib"
 gluPerspective(fovy.d,aspect.d,zNear.d,zFar.d) ;sets up a perspective projection matrix
EndImport

Import "opengl32.lib"
 glClearDepth(depth.d) ;specifies the clear value for the depth buffer
 glVertex3d(x.d,y.d,z.d) ;specifies a vertex
EndImport

;Start of Lesson 28

Structure POINT_3D ;Structure For A 3-Dimensional Point ( NEW )
 x.d : y.d : z.d
EndStructure

Structure BEZIER_PATCH ;Structure For A 3rd Degree Bezier Patch ( NEW )
 anchors.POINT_3D[4*4] ;4x4 Grid Of Anchor Points
 dlBPatch.l ;Display List For Bezier Patch
 texture.l ;Texture For The Patch
EndStructure

Global hDC.l ;Private GDI Device Context
Global hRC.l ;Permanent Rendering Context
Global hWnd.l ;Holds Our Window Handle
Global hInstance.l ;Holds The Instance Of The Application

Global DMsaved.DEVMODE ;Saves The Previous Screen Settings ( NEW )

Global Dim keys.b(256) ;Array Used For The Keyboard Routine
Global active.b=#True ;Window Active Flag Set To TRUE By Default
Global fullscreen.b=#True ;Fullscreen Flag Set To Fullscreen Mode By Default

Global rotz.f=180.0 ;Rotation About The Z Axis
Global mybezier.BEZIER_PATCH ;The Bezier Patch We're Going To Use ( NEW )
Global showCPoints.b=#True ;Toggles Displaying The Control Point Grid ( NEW )
Global divs.l=7 ;Number Of Intrapolations (Controls Poly Resolution) ( NEW )

Declare.l WndProc(hWnd.l,uMsg.l,wParam.l,lParam.l) ;Declaration For WndProc

Procedure.l pointAdd(*p.POINT_3D,*q.POINT_3D) ;Adds 2 Points

 Static r.POINT_3D
 r\x=*p\x+*q\x : r\y=*p\y+*q\y : r\z=*p\z+*q\z
 ProcedureReturn r
 
EndProcedure

Procedure.l pointTimes(c.d,*p.POINT_3D) ;Multiplies A Point And A Constant

 Static r.POINT_3D
 r\x=*p\x*c : r\y=*p\y*c : r\z=*p\z*c
 ProcedureReturn r
 
EndProcedure

Procedure makePoint(*p.POINT_3D,a.d,b.d,c.d) ;Function For Quick Point Creation

 *p\x=a : *p\y=b : *p\z=c
 
EndProcedure

Procedure setPoint(*p.POINT_3D,*q.POINT_3D) ;Set p.point equal to q.point

 *p\x=*q\x : *p\y=*q\y : *p\z=*q\z
 
EndProcedure

;Calculates 3rd Degree Polynomial Based On Array Of 4 Points
;And A Single Variable (u) Which Is Generally Between 0 And 1

Procedure.l Bernstein(u.f, Array p.POINT_3D(1))

 Protected a.POINT_3D,b.POINT_3D,c.POINT_3D,d.POINT_3D
 Static r.POINT_3D
 
 setPoint(a,pointTimes(Pow(u,3),p(0)))
 setPoint(b,pointTimes(3*Pow(u,2)*(1-u),p(1)))
 setPoint(c,pointTimes(3*u*Pow((1-u),2),p(2)))
 setPoint(d,pointTimes(Pow((1-u),3),p(3)))
 
 setPoint(a,pointAdd(a,b))
 setPoint(c,pointAdd(c,d))
 setPoint(r,pointAdd(a,c))
 
 ProcedureReturn r
 
EndProcedure

;Generates A Display List Based On The Data In The Patch
;And The Number Of Divisions

Procedure.l genBezier(*patch.BEZIER_PATCH,divs.l)

 Protected u.l=0,v.l
 Protected py.f,px.f,pyold.f
 Protected drawlist.l
 Protected Dim anchors.POINT_3D(4)
 Protected Dim temp.POINT_3D(4)
 Protected Dim last.POINT_3D(divs+1) ;Array Of Points To Mark The First Line Of Polys
 
 drawlist=glGenLists_(1) ;Make The Display List
 
 If *patch\dlBPatch<>0 ;Get Rid Of Any Old Display Lists
  glDeleteLists_(*patch\dlBPatch,1)
 EndIf
 
 ;The First Derived Curve (Along X-Axis)
 setPoint(temp(0),*patch\anchors[(0*4)+3])
 setPoint(temp(1),*patch\anchors[(1*4)+3])
 setPoint(temp(2),*patch\anchors[(2*4)+3])
 setPoint(temp(3),*patch\anchors[(3*4)+3])
 
 For v=0 To divs ;Create The First Line Of Points
  px=v/divs ;Percent Along Y-Axis
  ;Use The 4 Points From The Derived Curve To Calculate The Points Along That Curve
  setPoint(last(v),Bernstein(px,temp()))
 Next
 
 glNewList_(drawlist,#GL_COMPILE) ;Start A New Display List
 glBindTexture_(#GL_TEXTURE_2D,*patch\texture) ;Bind The Texture
 
 For u=1 To divs
  py=u/divs ;Percent Along Y-Axis
  pyold=(u-1.0)/divs ;Percent Along Old Y Axis
 
  ;Calculate New Bezier Points
  setPoint(anchors(0),*patch\anchors[(0*4)+0])
  setPoint(anchors(1),*patch\anchors[(0*4)+1])
  setPoint(anchors(2),*patch\anchors[(0*4)+2])
  setPoint(anchors(3),*patch\anchors[(0*4)+3])
  setPoint(temp(0),Bernstein(py,anchors())) ;Note: can't pass static array as parameter
 
  setPoint(anchors(0),*patch\anchors[(1*4)+0])
  setPoint(anchors(1),*patch\anchors[(1*4)+1])
  setPoint(anchors(2),*patch\anchors[(1*4)+2])
  setPoint(anchors(3),*patch\anchors[(1*4)+3])
  setPoint(temp(1),Bernstein(py,anchors()))
 
  setPoint(anchors(0),*patch\anchors[(2*4)+0])
  setPoint(anchors(1),*patch\anchors[(2*4)+1])
  setPoint(anchors(2),*patch\anchors[(2*4)+2])
  setPoint(anchors(3),*patch\anchors[(2*4)+3])
  setPoint(temp(2),Bernstein(py,anchors()))
 
  setPoint(anchors(0),*patch\anchors[(3*4)+0])
  setPoint(anchors(1),*patch\anchors[(3*4)+1])
  setPoint(anchors(2),*patch\anchors[(3*4)+2])
  setPoint(anchors(3),*patch\anchors[(3*4)+3])
  setPoint(temp(3),Bernstein(py,anchors()))
 
  glBegin_(#GL_TRIANGLE_STRIP) ;Begin A New Triangle Strip
  For v=0 To divs
   px=v/divs ;Percent Along The X-Axis
   
   glTexCoord2f_(pyold,px) ;Apply The Old Texture Coords
   glVertex3d(last(v)\x,last(v)\y,last(v)\z) ;Old Point
   
   setPoint(last(v),Bernstein(px,temp())) ;Generate New Point
   glTexCoord2f_(py,px) ;Apply The New Texture Coords
   glVertex3d(last(v)\x,last(v)\y,last(v)\z) ;New Point
  Next
  glEnd_() ;End The Triangle Strip
 
 Next
 
 glEndList_() ;End The List
 
 Dim last.POINT_3D(0) ;Free The Old Vertices Array
 ProcedureReturn drawlist ;Return The Display List
 
EndProcedure

Procedure initBezier() ;Set The Bezier Vertices

 makePoint(mybezier\anchors[(0*4)+0],-0.75,-0.75,-0.5 )
 makePoint(mybezier\anchors[(0*4)+1],-0.25,-0.75, 0.0 )
 makePoint(mybezier\anchors[(0*4)+2], 0.25,-0.75, 0.0 )
 makePoint(mybezier\anchors[(0*4)+3], 0.75,-0.75,-0.5 )
 makePoint(mybezier\anchors[(1*4)+0],-0.75,-0.25,-0.75)
 makePoint(mybezier\anchors[(1*4)+1],-0.25,-0.25, 0.5 )
 makePoint(mybezier\anchors[(1*4)+2], 0.25,-0.25, 0.5 )
 makePoint(mybezier\anchors[(1*4)+3], 0.75,-0.25,-0.75)
 makePoint(mybezier\anchors[(2*4)+0],-0.75, 0.25, 0.0 )
 makePoint(mybezier\anchors[(2*4)+1],-0.25, 0.25,-0.5 )
 makePoint(mybezier\anchors[(2*4)+2], 0.25, 0.25,-0.5 )
 makePoint(mybezier\anchors[(2*4)+3], 0.75, 0.25, 0.0 )
 makePoint(mybezier\anchors[(3*4)+0],-0.75, 0.75,-0.5 )
 makePoint(mybezier\anchors[(3*4)+1],-0.25, 0.75,-1.0 )
 makePoint(mybezier\anchors[(3*4)+2], 0.25, 0.75,-1.0 )
 makePoint(mybezier\anchors[(3*4)+3], 0.75, 0.75,-0.5 )
 mybezier\dlBPatch=0 ;Go Ahead And Initialize This To NULL
 
EndProcedure

Procedure.b LoadGLTexture(*texPntr.LONG,name.s) ;Load Bitmaps And Convert To Textures

 Protected success.b=#False
 Protected *TextureImage.AUX_RGBImageRec=#Null
 Protected test.l=#Null
 
 If *texPntr=0 ;invalid pointer
  ProcedureReturn #False
 EndIf
 
 glGenTextures_(1,@*texPntr\l) ;Generate 1 Texture
 
 test=ReadFile(#PB_Any,name) ;Test To See If The File Exists
 
 If test<>#Null ;If It Does
  CloseFile(test) ;Close The File
  *TextureImage=auxDIBImageLoad(name) ;And Load The Texture
 EndIf
 
 If *TextureImage<>#Null ;If It Loaded
  success=#True
 
  ;Typical Texture Generation Using Data From The Bitmap
  glBindTexture_(#GL_TEXTURE_2D,*texPntr\l)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MIN_FILTER,#GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MAG_FILTER,#GL_LINEAR)
  glTexImage2D_(#GL_TEXTURE_2D,0,3,*TextureImage\sizeX,*TextureImage\sizeY,0,#GL_RGB,#GL_UNSIGNED_BYTE,*TextureImage\Data)
 EndIf
 
 If *TextureImage\Data
  ;FreeMemory(*TextureImage\Data)
 EndIf
 
 ProcedureReturn success
 
EndProcedure

Procedure ReSizeGLScene(width.l,height.l) ;Resize And Initialize The GL Window
 
 If height=0 : height=1 : EndIf ;Prevent A Divide By Zero Error
 
 glViewport_(0,0,width,height) ;Reset The Current Viewport
 
 glMatrixMode_(#GL_PROJECTION) ;Select The Projection Matrix
 glLoadIdentity_() ;Reset The Projection Matrix
 
 gluPerspective(45.0,Abs(width/height),0.1,100.0) ;Calculate The Aspect Ratio Of The Window
 
 glMatrixMode_(#GL_MODELVIEW) ;Select The Modelview Matrix
 glLoadIdentity_() ;Reset The Modelview Matrix
 
EndProcedure

Procedure.l InitGL() ;All Setup For OpenGL Goes Here

 glEnable_(#GL_TEXTURE_2D) ;Enable Texture Mapping
 glShadeModel_(#GL_SMOOTH) ;Enable Smooth Shading
 glClearColor_(0.05,0.05,0.05,0.5) ;Black Background
 glClearDepth(1.0) ;Depth Buffer Setup
 glEnable_(#GL_DEPTH_TEST) ;Enables Depth Testing
 glDepthFunc_(#GL_LEQUAL) ;The Type Of Depth Testing To Do
 glHint_(#GL_PERSPECTIVE_CORRECTION_HINT,#GL_NICEST) ;Really Nice Perspective Calculations
 
 initBezier() ;Initialize the Bezier's Control Grid
 If LoadGLTexture(@mybezier\texture,"Data/NeHe.bmp")=0 ;Load The Texture
  ProcedureReturn #False
 EndIf
 mybezier\dlBPatch=genBezier(mybezier,divs) ;Generate The Patch
 
 ProcedureReturn #True ;Initialization Went OK
 
EndProcedure

Procedure.l DrawGLScene() ;Here's Where We Do All The Drawing

 Protected i.l,j.l
 
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) ;Clear Screen And Depth Buffer
 glLoadIdentity_() ;Reset The Current Modelview Matrix
 glTranslatef_(0.0,0.0,-4.0) ;Move Into The Screen 4.0
 glRotatef_(-75.0,1.0,0.0,0.0)
 glRotatef_(rotz,0.0,0.0,1.0) ;Rotate The Triangle On The Z-Axis
 
 glCallList_(mybezier\dlBPatch) ;Call The Bezier's Display List (This Need Only Be Updated When The Patch Changes)
 
 If showCPoints ;If Drawing The Grid Is Toggled On
  glDisable_(#GL_TEXTURE_2D)
  glColor3f_(1.0,0.0,0.0)
  For i=0 To 4-1 ;Draw The Horizontal Lines
   glBegin_(#GL_LINE_STRIP)
   For j=0 To 4-1
    glVertex3d(mybezier\anchors[(i*4)+j]\x,mybezier\anchors[(i*4)+j]\y,mybezier\anchors[(i*4)+j]\z)
   Next
   glEnd_()
  Next
  For i=0 To 4-1 ;Draw The Vertical Lines
   glBegin_(#GL_LINE_STRIP)
   For j=0 To 4-1
    glVertex3d(mybezier\anchors[(j*4)+i]\x,mybezier\anchors[(j*4)+i]\y,mybezier\anchors[(j*4)+i]\z)
   Next
   glEnd_()
  Next
  glColor3f_(1.0,1.0,1.0)
  glEnable_(#GL_TEXTURE_2D)
 EndIf
 
 ProcedureReturn #True ;Keep Going
 
EndProcedure

Procedure KillGLWindow() ;Properly Kill The Window

 If fullscreen ;Are We In Fullscreen Mode?
  If ChangeDisplaySettings_(#Null,#CDS_TEST)=0 ;If The Shortcut Doesn't Work ( NEW )
   ChangeDisplaySettings_(#Null,#CDS_RESET) ;Do It Anyway (To Get The Values Out Of The Registry) ( NEW )
   ChangeDisplaySettings_(DMsaved,#CDS_RESET) ;Change It To The Saved Settings ( NEW )
  Else
   ChangeDisplaySettings_(#Null,#CDS_RESET) ;If It Works, Go Right Ahead ( NEW )
  EndIf
  ShowCursor_(#True) ;Show Mouse Pointer
 EndIf
 
 If hRC ;Do We Have A Rendering Context?
  If wglMakeCurrent_(#Null,#Null)=0 ;Are We Able To Release The DC And RC Contexts?
   MessageBox_(#Null,"Release Of DC And RC Failed.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
  EndIf
  If wglDeleteContext_(hRC)=0 ;Are We Able To Delete The RC?
   MessageBox_(#Null,"Release Rendering Context Failed.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
  EndIf
  hRC=#Null ;Set RC To NULL
 EndIf
 
 If hDC And ReleaseDC_(hWnd,hDC)=0 ;Are We Able To Release The DC
  MessageBox_(#Null,"Release Device Context Failed.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
  hDC=#Null ;Set DC To NULL
 EndIf
 
 If hWnd And DestroyWindow_(hWnd)=0 ;Are We Able To Destroy The Window?
   MessageBox_(#Null,"Could Not Release hWnd.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
   hWnd=#Null ;Set hWnd To NULL
 EndIf
 
 If UnregisterClass_("OpenGL",hInstance)=0 ;Are We Able To Unregister Class
  MessageBox_(#Null,"Could Not Unregister Class.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
  hInstance=#Null ;Set hInstance To NULL
 EndIf
 
EndProcedure

;This Code Creates Our OpenGL Window. Parameters Are:
;title - Title To Appear At The Top Of The Window
;width - Width Of The GL Window Or Fullscreen Mode
;height - Height Of The GL Window Or Fullscreen Mode
;bits - Number Of Bits To Use For Color (8/16/24/32)
;fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)

Procedure.b CreateGLWindow(title.s,width.l,height.l,bits.l,fullscreenflag.b)

 Protected PixelFormat.l ;Holds The Results After Searching For A Match
 Protected wc.WNDCLASS ;Windows Class Structure
 Protected dwExStyle.l ;Window Extended Style
 Protected dwStyle.l ;Window Style
 Protected WindowRect.RECT ;Grabs Rectangle Upper Left / Lower Right Values
 Protected wpos.POINT ;Window position
 
 WindowRect\left=0 ;Set Left Value To 0
 WindowRect\right=width ;Set Right Value To Requested Width
 WindowRect\top=0 ;Set Top Value To 0
 WindowRect\bottom=height ;Set Bottom Value To Requested Height
 
 fullscreen=fullscreenflag ;Set The Global Fullscreen Flag
 
 hInstance=GetModuleHandle_(#Null) ;Grab An Instance For Our Window
 
 wc\style=#CS_HREDRAW | #CS_VREDRAW | #CS_OWNDC ;Redraw On Size, And Own DC For Window
 wc\lpfnWndProc=@WndProc() ;WndProc Handles Messages
 wc\cbClsExtra=0 ;No Extra Window Data
 wc\cbWndExtra=0 ;No Extra Window Data
 wc\hInstance=hInstance ;Set The Instance
 wc\hIcon=LoadIcon_(#Null,#IDI_WINLOGO) ;Load The Default Icon
 wc\hCursor=LoadCursor_(#Null,#IDC_ARROW) ;Load The Arrow Pointer
 wc\hbrBackground=#Null ;No Background Required For GL
 wc\lpszMenuName=#Null ;We Don't Want A Menu
 wc\lpszClassName=@"OpenGL" ;Set The Class Name 
 
 If RegisterClass_(wc)=0 ;Attempt To Register The Window Class
  MessageBox_(#Null,"Failed To Register The Window Class.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 EnumDisplaySettings_(#Null,#ENUM_CURRENT_SETTINGS,DMsaved) ;Save The Current Display State ( NEW )
 
 If fullscreen ;Attempt Fullscreen Mode?
 
  Protected dmScreenSettings.DEVMODE ;Device Mode
  dmScreenSettings\dmSize=SizeOf(DEVMODE) ;Size Of The Devmode Structure
  dmScreenSettings\dmFields=#DM_BITSPERPEL | #DM_PELSWIDTH | #DM_PELSHEIGHT ;bit flags to specify the members of DEVMODE that were initialized
  dmScreenSettings\dmBitsPerPel=bits ;Selected Bits Per Pixel
  dmScreenSettings\dmPelsWidth=width ;Selected Screen Width in pixels
  dmScreenSettings\dmPelsHeight=height ;Selected Screen Height in pixels
 
  ;Try To Set Selected Mode And Get Results. Note: CDS_FULLSCREEN Gets Rid Of Start Bar
  If ChangeDisplaySettings_(dmScreenSettings,#CDS_FULLSCREEN)<>#DISP_CHANGE_SUCCESSFUL
   ;If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode
   If MessageBox_(#Null,"The Requested Fullscreen Mode Is Not Supported By"+Chr(10)+"Your Video Card. Use Windowed Mode Instead?","NeHe GL",#MB_YESNO | #MB_ICONEXCLAMATION)=#IDYES
    fullscreen=#False ;Windowed Mode Selected.  Fullscreen = FALSE
   Else
    ;Pop Up A Message Box Letting User Know The Program Is Closing
    MessageBox_(#Null,"Program Will Now Close.","ERROR",#MB_OK | #MB_ICONSTOP)
    ProcedureReturn #False
   EndIf
  EndIf
 
 EndIf
 
 If fullscreen ;Are We Still In Fullscreen Mode?
  dwExStyle=#WS_EX_APPWINDOW ;Window Extended Style
  dwStyle=#WS_POPUP ;Windows Style
  ShowCursor_(#False) ;Hide Mouse Pointer
 Else
  dwExStyle=#WS_EX_APPWINDOW | #WS_EX_WINDOWEDGE ;Window Extended Style
  dwStyle=#WS_OVERLAPPEDWINDOW ;Windows Style
 EndIf
 
 AdjustWindowRectEx_(WindowRect,dwStyle,#False,dwExStyle) ;Adjust Window To True Requested Size
 
 If fullscreen=0 ;if not fullscreen mode calculate screen centered window
  wpos\x=(GetSystemMetrics_(#SM_CXSCREEN)/2)-((WindowRect\right-WindowRect\left)/2)
  wpos\y=(GetSystemMetrics_(#SM_CYSCREEN)/2)-((WindowRect\bottom-WindowRect\top)/2)
 EndIf
 
 ;CreateWindowEx_(Extended Window Style, Class Name, Window Title, Window Style, Window X Position, Window Y Position, Width, Height, No Parent Window, No Menu, Instance, No Creation Data)
 hWnd=CreateWindowEx_(dwExStyle,"OpenGL",title,dwStyle | #WS_CLIPSIBLINGS | #WS_CLIPCHILDREN,wpos\x,wpos\y,WindowRect\right-WindowRect\left,WindowRect\bottom-WindowRect\top,#Null,#Null,hInstance,#Null)
 If hWnd=0
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Window Creation Error.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 Protected pfd.PIXELFORMATDESCRIPTOR ;pfd Tells Windows How We Want Things To Be
 pfd\nSize=SizeOf(PIXELFORMATDESCRIPTOR) ;Size Of This Structure
 pfd\nVersion=1 ;Version Number
 pfd\dwFlags=#PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW ;Format Must Support Window, OpenGL, Double Buffering
 pfd\iPixelType=#PFD_TYPE_RGBA ;Request An RGBA Format
 pfd\cColorBits=bits ;Select Our Color Depth
 pfd\cRedBits=0 ;Color Bits Ignored
 pfd\cRedShift=0
 pfd\cGreenBits=0
 pfd\cGreenShift=0
 pfd\cBlueBits=0
 pfd\cBlueShift=0
 pfd\cAlphaBits=0 ;No Alpha Buffer
 pfd\cAlphaShift=0 ;Shift Bit Ignored
 pfd\cAccumBits=0 ;No Accumulation Buffer
 pfd\cAccumRedBits=0 ;Accumulation Bits Ignored
 pfd\cAccumGreenBits=0
 pfd\cAccumBlueBits=0
 pfd\cAccumAlphaBits=0
 pfd\cDepthBits=16 ;16Bit Z-Buffer (Depth Buffer)
 pfd\cStencilBits=0 ;No Stencil Buffer
 pfd\cAuxBuffers=0 ;No Auxiliary Buffer
 pfd\iLayerType=#PFD_MAIN_PLANE ;Main Drawing Layer
 pfd\bReserved=0 ;Reserved
 pfd\dwLayerMask=0 ;Layer Masks Ignored
 pfd\dwVisibleMask=0
 pfd\dwDamageMask=0
 
 hDC=GetDC_(hWnd)
 If hDC=0 ;Did We Get A Device Context?
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Can't Create A GL Device Context.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 PixelFormat=ChoosePixelFormat_(hDC,pfd)
 If PixelFormat=0 ;Did Windows Find A Matching Pixel Format?
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Can't Find A Suitable PixelFormat.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 If SetPixelFormat_(hDC,PixelFormat,pfd)=0 ;Are We Able To Set The Pixel Format?
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Can't Set The PixelFormat.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 hRC=wglCreateContext_(hDC)
 If hRC=0 ;Are We Able To Get A Rendering Context?
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Can't Create A GL Rendering Context.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 If wglMakeCurrent_(hDC,hRC)=0 ;Try To Activate The Rendering Context
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Can't Activate The GL Rendering Context.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 ShowWindow_(hWnd,#SW_SHOW) ;Show The Window
 SetForegroundWindow_(hWnd) ;Slightly Higher Priority
 SetFocus_(hWnd) ;Sets Keyboard Focus To The Window
 ReSizeGLScene(width,height) ;Set Up Our Perspective GL Screen
 
 If InitGL()=0 ;Initialize Our Newly Created GL Window
  KillGLWindow() ;Reset The Display
  MessageBox_(#Null,"Initialization Failed.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
  ProcedureReturn #False
 EndIf
 
 ProcedureReturn #True ;Success
 
EndProcedure

Procedure.l WndProc(hWnd.l,uMsg.l,wParam.l,lParam.l)

 Select uMsg ;Check For Windows Messages
 
  Case #WM_ACTIVATE ;Watch For Window Activate Message
   If HiWord(wParam)=0 ;Check Minimization State
    active=#True ;Program Is Active
   Else
    active=#False ;Program Is No Longer Active
   EndIf
   ProcedureReturn 0 ;Return To The Message Loop
   
  Case #WM_SYSCOMMAND ;Intercept System Commands
   Select wParam ;Check System Calls
    Case #SC_SCREENSAVE ;Screensaver Trying To Start?
     ProcedureReturn 0 ;Prevent From Happening
    Case #SC_MONITORPOWER ;Monitor Trying To Enter Powersave?
     ProcedureReturn 0 ;Prevent From Happening
   EndSelect
   
  Case #WM_CLOSE ;Did We Receive A Close Message?
   PostQuitMessage_(0) ;Send A Quit Message
   ProcedureReturn 0 ;Jump Back
   
  Case #WM_KEYDOWN ;Is A Key Being Held Down?
   keys(wParam)=#True ;If So, Mark It As TRUE
   ProcedureReturn 0 ;Jump Back
   
  Case #WM_KEYUP ;Has A Key Been Released?
   keys(wParam)=#False ;If So, Mark It As FALSE
   ProcedureReturn 0 ;Jump Back
   
  Case #WM_SIZE ;Resize The OpenGL Window
   ReSizeGLScene(LoWord(lParam),HiWord(lParam)) ;LoWord=Width, HiWord=Height
   ProcedureReturn 0 ;Jump Back
   
 EndSelect
 
 ;Pass All Unhandled Messages To DefWindowProc
 ProcedureReturn DefWindowProc_(hWnd,uMsg,wParam,lParam)
 
EndProcedure

Procedure.l WinMain() ;Main Program

 Protected msg.MSG ;Windows Message Structure
 Protected done.b ;Bool Variable To Exit Loop
 
 ;Ask The User Which Screen Mode They Prefer
 If MessageBox_(#Null,"Would You Like To Run In Fullscreen Mode?","Start FullScreen?",#MB_YESNO | #MB_ICONQUESTION)=#IDNO
  fullscreen=#False ;Windowed Mode
 EndIf
 
 If CreateGLWindow("David Nikdel & NeHe's Bezier Tutorial",640,480,16,fullscreen)=0 ;Create The Window
  ProcedureReturn 0 ;Quit If Window Was Not Created
 EndIf
 
 While done=#False ;Loop That Runs While done=FALSE
 
  If PeekMessage_(msg,#Null,0,0,#PM_REMOVE) ;Is There A Message Waiting?
 
   If msg\message=#WM_QUIT ;Have We Received A Quit Message?
    done=#True ;If So done=TRUE
   Else ;If Not, Deal With Window Messages
    TranslateMessage_(msg) ;Translate The Message
    DispatchMessage_(msg) ;Dispatch The Message
   EndIf
   
  Else ;If There Are No Messages
 
   ;Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
   If (active And DrawGLScene()=0) Or keys(#VK_ESCAPE) ;Active? Was There A Quit Received?
    done=#True ;ESC or DrawGLScene Signalled A Quit
  Else ;Not Time To Quit, Update Screen
     Delay(5)
    SwapBuffers_(hDC) ;Swap Buffers (Double Buffering)
   EndIf
   
   If keys(#VK_LEFT) ;Left Arrow
    rotz-0.8 ;Rotate Left
   EndIf
   If keys(#VK_RIGHT) ;Right Arrow
    rotz+0.8 ;Rotate Right
   EndIf
   If keys(#VK_UP) And divs<32 ;Up Arrow
    divs+1 ;Resolution Up
    mybezier\dlBPatch=genBezier(mybezier,divs) ;Update The Patch
    keys(#VK_UP)=#False
   EndIf
   If keys(#VK_DOWN) And divs>1 ;Down Arrow
    divs-1 ;Resolution Down
    mybezier\dlBPatch=genBezier(mybezier,divs) ;Update The Patch
    keys(#VK_DOWN)=#False
   EndIf
   If keys(#VK_SPACE) ;Spacebar
    showCPoints=~showCPoints & 1 ;Toggles showCPoints
    keys(#VK_SPACE)=#False
   EndIf
   
   If keys(#VK_F1) ;Is F1 Being Pressed?
    keys(#VK_F1)=#False ;If So Make Key FALSE
    KillGLWindow() ;Kill Our Current Window
    fullscreen=~fullscreen & 1 ;Toggle Fullscreen / Windowed Mode
    ;Recreate Our OpenGL Window
    If CreateGLWindow("David Nikdel & NeHe's Bezier Tutorial",640,480,16,fullscreen)=0
     ProcedureReturn 0 ;Quit If Window Was Not Created
    EndIf
   EndIf
   
  EndIf
 
 Wend
 
 ;Shutdown
 KillGLWindow() ;Kill The Window
 End ;Exit The Program
 
EndProcedure

WinMain() ;run the main program
Last edited by hagibaba on Sun Jul 01, 2007 11:22 pm, edited 4 times in total.
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Great! :D
mpz
Enthusiast
Enthusiast
Posts: 494
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: David Nikdel & NeHe's Bezier Tutorial (Lesson 28)

Post by mpz »

Hello,

next code actualised for x86/x64, PB 5.73.

Sorry, but you "must" get the original texture from the internet, i have not purebasic textures for that

P.S:I think the code can work with linux and mac Osx too.


Greetings Michael

C Code and texture
iamyaker.googlepages.com/28_Bezier.rar

Code: Select all

;David Nikdel & NeHe's Bezier Tutorial (Lesson 28)
;http://nehe.gamedev.net and http://www.tiptup.com
;https://nehe.gamedev.net/tutorial/bezier_patches__fullscreen_fix/18003/
;Note: requires a bitmap in path "Data/NeHe.bmp"
;Credits: Nico Gruener, Dreglor, traumatic, hagibaba
;Author: MPz
;Date: 1 Nov 2021
;Note: up-to-date with PB v5.73 (Windows)

UsePNGImageDecoder() 

;Start of Lesson 28

Structure POINT_3D ;Structure For A 3-Dimensional Point ( NEW )
 x.d : y.d : z.d
EndStructure

Structure BEZIER_PATCH ;Structure For A 3rd Degree Bezier Patch ( NEW )
 anchors.POINT_3D[4*4] ;4x4 Grid Of Anchor Points
 dlBPatch.i ;Display List For Bezier Patch
 texture.i ;Texture For The Patch
EndStructure

Global hDC.l ;Private GDI Device Context
Global hRC.l ;Permanent Rendering Context
Global hWnd.l ;Holds Our Window Handle
Global hInstance.l ;Holds The Instance Of The Application

Global DMsaved.DEVMODE ;Saves The Previous Screen Settings ( NEW )

Global Dim keys.b(256) ;Array Used For The Keyboard Routine
Global active.b=#True ;Window Active Flag Set To TRUE By Default
Global fullscreen.b=#True ;Fullscreen Flag Set To Fullscreen Mode By Default

Global rotz.f=180.0 ;Rotation About The Z Axis
Global mybezier.BEZIER_PATCH ;The Bezier Patch We're Going To Use ( NEW )
Global showCPoints.b=#True ;Toggles Displaying The Control Point Grid ( NEW )
Global divs.l=7 ;Number Of Intrapolations (Controls Poly Resolution) ( NEW )

Procedure pointAdd(*p.POINT_3D,*q.POINT_3D) ;Adds 2 Points

 Static r.POINT_3D
 r\x=*p\x+*q\x : r\y=*p\y+*q\y : r\z=*p\z+*q\z
 ProcedureReturn r
 
EndProcedure

Procedure pointTimes(c.d,*p.POINT_3D) ;Multiplies A Point And A Constant

 Static r.POINT_3D
 r\x=*p\x*c : r\y=*p\y*c : r\z=*p\z*c
 ProcedureReturn r
 
EndProcedure

Procedure makePoint(*p.POINT_3D,a.d,b.d,c.d) ;Function For Quick Point Creation

 *p\x=a : *p\y=b : *p\z=c
 
EndProcedure

Procedure setPoint(*p.POINT_3D,*q.POINT_3D) ;Set p.point equal to q.point

 *p\x=*q\x : *p\y=*q\y : *p\z=*q\z
 
EndProcedure

;Calculates 3rd Degree Polynomial Based On Array Of 4 Points
;And A Single Variable (u) Which Is Generally Between 0 And 1

Procedure Bernstein(u.f, Array p.POINT_3D(1))

 Protected a.POINT_3D,b.POINT_3D,c.POINT_3D,d.POINT_3D
 Static r.POINT_3D
 
 setPoint(a,pointTimes(Pow(u,3),p(0)))
 setPoint(b,pointTimes(3*Pow(u,2)*(1-u),p(1)))
 setPoint(c,pointTimes(3*u*Pow((1-u),2),p(2)))
 setPoint(d,pointTimes(Pow((1-u),3),p(3)))
 
 setPoint(a,pointAdd(a,b))
 setPoint(c,pointAdd(c,d))
 setPoint(r,pointAdd(a,c))
 
 ProcedureReturn r
 
EndProcedure

;Generates A Display List Based On The Data In The Patch
;And The Number Of Divisions

Procedure genBezier(*patch.BEZIER_PATCH,divs.l)

 Protected u.l=0,v.l
 Protected py.f,px.f,pyold.f
 Protected drawlist.i
 Protected Dim anchors.POINT_3D(4)
 Protected Dim temp.POINT_3D(4)
 Protected Dim last.POINT_3D(divs+1) ;Array Of Points To Mark The First Line Of Polys
 
 drawlist=glGenLists_(1) ;Make The Display List
 
 If *patch\dlBPatch<>0 ;Get Rid Of Any Old Display Lists
  glDeleteLists_(*patch\dlBPatch,1)
 EndIf
 
 ;The First Derived Curve (Along X-Axis)
 setPoint(temp(0),*patch\anchors[(0*4)+3])
 setPoint(temp(1),*patch\anchors[(1*4)+3])
 setPoint(temp(2),*patch\anchors[(2*4)+3])
 setPoint(temp(3),*patch\anchors[(3*4)+3])
 
 For v=0 To divs ;Create The First Line Of Points
  px=v/divs ;Percent Along Y-Axis
  ;Use The 4 Points From The Derived Curve To Calculate The Points Along That Curve
  setPoint(last(v),Bernstein(px,temp()))
 Next
 
 glNewList_(drawlist,#GL_COMPILE) ;Start A New Display List
 glBindTexture_(#GL_TEXTURE_2D,*patch\texture) ;Bind The Texture
 
 For u=1 To divs
  py=u/divs ;Percent Along Y-Axis
  pyold=(u-1.0)/divs ;Percent Along Old Y Axis
 
  ;Calculate New Bezier Points
  setPoint(anchors(0),*patch\anchors[(0*4)+0])
  setPoint(anchors(1),*patch\anchors[(0*4)+1])
  setPoint(anchors(2),*patch\anchors[(0*4)+2])
  setPoint(anchors(3),*patch\anchors[(0*4)+3])
  setPoint(temp(0),Bernstein(py,anchors())) ;Note: can't pass static array as parameter
 
  setPoint(anchors(0),*patch\anchors[(1*4)+0])
  setPoint(anchors(1),*patch\anchors[(1*4)+1])
  setPoint(anchors(2),*patch\anchors[(1*4)+2])
  setPoint(anchors(3),*patch\anchors[(1*4)+3])
  setPoint(temp(1),Bernstein(py,anchors()))
 
  setPoint(anchors(0),*patch\anchors[(2*4)+0])
  setPoint(anchors(1),*patch\anchors[(2*4)+1])
  setPoint(anchors(2),*patch\anchors[(2*4)+2])
  setPoint(anchors(3),*patch\anchors[(2*4)+3])
  setPoint(temp(2),Bernstein(py,anchors()))
 
  setPoint(anchors(0),*patch\anchors[(3*4)+0])
  setPoint(anchors(1),*patch\anchors[(3*4)+1])
  setPoint(anchors(2),*patch\anchors[(3*4)+2])
  setPoint(anchors(3),*patch\anchors[(3*4)+3])
  setPoint(temp(3),Bernstein(py,anchors()))
 
  glBegin_(#GL_TRIANGLE_STRIP) ;Begin A New Triangle Strip
  For v=0 To divs
   px=v/divs ;Percent Along The X-Axis
   
   glTexCoord2f_(pyold,px) ;Apply The Old Texture Coords
   glVertex3d_(last(v)\x,last(v)\y,last(v)\z) ;Old Point
   
   setPoint(last(v),Bernstein(px,temp())) ;Generate New Point
   glTexCoord2f_(py,px) ;Apply The New Texture Coords
   glVertex3d_(last(v)\x,last(v)\y,last(v)\z) ;New Point
  Next
  glEnd_() ;End The Triangle Strip
 
 Next
 
 glEndList_() ;End The List
 
 Dim last.POINT_3D(0) ;Free The Old Vertices Array
 ProcedureReturn drawlist ;Return The Display List
 
EndProcedure

Procedure initBezier() ;Set The Bezier Vertices

 makePoint(mybezier\anchors[(0*4)+0],-0.75,-0.75,-0.5 )
 makePoint(mybezier\anchors[(0*4)+1],-0.25,-0.75, 0.0 )
 makePoint(mybezier\anchors[(0*4)+2], 0.25,-0.75, 0.0 )
 makePoint(mybezier\anchors[(0*4)+3], 0.75,-0.75,-0.5 )
 makePoint(mybezier\anchors[(1*4)+0],-0.75,-0.25,-0.75)
 makePoint(mybezier\anchors[(1*4)+1],-0.25,-0.25, 0.5 )
 makePoint(mybezier\anchors[(1*4)+2], 0.25,-0.25, 0.5 )
 makePoint(mybezier\anchors[(1*4)+3], 0.75,-0.25,-0.75)
 makePoint(mybezier\anchors[(2*4)+0],-0.75, 0.25, 0.0 )
 makePoint(mybezier\anchors[(2*4)+1],-0.25, 0.25,-0.5 )
 makePoint(mybezier\anchors[(2*4)+2], 0.25, 0.25,-0.5 )
 makePoint(mybezier\anchors[(2*4)+3], 0.75, 0.25, 0.0 )
 makePoint(mybezier\anchors[(3*4)+0],-0.75, 0.75,-0.5 )
 makePoint(mybezier\anchors[(3*4)+1],-0.25, 0.75,-1.0 )
 makePoint(mybezier\anchors[(3*4)+2], 0.25, 0.75,-1.0 )
 makePoint(mybezier\anchors[(3*4)+3], 0.75, 0.75,-0.5 )
 mybezier\dlBPatch=0 ;Go Ahead And Initialize This To NULL
 
EndProcedure

Procedure LoadGLTexture(*texPntr.INTEGER,name.s) ;Load Bitmaps And Convert To Textures

 Protected success.b=#False
 Protected TEST.i=#Null
 
 If *texPntr=0 ;invalid pointer
  ProcedureReturn #False
 EndIf
 
 glGenTextures_(1,@*texPntr\i) ;Generate 1 Texture
 
 TEST=ReadFile(#PB_Any,name) ;Test To See If The File Exists
 
 If TEST<>#Null ;If It Does
   CloseFile(TEST) ;Close The File
   
    LoadImage(0,name) ; Load texture with name
    *pointer = EncodeImage(0, #PB_ImagePlugin_BMP,0,24 );  
    FreeImage(0)

 EndIf
 
 If *pointer<>#Null ;If It Loaded
  success=#True
 
  ;Typical Texture Generation Using Data From The Bitmap
  glBindTexture_(#GL_TEXTURE_2D,*texPntr\i)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MIN_FILTER,#GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MAG_FILTER,#GL_LINEAR)
  glTexImage2D_(#GL_TEXTURE_2D,0,3,PeekL(*pointer+18),PeekL(*pointer+22),0,#GL_RGB,#GL_UNSIGNED_BYTE,*pointer+54)
  FreeMemory(*pointer)
 EndIf
 
 
 ProcedureReturn success
 
EndProcedure

Procedure ReSizeGLScene(width.l,height.l) ;Resize And Initialize The GL Window

 If height=0 : height=1 : EndIf ;Prevent A Divide By Zero Error
 
 ResizeGadget(0, 0, 0, width, height)
 
 glViewport_(0,0,width,height) ;Reset The Current Viewport
 
 glMatrixMode_(#GL_PROJECTION) ;Select The Projection Matrix
 glLoadIdentity_() ;Reset The Projection Matrix
 
 gluPerspective_(45.0,Abs(width/height),0.1,100.0) ;Calculate The Aspect Ratio Of The Window
 
 glMatrixMode_(#GL_MODELVIEW) ;Select The Modelview Matrix
 glLoadIdentity_() ;Reset The Modelview Matrix
 
EndProcedure

Procedure InitGL() ;All Setup For OpenGL Goes Here

 glEnable_(#GL_TEXTURE_2D) ;Enable Texture Mapping
 glShadeModel_(#GL_SMOOTH) ;Enable Smooth Shading
 glClearColor_(0.05,0.05,0.05,0.5) ;Black Background
 glClearDepth_(1.0) ;Depth Buffer Setup
 glEnable_(#GL_DEPTH_TEST) ;Enables Depth Testing
 glDepthFunc_(#GL_LEQUAL) ;The Type Of Depth Testing To Do
 glHint_(#GL_PERSPECTIVE_CORRECTION_HINT,#GL_NICEST) ;Really Nice Perspective Calculations
 
 initBezier() ;Initialize the Bezier's Control Grid
 
 ;If LoadGLTexture(@mybezier\texture,"Data/NeHe.bmp")=0 ; Load The Texture
 ;  ProcedureReturn #False
 ;EndIf
 
 mybezier\dlBPatch=genBezier(mybezier,divs) ;Generate The Patch
 ProcedureReturn #True ;Initialization Went OK

EndProcedure

Procedure DrawScene(Gadget)
  
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) ;Clear Screen And Depth Buffer
 glLoadIdentity_() ;Reset The Current Modelview Matrix
 glTranslatef_(0.0,0.0,-4.0) ;Move Into The Screen 4.0
 glRotatef_(-75.0,1.0,0.0,0.0)
 glRotatef_(rotz,0.0,0.0,1.0) ;Rotate The Triangle On The Z-Axis
 
 glCallList_(mybezier\dlBPatch) ;Call The Bezier's Display List (This Need Only Be Updated When The Patch Changes)
 
 If showCPoints ;If Drawing The Grid Is Toggled On
  glDisable_(#GL_TEXTURE_2D)
  glColor3f_(1.0,0.0,0.0)
  For i=0 To 4-1 ;Draw The Horizontal Lines
   glBegin_(#GL_LINE_STRIP)
   For j=0 To 4-1
    glVertex3d_(mybezier\anchors[(i*4)+j]\x,mybezier\anchors[(i*4)+j]\y,mybezier\anchors[(i*4)+j]\z)
   Next
   glEnd_()
  Next
  For i=0 To 4-1 ;Draw The Vertical Lines
   glBegin_(#GL_LINE_STRIP)
   For j=0 To 4-1
    glVertex3d_(mybezier\anchors[(j*4)+i]\x,mybezier\anchors[(j*4)+i]\y,mybezier\anchors[(j*4)+i]\z)
   Next
   glEnd_()
  Next
  glColor3f_(1.0,1.0,1.0)
  glEnable_(#GL_TEXTURE_2D)
 EndIf
  
  SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
  
  ProcedureReturn #True ;Keep Going

EndProcedure

Procedure CreateGLWindow(title.s,WindowWidth.l,WindowHeight.l,bits.l=16,fullscreenflag.b=0,Vsync.b=0)
  
  If InitKeyboard() = 0 Or InitSprite() = 0 Or InitMouse() = 0
    MessageRequester("Error", "Can't initialize Keyboards or Mouse", 0)
    End
  EndIf

  If fullscreenflag
    hWnd = OpenWindow(0, 0, 0, WindowWidth, WindowHeight, title, #PB_Window_BorderLess|#PB_Window_Maximize )
    OpenWindowedScreen(WindowID(0), 0, 0,WindowWidth(0),WindowHeight(0)) 
  Else  
    hWnd = OpenWindow(0, 1, 1, WindowWidth, WindowHeight, title,#PB_Window_MinimizeGadget |  #PB_Window_MaximizeGadget | #PB_Window_SizeGadget ) 
    OpenWindowedScreen(WindowID(0), 1, 1, WindowWidth,WindowHeight) 
  EndIf
  
  If bits = 24
    OpenGlFlags + #PB_OpenGL_24BitDepthBuffer 
  EndIf
  
  If bits = 32
    OpenGlFlags + #PB_OpenGL_24BitDepthBuffer + #PB_OpenGL_8BitStencilBuffer
  EndIf
  
  If Vsync = 0
    OpenGlFlags + #PB_OpenGL_NoFlipSynchronization
  EndIf
  
  OpenGLGadget(0, 0, 0, WindowWidth(0),WindowHeight(0),OpenGlFlags)
  
  SetActiveGadget(0) 
  
  ReSizeGLScene(WindowWidth(0),WindowHeight(0))
  ;hDC = GetDC_(hWnd)
  
EndProcedure

CreateGLWindow("David Nikdel & NeHe's Bezier Tutorial (Lesson 28)",640,480,16,0)


LoadGLTexture(@mybezier\texture,#PB_Compiler_Home + "examples/3d/Data/Textures/DosCarte.png")
;LoadGLTexture(@mybezier\texture,"Data/NeHe.bmp")=0 ;Load The Texture original
 
InitGL() 


Repeat

  Repeat 
    Event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        Quit = 1
      Case #PB_Event_SizeWindow  
        ReSizeGLScene(WindowWidth(0),WindowHeight(0)) ;LoWord=Width, HiWord=Height
    EndSelect
  
  Until Event = 0
  
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Escape) ;  Esc key to exit
    Quit = 1
  EndIf      
    
   If KeyboardPushed(#PB_Key_Left) ;Left Arrow
    rotz-0.8 ;Rotate Left
  EndIf
  
   If KeyboardPushed(#PB_Key_Right) ;Right Arrow
    rotz+0.8 ;Rotate Right
   EndIf
   
   If KeyboardPushed(#PB_Key_Up) And divs<32 And lUp=0;Up Arrow
      lUp=#True
     divs+1 ;Resolution Up
    mybezier\dlBPatch=genBezier(mybezier,divs) ;Update The Patch
   ElseIf Not KeyboardPushed(#PB_Key_Up) ;Has L Key Been Released?
      lUp=#False ;If So, lp Becomes FALSE
   EndIf
   
   If KeyboardPushed(#PB_Key_Down) And divs>1 And ldown=0;Down Arrow
     ldown=#True
     divs-1 ;Resolution Down
    mybezier\dlBPatch=genBezier(mybezier,divs) ;Update The Patch
   ElseIf Not KeyboardPushed(#PB_Key_Down) ;Has L Key Been Released?
      ldown=#False ;If So, lp Becomes FALSE
   EndIf
   
   If KeyboardPushed(#PB_Key_Space) And lspace = 0;Spacebar
     lspace=#True
     showCPoints=~showCPoints & 1 ;Toggles showCPoints
   ElseIf Not KeyboardPushed(#PB_Key_Space) ;Has L Key Been Released?
      lspace=#False ;If So, lp Becomes FALSE
   EndIf
   
  DrawScene(0)
  
  Delay(5)
Until Quit = 1


 
Working on - MP3D Library - PB 5.73 version ready for download
Post Reply