OpenGL le mode Ecritures

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
kernadec
Messages : 1594
Inscription : ven. 25/avr./2008 11:14

OpenGL le mode Ecritures

Message par kernadec »

bjr à tous
Je vous poste un code applePi du forum English d'avril 2018
comme il n'est pas dit dans son titre qu'il est aussi un exemple
pour placer du texte dans une fenêtre OpenGL..
pour ne pas passer à coté de cet exemple d' écritures...voilà!!
je le poste ici pour que cet exemple soit dans les recherches
OpenGL de ce Forum.

cela dit jolie pomme :wink:

cordialement

Code : Tout sélectionner

; Ecritures avec OPENGL par: applePi  code forum english
;#### https://www.purebasic.fr/english/viewtopic.php?f=36&t=70637


EnableExplicit

Declare FillCurveData()
Declare FillCircleData()
Declare AxisData()
Declare drawGrid(width.f, height.f, grid_width.f)

Declare DrawCurve()

Structure vertices
  x.f
  y.f
  z.f
  r.f
  g.f
  b.f
  a.f
EndStructure


Global Dim vert.vertices(500000) ; for the CurveData
Global Dim vert2.vertices(500000); for the CircleData()
Global Dim vert3.vertices(4)     ; for the x-y axis
Global Dim vert4.vertices(1000)  ; for the Grid

Global v1.vertices
Global v2.vertices

Global ro, ro2, event, rot

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 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 base.l ;Base Display List For The Font Set

Global swidth.l ;screen width (Note: added code to print window size)
Global sheight.l;screen height
Global width.l = 700
Global height.l = 700
swidth = width
sheight = height

OpenWindow(0, 0, 0, width, height, "OpenGL fonts", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(100,100,40))
OpenGLGadget(0, 25, 15, 650, 650, #PB_OpenGL_Keyboard)

hDC=GetDC_(WindowID(0))

FillCurveData()
FillCircleData()
AxisData()
drawGrid(5.0, 3.0, 0.1) ; drawGrid(width.f, height.f, grid_width.f)

;wingdi.h constants
#ANTIALIASED_QUALITY=4 ;for CreateFont_()
#DM_BITSPERPEL=$40000
#DM_PELSWIDTH=$80000
#DM_PELSHEIGHT=$100000


Procedure BuildFont(name.s,height.l,bold.l,italic.b,symbol.l) ;Build Our Bitmap Font
                                                              ;BuildFont("Courier New",24,1,0,0) ;Build The Font
  Protected font.l                                            ;Windows Font ID
  Protected oldfont.l                                         ;Used For Good House Keeping
  
  If bold : bold=#FW_BOLD : Else : bold=#FW_NORMAL : EndIf ;font weight
  If symbol : symbol=#SYMBOL_CHARSET : Else : symbol=#ANSI_CHARSET : EndIf ;character set
  
  base=glGenLists_(96) ;Storage For 96 Characters
  
  ;CreateFont_(Height, Width, Angle Of Escapement, Orientation Angle, Weight, Italic, Underline, Strikeout, Character Set, Output Precision, Clipping Precision, Output Quality, Family And Pitch, Name)
  font=CreateFont_(-height,0,0,0,bold,italic,#False,#False,symbol,#OUT_TT_PRECIS,#CLIP_DEFAULT_PRECIS,#ANTIALIASED_QUALITY,#FF_DONTCARE | #DEFAULT_PITCH,name)
  
  oldfont=SelectObject_(hDC,font) ;Selects The Font We Want
  wglUseFontBitmaps_(hDC,32,96,base) ;Builds 96 Characters Starting At Character 32
  SelectObject_(hDC,oldfont)         ;reselect the old font again
  DeleteObject_(font)                ;Delete The Font
  
EndProcedure

Procedure KillFont() ;Delete The Font List
  
  glDeleteLists_(base,96) ;Delete All 96 Characters
  
EndProcedure

Procedure glPrint(text.s) ;Custom GL "Print" Routine
  
  If text="" ;If There's No Text
    ProcedureReturn #False ;Do Nothing
  EndIf
  
  glPushAttrib_(#GL_LIST_BIT) ;Pushes The Display List Bits
  glListBase_(base-32)        ;Sets The Base Character to 32
  glCallLists_(Len(text),#GL_UNSIGNED_BYTE,text) ;Draws The Display List Text
  glPopAttrib_()                                 ;Pops The Display List Bits
  
EndProcedure

Procedure.l InitGL() ;All Setup For OpenGL Goes Here
  
  ;BuildFont(name,height,bold,italic,symbol)
  BuildFont("Courier New",24,1,0,0) ;Build The Font
  
  glShadeModel_(#GL_SMOOTH) ;Enable Smooth Shading
  glClearColor_(0.0,0.0,0.0,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
  
  ProcedureReturn #True ;Initialization Went OK
  
EndProcedure

InitGL()

glEnable_(#GL_POINT_SMOOTH);
glHint_(#GL_POINT_SMOOTH_HINT, #GL_NICEST);
glEnable_(#GL_LINE_WIDTH)
glEnable_(#GL_LINE_SMOOTH);
glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)

Repeat
  
  event = WindowEvent()
  
  rot+1
  ;glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) 
  
  glMatrixMode_(#GL_PROJECTION)
  glLoadIdentity_()
  ;gluOrtho2D_(-1, 1, -1, 1)
  ;gluOrtho2D_(-2, 2, -2, 2)
  ;glOrtho_(-1,1,-1,1,-1,1)
  glOrtho_(-2,2,-2,2,-2,2)
  ;glOrtho_(-3,3,-3,3,-3,3)
  gluPerspective_(75.0, 1, 0.1, 100.0) ; essential to see the text
  glMatrixMode_(#GL_MODELVIEW)
  ;============================================
  
  glLoadIdentity_()
  glPushMatrix_()
  ;glTranslatef_(0, 0, -1)
  DrawCurve()
  glPopMatrix_()
  ;----------------------------------------------
  glPushMatrix_()
  glLoadIdentity_()
  glTranslatef_(0.0,9.0,-9) ;Move 10 Units Into The Screen
  
  glColor3f_(0,0,1)
  glRasterPos2f_(-8, 1)
  glPrint("Active OpenGL Text With NeHe -                              ") ;Print GL Text To The Screen
  
  glColor3f_(1,0,1)
  glRasterPos2f_(-12,3) ;position text 2 across, -3 down
  glPrint(Str(swidth)+"x"+Str(sheight)+"        ") ;print window size at 2,-3
  
  glColor3f_(1,0,0)
  glRasterPos2f_(-12, -9)
  glPrint("-1  ")
  
  glPopMatrix_()
  
  Delay(10)
  
  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Until event = #PB_Event_CloseWindow
KillFont()


Procedure FillCurveData()
  Protected.l b = 0
  Protected.f x, y, inc, t ,q
  
  inc = 0.001
  t.f = -1
  While t <= 2*#PI
    ;Cayley's_sextic curve parametric equations
    x.f = Pow(Cos(t),3)* Cos(3*t)
    y.f = Pow(Cos(t),3)* Sin(3*t)
    
    
    vert(b)\x = x ; position
    vert(b)\y = y
    
    vert(b)\r = 1 ; color red
    vert(b)\g = 0
    vert(b)\b = 0
    
    t + inc
    b + 1
    
  Wend
  
  ReDim vert.vertices(b)
  
  SetWindowTitle(0, Str(b)+" vertices in the Red curve")
  
EndProcedure

Procedure FillCircleData()
  Protected b = 0
  Protected.f x, y, inc, t
  inc = 0.001
  
  While t<=2*#PI
    vert2(b)\r = 1 :vert2(b)\g = 1 :vert2(b)\b = 0
    ; Circle parametric equations
    vert2(b)\x = Cos(t)*1
    vert2(b)\y = Sin(t)*1 
    
    t + inc
    b + 1
    
  Wend
  
  
  ReDim vert2.vertices(b)
  
EndProcedure

Procedure AxisData()
  
  ;Data For the perpendicular coordinates x-y
  ; Horizontal Blue Line
  vert3(0)\r = 1.0 :vert3(0)\g = 0.6 : vert3(0)\b = 0
  vert3(0)\x = -#PI: vert3(0)\y = 0
  vert3(1)\r = 1.0 :vert3(1)\g = 0.6 : vert3(1)\b = 0
  vert3(1)\x = #PI: vert3(1)\y = 0
  
  ;Vertical Blue Line
  vert3(2)\r = 1 :vert3(2)\g = 0.6 : vert3(2)\b = 0
  vert3(2)\x = 0: vert3(2)\y = -#PI
  vert3(3)\r = 1 :vert3(3)\g = 0.6 : vert3(3)\b = 0
  vert3(3)\x = 0: vert3(3)\y = #PI
EndProcedure

Procedure drawGrid(width.f, height.f, grid_width.f)
  ;horizontal lines
  ;For(float i=-height; i<height; i+=grid_width){
  Protected b=0
  Protected.f i = -height
  While i < height
    ;Vertex v1 = {-width, i, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
    v1\x = -width: v1\y= i
    v1\r=1 : v1\g=1: v1\b =1 : v1\a = 1
    ;Vertex v2 = {width, i, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
    v2\x = width: v2\y= i
    v2\r=1 : v2\g=1: v2\b =1 : v2\a = 1
    
    ;drawLineSegment(@v1, @v2, 1)
    vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
    vert4(b)\x = v1\x
    vert4(b)\y = v1\y
    b+1
    vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
    vert4(b)\x = v2\x
    vert4(b)\y = v2\y
    
    b + 1;
    i + grid_width
  Wend
  
  ;vertical lines
  ;for(float i=-width; i<width; i+=grid_width){
  i.f = -width
  
  While i < width
    ;Vertex v1 = {i, -height, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
    v1\x = i: v1\y= -height
    v1\r=1 : v1\g=1: v1\b =1 : v1\a = 1
    ;Vertex v2 = {i, height, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
    v2\x = i: v2\y= height
    v2\r=1 : v2\g=1: v2\b =1 : v2\a = 1
    
    ;drawLineSegment(@v1, @v2, 1)
    vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
    vert4(b)\x = v1\x
    vert4(b)\y = v1\y
    b+1
    vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
    vert4(b)\x = v2\x
    vert4(b)\y = v2\y
    
    i + grid_width
    b+1
  Wend
  ReDim vert4.vertices(b)
EndProcedure

Procedure DrawCurve()
  ;SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)
  ;glClearColor_(0.2, 0.2, 0.1, 1.0) ; background color
  glClearColor_(0.5, 0.8, 0.9, 0.0) ; background color
  gluLookAt_(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  glEnableClientState_(#GL_VERTEX_ARRAY)
  glEnableClientState_(#GL_COLOR_ARRAY)
  
  
  glDisable_(#GL_LINE_SMOOTH)
  ;draw the Grid
  glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert4(0)\x)
  glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert4(0)\r)
  glLineWidth_(1)
  glDrawArrays_(#GL_LINES, 0, ArraySize(vert4()))
  
  glEnable_(#GL_LINE_WIDTH)
  glEnable_(#GL_LINE_SMOOTH);
  glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
  ;plot the perpendicular x-y Lines
  glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert3(0)\x)
  glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert3(0)\r)
  glLineWidth_(2)
  glDrawArrays_(#GL_LINES, 0, ArraySize(vert3()))
  
  ;circle
  glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert2(0)\x)
  glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert2(0)\r)
  glLineWidth_(2)
  glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vert2()))
  ;glDrawArrays_(#GL_LINES, 0, ArraySize(vert2()))
  ;glDrawArrays_(#GL_POINTS, 0, ArraySize(vert2()))
  
  
  ro+1
  glRotatef_(ro, 0, 0, 1)
  ;Cayley's_sextic Curve
  glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert(0)\x)
  glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert(0)\r)
  glLineWidth_(1)
  glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vert()))
  ;glDrawArrays_(#GL_POINTS, 0, ArraySize(vert()))
  
  glDisableClientState_(#GL_COLOR_ARRAY)
  glDisableClientState_(#GL_VERTEX_ARRAY)
  
EndProcedure
Dernière modification par kernadec le mar. 10/déc./2019 20:42, modifié 1 fois.
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: OpenGL le mode Ecritures

Message par Micoute »

Merci beaucoup pour le partage.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Répondre