Changement d'échelle raté en OpenGL [Résolu]

Programmation avancée de jeux en PureBasic
Avatar de l’utilisateur
SPH
Messages : 4723
Inscription : mer. 09/nov./2005 9:53

Changement d'échelle raté en OpenGL [Résolu]

Message par SPH »

Salut,

J'ai attaqué aujourd'hui le "moteur" de rendu de mes polygones. Sur mon ordi, je suis en 1920/1080
J'ai bidouiller une echelle (xf.F et yf.f) pour que les polygones s'adaptent a la taille d'ecran de bureau. Mais voilà, ca ne s'adapte pas (si vous n'etes pas en 1920/1080).

; a la ligne 29 et 30, creation d'une echelle de grandeur
; a la ligne 175, utilisation de cette echelle (mais ca ne marche pas)

Les polygones (et le code ci dessous) se trouvent dans ce fichier : http://xmas.free.fr/polygones.zip

http://xmas.free.fr/lion_another_world.bmp

Code : Tout sélectionner

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  SPH(2021) ;; Polygons_Run_3 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;EnableExplicit

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration

;These two GL constants are used for texture creation. Don't change their values.
#GL_BGR = $80E0
#GL_BGRA = $80E1

If ExamineDesktops()
  ddw=DesktopWidth(0)
  ddh=DesktopHeight(0)
Else
  ddw=1024
  ddh=768
EndIf

;   ddw=1024
;   ddh=768


xf.f=1920/ddw
yf.f=1080/ddh

; a la ligne 29 et 30, creation d'une echelle de grandeur
; a la ligne 175, utilisation de cette echelle


;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Polygons_Maker",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh)
If screenGL=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf


SetupOpenGL()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter

;;;;;;;;;;;;;;;;;;;
;chemin$="f:\AnotherEarth\"
file$="22.pol"
;;;;;;;;;;;;;;;

glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************

If FileSize(chemin$+file$)=-1
  Beep_(500,200)
Else
  If ReadFile(1, chemin$+file$)
    long=Lof(1)/4
    Dim poly_mx2(long)
    Dim poly_my2(long)
    For i=1 To long
      poly_mx2(i-1)=ReadWord(1)
      poly_my2(i-1)=ReadWord(1)
      ;     Debug ("poly_mx2() = "+Str(poly_mx2(i-1))+"  -  "+"poly_my2() = "+Str(poly_my2(i-1)))
    Next
    CloseFile(1)
  EndIf
EndIf


;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
timer=ElapsedMilliseconds()

;-MAIN REPEAT
Repeat ;- ..... Repeat
  
 ; glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
;##############################################
;##############################################
;##############################################

  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
          Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
          ;;;;;;;          
          Select EventType()
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_LeftClick
              End
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            Case #PB_EventType_RightDoubleClick 
              End
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_RightClick
              End
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_MiddleButtonDown
              End
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
          EndSelect
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
        Case 666
          End
      EndSelect
  EndSelect
  
;##############################################
;##############################################
;##############################################

  nb2=0
  ii=0
  While poly_my2(nb2)>0
    ii=poly_my2(nb2)
    alpha2.f=poly_mx2(nb2+4)/32767
    glBegin_(#GL_POLYGON)
    If alpha2=1
      glBlendFunc_(#GL_SRC_ALPHA,0)
      glColor4f_(poly_mx2(nb2+1)/32767,poly_mx2(nb2+2)/32767,poly_mx2(nb2+3)/32767,1)
    Else
      glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
      glColor4f_(poly_mx2(nb2+1)/32767,poly_mx2(nb2+2)/32767,poly_mx2(nb2+3)/32767,alpha2)
    EndIf
    
    For i=1 To poly_my2(nb2)-5
      glVertex2f_(poly_mx2(nb2+4+i)/xf.f,poly_my2(nb2+4+i)/yf.f)
    Next
    glEnd_()
    nb2+ii
  Wend
  
  ;========================================================
  ;=======================================================
  
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
ForEver

;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*********************************************************************************************************************************
;*************************************************************** ******************************************************************
;*********************************************************************************************************************************
;************************************************************************* *******************************************************
;*********************************************************************************************************************************

Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure

Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure

Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure

Dernière modification par SPH le ven. 22/janv./2021 10:51, modifié 1 fois.
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.00 - 64 bits
Avatar de l’utilisateur
venom
Messages : 3072
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: changement d'echelle raté en OpenGL

Message par venom »

Chez moi l'image fait tout mon écran (1366x768). C'est quoi le souci ?






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
SPH
Messages : 4723
Inscription : mer. 09/nov./2005 9:53

Re: changement d'echelle raté en OpenGL

Message par SPH »

venom a écrit :Chez moi l'image fait tout mon écran (1366x768). C'est quoi le souci ?
Tu me confirmes qu'en executant mon code, ca te donne cette image : http://xmas.free.fr/lion_another_world.bmp
:?:
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.00 - 64 bits
Avatar de l’utilisateur
venom
Messages : 3072
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: changement d'echelle raté en OpenGL

Message par venom »

oui oui cette image au complet :wink:






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
SPH
Messages : 4723
Inscription : mer. 09/nov./2005 9:53

Re: changement d'echelle raté en OpenGL

Message par SPH »

Alors, c'est une excellente chose

Merci pour ton test
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.00 - 64 bits
Répondre