programme opengl pour faire de la 2d
Publié : mar. 23/janv./2024 14:11
bonjour
depuis quelque temps je travail sur opengl , mon but est de faire de la géométrie shader.
pour le moment voici se que j’arrive a faire , malheureusement ce n'est pas encore en opengl moderne( shader ).
le programme est divisé en 4 parties ( cala permet de simplifier l'ajout de commandes)
partie 1 : les commandes de bases qui servent a toutes les autres commandes (couleur , position ...)
partie 2 : l'affichage des polygones
partie 3 : test la position de la sourie
partie 4 : les commandes une a une ( tracer de lignes , cercles ...)
pour fonctionne vous devez :
ouvrir une fentre avec la commande : OpenWindow2d( longueur , hauteur , option )
toutes les commandes doivent etre entre "draw_start()" et "draw_stop()"
les fontes et les texture doivent etres charger apres la commande "OpenWindow2d"
voici quelques commandes de bases :
color(rgb) : la couleur du tracer
alpha(alpha) : transparence du tracer
size() ; épaisseur des ligne
angle(x,y,z) ; rotation
stroke(x) ; trace le contour des polygones ( mais pas des lignes)
LineStipple() : trace les lignes en pointillé
full(0/1) : trace un polygone vide ou plien
origin : "déplace les coordonnées de tracage"
size : zoom / "dézoom" le tracage
...
les commandes ajoutés ( partie 4 )
- lignes :
draw_linexy (comme linexy de purebasic)
draw_line (comme line de purebasic , pas vraiment testé)
draw_linev ( trace une ligne verticale )
draw_lineh ( trace une ligne horizontale )
draw_line_list ( trace une suite de lignes , le dernier point de la premiere ligne est le premier point de la dexieme , commande à simplifier )
- draw_sprite
- text : ( pas testé )
loadfont_ ( charge une fonte , nom de commande a changer )
draw_text
- cercle
draw_circle
draw_ellipse
- rectangle
draw_quad
draw_rect
draw_square
- draw_rect_round
par défaut, dans "opengl draw v8 .pbi" (partie 1), a la fin du fichier, seule la commande "includefile commandes\opengl_draw_line.pbi" est compilé
pour les commandes à venir, enlevez le point-virgule devant la ligne "includefile" de la commande correspondante.
partie 1
"opengl draw v8.pbi"
partie 2
"opengl_rotate.pbi"
partie 3
"opengl_point_in_polygon.pbi"
partie 4
"opengl_draw_line.pbi"
code demo pour le tracer de lignes
depuis quelque temps je travail sur opengl , mon but est de faire de la géométrie shader.
pour le moment voici se que j’arrive a faire , malheureusement ce n'est pas encore en opengl moderne( shader ).
le programme est divisé en 4 parties ( cala permet de simplifier l'ajout de commandes)
partie 1 : les commandes de bases qui servent a toutes les autres commandes (couleur , position ...)
partie 2 : l'affichage des polygones
partie 3 : test la position de la sourie
partie 4 : les commandes une a une ( tracer de lignes , cercles ...)
pour fonctionne vous devez :
ouvrir une fentre avec la commande : OpenWindow2d( longueur , hauteur , option )
toutes les commandes doivent etre entre "draw_start()" et "draw_stop()"
les fontes et les texture doivent etres charger apres la commande "OpenWindow2d"
voici quelques commandes de bases :
color(rgb) : la couleur du tracer
alpha(alpha) : transparence du tracer
size() ; épaisseur des ligne
angle(x,y,z) ; rotation
stroke(x) ; trace le contour des polygones ( mais pas des lignes)
LineStipple() : trace les lignes en pointillé
full(0/1) : trace un polygone vide ou plien
origin : "déplace les coordonnées de tracage"
size : zoom / "dézoom" le tracage
...
les commandes ajoutés ( partie 4 )
- lignes :
draw_linexy (comme linexy de purebasic)
draw_line (comme line de purebasic , pas vraiment testé)
draw_linev ( trace une ligne verticale )
draw_lineh ( trace une ligne horizontale )
draw_line_list ( trace une suite de lignes , le dernier point de la premiere ligne est le premier point de la dexieme , commande à simplifier )
- draw_sprite
- text : ( pas testé )
loadfont_ ( charge une fonte , nom de commande a changer )
draw_text
- cercle
draw_circle
draw_ellipse
- rectangle
draw_quad
draw_rect
draw_square
- draw_rect_round
par défaut, dans "opengl draw v8 .pbi" (partie 1), a la fin du fichier, seule la commande "includefile commandes\opengl_draw_line.pbi" est compilé
pour les commandes à venir, enlevez le point-virgule devant la ligne "includefile" de la commande correspondante.
partie 1
"opengl draw v8.pbi"
Code : Tout sélectionner
;-------------------------------------
;- ----- VARIABLES
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseGIFImageDecoder()
;#GL_COLOR_BUFFER_BIT = $00004000
;#GL_DEPTH_BUFFER_BIT = $00000100
;#GL_ARRAY_BUFFER = $8892
;#GL_ELEMENT_ARRAY_BUFFER = $8893
;#GL_MODELVIEW = $1700
;#GL_PROJECTION = $1701
;#GL_SMOOTH = $1D01
;#GL_DEPTH_TEST = $0B71
;#GL_CULL_FACE = $0B44
;#GL_STATIC_DRAW = $88E4
;#GL_VERTEX_ARRAY = $8074
;#GL_FLOAT = $1406
;#GL_TRIANGLES = $0004
;#GL_UNSIGNED_BYTE = $1401
;#GL_UNSIGNED_SHORT = $1403
;#GL_UNSIGNED_INT = $1405
;Prototype PfnGlGenBuffersProc(n.i, *buffers)
;Global glGenBuffers.PfnGlGenBuffersProc
;glGenBuffers = wglGetProcAddress_("glGenBuffers")
;Prototype PfnGlBindbufferProc(target.l, buffer.i)
;Global glBindBuffer.PfnGlBindbufferProc
;glBindBuffer = wglGetProcAddress_("glBindBuffer")
;Prototype PfnGlBufferDataProc(target.l, size.i, *Data_, usage.l)
;Global glBufferData.PfnGlBufferDataProc
;glBufferData = wglGetProcAddress_( "glBufferData" )
Define i
Global Dim si.f(1023)
Global Dim co.f(1023)
#angleconv=1024.0/2.0/#PI
For i=0 To 1023
si(i)=Sin(i/#angleconv)
co(i)=Cos(i/#angleconv)
Next
;EnableExplicit
Procedure.f fcos(x.f)
Protected i
i=x*#angleconv
i & 1023
ProcedureReturn co(i)
EndProcedure
Procedure.f fsin(x.f)
Protected i
i=x*#angleconv
i & 1023
ProcedureReturn si(i)
EndProcedure
Global Dim rcos.f(500)
Global Dim rsin.f(500)
For i = 0 To 499
rCos(i) = Cos(Radian(i))
rSin(i) = Sin(Radian(i))
Next
Global Dim vert.Vector3(4096)
Structure vertices_tex
x.f
y.f
EndStructure
Global Dim verttex.vertices_tex(4096)
Global Dim pos_in.point(4096)
Global Dim pos_out.point(4096)
Macro vert2(va,vx,vy)
vert( va )\x = vx
vert( va )\y = vy
vert( va )\z = 0
EndMacro
Macro vert3(va,vx,vy,vz)
vert( va )\x = vx
vert( va )\y = vy
vert( va )\z = vz
EndMacro
Structure gl_liste
x.f
y.f
z.f
EndStructure
Global BuffId.i,iiId.i
;- -precalcul : texture
Structure texture
x.f
y.f
EndStructure
Global Dim vect_texture.texture(90)
For i = 0 To 89
vect_texture(i)\x = rCos((i * 4)) * 0.5 + 0.5
vect_texture(i)\y = rSin((i * 4)) * 0.5 - 0.5
Next
;- -precalcul : cercle
Global Dim vect_circle.Vector3(90)
For i = 0 To 89
vect_circle(i)\x = rCos((i * 4))
vect_circle(i)\y = rSin((i * 4))
vect_circle(i)\z = 0
Next
Global gl_shader$
Global screen_scale_x.f
Global screen_scale_y.f
Global gl_id = 1
Global gl_mousex = 0
Global gl_mousey = 0
Global gl_fps_count
Global gl_fps_time
Global gl_fps
Global gl_LineStipple_factor.l = 0
Global gl_LineStipple_pattern.l = 0
Global gl_size.f = 1
Global gl_test = 1
Global gl_loop
Global gl_Translationx.f = 0
Global gl_Translationy.f = 0
;Global gl_point_positionx = 0
;Global gl_point_positiony = 0
Global gl_point_rotatex.f = 0
Global gl_point_rotatey.f = 0
; -----------------
Global gl_full = 0
Global gl_originx.f = 0
Global gl_originy.f = 0
Global gl_angle_x.f = 0
Global gl_angle_y.f = 0
Global gl_angle_z.f = 0
Global gl_angle_xyz.i = 0
Global gl_alpha.l = 255
Global gl_color_r.b = $7f
Global gl_color_g.b = $7f
Global gl_color_b.b = $7f
Global gl_font = 0
Global gl_font_size = 8
Global gl_scalex.f = 1
Global gl_scaley.f = 1
Global gl_stroke = -1
Global gl_stroke_r = 0
Global gl_stroke_g = 0
Global gl_stroke_b = 0
Global gl_stroke_size = 1
Global gl_text_sizex.f = 16
Global gl_text_sizey.f = 16
;-------------------------------------
;- -----COMMANDES
;-------------------------------------
Macro point_set_rotate(x,y)
gl_point_rotatex = x
gl_point_rotatey = y
EndMacro
Macro gl_Translation(x,y)
gl_Translationx = x
gl_Translationy = y
EndMacro
Macro LineStipple(v1,v2 = 1)
If v1 <> 0
glEnable_(#GL_LINE_STIPPLE)
glLineStipple_(v1,v2)
gl_LineStipple_factor = 1
Else
glDisable_(#GL_LINE_STIPPLE)
gl_LineStipple_factor = 0
EndIf
EndMacro
Macro loop(var)
gl_loop = var
EndMacro
Macro full(var)
gl_full = var
EndMacro
;**********************************************
;- --- couleurs ---
Macro Color(rgb)
gl_color_r = (rgb >> 16) & $ff
gl_color_g = (rgb >> 8) & $ff
gl_color_b = (rgb & $ff)
EndMacro
Macro Color_rgb(r,g,b)
gl_color_r = r
gl_color_g = g
gl_color_b = b
EndMacro
Macro Color_rgba(rgba)
gl_alpha = a
gl_color_r = r
gl_color_g = g
gl_color_b = b
EndMacro
Macro Alpha(a)
gl_alpha = a
EndMacro
;**********************************************
;- --- angles ---
Macro angle(x,y,z)
gl_angle_x = x
gl_angle_y = y
gl_angle_z = z
If gl_angle_x = 0 And gl_angle_y = 0 And gl_angle_z = 0
gl_angle_xyz = 0
Else
gl_angle_xyz = 1
EndIf
EndMacro
Macro angleX(x)
gl_angle_x = x
If gl_angle_x = 0 And gl_angle_y = 0 And gl_angle_z = 0
gl_angle_xyz = 0
Else
gl_angle_xyz = 1
EndIf
EndMacro
Macro angleY(y)
gl_angle_y = y
If gl_angle_x = 0 And gl_angle_y = 0 And gl_angle_z = 0
gl_angle_xyz = 0
Else
gl_angle_xyz = 1
EndIf
EndMacro
Macro angleZ(z)
gl_angle_z = z
If gl_angle_x = 0 And gl_angle_y = 0 And gl_angle_z = 0
gl_angle_xyz = 0
Else
gl_angle_xyz = 1
EndIf
EndMacro
;**********************************************
Macro stroke(rgb)
gl_stroke_r = (rgb >> 16) & $ff
gl_stroke_g = (rgb >> 8) & $ff
gl_stroke_b = (rgb & $ff)
gl_stroke = rgb
EndMacro
Macro no_stroke()
gl_stroke = -1
EndMacro
Macro stroke_size(var)
gl_stroke_size = var
EndMacro
Macro size(a)
gl_size = a
glLineWidth_(a)
EndMacro
Macro test(a)
gl_test = a
EndMacro
Macro Origin(x,y)
gl_originx = x
gl_originy = y
EndMacro
Macro scale(var)
gl_scalex = var
gl_scaley = var
EndMacro
Macro scalex(x)
gl_scalex = x
EndMacro
Macro scaley(y)
gl_scaley = y
EndMacro
Macro scalexy(x,y)
gl_scalex = x
gl_scaley = y
EndMacro
Macro text_size(x,y)
gl_text_sizex = x
gl_text_sizey = y
EndMacro
Procedure cls(rgb)
Protected r,g,b , r1.f , b1.f , g1.f
r = (rgb >> 16) & 255
g = (rgb >> 8) & 255
b = (rgb & 255)
r1.f = r / 255
g1.f = g / 255
b1.f = b / 255
glClearColor_(r1,g1,b1,0.0)
EndProcedure
;-- ---------------------------
Macro draw_Start()
gl_mousex = WindowMouseX(0)
gl_mousey = WindowHeight(0 , #PB_Window_InnerCoordinate) - WindowMouseY(0)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT | #GL_STENCIL_BUFFER_BIT )
glVertexPointer_(3, #GL_FLOAT, 0, @vert())
glEnableClientState_(#GL_VERTEX_ARRAY)
EndMacro
Macro draw_stop()
glDisableClientState_(#GL_VERTEX_ARRAY)
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True) ; copie l'ecran "opengl" dans la fenetre "0"
If ElapsedMilliseconds()>gl_fps_time
gl_fps = gl_fps_count
gl_fps_count = 0
gl_fps_time = ElapsedMilliseconds() + 1000
EndIf
gl_fps_count = gl_fps_count + 1
EndMacro
;-- ----------------------------
Procedure window_resize()
FreeGadget(0)
OpenGLGadget(0,0,0,WindowWidth(0) ,WindowHeight(0) ,#PB_OpenGL_Keyboard | #PB_OpenGL_NoFlipSynchronization )
glLoadIdentity_()
glOrtho_(0, screen_scale_x , 0, screen_scale_y , -1, 1)
glMatrixMode_(#GL_PROJECTION)
glEnable_(#GL_BLEND)
EndProcedure
Procedure LoadTextures_(t$)
LoadImage(0,GetCurrentDirectory() + t$)
If Not IsImage(0) : Debug "image non charger" : ProcedureReturn 0 : EndIf
StartDrawing(ImageOutput(0))
MemoryAddress = DrawingBuffer()
Depth = ImageDepth(0)
ImageW = ImageWidth(0)
ImageH = ImageHeight(0)
StopDrawing()
StartDrawing(WindowOutput(0))
DrawImage(ImageID(0),0,0)
StopDrawing()
Delay(10)
Define.i TextureHandle
glGenTextures_(1, @TextureHandle)
glBindTexture_(#GL_TEXTURE_2D, TextureHandle)
If Depth = 32
glTexImage2D_(#GL_TEXTURE_2D, 0, 4, ImageW, ImageH, 0, #GL_BGRA_EXT, #GL_UNSIGNED_BYTE, MemoryAddress)
EndIf
If Depth = 24
glTexImage2D_(#GL_TEXTURE_2D, 0, 3, ImageW, ImageH, 0, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, MemoryAddress)
EndIf
glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MAG_FILTER,#GL_NEAREST)
glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MIN_FILTER,#GL_NEAREST)
FreeImage(0)
ProcedureReturn TextureHandle
EndProcedure
Procedure OpenWindow2d(lg,ht,opt)
screen_scale_x = lg
screen_scale_y = ht
OpenWindow(0,0,0,lg,ht,"OpenGL",opt )
OpenGLGadget(0,0,0,lg,ht,#PB_OpenGL_Keyboard | #PB_OpenGL_NoFlipSynchronization )
glVertexPointer_(3, #GL_FLOAT, 0, @vert())
;BindEvent(#PB_Event_SizeWindow,@window_resize())
glLoadIdentity_()
glOrtho_(0, lg, 0, ht, -1000, 1000)
glMatrixMode_(#GL_MODELVIEW)
;glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT | #GL_STENCIL_BUFFER_BIT )
glBlendFunc_(#GL_SRC_ALPHA , #GL_ONE_MINUS_SRC_ALPHA)
;glEnable_(#GL_STENCIL_TEST)
;glStencilMask_($FF)
;glStencilFunc_(#GL_EQUAL, 1, $FF)
;glColorMask_(1,1,1,1)
;glStencilFunc_(#GL_ALWAYS,1,1);
;glStencilOp_(#GL_REPLACE,#GL_KEEP,#GL_REPLACE )
;glStencilOp_(#GL_KEEP,#GL_KEEP,#GL_REPLACE)
glEnable_(#GL_BLEND) ; couleur de transparence
glDisable_(#GL_DEPTH_TEST); ; desactiver le test de profondeur
;glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
;glEnable_(#GL_LINE_SMOOTH)
;glShadeModel_(#GL_SMOOTH)
;glEnable_(#GL_LIGHT0)
;glEnable_(#GL_LIGHTING)
;glEnable_(#GL_COLOR_MATERIAL)
;glEnable_(#GL_DEPTH_TEST) ; option pour le rendu en profondeur : gl_plane
;glBlendFunc_(#GL_ZERO, #GL_ONE)
;glPolygonMode_(#GL_FRONT_AND_BACK, #GL_FILL)
;glEnable_(#GL_CULL_FACE)
;glCullFace_(#GL_FRONT_AND_BACK);
;glCullFace_(#GL_FRONT);
;glCullFace_(#GL_BACK);
;glFrontFace_(#GL_CW) ;
;glEnable_(#GL_TEXTURE_2D)
;glEnable_(#GL_ALPHA_TEST)
;glShadeModel_ (#GL_SMOOTH)
;glEnable_(#GL_LIGHTING);
;glEnable_(#GL_LIGHT0) ;
;glEnable_(#GL_DEPTH_TEST);
;glColorMaterial_(#GL_FRONT, #GL_AMBIENT_AND_DIFFUSE)
;glEnable_(#GL_COLOR_MATERIAL) ; color tracking
;glEnable_(#GL_NORMALIZE)
;glEnable_(#GL_LINE_SMOOTH);
;glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
; selectionne la zone d'affichage
;glEnable_(#GL_SCISSOR_TEST)
;glScissor_(100,100,800,600)
;glGenBuffers = wglGetProcAddress_("glGenBuffers")
;glBindBuffer = wglGetProcAddress_("glBindBuffer")
;glBufferData = wglGetProcAddress_("glBufferData")
;glEnableVertexAttribArray = wglGetProcAddress_("glEnableVertexAttribArray")
;glVertexAttribPointer = wglGetProcAddress_("glVertexAttribPointer")
;glDisableVertexAttribArray = wglGetProcAddress_("glDisableVertexAttribArray")
;glDrawArrays = wglGetProcAddress_("glDrawArrays")
;glGenBuffers(1, @vbox)
;glBindBuffer(#GL_ARRAY_BUFFER, vbox)
;glBufferData(#GL_ARRAY_BUFFER,1024*4,@vert(), #GL_STATIC_DRAW)
;glBindBuffer(#GL_ARRAY_BUFFER,0)
;glPolygonMode_(#GL_FRONT_AND_BACK,#GL_LINE)
;glGenBuffers = wglGetProcAddress_("glGenBuffers")
;glBindBuffer = wglGetProcAddress_("glBindBuffer")
;glBufferData = wglGetProcAddress_("glBufferData")
;glBufferSubData = wglGetProcAddress_("glBufferSubData")
;glGenVertexArrays = wglGetProcAddress_("glGenVertexArrays")
;glBindVertexArray = wglGetProcAddress_("glBindVertexArray")
;glEnableVertexAttribArray = wglGetProcAddress_("glEnableVertexAttribArray")
;glVertexAttribPointer = wglGetProcAddress_("glVertexAttribPointer")
;glDrawArrays = wglGetProcAddress_("glDrawArrays")
;glIndexPointer = wglGetProcAddress_("glIndexPointer")
;glDeleteBuffers = wglGetProcAddress_("glDeleteBuffers")
;glDeleteVertexArrays = wglGetProcAddress_("glDeleteVertexArrays")
;glDisableVertexAttribArray = wglGetProcAddress_("glDisableVertexAttribArray")
;glMapBuffer = wglGetProcAddress_("glMapBuffer")
;glUnmapBuffer = wglGetProcAddress_("glUnmapBuffer")
;glActiveTexture = wglGetProcAddress_("glActiveTexture")
;glUniformMatrix4fv=wglGetProcAddress_("glUniformMatrix4fv")
;glCreateShader = wglGetProcAddress_("glCreateShader")
;glCreateProgram = wglGetProcAddress_("glCreateProgram")
;glCompileShader = wglGetProcAddress_("glCompileShader")
;glDeleteShader = wglGetProcAddress_("glDeleteShader")
;glLinkProgram = wglGetProcAddress_("glLinkProgram")
;glUseProgram = wglGetProcAddress_("glUseProgram")
;glAttachShader = wglGetProcAddress_("glAttachShader")
;glShaderSource = wglGetProcAddress_("glShaderSource")
;glGetUniformLocation = wglGetProcAddress_("glGetUniformLocation")
;glUniform1i = wglGetProcAddress_("glUniform1i")
;glUniform2i = wglGetProcAddress_("glUniform2i")
;glUniform1f = wglGetProcAddress_("glUniform1f")
;glUniform2f = wglGetProcAddress_("glUniform2f")
;glGetShaderInfoLog = wglGetProcAddress_("glGetShaderInfoLog")
EndProcedure
IncludeFile "commandes\opengl_rotate.pbi" ; toutes les commades draw depandes de celle-ci . a mettre avec les fichiers 'draw'
IncludeFile "commandes\opengl_point_in_polygon.pbi" ; toutes les commades draw depandes de celle-ci . a mettre avec les fichiers 'draw'
IncludeFile "commandes\opengl_draw_line.pbi" ; trace des lignes droites
;IncludeFile "commandes\opengl_draw_quad.pbi" ; trace des quad.
;IncludeFile "commandes\opengl_draw_circle.pbi" ; trace des cercles (et ovals)
;IncludeFile "commandes\opengl_draw_polyreg.pbi" ; trace des polygones regulier
;IncludeFile "commandes\opengl_draw_quad_round.pbi" ; trace des quad. arrondi
;IncludeFile "commandes\opengl_draw_triangle.pbi" ; trace des triangles
;IncludeFile "commandes\opengl_draw_polygon.pbi" ; trace des polygones
;IncludeFile "commandes\opengl_draw_grid.pbi" ; trace une grille
;IncludeFile "commandes\opengl_draw_arc.pbi" ; trace des arcs de cercle (et ovals)
;IncludeFile "commandes\opengl_draw_text.pbi" ; affiche tu text
;IncludeFile "commandes\opengl_draw_sprite.pbi" ; affiche des sprites
;IncludeFile "opengl_draw_bezier.pbi" ; trace des lignes de bezier
;IncludeFile "opengl_draw_star.pbi" ; trace des etoiles
;IncludeFile "opengl_draw_hypocycloid.pbi"; trace des hypocycloids
;IncludeFile "opengl_draw_heart.pbi" ; trace des coeurs
;IncludeFile "opengl_draw_astroid.pbi" ; trace des astroids
;IncludeFile "opengl_draw_deltoid.pbi" ; trace des deltoids
;IncludeFile "opengl_draw_Epicychoid.pbi" ; trace des Epicychoids
;IncludeFile "opengl_draw_conchoid.pbi" ; trace des conchoids
;IncludeFile "opengl_draw_super_formula.pbi" ; trace des superformula
partie 2
"opengl_rotate.pbi"
Code : Tout sélectionner
Macro cal_pos_ray(px,py,lg,ht)
ray = (lg * lg * gl_scalex) + (ht * ht * gl_scaley)
EndMacro
Macro macro_gl2d_draw_stroke(gl_count)
If gl2d_draw_stroke = 2 : ProcedureReturn : EndIf
glLineWidth_(gl_stroke_size)
If gl_stroke <> -1
glColor4ub_( gl_stroke_r , gl_stroke_g , gl_stroke_b , 255 )
If gl2d_draw_stroke = 0
glDrawArrays_(#GL_LINE_LOOP, 0, gl_count)
EndIf
If gl2d_draw_stroke = 1
For gl2d_i = 0 To (gl_count >> 1)
pos_out(gl2d_i)\x = vert((0 + gl2d_i *2 ))\x
pos_out(gl2d_i)\y = vert((0 + gl2d_i *2 ))\y
pos_in(gl2d_i)\x = vert((1 + gl2d_i * 2 ))\x
pos_in(gl2d_i)\y = vert((1 + gl2d_i * 2 ))\y
Next
For gl2d_i = 0 To gl_count >> 1
vert(gl2d_i)\x = pos_out(gl2d_i)\x
vert(gl2d_i)\y = pos_out(gl2d_i)\y
Next
glDrawArrays_(#GL_LINE_LOOP, 0, gl_count >> 1)
For gl2d_i = 0 To gl_count >> 1
vert(gl2d_i)\x = pos_in(gl2d_i)\x
vert(gl2d_i)\y = pos_in(gl2d_i)\y
Next
glDrawArrays_(#GL_LINE_LOOP, 0, gl_count >> 1)
EndIf
EndIf
glLineWidth_(gl_size)
EndMacro
Macro macro_gl_translation(r,s)
For i = 0 To gl_count
vert( i )\x = vert( i )\x + r
vert( i )\y = vert( i )\y + s
Next
EndMacro
Macro macro_gl_rotate(a,b,angle)
sinTheta = fSin(Radian(angle))
cosTheta = fCos(Radian(angle))
For i = 0 To gl_count
a = vert( i )\a
b = vert( i )\b
vert( i )\a = a * cosTheta - b * sinTheta
vert( i )\b = b * cosTheta + a * sinTheta
Next
EndMacro
Macro macro_gl_translate_and_scale(r,s)
For i = 0 To gl_count
vert( i )\x = (gl_scalex * (vert( i )\x + r)) + gl_originx
vert( i )\y = (gl_scaley * (vert( i )\y + s)) + gl_originy
Next
EndMacro
Procedure gl_rotate( gl_count , r.f , s.f , gl_opt , gl2d_draw_stroke = 0)
Protected sinTheta.f , cosTheta.f , i , x.f , y.f , z.f
glColor4ub_( gl_color_r , gl_color_g , gl_color_b , gl_alpha )
macro_gl_translation(-r,-s)
If gl_angle_xyz <> 0
macro_gl_rotate(z,y,gl_angle_x)
macro_gl_rotate(x,z,gl_angle_y)
macro_gl_rotate(x,y,gl_angle_z)
EndIf
macro_gl_translate_and_scale(r,s)
glDrawArrays_(gl_opt, 0, gl_count)
macro_gl2d_draw_stroke(gl_count)
EndProcedure
Macro macro_gl2d_rotate2(a,b,angle,gl_count)
sinTheta = fSin(Radian(angle))
cosTheta = fCos(Radian(angle))
For i = 0 To gl_count - 1
a = vert( i )\a
b = vert( i )\b
vert( i )\a = (a * cosTheta) - (b * sinTheta)
vert( i )\b = (b * cosTheta) + (a * sinTheta)
Next
EndMacro
Macro macro_gl2d_cal_pos(gl_count)
If gl_angle_xyz <> 0
macro_gl2d_rotate2(z,y,gl_angle_x,gl_count)
macro_gl2d_rotate2(x,z,gl_angle_y,gl_count)
macro_gl2d_rotate2(x,y,gl_angle_z,gl_count)
EndIf
For i = 0 To gl_count - 1
vert( i )\x = (gl_scalex * (vert( i )\x * rayx + px1 )) + gl_originx
vert( i )\y = (gl_scaley * (vert( i )\y * rayy + py1 )) + gl_originy
Next
glColor4ub_( gl_color_r , gl_color_g , gl_color_b , gl_alpha )
EndMacro
Procedure gl2d_draw_entity_texture(gl_count , px1.f , py1.f , rayx.f , rayy.f , tex)
Protected x.f , y.f , z.f , sinTheta.f , cosTheta.f , gl2d_draw_stroke , i
glTexCoordPointer_(2, #GL_FLOAT, 0, @vect_texture() )
glEnable_(#GL_TEXTURE_2D)
glEnableClientState_(#GL_TEXTURE_COORD_ARRAY)
glBindTexture_(#GL_TEXTURE_2D, tex)
macro_gl2d_cal_pos(gl_count)
glDrawArrays_(#GL_TRIANGLE_FAN, 0, gl_count)
glDisable_(#GL_TEXTURE_2D)
glDisableClientState_(#GL_TEXTURE_COORD_ARRAY)
macro_gl2d_draw_stroke(gl_count)
EndProcedure
Procedure gl2d_draw_entity(gl_count , gl_opt , px1.f , py1.f , rayx.f , rayy.f , gl2d_draw_stroke = 0)
;py1 = gl_screen_scale_y - py1
Protected x.f , y.f , z.f , sinTheta.f , cosTheta.f , i
macro_gl2d_cal_pos(gl_count)
glDrawArrays_(gl_opt, 0, gl_count)
macro_gl2d_draw_stroke(gl_count)
EndProcedure
partie 3
"opengl_point_in_polygon.pbi"
Code : Tout sélectionner
Procedure test_point_in_cirle(px,py,ray)
If gl_test <> 0
If ( ( gl_mousex - px )*( gl_mousex - px ) + ( gl_mousey - py ) * ( gl_mousey - py ) ) < (ray * ray)
ProcedureReturn 1
EndIf
EndIf
ProcedureReturn 0
EndProcedure
Procedure test_point_in_cirle2(px,py,ray)
If gl_test <> 0
If ( ( gl_mousex - px )*( gl_mousex - px ) + ( gl_mousey - py ) * ( gl_mousey - py ) ) < ray
ProcedureReturn 1
EndIf
EndIf
ProcedureReturn 0
EndProcedure
Procedure test_point_in_polygon_v1(nb , start = 0)
Protected i.i
nb = nb - 1
For i = start To nb - 1
If (( vert(i + 1)\x - vert(i)\x ) * ( gl_mousey - vert(i + 1)\y ) - ( gl_mousex - vert(i + 1)\x) * ( vert(i + 1)\y - vert(i)\y )) <=0
ProcedureReturn 0
EndIf
Next
If ( vert(start)\x - vert(nb)\x ) * ( gl_mousey - vert(start)\y ) - ( gl_mousex - vert(start)\x) * ( vert(start)\y - vert(nb)\y ) <=0
ProcedureReturn 0
EndIf
ProcedureReturn 1
EndProcedure
Procedure test_point_in_polygon(nb , start = 0)
Protected i , inside , x.f , y.f
Protected.point new , old, lp, rp
If nb < 3 Or gl_test = 0 : ProcedureReturn 0 : EndIf
nb = nb - 1
inside = 0
x = gl_mousex
y = gl_mousey
old\x = vert(nb + start)\x
old\y = vert(nb + start)\y
For i = start To nb + start
If vert(i)\x > old\x
lp = old
rp\x = vert(i)\x
rp\y = vert(i)\y
Else
lp\x = vert(i)\x
lp\y = vert(i)\y
rp = old
EndIf
If lp\x < x And x <= rp\x And (y - lp\y) * (rp\x - lp\x) < (rp\y - lp\y) * (x - lp\x)
inside = ~inside
EndIf
old\x = vert(i)\x
old\y = vert(i)\y
Next
ProcedureReturn ((inside & 1) * gl_id)
EndProcedure
Procedure test_point_in_polygonV2(nb , Array pip.Point(1))
Protected i , inside , x.f , y.f
Protected.point old, lp, rp
If nb < 3 Or gl_test = 0 : ProcedureReturn 0 : EndIf
nb = nb - 1
inside = 0
x = gl_mousex
y = gl_mousey
old\x = pip(nb)\x
old\y = pip(nb)\y
For i = 0 To nb
If pip(i)\x > old\x
lp = old
rp = pip(i)
Else
lp = pip(i)
rp = old
EndIf
If lp\x < x And x <= rp\x And (y - lp\y) * (rp\x - lp\x) < (rp\y - lp\y) * (x - lp\x)
inside = ~inside
EndIf
old = pip(i)
Next
ProcedureReturn ((inside & 1) * gl_id)
EndProcedure
partie 4
"opengl_draw_line.pbi"
Code : Tout sélectionner
Macro Macro_Draw_line()
Select gl2d_rot
Case 1
gl_rotate( 2 , vert(0)\x, vert(0)\y , #GL_LINES , 2)
Case 2
gl_rotate( 2 , vert(1)\x, vert(1)\y , #GL_LINES , 2)
Default
gl_rotate( 2 , ((vert(0)\x + vert(1)\x ) * 0.5) , ((vert(0)\y + vert(1)\y ) * 0.5) , #GL_LINES , 2)
EndSelect
EndMacro
Macro Macro_Draw_Line_Fat(gl2d_px1 , gl2d_py1 , gl2d_px2 , gl2d_py2)
Protected gl2d_d.f , gl2d_x.f , gl2d_y.f
gl2d_d = Sqr((gl2d_px2 - gl2d_px1)*(gl2d_px2 - gl2d_px1) + (gl2d_py2- gl2d_py1)*(gl2d_py2 - gl2d_py1)) * 2
gl2d_y = gl_size * (gl2d_px2 - gl2d_px1) / gl2d_d
gl2d_x = -gl_size * (gl2d_py2 - gl2d_py1) / gl2d_d
vert2(0 , gl2d_px1 - gl2d_x , gl2d_py1 - gl2d_y )
vert2(1 , gl2d_px1 + gl2d_x , gl2d_py1 + gl2d_y )
vert2(2 , gl2d_px2 - gl2d_x , gl2d_py2 - gl2d_y )
vert2(3 , gl2d_px2 + gl2d_x , gl2d_py2 + gl2d_y )
Select gl2d_rot
Case 1
gl_rotate( 4 , gl2d_px1, gl2d_py1 , #GL_TRIANGLE_STRIP , 2)
Case 2
gl_rotate( 4 , gl2d_px2, gl2d_py2 , #GL_TRIANGLE_STRIP , 2)
Default
gl_rotate( 4 , (gl2d_px1 + gl2d_px2) * 0.5 , (gl2d_py1 + gl2d_py2) * 0.5 , #GL_TRIANGLE_STRIP , 2)
EndSelect
EndMacro
Procedure Draw_linexy(gl2d_px1.f , gl2d_py1.f , gl2d_px2.f , gl2d_py2.f , gl2d_rot.f = 0)
If gl_size > 1 And gl_LineStipple_factor = 0
Macro_Draw_Line_Fat(gl2d_px1 , gl2d_py1 , gl2d_px2 , gl2d_py2)
ProcedureReturn 0
EndIf
glLineWidth_(gl_size)
vert2(0,gl2d_px1,gl2d_py1)
vert2(1,gl2d_px2,gl2d_py2)
Macro_Draw_line()
ProcedureReturn 0
EndProcedure
Procedure Draw_line(gl2d_px1.f , gl2d_py1.f , gl2d_l.f , gl2d_h.f , gl2d_rot.f = 0)
If gl_size > 1 And gl_LineStipple_factor = 0
Macro_Draw_Line_Fat(gl2d_px1 , gl2d_py1 , gl2d_px1 + gl2d_l,gl2d_py1 + gl2d_h )
ProcedureReturn 0
EndIf
glLineWidth_(gl_size)
vert2(0,gl2d_px1,gl2d_py1)
vert2(1,(gl2d_px1 + gl2d_l),(gl2d_py1 + gl2d_h))
Macro_Draw_line()
EndProcedure
Procedure draw_lineH(gl2d_px1.f , gl2d_py1.f , gl2d_l.f , gl2d_rot.f = 0)
If gl_size > 1 And gl_LineStipple_factor = 0
Macro_Draw_Line_Fat(gl2d_px1,gl2d_py1,gl2d_px1 + gl2d_l,gl2d_py1 )
ProcedureReturn 0
EndIf
glLineWidth_(gl_size)
vert2(0,gl2d_px1,gl2d_py1)
vert2(1,gl2d_px1 + gl2d_l,gl2d_py1)
Macro_Draw_line()
ProcedureReturn 0
EndProcedure
Procedure draw_lineV(gl2d_px1.f , gl2d_py1.f , gl2d_h.f , gl2d_rot.f = 0)
If gl_size > 1 And gl_LineStipple_factor = 0
Macro_Draw_Line_Fat(gl2d_px1,gl2d_py1,gl2d_px1,gl2d_py1 + gl2d_h )
ProcedureReturn 0
EndIf
glLineWidth_(gl_size)
vert2(0,gl2d_px1,gl2d_py1)
vert2(1,gl2d_px1,gl2d_py1+gl2d_h)
Macro_Draw_line()
ProcedureReturn 0
EndProcedure
Procedure Draw_line_list(List gl_lines.gl_liste())
Protected gl2d_count = 0
ForEach gl_lines()
vert2(gl2d_count ,gl_lines()\x + gl_Translationx , gl_lines()\y + gl_Translationy )
gl2d_count = gl2d_count + 1
Next
If gl_angle_xyz = 1
gl_rotate( gl2d_count , gl_point_rotatex + gl_Translationx , gl_point_rotatey + gl_Translationy , #GL_LINE_STRIP)
EndIf
EndProcedure
code demo pour le tracer de lignes
Code : Tout sélectionner
IncludeFile "opengl draw v8.pbi"
lg =1200
ht = 800
NewList mylines.gl_liste()
Read nb.i
For i = 1 To nb
AddElement(mylines())
Read.f mylines()\x
Read.f mylines()\y
Next
OpenWindow2d( lg , ht , #PB_Window_SystemMenu )
origin(50,-50)
scale(1)
cls(0)
Repeat
SetWindowTitle(0,"fps : "+Str(gl_fps))
Event = WindowEvent()
draw_start()
;gl_rotate_line = 0 , 1 , 2
a = a + 1
If a>359 : a = 0 : EndIf
size(1)
angle(0,0,0)
Alpha(255)
LineStipple(0)
test(1)
color($ff00ff)
size(15)
If draw_linexy(50,550,950,550)>0
color($ff00)
draw_linexy(50,550,950,550)
EndIf
;test(0)
x = 0
y = 0
angle(a,0,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 200
y = 0
angle(0,a,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 400
y = 0
angle(0,0,a)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 600
y = 0
size(10)
angle(0,0,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
angle(0,0,a)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 0
y = 200
Alpha(127)
size(10)
angle(0,0,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 200
y = 200
LineStipple(1)
Alpha(255)
size(10)
angle(0,0,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 200
y = 200
LineStipple(1)
Alpha(255)
size(10)
angle(0,0,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 400
y = 200
LineStipple(2,127)
size(10)
angle(0,0,a)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y , 2 )
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y , 2 )
angle(0,0,0)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y)
x = 600
y = 200
LineStipple(0)
size(1)
angle(0,0,a)
color($ff0000)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y , 1)
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y , 1)
color($ff0000)
angle(0,0,0)
draw_linexy(100 + x , 200 + y , 300 + x , 200 + y )
color($ff00)
draw_linexy(200 + x , 100 + y , 200 + x , 300 + y )
;angle(0,0,a)
LineStipple(0)
For i = 0 To 4999
color(Random($ffffff,$0f0f0f))
Alpha(Random(25,15))
size(Random(10,2))
x1 = Random(400,200)
y1 = Random(600,400)
x2 = Random(400,200)
y2 = Random(600,400)
draw_linexy(100 + x1 , 200 + y1 , 300 + x2 , 200 + y2 , 0)
Next
angle(0,0,0)
Alpha(255)
size(1)
color($ff00ff)
For Width = 1 To 180 Step 5
draw_Line(800, 600, Width, 180)
Next
size(2)
color($ff00ff)
point_set_rotate(150,700)
;gl_Translation(400,-200)
angle(-a,a,a)
draw_line_list(mylines())
draw_stop()
Delay(1)
Until Event=#PB_Event_CloseWindow
DataSection
Data.i 7
Data.f 50,700
Data.f 50,600
Data.f 250,600
Data.f 250,700
Data.f 50,700
Data.f 150,750
Data.f 250,700