Author Topic: Nehe tutorial in a dynamic dialog  (Read 7731 times)

0 Members and 2 Guests are viewing this topic.

Arnold

  • Guest
Re: Nehe tutorial in a dynamic dialog
« Reply #30 on: April 19, 2018, 08:09:28 AM »
I could not resist and tried some of Peter's images. There are some very nice results possible.
« Last Edit: April 30, 2018, 12:36:09 AM by Arnold »

Arnold

  • Guest
Re: Nehe tutorial in a dynamic dialog
« Reply #31 on: May 03, 2018, 03:18:18 AM »
Hello,

this is Nehe tutorial lesson 11, adapted for Oxygenbasic. As an exercise I used MainWindow (winutil.inc) as main. I think the app works quite nice.

Roland

Code: OxygenBasic
  1. 'http://nehe.gamedev.net/tutorial/flag_effect_(waving_texture)/16002/
  2. 'Adapted from Nehe tutorial lesson 11, uses MainWindow (winutil.inc)
  3. 'F1 opens Help
  4.  
  5. $ filename "NeheTut11.exe"
  6. 'uses RTL32
  7. 'uses RTL64
  8.  
  9. sys hAccel
  10. sys HelpDlg
  11.  
  12. macro InMessageLoop
  13.    if TranslateAccelerator( hWnd, hAccel, @wm ) = 0 then    
  14.      if not IsDialogMessage(hWnd, &wm) then
  15.        if IsDialogMessage( HelpDlg,  &wm ) = 0 then
  16.          TranslateMessage(&wm)
  17.          DispatchMessage(&wm)
  18.        end if  
  19.      end if
  20.    end if
  21. end macro
  22.  
  23. % FileDialogs       'opens FileDialog in winutil.inc
  24.  
  25. #case capital
  26. typedef sys SYS     'FileDialog.inc
  27. typedef char CHAR   'FileDialog.inc
  28.  
  29. uses winutil
  30. uses glWinUtil
  31. uses imgwin
  32.  
  33. '% review
  34. uses dialogs
  35. #autodim off
  36.  
  37. indexbase 0
  38.  
  39. % DS_CENTER=0x0800
  40. % SM_CYCAPTION 4
  41. % SWP_NOSIZE 0X1
  42. % SWP_NOZORDER 0X4
  43.  
  44. #define IDM_Load  1001
  45. #define IDM_Exit  1002
  46. #define IDM_Play  1003
  47. #define IDM_Reset 1004
  48. #define IDM_Help  1005
  49.  
  50. 'Ids for text controls in Help
  51. % IDC_LText1=1131
  52. % IDC_LText2=1132
  53.  
  54. /*
  55. declare sub InitGL()
  56. declare function initMenu(sys hWnd) as sys
  57. declare sub LoadGLTextures()
  58. declare sub DrawGLScene ()
  59. declare sub ReSizeGLScene (GLsizei width, GLsizei height)
  60. declare function GetTheFileName(string name, int a, optional string myfilter="", mytitle="") as string
  61. */
  62. #lookahead
  63.  
  64.  
  65. float points[45*45*3]    ' The Array For The Points On The Grid Of Our "Wave"
  66. macro points3d float* (r,x1,y1,z1)
  67.   'r return value
  68.  @r=@points( (x1)*45*3 + (y1)*3 + (z1) )
  69. end macro
  70.  
  71. int wiggle_count = 0     ' Counter Used To Control How Fast Flag Waves
  72.  
  73. GLfloat    xrot          'X Rotation
  74. GLfloat    yrot          'Y Rotation
  75. GLfloat    zrot          'Z Rotation
  76. GLfloat hold             'Temporarily Holds A Floating Point Value
  77.  
  78. static bool p_toggle   'Play ON/OFF
  79.  
  80. sys GdiplusToken
  81. GLuint texture[1]      'Storage for one Texture
  82. GLuint wi[1],ht[1]
  83.  
  84. 'Prepare Textures
  85. '----------------
  86. glGenTextures 1, texture
  87.  
  88.  
  89. bool is_loaded
  90. macro refreshScene()
  91.   if is_loaded then DrawGlScene()
  92.   SwapBuffers(hWndHDC)
  93. end macro
  94.  
  95. string fn=""
  96.  
  97. sys hWndHDC
  98. sys hMenu
  99. sys hInstance=inst
  100.  
  101. MainWindow(640,480,WS_OVERLAPPEDWINDOW, 2)
  102.  
  103. function WndProc ( hWnd, wMsg, wParam, lParam ) as sys callback
  104.  
  105.   select wMsg
  106.     case WM_CREATE
  107.     hMenu = initMenu(hWnd)
  108.     if SetMenu( hWnd, hMenu ) = 0 then
  109.       mbox "SetMenu hMenu failed!"
  110.     end if
  111.  
  112.     sys hGLrc
  113.     hWndhDC = GetDC( hWnd )
  114.  
  115.     SetWindowText(hWnd, "Nehe Lesson 11")
  116.  
  117.     'glWinUtil.inc
  118.    SelectPixelFormat(hWndHDC, 1)
  119.  
  120.     hGLrc = wglCreateContext( hWndHDC )
  121.     if hGLrc = 0 then
  122.       MessageBox( hWnd, "wglCreateContext failed", 0, 0 )
  123.       DestroyWindow( hWnd )
  124.     end if
  125.  
  126.     if wglMakeCurrent( hWndHDC, hGLrc ) = 0 then
  127.       MessageBox( hWnd, "wglMakeCurrent failed", 0, 0 )
  128.       DestroyWindow( hWnd )
  129.     end if
  130.  
  131.     InitGL()
  132.  
  133.     case WM_COMMAND
  134.       select loword(wParam)
  135.       case IDM_Exit      
  136.          SendMessage hWnd, WM_CLOSE, 0, 0
  137.  
  138.       case IDCANCEL
  139.          SendMessage hWnd, WM_CLOSE, 0, 0
  140.          
  141.       case IDM_Load
  142.          fn=GetTheFileName("", 0, "", "Please select Image to load")
  143.          if fn then
  144.            is_loaded=true
  145.            LoadGLTextures()
  146.            refreshScene
  147.          end if
  148.  
  149.       case IDM_Play
  150.          p_toggle = not p_toggle
  151.          
  152.       case IDM_Reset
  153.          xrot=0.0f
  154.          yrot=0.0f
  155.          zrot=0.0f
  156.  
  157.       case IDM_Help
  158.         'Open Help Window if not already open
  159.        if IsWindow( HelpDlg ) = 0 then
  160.           sys lpdt    
  161.           dyn::init(lpdt)
  162.  
  163.           Dialog( 2,  0, 0, 150, 100, "Buttons used:", lpdt,
  164.                  WS_OVERLAPPED or WS_SYSMENU or DS_CENTER or WS_VISIBLE or DS_SETFONT,
  165.                  10, "MS Sans Serif" )
  166.  
  167.           LTEXT( "", IDC_LText1,  5, 5, 50, 120 )
  168.           LTEXT( "", IDC_LText2, 55, 5,110, 120 )
  169.  
  170.           HelpDlg = CreateModelessDialog( hWnd, @HelpDlgProc, 0, lpdt )
  171.         end if
  172.  
  173.       end select
  174.        
  175.     case WM_PAINT
  176.        refreshScene
  177.  
  178.     case WM_SIZE    
  179.        ReSizeGLScene(loword(lParam),hiword(lParam)) ' LoWord=Width, HiWord=Height
  180.       refreshScene
  181.  
  182.     case WM_ERASEBKGND
  183.        return 0
  184.  
  185.     case WM_CLOSE
  186.        DestroyWindow(hWnd)
  187.  
  188.     case WM_DESTROY
  189.        glDeleteTextures 1, texture
  190.        Gdiplus 0
  191.  
  192.        if hGLrc then                                 'Rendering Context?
  193.         if not wglMakeCurrent(null,null) then       'Release the DC And RC Contexts
  194.           MessageBox(null,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
  195.          end if
  196.          if not wglDeleteContext(hGLrc) then         'Delete the RC
  197.           MessageBox(null,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
  198.          end if
  199.          hGLrc=null                                  'Set RC to null
  200.       end if
  201.  
  202.        if hWndHDC then
  203.          if not ReleaseDC(hWnd,hWndHDC) then         'Release The DC
  204.           MessageBox(null,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
  205.            hWndHDC=null                              'Set DC to null
  206.         end if
  207.        end if                
  208.        PostQuitMessage 0
  209.  
  210.     case else          
  211.        return DefWindowProc hWnd,wMsg,wParam,lParam
  212.  
  213.     end select
  214.  
  215. end function ' WndProc
  216.  
  217.  
  218. '====================================================================
  219.  
  220. function initMenu(sys hWnd) as sys
  221.  
  222.    indexbase 1
  223.  
  224.    MENU(hMenu)
  225.  
  226.    BEGIN
  227.      POPUP "&File"
  228.      BEGIN
  229.        MENUITEM "&Load Image..." tab "Ctrl+O", IDM_Load
  230.        MENUITEM "SEPARATOR"
  231.        MENUITEM "E&xit" tab "Alt+F4", IDM_Exit
  232.      ENDMenu
  233.      POPUP "&View"
  234.      BEGIN
  235.        MENUITEM "&Toggle Start / Stop" tab "Ctrl+P", IDM_Play
  236.        MENUITEM "SEPARATOR"
  237.        MENUITEM "&Reset" tab "Ctrl+R", IDM_Reset
  238.      ENDMenu
  239.      POPUP "&Help"
  240.      BEGIN
  241.        MENUITEM "&Key Options" tab "F1", IDM_Help
  242.      ENDMenu
  243.    ENDMenu
  244.  
  245.    'Accelerators
  246.   indexbase 0
  247.    ACCEL accl[3] = {
  248.    {FVIRTKEY | FCONTROL, asc("O"), IDM_Load      },
  249.    {FVIRTKEY | FCONTROL, asc("P"), IDM_Play     },
  250.    {FVIRTKEY | FCONTROL, asc("R"), IDM_Reset     },
  251.    {FVIRTKEY,            VK_F1,    IDM_Help      }
  252.    }
  253.  
  254.    hAccel = CreateAcceleratorTable( @accl, 4 )
  255.  
  256.    return hMenu
  257. end function
  258.  
  259. sub LoadGLTextures()  ' Load Bitmaps And Convert To Textures
  260.   static sys res
  261.    string imgs[1]=""
  262.    'load an image file directly as a new OpenGL texture
  263.   LoadPixImage fn,res,imgs[1],wi[1],ht[1]
  264.  
  265.    'Typical Texture Generation Using Data From The Bitmap
  266.   glBindTexture(GL_TEXTURE_2D, texture[1])
  267.    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
  268.    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
  269.    glTexImage2D GL_TEXTURE_2D, 0, 4, wi[1], ht[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, strptr imgs[1]
  270. end sub
  271.  
  272. sub InitGL()
  273.    GDIplus 1
  274.  
  275.    'SYNC FRAME RATE - 60HZ
  276.   sys p=wglgetprocaddress "wglSwapIntervalEXT" : call p 1
  277.  
  278.    glEnable(GL_TEXTURE_2D)                            'Enable Texture Mapping
  279.                                                      'All Setup For OpenGL Goes Here
  280.   glShadeModel(GL_SMOOTH)                            'Enable Smooth Shading
  281.   glClearColor(0.0f, 0.0f, 0.0f, 0.5f)               'Black Background
  282.   glClearDepth(1.0f)                                 'Depth Buffer Setup
  283.   glEnable(GL_DEPTH_TEST)                            'Enables Depth Testing
  284.   glDepthFunc(GL_LEQUAL)                             'The Type Of Depth Testing To Do
  285.   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)  'Really Nice Perspective Calculations
  286.   glPolygonMode( GL_BACK, GL_FILL )                  ' Back Face Is Solid
  287.   glPolygonMode( GL_FRONT, GL_LINE )                 ' Front Face Is Made Of Lines
  288.  
  289.    ' Loop Through The X Plane
  290.   int x,y
  291.    float xf,yf
  292.    for x=0 to <45
  293.       xf=float(x)
  294.       for y=0 to <45
  295.          yf=float(y)
  296.          points3d(x,y,0)= (xf/5.0f)-4.5f
  297.          points3d(x,y,1)= (yf/5.0f)-4.5f
  298.          points3d(x,y,2)= sin((((xf/5.0f)*40.0f)/360.0f)*3.141592654*2.0f)
  299.       next y
  300.    next x
  301.  
  302. end sub
  303.  
  304. sub DrawGLScene ()
  305.    int x, y
  306.    float xf,yf
  307.    float float_x, float_y, float_xb, float_yb
  308.  
  309.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)   ' Clear The Screen And The Depth Buffer
  310.   glLoadIdentity()                                     ' Reset The View
  311.  
  312.    glTranslatef(0.0f,0.0f,-12.0f)
  313.      
  314.    glRotatef(xrot,1.0f,0.0f,0.0f)
  315.    glRotatef(yrot,0.0f,1.0f,0.0f)  
  316.    glRotatef(zrot,0.0f,0.0f,1.0f)
  317.  
  318.    glBindTexture(GL_TEXTURE_2D, texture[0])
  319.  
  320.    glBegin(GL_QUADS)
  321.    for x = 0 to <44
  322.        xf=float(x)
  323.       for y = 0 to <44
  324.          yf=float(y)
  325.          float_x = xf/44.0f
  326.          float_y = yf/44.0f
  327.          float_xb = (xf+1)/44.0f
  328.          float_yb = (yf+1)/44.0f
  329.  
  330.          glTexCoord2f( float_x, float_y)
  331.          glVertex3f( points3d(x,y,0), points3d(x,y,1), points3d(x,y,2) )
  332.  
  333.          glTexCoord2f( float_x, float_yb )
  334.          glVertex3f( points3d(x,y+1,0), points3d(x,y+1,1), points3d(x,y+1,2) )
  335.  
  336.          glTexCoord2f( float_xb, float_yb )
  337.          glVertex3f( points3d(x+1,y+1,0), points3d(x+1,y+1,1), points3d(x+1,y+1,2) )
  338.  
  339.          glTexCoord2f( float_xb, float_y )
  340.          glVertex3f( points3d(x+1,y,0), points3d(x+1,y,1), points3d(x+1,y,2) )
  341.       next y
  342.    next x
  343.    glEnd()
  344.  
  345.    if wiggle_count = 2 then
  346.       for y = 0 to <45
  347.          hold=points3d(0,y,2)
  348.          for x = 0 to <44
  349.             points3d(x,y,2) = points3d(x+1,y,2)
  350.          next x
  351.          points3d(44,y,2)=hold
  352.       next y
  353.       wiggle_count = 0
  354.    end if
  355.  
  356.    if p_toggle then
  357.      wiggle_count++
  358.    
  359.      xrot+=0.3f
  360.      yrot+=0.2f
  361.      zrot+=0.4f
  362.    end if    
  363. end sub
  364.  
  365. sub ReSizeGLScene (GLsizei width, GLsizei height)  ' Resize And Initialize The GL Window
  366.   if height=0 then              ' Prevent A Divide By Zero By
  367.     height=1                    ' Making Height Equal One
  368.   end if
  369.  
  370.    glViewport(0,0,width,height)  ' Reset The Current Viewport
  371.  
  372.    glMatrixMode(GL_PROJECTION)   ' Select The Projection Matrix
  373.   glLoadIdentity()              ' Reset The Projection Matrix
  374.  
  375.    ' Calculate The Aspect Ratio Of The Window
  376.   gluPerspective(45.0f, width/ height,0.1f,100.0f)
  377.  
  378.    glMatrixMode(GL_MODELVIEW)    ' Select The Modelview Matrix
  379.   glLoadIdentity()              ' Reset The Modelview Matrix
  380. end sub
  381.  
  382. function GetTheFileName(string name, int a, optional string myfilter="", mytitle="") as string
  383.    'adapted from FileDialog.inc
  384.   sys hwnd
  385.    char fdir[512] : GetCurrentDirectory 512, fdir
  386.    string dir = fdir
  387.  
  388.    string sep=chr(0)
  389.    string filter=
  390.      "images"+sep+"*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.gif;*.ico"+sep+
  391.      "all files"+sep+"*.*"+sep+sep
  392.    if len(myfilter) then filter=myfilter + sep
  393.  
  394.    string title
  395.    if a then
  396.      title="Save File as"
  397.    else
  398.      title="Load File"
  399.    end if
  400.    if len mytitle then title=mytitle
  401.  
  402.    sys flags = OFN_EXPLORER or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY
  403.    string fname = FileDialog(dir,filter,title,name,hwnd,flags,a)
  404.  
  405.    return fname
  406. end function
  407.  
  408. function HelpDlgProc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
  409.    string Result
  410.    sys hLText1=GetDlgItem(hDlg, IDC_LText1)
  411.    sys hLText2=GetDlgItem(hDlg, IDC_LText2)
  412.  
  413.    select case uMsg
  414.  
  415.      case WM_INITDIALOG
  416.         string HelpText1 =
  417. "
  418. F1          
  419. Ctrl-O
  420. Ctrl-P
  421. Ctrl-R
  422. ESC
  423. Alt-F4  
  424. "
  425.         string HelpText2 =
  426. "      
  427. This Help  
  428. Load an image      
  429. Start / Stop Animation
  430. Reset to original state    
  431. Quit
  432. Quit
  433. "
  434.         SetWindowText (hLText1, HelpText1)
  435.         SetWindowText (hLText2, HelpText2)      
  436.        
  437.      case WM_COMMAND
  438.         select case loword(wParam)      
  439.           case IDCANCEL
  440.              DestroyWindow( hDlg )
  441.         end select
  442.  
  443.      case WM_CLOSE
  444.         DestroyWindow( hDlg )
  445.  
  446.         'Reset Help dialog
  447.        HelpDlg = 0
  448.  
  449.    end select
  450.  
  451.    return 0
  452. end function
  453.  
« Last Edit: May 03, 2018, 03:28:27 AM by Arnold »