Author Topic: how to creat a simple open gl line by dragging with a mouse click ?  (Read 1492 times)

0 Members and 1 Guest are viewing this topic.

Frankolinox

  • Guest
good morning, I have a simple question:

a) how I can create a simple openGL line (2 points) from left to right via mouse click?
such simple thing for a cad program for example :-)

b) create four points same way for closing a rectangle?

many thanks in advance good new year 2019 for you, regards, frank
« Last Edit: January 11, 2019, 12:09:15 AM by Frankolinox »

Mike Lobanovsky

  • Guest
Re: how to creat a simple open gl line?
« Reply #1 on: January 02, 2019, 07:53:20 AM »
Google for glBegin(GL_LINES)/glEnd and glRect() or glBegin(GL_QUADS)/glEnd() to draw an OpenGL line and rectangle (a.k.a. quad), respectively.

For the line you'll need two 2D (flat) or 3D (volumetric) vertices to denote the line's endpoints, and for the quad you'll need two 2D/3D vertices to denote the quad's opposite corners (glRect) or four 2D/3D vertices to denote all the four corners of the quad (glBegin(GL_QUADS)).

There are a lot of OpenGL examples using basic primitives all over the net.

Charles Pegge

  • Guest
Re: how to creat a simple open gl line?
« Reply #2 on: January 02, 2019, 05:54:29 PM »
We have a few line examples in projectsA/OpenglConsole, and examples/openGl, but I think Frank wants to do CAD (or computer-aided-art) which requires a whole new framework.

Frankolinox

  • Guest
Re: how to creat a simple open gl line?
« Reply #3 on: January 03, 2019, 12:18:22 AM »
thanks mike and charles

1) I thought first about a cad program like fbcad of owen (written in freebasic), but also a 3d designer to create shapes in three dimension.. (more concerned to "blender 3d" software) like openGL example oxygen did it already in some examples... I will try to create an example scene so far as I know the stuff ;) yes it's important if you are creating 2d dimensions or 3d dimension objects...

2) where I can download latest oxygenbasic zip files plus scene examples?

thanks, see you soon

Frankolinox

  • Guest
Re: how to creat a simple open gl line by dragging with a mouse click ?
« Reply #4 on: January 10, 2019, 09:42:54 AM »
how to fix my first try? :-)
what is a line here in oxygenbasic, a vertex point?
1) it's my first step, only to include a simple line here by a menu
2) then I will try to create a line by dragging mouse from point A to point B

kind regards, I have used the "menu_object" of openGL example, thanks

Code: OxygenBasic
  1.   '#compact
  2.  includepath "$\inc\"
  3.   $ FileName  "t.exe"
  4.   'include    "RTL32.inc"
  5.  'include    "RTL64.inc"
  6.  '
  7.  
  8.   % MultiSamples 4
  9.   % ExplicitMain
  10.   '% title        "Pick / move objects / right-click for menu"
  11.  % title        "Create+Pick / move objects / Left+right-click for menu"
  12.   % fontA        "Arial",FW_SEMIBOLD
  13.  
  14.   'macro keydown
  15.  'case 27 : 'no action
  16.  'case 32 : 'no action
  17.  'end macro
  18.  '
  19.  include "OpenglSceneFrame.inc"
  20.   '
  21.  'includepath "$\examples\opengl\"
  22.  include "glo2\shapes.inc"
  23.   include "glo2\materials.inc"
  24.  
  25.   %TPM_LEFTBUTTON   0x0000
  26.   %TPM_RIGHTBUTTON  0x0002
  27.   %TPM_LEFTALIGN    0x0000
  28.  
  29.   'SCENE GLOBALS
  30.  ==============
  31.  
  32.   indexbase 1
  33.   sys    texn[16]     'ARRAY OF TEXTURE NUMBERS
  34.  sys    GdiplusToken '
  35.  float  ang1         'ANIMATION ANGLE
  36.  sys    cmd          'COMMAND MESSAGE WPARAM
  37.  sys   cube,sphere,tors,helix,line 'SHAPES
  38.  sys   picknext
  39.  
  40.   %cone -1
  41.  
  42.  
  43.   function WndMessages( hWnd, wMsg, wParam, lparam ) as sys, link WndProcExtra
  44.   ============================================================================
  45.   '
  46.  static sys         hMenu,hSubMenu,hCursorMenu
  47.   static String      szAppName
  48.   static POINT       pt  
  49.   '
  50.  select wMsg
  51.   ===========
  52.      
  53.   case WM_CREATE
  54.   '
  55.  if mincreate then return
  56.   '
  57.  hMenu = CreateMenu  
  58.   hSubMenu = CreateMenu  
  59.   hSubMenu1= CreateMenu  
  60.   AppendMenu hSubMenu1,      MF_STRING,    4021,        "Shiny Red"
  61.   AppendMenu hSubMenu1,      MF_STRING,    4022,        "Shiny Black"
  62.   AppendMenu hSubMenu1,      MF_STRING,    4023,        "Steel"
  63.   AppendMenu hSubMenu1,      MF_STRING,    4024,        "Bronze"
  64.   AppendMenu hSubMenu1,      MF_STRING,    4025,        "Silver"
  65.   AppendMenu hSubMenu1,      MF_STRING,    4026,        "Gold"
  66.   AppendMenu hSubMenu ,      MF_POPUP,     hSubMenu1,   "&Materials"
  67.   '
  68.  hSubMenu1= CreateMenu  
  69.   AppendMenu     hSubMenu1,  MF_STRING,    4041,        "Cube"
  70.   AppendMenu     hSubMenu1,  MF_STRING,    4042,        "Cone"
  71.   AppendMenu     hSubMenu1,  MF_STRING,    4043,        "Sphere"
  72.   AppendMenu     hSubMenu1,  MF_STRING,    4044,        "Torus"
  73.   AppendMenu     hSubMenu1,  MF_STRING,    4045,        "Helix"
  74.   AppendMenu     hSubMenu1,  MF_STRING,    4046,        "Lines"
  75.   AppendMenu     hSubMenu ,  MF_POPUP,       hSubMenu1, "&Shapes"
  76.  
  77. ' AppendMenu     hSubMenu ,  MF_SEPARATOR, 0,            null
  78.  AppendMenu     hSubMenu ,  MF_STRING,    4005,        "E&xit"
  79.   '
  80.  AppendMenu     hMenu,        MF_POPUP,     hSubMenu,  "&Objects"
  81.   '
  82. '  CheckMenuItem  hMenu,4022,   MF_CHECKED
  83. '  EnableMenuItem hMenu,4024,   MF_DISABLED or MF_GRAYED
  84.  '
  85.  hCursorMenu = GetSubMenu(hMenu, 0)
  86.   '      
  87.  case WM_COMMAND
  88.  
  89.   if wparam = 4005 then 'Exit        
  90.    SendMessage hwnd, WM_CLOSE, 0, 0
  91.   end if
  92.   cmd=wParam
  93.  
  94.   case WM_KEYDOWN
  95.     cmd=12
  96.     act=1
  97.   case WM_KEYUP
  98.     cmd=12
  99. case WM_LBUTTONUP
  100.     GetClientRect crect
  101.     GetCursorPos(pt)
  102.     TrackPopupMenu(hCursorMenu, TPM_LEFTALIGN or TPM_LEFTBUTTON or TPM_RIGHTBUTTON,
  103.     pt.x, pt.y, 0, hwnd, null)
  104.     print "Left create Lines"
  105.     bright=0 : return 1
  106.    
  107.   case WM_RBUTTONUP
  108.     GetClientRect crect
  109.     GetCursorPos(pt)
  110.     TrackPopupMenu(hCursorMenu, TPM_LEFTALIGN or TPM_LEFTBUTTON or TPM_RIGHTBUTTON,
  111.     pt.x, pt.y, 0, hwnd, null)
  112.     bright=0 : return 1
  113.   end select
  114.   end function
  115.  
  116.  
  117. '=============================================xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  118.  
  119.    static single s1,s2,s3,s4,ang1,angi1=1,ra,ri
  120.   '
  121.  glLoadIdentity
  122.   glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
  123.   glClearColor 0.5, 0, 0, 0
  124.   '
  125.  '
  126.  glrotatef    ang1, 0,0,1
  127.   gltranslatef 0,0,-1
  128.   glscalef     0.001,0.001,0.001
  129.   '
  130.  glLineWidth 3.0
  131.   '
  132.  glBegin GL_LINES
  133.   glColor4ub  250,250,150,250
  134.   glVertex2i -100,-100
  135.   glVertex2i  100, 100
  136.   glColor4ub  150,250,250,250
  137.   glVertex2i -100, 100
  138.   glVertex2i  100,-100
  139.   glEnd
  140.   '
  141.  glLineWidth 2.0
  142.   '
  143.  ri=0
  144.   glBegin GL_LINE_LOOP
  145.   for i=0 to 360 step 10
  146.     ra=rad(i)
  147.     glVertex2f cos(ra)*(170+ri),sin(ra)*(170+ri)
  148.     ri=30-ri
  149.   next
  150.   glEnd
  151.   '
  152. ' ang1+=angi1
  153. ' if ang1>360 then ang1-=360
  154.  '
  155. ' end sub
  156.  
  157.  
  158. '  sub Release(sys hwnd)
  159.  '====================
  160. '  end sub
  161.  
  162. '=============================================xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  163.  
  164.  
  165.   =================
  166.   class SceneObject
  167.   =================
  168.   '
  169.  float  p.x,p.y,p.z    'POSITION
  170.  float  sc,rz,ry       'PROPORTIONS
  171.  float  sm             'SMOOTHING
  172.  sys    n              'FACETS
  173.  sys    shape
  174.   '
  175.  float  a.x,a.y,a.z    'ORIENTATION
  176.  float  ax,ay,az       'ANCHOR ROTATION
  177.  float  bx,by,bz       'ANCHOR POSITION
  178.  '
  179.  Materials*ma
  180.   '
  181.  method set(float px,py,pz,psc,prz,pry,psm,pn)
  182.   p.x=px : p.y=py : p.z=pz : sc=psc : rz=prz : ry=pry
  183.   sm=psm
  184.   n=pn
  185.   end method
  186.   '
  187.  method set(materials*m)
  188.   @this.ma=@m
  189.   end method
  190.   '
  191.  method set(sys sh)
  192.   shape=sh
  193.   end method
  194.   '
  195.  method Render()
  196.   if @ma then ma.act
  197.   glPushMatrix
  198.   gltranslatef  p.x,p.y,p.z
  199.   glrotatef     a.x,1,0,0 'rotate yz  : PITCH
  200.  glrotatef     a.y,0,1,0 'rotate xz  : YAW
  201.  glrotatef     a.z,0,0,1 'rotate xy  : ROLL
  202.  glscalef      sc,sc,sc
  203.   if shape=-1
  204.     ConeFaces     n,rz,ry,sm
  205.   elseif shape>0
  206.     glCallList shape
  207.   end if
  208.   a.y+=1 : if a.y=360 then a.y=0
  209.   glPopMatrix
  210.   end method
  211.   '
  212.  method anchor()
  213.   ax=a.x : ay=a.y : az=a.z
  214.   bx=p.x : by=p.y : bz=p.z
  215.   end method
  216.   '
  217.  method move(single mx,my,mz)
  218.   p.x=bx+mx : p.y=by+my : p.z=bz+mz
  219.   limit
  220.   end method
  221.   '
  222.  method rotate(single mx,my,mz)
  223.   a.x=ax+mx : a.y=ay+my : a.z=az+mz
  224.   end method
  225.   '
  226.  method limit()
  227.   float d=-p.z,id=-1/p.z
  228.   if  p.x*id<-0.5 then p.x=-.5*d
  229.   if  p.y*id<-0.4 then p.y=-.4*d
  230.   if p.z>-1.0 then p.z=-1.0
  231.   if p.x*id>.5  then p.x=.5*d
  232.   if p.y*id>.4 then p.y=.4*d
  233.   end method
  234.   '
  235.  method drag()
  236.   float dx,dy,dz
  237.   dx=mposx-sposx
  238.   dy=sposy-mposy
  239.   if key[VK_CONTROL]
  240.     'a.z=-dx : a.x=-dy
  241.    'rotate(-0.5*dy,0.0,-0.5*dx)
  242.    rotate(-0.5*dy,0.0,0.0)
  243.   elseif key[VK_SHIFT]
  244.     dx=mposx-sposx
  245.     dz=(mposy-sposy)/crect.right  
  246.     move 0,0,20*dz
  247.   else
  248.     dx=mposx-sposx
  249.     dy=sposy-mposy
  250.     dz=-p.z/crect.right
  251.     move dx*dz,dy*dz,0
  252.   end if
  253.   end method
  254.   '
  255.  end class
  256.  
  257.  
  258.   ========================
  259.   sub initialize(sys hWnd)
  260.   ========================
  261.   '
  262.  GDIplus 1
  263.   '
  264.  cube=CompileList   : CubeForm      : glEndList
  265.  
  266.   '--- line=CompileList   : ? (Vertex?)      : glEndList
  267.  
  268.   sphere=CompileList : Spheric 1,1,6 : glEndList
  269.   tors=CompileList   : torus 1.,.20  : glEndList
  270.   helix=CompileList : toroid 1.,.25,.8,.16,5.,6. : glEndList
  271.   end sub
  272.  
  273.  
  274.   =====================
  275.   sub Release(sys hwnd)
  276.   =====================
  277.   '
  278.  DeleteAllGlCompiled
  279.   Gdiplus 0
  280.   end sub
  281.  
  282.   ===================
  283.   sub scene(sys hWnd)
  284.   ===================
  285.   '
  286.  static single ra,ri,angi1=.5
  287.   '
  288.  '
  289.  ActiveFrame
  290.   glClearColor 0.5, 0.5, 0.7, 0.0
  291.   Fog          0.5, 0.5, 0.7, 0.035 'rgb and density
  292.  BeginPick
  293.   '
  294.  StandardLighting li
  295.   StandardMaterial ma
  296.   'glEnable GL_TEXTURE_2D
  297.  '
  298.  sys   t1=texn[1] 'texture
  299.  '
  300.  '
  301.  static SceneObject c[100]
  302.   '
  303.  'INITIAL DATA
  304.  
  305.   if c.sc=0
  306.     '
  307.    'shape    x     y     z     sc   rz ry   sm n
  308.    '
  309.    c[7].set  2.5, -0.9, -16.0, 0.5, 0, 4. , 0, 4
  310.     c[6].set  6.5, -0.9, -16.0, 0.5, 0, 4. , 0, 4
  311.     c[5].set  2.5, -0.9, -8.0 , 0.5, 0, 2. , 0, 10
  312.     c[4].set  0.5, -0.9, -4.0 , 0.5, 0, 3. , 0, 15
  313.     c[3].set -0.5, -0.6, -2.0 , 0.5,.5, 1. , 1, 30
  314.     c[2].set -0.5, -0.1, -2.0 , .25,1., 2. , 1, 30
  315.     c[1].set -0.5,  0.4, -2.0 , .25,0., 2. , 1, 30
  316.     '
  317.    c[7].set line
  318.     c[6].set cube
  319.     c[5].set helix
  320.     c[4].set cone
  321.     c[3].set tors
  322.     c[2].set sphere
  323.     c[1].set cone
  324.     '
  325.    c[6].set RedShinyMaterial
  326.     c[5].set BlackShinyMaterial
  327.     c[4].set SteelMaterial
  328.     c[3].set BronzeMaterial
  329.     c[2].set SilverMaterial
  330.     c[1].set GoldMaterial
  331.     picknext=7
  332.   end if
  333.   '
  334.  'PICK OR RENDER MODE
  335.  '
  336.  a=lastkey-48
  337.   select a
  338.   case 1 to 6 : picked=a 'number keys 1..6
  339.  end select
  340.   '
  341.  MoveObjectWithKeys c[picked], 0.01, 1.0
  342.   '
  343.  '
  344.  'RESPOND TO MENU SELECTION
  345.  '
  346.  if cmd
  347.     if picked
  348.       if cmd=12
  349.         if bleft or bright
  350.           c[picked].anchor
  351.           sposx=mposx : sposy=mposy
  352.         end if
  353.         goto ncmd
  354.       end if
  355.       materials *m
  356.       select cmd
  357.       case 4021 : @m=@RedShinyMaterial
  358.       case 4022 : @m=@BlackShinyMaterial
  359.       case 4023 : @m=@SteelMaterial
  360.       case 4024 : @m=@BronzeMaterial
  361.       case 4025 : @m=@SilverMaterial
  362.       case 4026 : @m=@GoldMaterial
  363.       end select
  364.       if @m
  365.         c[picked].set m
  366.         goto ncmd
  367.       end if
  368.     end if
  369.     sys shape
  370.     select cmd
  371.     case 4041 : shape=cube
  372.     case 4042 : shape=cone
  373.     case 4043 : shape=sphere
  374.     case 4044 : shape=tors
  375.     case 4045 : shape=helix
  376.     case 4046 : shape=line
  377.  
  378.     end select
  379.     if shape then
  380.       if picked=0 then
  381.         if picknext<=100 then
  382.           picked=picknext
  383.           c[picked].set -0.0, -0.0, -5.0 , 0.5,.5, 1. , 1, 30
  384.           c[picked].set SilverMaterial
  385.           picknext++
  386.         end if
  387.       end if
  388.       if picked then c[picked].set shape
  389.     end if
  390.   end if
  391.   ncmd:
  392.   cmd=0 'FINISHED WITH COMMAND
  393.  '
  394.  'RENDER OBJECTS
  395.  '
  396.  sys i
  397.   SceneObject *cc
  398.   for i=1,i<picknext
  399.     PickLabel i
  400.     @cc=@c[i]
  401.     if bleft
  402.       if picked=i
  403.         cc.drag
  404.       end if
  405.     else
  406.       cc.anchor
  407.     end if
  408.     cc.render
  409.   next
  410.   '
  411.  'PRINTING INFO / LABELS
  412.  '
  413.  'glDisable GL_TEXTURE_2D
  414.  glDisable GL_LIGHTING
  415.   if picked
  416.     glPushMatrix
  417.     glLoadIdentity
  418.     gltranslatef -.5,.35,-1.0
  419.     float w,h
  420.     glscalef     .07,.07,.01
  421.     GetWordArea  "Shape:",w,h
  422.     if pick
  423.       PutBoxArea   w,h
  424.     else
  425.       glColor3f    .99,.99,.00
  426.       gprint       "Shape: "
  427.       glColor3f    .99,.99,.99
  428.       gprint       picked
  429.     end if
  430.     glPopMatrix
  431.   end if
  432.   '
  433.  EndPick
  434.   '
  435.  end sub
  436.  
  437.   MainWindow width,height,WS_OVERLAPPEDWINDOW
  438.  

any help is welcome (old)

new edit: I think I have solved the problem to an openGL *.inc file this morning more to come later ;)  need no help any more lol

frank

« Last Edit: January 12, 2019, 12:15:26 AM by Frankolinox »

Frankolinox

  • Guest
Re: how to creat a simple open gl line by dragging with a mouse click ?
« Reply #5 on: January 14, 2019, 06:15:02 AM »
here is my solution :-)


Code: [Select]
  '#compact
  includepath "$\inc\"
  $ FileName  "t.exe"
  'include    "RTL32.inc"
  'include    "RTL64.inc"
  '

  % MultiSamples 4
  % ExplicitMain
  '% title        "Pick / move objects / right-click for menu"
  % title        "Create+Pick / move objects / Left+right-click for menu"
  % fontA        "Arial",FW_SEMIBOLD

  'macro keydown
  'case 27 : 'no action
  'case 32 : 'no action
  'end macro
  '
  include "OpenglSceneFrame.inc"
  '
  'includepath "$\examples\opengl\"
  include "glo2\shapes.inc"
  include "glo2\materials.inc"

  %TPM_LEFTBUTTON   0x0000
  %TPM_RIGHTBUTTON  0x0002
  %TPM_LEFTALIGN    0x0000

  'SCENE GLOBALS
  ==============

  indexbase 1
  sys    texn[16]     'ARRAY OF TEXTURE NUMBERS
  sys    GdiplusToken '
  float  ang1         'ANIMATION ANGLE
  sys    cmd          'COMMAND MESSAGE WPARAM
  sys   cube,sphere,tors,helix,line 'SHAPES
  sys   picknext

  %cone -1


  function WndMessages( hWnd, wMsg, wParam, lparam ) as sys, link WndProcExtra
  ============================================================================
  '
  static sys         hMenu,hSubMenu,hCursorMenu
  static String      szAppName
  static POINT       pt 
  '
  select wMsg
  ===========
     
  case WM_CREATE
  '
  if mincreate then return
  '
  hMenu = CreateMenu 
  hSubMenu = CreateMenu 
  hSubMenu1= CreateMenu 
  AppendMenu hSubMenu1,      MF_STRING,    4021,        "Shiny Red"
  AppendMenu hSubMenu1,      MF_STRING,    4022,        "Shiny Black"
  AppendMenu hSubMenu1,      MF_STRING,    4023,        "Steel"
  AppendMenu hSubMenu1,      MF_STRING,    4024,        "Bronze"
  AppendMenu hSubMenu1,      MF_STRING,    4025,        "Silver"
  AppendMenu hSubMenu1,      MF_STRING,    4026,        "Gold"
  AppendMenu hSubMenu ,      MF_POPUP,     hSubMenu1,   "&Materials"
  '
  hSubMenu1= CreateMenu 
  AppendMenu     hSubMenu1,  MF_STRING,    4041,        "Cube"
  AppendMenu     hSubMenu1,  MF_STRING,    4042,        "Cone"
  AppendMenu     hSubMenu1,  MF_STRING,    4043,        "Sphere"
  AppendMenu     hSubMenu1,  MF_STRING,    4044,        "Torus"
  AppendMenu     hSubMenu1,  MF_STRING,    4045,        "Helix"
  AppendMenu     hSubMenu1,  MF_STRING,    4046,        "Lines"
  AppendMenu     hSubMenu ,  MF_POPUP,       hSubMenu1, "&Shapes"

' AppendMenu     hSubMenu ,  MF_SEPARATOR, 0,            null
  AppendMenu     hSubMenu ,  MF_STRING,    4005,        "E&xit"
  '
  AppendMenu     hMenu,        MF_POPUP,     hSubMenu,  "&Objects"
  '
'  CheckMenuItem  hMenu,4022,   MF_CHECKED
'  EnableMenuItem hMenu,4024,   MF_DISABLED or MF_GRAYED
  '
  hCursorMenu = GetSubMenu(hMenu, 0)
  '     
  case WM_COMMAND

  if wparam = 4005 then 'Exit       
    SendMessage hwnd, WM_CLOSE, 0, 0
  end if
  cmd=wParam
 
  case WM_KEYDOWN
    cmd=12
    act=1
  case WM_KEYUP
    cmd=12
case WM_LBUTTONUP
    GetClientRect crect
    GetCursorPos(pt)
    TrackPopupMenu(hCursorMenu, TPM_LEFTALIGN or TPM_LEFTBUTTON or TPM_RIGHTBUTTON,
    pt.x, pt.y, 0, hwnd, null)
    'print "Left create Lines"
    bright=0 : return 1
   
  case WM_RBUTTONUP
    GetClientRect crect
    GetCursorPos(pt)
    TrackPopupMenu(hCursorMenu, TPM_LEFTALIGN or TPM_LEFTBUTTON or TPM_RIGHTBUTTON,
    pt.x, pt.y, 0, hwnd, null)
    bright=0 : return 1
  end select
  end function

 sub lines(single x,y)
  ==========================
  glPushMatrix
 
  glLineWidth 4.0
  gltranslatef 0,0,-1
  glscalef     0.01,0.01,0.01
  '
  glBegin GL_LINES
  glColor4ub  250,250,150,250
  glVertex2i -100,-100
  glVertex2i  100, 100
  glColor4ub  150,250,250,250
  glVertex2i -100, 100
  glVertex2i  100,-100
  glEnd
  '
  'glLineWidth 2.0
 
  glPopMatrix
  end sub


  =================
  class SceneObject
  =================
  '
  float  p.x,p.y,p.z    'POSITION
  float  sc,rz,ry       'PROPORTIONS
  float  sm             'SMOOTHING
  sys    n              'FACETS
  sys    shape
  '
  float  a.x,a.y,a.z    'ORIENTATION
  float  ax,ay,az       'ANCHOR ROTATION
  float  bx,by,bz       'ANCHOR POSITION
  '
  Materials*ma
  '
  method set(float px,py,pz,psc,prz,pry,psm,pn)
  p.x=px : p.y=py : p.z=pz : sc=psc : rz=prz : ry=pry
  sm=psm
  n=pn
  end method
  '
  method set(materials*m)
  @this.ma=@m
  end method
  '
  method set(sys sh)
  shape=sh
  end method
  '
  method Render()
  if @ma then ma.act
  glPushMatrix
  gltranslatef  p.x,p.y,p.z
  glrotatef     a.x,1,0,0 'rotate yz  : PITCH
  glrotatef     a.y,0,1,0 'rotate xz  : YAW
  glrotatef     a.z,0,0,1 'rotate xy  : ROLL
  glscalef      sc,sc,sc
  if shape=-1
    ConeFaces     n,rz,ry,sm
  elseif shape>0
    glCallList shape
  end if
  a.y+=1 : if a.y=360 then a.y=0
  glPopMatrix
  end method
  '
  method anchor()
  ax=a.x : ay=a.y : az=a.z
  bx=p.x : by=p.y : bz=p.z
  end method
  '
  method move(single mx,my,mz)
  p.x=bx+mx : p.y=by+my : p.z=bz+mz
  limit
  end method
  '
  method rotate(single mx,my,mz)
  a.x=ax+mx : a.y=ay+my : a.z=az+mz
  end method
  '
  method limit()
  float d=-p.z,id=-1/p.z
  if  p.x*id<-0.5 then p.x=-.5*d
  if  p.y*id<-0.4 then p.y=-.4*d
  if p.z>-1.0 then p.z=-1.0
  if p.x*id>.5  then p.x=.5*d
  if p.y*id>.4 then p.y=.4*d
  end method
  '
  method drag()
  float dx,dy,dz
  dx=mposx-sposx
  dy=sposy-mposy
  if key[VK_CONTROL]
    'a.z=-dx : a.x=-dy
    'rotate(-0.5*dy,0.0,-0.5*dx)
    rotate(-0.5*dy,0.0,0.0)
  elseif key[VK_SHIFT]
    dx=mposx-sposx
    dz=(mposy-sposy)/crect.right   
    move 0,0,20*dz
  else
    dx=mposx-sposx
    dy=sposy-mposy
    dz=-p.z/crect.right
    move dx*dz,dy*dz,0
  end if
  end method
  '
  end class


  ========================
  sub initialize(sys hWnd)
  ========================
  '
  GDIplus 1
  '
  cube=CompileList   : CubeForm      : glEndList
  line=CompileList   : lines 1,1      : glEndList

  sphere=CompileList : Spheric 1,1,6 : glEndList
  tors=CompileList   : torus 1.,.20  : glEndList
  helix=CompileList : toroid 1.,.25,.8,.16,5.,6. : glEndList
  end sub


  =====================
  sub Release(sys hwnd)
  =====================
  '
  DeleteAllGlCompiled
  Gdiplus 0
  end sub

  ===================
  sub scene(sys hWnd)
  ===================
  '
  static single ra,ri,angi1=.5
  '
  '
  ActiveFrame
  glClearColor 0.5, 0.5, 0.7, 0.0
  Fog          0.5, 0.5, 0.7, 0.035 'rgb and density
  BeginPick
  '
  StandardLighting li
  StandardMaterial ma
  'glEnable GL_TEXTURE_2D
  '
  sys   t1=texn[1] 'texture
  '
  '
  static SceneObject c[100]
  '
  'INITIAL DATA

  if c.sc=0
    '
    'shape    x     y     z     sc   rz ry   sm n
    '
    c[7].set  2.5, -0.9, -4.0, 0.5, 0, 4. , 0, 4
    c[6].set  6.5, -0.9, -16.0, 0.5, 0, 4. , 0, 4
    c[5].set  2.5, -0.9, -8.0 , 0.5, 0, 2. , 0, 10
    c[4].set  0.5, -0.9, -4.0 , 0.5, 0, 3. , 0, 15
    c[3].set -0.5, -0.6, -2.0 , 0.5,.5, 1. , 1, 30
    c[2].set -0.5, -0.1, -2.0 , .25,1., 2. , 1, 30
    c[1].set -0.5,  0.4, -2.0 , .25,0., 2. , 1, 30
    '
    c[7].set line
    c[6].set cube
    c[5].set helix
    c[4].set cone
    c[3].set tors
    c[2].set sphere
    c[1].set cone
    '
    c[6].set RedShinyMaterial
    c[5].set BlackShinyMaterial
    c[4].set SteelMaterial
    c[3].set BronzeMaterial
    c[2].set SilverMaterial
    c[1].set GoldMaterial
    picknext=7
  end if
  '
  'PICK OR RENDER MODE
  '
  a=lastkey-48
  select a
  case 1 to 6 : picked=a 'number keys 1..6
  end select
  '
  MoveObjectWithKeys c[picked], 0.01, 1.0
  '
  '
  'RESPOND TO MENU SELECTION
  '
  if cmd
    if picked
      if cmd=12
        if bleft or bright
          c[picked].anchor
          sposx=mposx : sposy=mposy
        end if
        goto ncmd
      end if
      materials *m
      select cmd
      case 4021 : @m=@RedShinyMaterial
      case 4022 : @m=@BlackShinyMaterial
      case 4023 : @m=@SteelMaterial
      case 4024 : @m=@BronzeMaterial
      case 4025 : @m=@SilverMaterial
      case 4026 : @m=@GoldMaterial
      end select
      if @m
        c[picked].set m
        goto ncmd
      end if
    end if
    sys shape
    select cmd
    case 4041 : shape=cube
    case 4042 : shape=cone
    case 4043 : shape=sphere
    case 4044 : shape=tors
    case 4045 : shape=helix
    case 4046 : shape=line

    end select
    if shape then
      if picked=0 then
        if picknext<=100 then
          picked=picknext
          c[picked].set -0.0, -0.0, -5.0 , 0.5,.5, 1. , 1, 30
          c[picked].set SilverMaterial
          picknext++
        end if
      end if
      if picked then c[picked].set shape
    end if
  end if
  ncmd:
  cmd=0 'FINISHED WITH COMMAND
  '
  'RENDER OBJECTS
  '
  sys i
  SceneObject *cc
  for i=1,i<picknext
    PickLabel i
    @cc=@c[i]
    if bleft
      if picked=i
        cc.drag
      end if
    else
      cc.anchor
    end if
    cc.render
  next
  '
  'PRINTING INFO / LABELS
  '
  'glDisable GL_TEXTURE_2D
  glDisable GL_LIGHTING
  if picked
    glPushMatrix
    glLoadIdentity
    gltranslatef -.5,.35,-1.0
    float w,h
    glscalef     .07,.07,.01
    GetWordArea  "Shape:",w,h
    if pick
      PutBoxArea   w,h
    else
      glColor3f    .99,.99,.00
      gprint       "Shape: "
      glColor3f    .99,.99,.99
      gprint       picked
    end if
    glPopMatrix
  end if
  '
  EndPick
  '
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW

Frankolinox

  • Guest
Re: how to creat a simple open gl line by dragging with a mouse click ?
« Reply #6 on: January 18, 2019, 12:41:09 AM »
here's my next example, all work in progress :-)
included a grid plane

Code: [Select]
  '#compact
  includepath "$\inc\"
  $ FileName  "t.exe"
  'include    "RTL32.inc"
  'include    "RTL64.inc"
  '

  % MultiSamples 4
  % ExplicitMain
  '% title        "Pick / move objects / right-click for menu"
  % title        "Create+Pick / move objects / Left+right-click for menu"
  % fontA        "Arial",FW_SEMIBOLD

  'macro keydown
  'case 27 : 'no action
  'case 32 : 'no action
  'end macro
  '
  include "OpenglSceneFrame.inc"
  '
  'includepath "$\examples\opengl\"
  include "glo2\shapes.inc"
  include "glo2\materials.inc"

  %TPM_LEFTBUTTON   0x0000
  %TPM_RIGHTBUTTON  0x0002
  %TPM_LEFTALIGN    0x0000

  'SCENE GLOBALS
  ==============

  indexbase 1
  sys    texn[16]     'ARRAY OF TEXTURE NUMBERS
  sys    GdiplusToken '
  float  ang1         'ANIMATION ANGLE
  sys    cmd          'COMMAND MESSAGE WPARAM
  sys   cube,sphere,tors,helix,line,grid 'SHAPES
  sys   picknext
  sys   i,j

  %cone -1


  function WndMessages( hWnd, wMsg, wParam, lparam ) as sys, link WndProcExtra
  ============================================================================
  '
  static sys         hMenu,hSubMenu,hCursorMenu
  static String      szAppName
  static POINT       pt 
  '
  select wMsg
  ===========
     
  case WM_CREATE
  '
  if mincreate then return
  '
  hMenu = CreateMenu 
  hSubMenu = CreateMenu 
  hSubMenu1= CreateMenu 
  AppendMenu hSubMenu1,      MF_STRING,    4021,        "Shiny Red"
  AppendMenu hSubMenu1,      MF_STRING,    4022,        "Shiny Black"
  AppendMenu hSubMenu1,      MF_STRING,    4023,        "Steel"
  AppendMenu hSubMenu1,      MF_STRING,    4024,        "Bronze"
  AppendMenu hSubMenu1,      MF_STRING,    4025,        "Silver"
  AppendMenu hSubMenu1,      MF_STRING,    4026,        "Gold"
  AppendMenu hSubMenu ,      MF_POPUP,     hSubMenu1,   "&Materials"
  '
  hSubMenu1= CreateMenu 
  AppendMenu     hSubMenu1,  MF_STRING,    4041,        "Cube"
  AppendMenu     hSubMenu1,  MF_STRING,    4042,        "Cone"
  AppendMenu     hSubMenu1,  MF_STRING,    4043,        "Sphere"
  AppendMenu     hSubMenu1,  MF_STRING,    4044,        "Torus"
  AppendMenu     hSubMenu1,  MF_STRING,    4045,        "Helix"
  AppendMenu     hSubMenu1,  MF_STRING,    4046,        "Lines"
  AppendMenu     hSubMenu1,  MF_STRING,    4047,        "grid"
  AppendMenu     hSubMenu ,  MF_POPUP,       hSubMenu1, "&Shapes"

' AppendMenu     hSubMenu ,  MF_SEPARATOR, 0,            null
  AppendMenu     hSubMenu ,  MF_STRING,    4005,        "E&xit"
  '
  AppendMenu     hMenu,        MF_POPUP,     hSubMenu,  "&Objects"
  '
'  CheckMenuItem  hMenu,4022,   MF_CHECKED
'  EnableMenuItem hMenu,4024,   MF_DISABLED or MF_GRAYED
  '
  hCursorMenu = GetSubMenu(hMenu, 0)
  '     
  case WM_COMMAND

  if wparam = 4005 then 'Exit       
    SendMessage hwnd, WM_CLOSE, 0, 0
  end if
  cmd=wParam
 
  case WM_KEYDOWN
    cmd=12
    act=1
  case WM_KEYUP
    cmd=12
case WM_LBUTTONUP
    GetClientRect crect
    GetCursorPos(pt)
    TrackPopupMenu(hCursorMenu, TPM_LEFTALIGN or TPM_LEFTBUTTON or TPM_RIGHTBUTTON,
    pt.x, pt.y, 0, hwnd, null)
    'print "Left create Lines"
    bright=0 : return 1
   
  case WM_RBUTTONUP
    GetClientRect crect
    GetCursorPos(pt)
    TrackPopupMenu(hCursorMenu, TPM_LEFTALIGN or TPM_LEFTBUTTON or TPM_RIGHTBUTTON,
    pt.x, pt.y, 0, hwnd, null)
    bright=0 : return 1
  end select
  end function

 sub lines(single x,y)
  ==========================
  glPushMatrix
 
  glLineWidth 4.0
  gltranslatef 0,0,-1
  glscalef     0.01,0.01,0.01
  '
  glBegin GL_LINES
  glColor4ub  250,250,150,250
  glVertex2i -100,-100
  glVertex2i  100, 100
  glColor4ub  150,250,250,250
  glVertex2i -100, 100
  glVertex2i  100,-100
  glEnd
  '
  'glLineWidth 2.0
 
  glPopMatrix
  end sub

sub grids(sys i,j)
  glLineWidth 2.0
  gltranslatef 0,-2,-1
  glBegin GL_LINES
  for i = -10 to 10
          for j = -10 to 10
            glVertex3i -10, 0,   j     
            glVertex3i  10, 0,   j     

            glVertex3i  i,  0, -10     
            glVertex3i  i,  0,  10     

          next
        next
  glEnd
end sub

  =================
  class SceneObject
  =================
  '
  float  p.x,p.y,p.z    'POSITION
  float  sc,rz,ry       'PROPORTIONS
  float  sm             'SMOOTHING
  sys    n              'FACETS
  sys    shape
  '
  float  a.x,a.y,a.z    'ORIENTATION
  float  ax,ay,az       'ANCHOR ROTATION
  float  bx,by,bz       'ANCHOR POSITION
  '
  Materials*ma
  '
  method set(float px,py,pz,psc,prz,pry,psm,pn)
  p.x=px : p.y=py : p.z=pz : sc=psc : rz=prz : ry=pry
  sm=psm
  n=pn
  end method
  '
  method set(materials*m)
  @this.ma=@m
  end method
  '
  method set(sys sh)
  shape=sh
  end method
  '
  method Render()
  if @ma then ma.act
  glPushMatrix
  gltranslatef  p.x,p.y,p.z
  glrotatef     a.x,1,0,0 'rotate yz  : PITCH
  glrotatef     a.y,0,1,0 'rotate xz  : YAW
  glrotatef     a.z,0,0,1 'rotate xy  : ROLL
  glscalef      sc,sc,sc
  if shape=-1
    ConeFaces     n,rz,ry,sm
  elseif shape>0
    glCallList shape
  end if
  a.y+=1 : if a.y=360 then a.y=0
  glPopMatrix
  end method
  '
  method anchor()
  ax=a.x : ay=a.y : az=a.z
  bx=p.x : by=p.y : bz=p.z
  end method
  '
  method move(single mx,my,mz)
  p.x=bx+mx : p.y=by+my : p.z=bz+mz
  limit
  end method
  '
  method rotate(single mx,my,mz)
  a.x=ax+mx : a.y=ay+my : a.z=az+mz
  end method
  '
  method limit()
  float d=-p.z,id=-1/p.z
  if  p.x*id<-0.5 then p.x=-.5*d
  if  p.y*id<-0.4 then p.y=-.4*d
  if p.z>-1.0 then p.z=-1.0
  if p.x*id>.5  then p.x=.5*d
  if p.y*id>.4 then p.y=.4*d
  end method
  '
  method drag()
  float dx,dy,dz
  dx=mposx-sposx
  dy=sposy-mposy
  if key[VK_CONTROL]
    'a.z=-dx : a.x=-dy
    'rotate(-0.5*dy,0.0,-0.5*dx)
    rotate(-0.5*dy,0.0,0.0)
  elseif key[VK_SHIFT]
    dx=mposx-sposx
    dz=(mposy-sposy)/crect.right   
    move 0,0,20*dz
  else
    dx=mposx-sposx
    dy=sposy-mposy
    dz=-p.z/crect.right
    move dx*dz,dy*dz,0
  end if
  end method
  '
  end class


  ========================
  sub initialize(sys hWnd)
  ========================
  '
  GDIplus 1
  '
  cube=CompileList   : CubeForm      : glEndList
  line=CompileList   : lines 1,1      : glEndList
  grid=CompileList   : grids 1,2     : glEndList
  sphere=CompileList : Spheric 1,1,6 : glEndList
  tors=CompileList   : torus 1.,.20  : glEndList
  helix=CompileList : toroid 1.,.25,.8,.16,5.,6. : glEndList
  end sub


  =====================
  sub Release(sys hwnd)
  =====================
  '
  DeleteAllGlCompiled
  Gdiplus 0
  end sub

  ===================
  sub scene(sys hWnd)
  ===================
  '
  static single ra,ri,angi1=.5
  '
  '
  ActiveFrame
  glClearColor 0.5, 0.5, 0.7, 0.0
  Fog          0.5, 0.5, 0.7, 0.035 'rgb and density
  BeginPick
  '
  StandardLighting li
  StandardMaterial ma
  'glEnable GL_TEXTURE_2D
  '
  sys   t1=texn[1] 'texture
  '
  '
  static SceneObject c[100]
  '
  'INITIAL DATA

  if c.sc=0
    '
    'shape    x     y     z     sc   rz ry   sm n
    '
    c[8].set  1.5, -1.9, -4.0, 0.5, 0, 4. , 0, 4
    c[7].set  2.5, -0.9, -4.0, 0.5, 0, 4. , 0, 4
    c[6].set  6.5, -0.9, -16.0, 0.5, 0, 4. , 0, 4
    c[5].set  2.5, -0.9, -8.0 , 0.5, 0, 2. , 0, 10
    c[4].set  0.5, -0.9, -4.0 , 0.5, 0, 3. , 0, 15
    c[3].set -0.5, -0.6, -2.0 , 0.5,.5, 1. , 1, 30
    c[2].set -0.5, -0.1, -2.0 , .25,1., 2. , 1, 30
    c[1].set -0.5,  0.4, -2.0 , .25,0., 2. , 1, 30
    '
    c[8].set grid
    c[7].set line
    c[6].set cube
    c[5].set helix
    c[4].set cone
    c[3].set tors
    c[2].set sphere
    c[1].set cone
    '
    c[6].set RedShinyMaterial
    c[5].set BlackShinyMaterial
    c[4].set SteelMaterial
    c[3].set BronzeMaterial
    c[2].set SilverMaterial
    c[1].set GoldMaterial
    picknext=8
  end if
  '
  'PICK OR RENDER MODE
  '
  a=lastkey-48
  select a
  case 1 to 7 : picked=a 'number keys 1..6
  end select
  '
  MoveObjectWithKeys c[picked], 0.01, 1.0
  '
  '
  'RESPOND TO MENU SELECTION
  '
  if cmd
    if picked
      if cmd=12
        if bleft or bright
          c[picked].anchor
          sposx=mposx : sposy=mposy
        end if
        goto ncmd
      end if
      materials *m
      select cmd
      case 4021 : @m=@RedShinyMaterial
      case 4022 : @m=@BlackShinyMaterial
      case 4023 : @m=@SteelMaterial
      case 4024 : @m=@BronzeMaterial
      case 4025 : @m=@SilverMaterial
      case 4026 : @m=@GoldMaterial
      end select
      if @m
        c[picked].set m
        goto ncmd
      end if
    end if
    sys shape
    select cmd
    case 4041 : shape=cube
    case 4042 : shape=cone
    case 4043 : shape=sphere
    case 4044 : shape=tors
    case 4045 : shape=helix
    case 4046 : shape=line
    case 4047 : shape=grid

    end select
    if shape then
      if picked=0 then
        if picknext<=100 then
          picked=picknext
          c[picked].set -0.0, -0.0, -5.0 , 0.5,.5, 1. , 1, 30
          c[picked].set SilverMaterial
          picknext++
        end if
      end if
      if picked then c[picked].set shape
    end if
  end if
  ncmd:
  cmd=0 'FINISHED WITH COMMAND
  '
  'RENDER OBJECTS
  '
  sys i
  SceneObject *cc
  for i=1,i<picknext
    PickLabel i
    @cc=@c[i]
    if bleft
      if picked=i
        cc.drag
      end if
    else
      cc.anchor
    end if
    cc.render
  next
  '
  'PRINTING INFO / LABELS
  '
  'glDisable GL_TEXTURE_2D
  glDisable GL_LIGHTING
  if picked
    glPushMatrix
    glLoadIdentity
    gltranslatef -.5,.35,-1.0
    float w,h
    glscalef     .07,.07,.01
    GetWordArea  "Shape:",w,h
    if pick
      PutBoxArea   w,h
    else
      glColor3f    .99,.99,.00
      gprint       "Shape: "
      glColor3f    .99,.99,.99
      gprint       picked
    end if
    glPopMatrix
  end if
  '
  EndPick
  '
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW