Author Topic: Problem with using ImgWin.inc  (Read 2037 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Problem with using ImgWin.inc
« on: February 15, 2018, 05:04:15 AM »
Hi Charles,

I am trying to port some NEHE tutorials using WinUtil.inc and glWinUtil.inc. This works fine for lesson 4 and 5. For lesson 6 I would like to load an image like you did in examples\OpenGl\ImageRotate1.o2bas. But if I try to include ImageWin.inc then I get an error:
Unidentified operand NULL in line 82

I experimented a little bit, but I did not succeed. What am I doing wrong? It would be great if GDIplus could be applied.

Roland

Code: [Select]
'Adapted from Nehe tutorial lesson 6

$ filename "tut06.exe"
'uses RTL32
'uses RTL64


uses winutil
uses glWinUtil
'uses imgwin


% SM_CYCAPTION 4
% SWP_NOSIZE 0X1
% SWP_NOZORDER 0X4


declare sub InitGL()
declare sub DrawGLScene ()
declare sub ReSizeGLScene (GLsizei width, GLsizei height)
declare sub O2Center(sys hwnd)

'#lookahead

GLfloat    xrot                'X Rotation
GLfloat    yrot                'Y Rotation
GLfloat    zrot                'Z Rotation

dim as GLuint texture[1]={0}   'Storage for one Texture

sys hWndHDC

MainWindow(640,480,WS_OVERLAPPEDWINDOW)

function WndProc ( hWnd, wMsg, wParam, lparam ) as sys callback

    select wMsg
      case WM_CREATE
      sys hGLrc
      hWndhDC = GetDC( hWnd )

      SetWindowText(hWnd, "Nehe Lesson 6")
      O2Center(hWnd)
     
      'glWinUtil.inc
      SelectPixelFormat(hWndHDC, 1)

      hGLrc = wglCreateContext( hWndHDC )
      if hGLrc = 0 then
        MessageBox( hWnd, "wglCreateContext failed", 0, 0 )
        DestroyWindow( hWnd )
      end if

      if wglMakeCurrent( hWndHDC, hGLrc ) = 0 then
        MessageBox( hWnd, "wglMakeCurrent failed", 0, 0 )
        DestroyWindow( hWnd )
      end if

      InitGL()

      case WM_PAINT
      DrawGlScene()
      SwapBuffers( hWndHDC )
       
      case WM_SIZE   
      ReSizeGLScene(loword(lParam),hiword(lParam))    ' LoWord=Width, HiWord=Height

      case WM_ERASEBKGND                              ' Avoid Flickering
      return 0
     
      case WM_CLOSE
      DestroyWindow(hWnd)
     
      case WM_DESTROY
      if hGLrc then                                 'Rendering Context?
        if not wglMakeCurrent(NULL,NULL) then       'Release the DC And RC Contexts   
          MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
        end if
        if not wglDeleteContext(hGLrc) then         'Delete the RC
          MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
        end if
        hGLrc=NULL                                  'Set RC to NULL
      end if

      if hWndHDC then
        if not ReleaseDC(hWnd,hWndHDC) then         'Release The DC
          MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
          hWndHDC=NULL                              'Set DC to NULL
        end if
      end if               
      PostQuitMessage 0

      case else           
      return DefWindowProc hWnd,wMsg,wParam,lParam
       
    end select

end function ' WndProc


'====================================================================

sub InitGL()
''   if LoadGLTextures()=0 then                      'Jump To Texture Loading Routine
'       message and return                           'If Texture Didn't Load Return FALSE
''   end if

  glEnable(GL_TEXTURE_2D)                            'Enable Texture Mapping
                                                     'All Setup For OpenGL Goes Here
  glShadeModel(GL_SMOOTH)                            'Enable Smooth Shading
  glClearColor(0.0f, 0.0f, 0.0f, 0.5f)               'Black Background
  glClearDepth(1.0f)                                 '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
end sub

sub DrawGLScene ()
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 'Clear Screen And Depth Buffer 
  glLoadIdentity()                    'Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,-5.0f)

    glRotatef(xrot,1.0f,0.0f,0.0f)
    glRotatef(yrot,0.0f,1.0f,0.0f)
    glRotatef(zrot,0.0f,0.0f,1.0f)

    glBindTexture(GL_TEXTURE_2D, texture[0])

    glBegin(GL_QUADS)
        'Front Face
        glTexCoord2f(0.0f, 0.0f) : glVertex3f(-1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f( 1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f( 1.0f,  1.0f,  1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f(-1.0f,  1.0f,  1.0f)
        'Back Face
        glTexCoord2f(1.0f, 0.0f) : glVertex3f(-1.0f, -1.0f, -1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f(-1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f( 1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f( 1.0f, -1.0f, -1.0f)
        'Top Face
        glTexCoord2f(0.0f, 1.0f) : glVertex3f(-1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f(-1.0f,  1.0f,  1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f( 1.0f,  1.0f,  1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f( 1.0f,  1.0f, -1.0f)
        'Bottom Face
        glTexCoord2f(1.0f, 1.0f) : glVertex3f(-1.0f, -1.0f, -1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f( 1.0f, -1.0f, -1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f( 1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f(-1.0f, -1.0f,  1.0f)
        'Right face
        glTexCoord2f(1.0f, 0.0f) : glVertex3f( 1.0f, -1.0f, -1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f( 1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f( 1.0f,  1.0f,  1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f( 1.0f, -1.0f,  1.0f)
        'Left Face
        glTexCoord2f(0.0f, 0.0f) : glVertex3f(-1.0f, -1.0f, -1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f(-1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f(-1.0f,  1.0f,  1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f(-1.0f,  1.0f, -1.0f)
    glEnd()

    xrot+=0.3f
    yrot+=0.2f
    zrot+=0.4f
end sub

sub ReSizeGLScene (GLsizei width, GLsizei height)    ' Resize And Initialize The GL Window
  if height=0 then              ' Prevent A Divide By Zero By
    height=1                    ' Making Height Equal One
  end if

  glViewport(0,0,width,height)  ' Reset The Current Viewport

  glMatrixMode(GL_PROJECTION)   ' Select The Projection Matrix
  glLoadIdentity()              ' Reset The Projection Matrix

  ' Calculate The Aspect Ratio Of The Window
  gluPerspective(45.0f, width/ height,0.1f,100.0f)

  glMatrixMode(GL_MODELVIEW)    ' Select The Modelview Matrix
  glLoadIdentity()              ' Reset The Modelview Matrix
end sub

Sub O2Center(sys hwnd)
    Dim As RECT WndRect
    Dim As sys x,y
    GetWindowRect(hwnd,&WndRect)
    x = (GETSYSTEMMETRICS(%SM_CXSCREEN) - (WndRect.Right-WndRect.Left))/2
    y = (GETSYSTEMMETRICS(%SM_CYSCREEN) - (WndRect.Bottom-WndRect.Top+GETSYSTEMMETRICS(%SM_CYCAPTION)))/2
    SetWindowPos (hWnd, NULL, x, y, 0, 0, SWP_NOSIZE OR SWP_NOZORDER)
End Sub

Charles Pegge

  • Guest
Re: Problem with using ImgWin.inc
« Reply #1 on: February 15, 2018, 09:40:08 AM »
Hi Roland,

imgwin is quite raw C (also gl.h) with a few capitalisation conflicts. So we have to use #case capital, and then ensure that there are no unintended uppercase words in the code.

[attachment deleted by admin]

Arnold

  • Guest
Re: Problem with using ImgWin.inc
« Reply #2 on: February 16, 2018, 01:13:27 AM »
Thank you Charles. Using GETSYSTEMMETRICS was a careless mistake of mine, but #case capital was the key. I have to experiment a little bit more as I am still doing something wrong, but I think it should be possible now to load an image with GDI plus and create a texture.

Roland

Arnold

  • Guest
Re: Problem with using ImgWin.inc
« Reply #3 on: February 16, 2018, 04:34:32 AM »
It works. What a surprise! But now I have to check first what I have done.

Arnold

  • Guest
Re: Problem with using ImgWin.inc - Solved
« Reply #4 on: February 16, 2018, 01:18:06 PM »
I have added a small menu with the option to load an image. All kind of images can be loaded: bmp, jpg, png, ico (more?). There are images in folder examples\images, projectB\Scintilla\, projectsB\GDI etc to test.
Maybe I have not applied the best solution, but the app works and I am quite happy with it. All is done by using Oxygenbasic without external libraries and this was my intention.

Roland

Code: [Select]
'Adapted from Nehe tutorial lesson 6

$ filename "tut06.exe"
'uses RTL32
'uses RTL64

'uses console

uses FileDialog

string fn=""

! GetCurrentDirectory lib "kernel32.dll" alias "GetCurrentDirectoryA" (dword nBufferLength,char* lpBuffer) as dword
function CurDir() as string
  char s[512]
  GetCurrentDirectory 512, s
  return s
end function

function GetTheFileName(string name, sys a,
            optional string myfilter="", mytitle="") as string
   'adapted from FileDialog.inc

   sys hwnd
   string dir=CurDir

   string sep=chr(0)
   string filter=
     "images"+sep+"*.bmp;*.jpg;*.png;*.ico"+sep+
     "all files"+sep+"*.*"+sep+sep
   if len(myfilter) then filter=myfilter + sep

   string title
   if a then
     title="Save File as"
   else
     title="Load File"
   end if
   if len mytitle then title=mytitle

   sys flags = OFN_EXPLORER or OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY

   string fname = FileDialog(dir,filter,title,name,hwnd,flags,a)
   return fname
end function

#case capital
uses winutil
uses glWinUtil
uses imgwin


% SM_CYCAPTION 4
% SWP_NOSIZE 0X1
% SWP_NOZORDER 0X4

#define IDM_FILE_OPEN 1001
#define IDM_APP_EXIT  1002

declare sub InitGL()
declare sub LoadGLTextures()
declare sub DrawGLScene ()
declare sub ReSizeGLScene (GLsizei width, GLsizei height)
declare sub O2Center(sys hwnd)

'#lookahead

GLfloat    xrot                'X Rotation
GLfloat    yrot                'Y Rotation
GLfloat    zrot                'Z Rotation

   
sys GdiplusToken
GLuint texture[1]              'Storage for one Texture
GLuint wi[1],ht[1]

sys hWndHDC

MainWindow(640,480,WS_OVERLAPPEDWINDOW)

function WndProc ( hWnd, wMsg, wParam, lParam ) as sys callback

    select wMsg
      case WM_CREATE
      sys hMenu = CreateMenu
      sys hMenuPopup = CreateMenu
      AppendMenu hMenu,      MF_POPUP,     hMenuPopup,       "&File"
      AppendMenu hMenuPopup, MF_STRING,    IDM_FILE_OPEN,    "&Load Image ..."
      AppendMenu hMenuPopup, MF_SEPARATOR, 0,                null
      AppendMenu hMenuPopup, MF_STRING,    IDM_APP_EXIT,     "E&xit"
      SetMenu    hwnd, hMenu
      sys hGLrc
      hWndhDC = GetDC( hWnd )

      SetWindowText(hWnd, "Nehe Lesson 6")
      O2Center(hWnd)
     
      'glWinUtil.inc
      SelectPixelFormat(hWndHDC, 1)

      hGLrc = wglCreateContext( hWndHDC )
      if hGLrc = 0 then
        MessageBox( hWnd, "wglCreateContext failed", 0, 0 )
        DestroyWindow( hWnd )
      end if

      if wglMakeCurrent( hWndHDC, hGLrc ) = 0 then
        MessageBox( hWnd, "wglMakeCurrent failed", 0, 0 )
        DestroyWindow( hWnd )
      end if

      InitGL()

      case WM_COMMAND
      if wParam = IDM_APP_EXIT then       
        SendMessage hWnd, WM_CLOSE, 0, 0
      end if
      if wParam = IDM_FILE_OPEN then
        fn=GetTheFileName("", 0, "", "Please select Image to load")
        LoadGLTextures()
      end if

      case WM_PAINT
      DrawGlScene()
      SwapBuffers( hWndHDC )
       
      case WM_SIZE   
      ReSizeGLScene(loword(lParam),hiword(lParam))    ' LoWord=Width, HiWord=Height

      case WM_ERASEBKGND                              ' Avoid Flickering
      return 0
     
      case WM_CLOSE
      DestroyWindow(hWnd)
     
      case WM_DESTROY
      glDeleteTextures 1, texture
      Gdiplus 0
     
      if hGLrc then                                 'Rendering Context?
        if not wglMakeCurrent(null,null) then       'Release the DC And RC Contexts   
          MessageBox(null,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
        end if
        if not wglDeleteContext(hGLrc) then         'Delete the RC
          MessageBox(null,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
        end if
        hGLrc=null                                  'Set RC to null
      end if

      if hWndHDC then
        if not ReleaseDC(hWnd,hWndHDC) then         'Release The DC
          MessageBox(null,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
          hWndHDC=null                              'Set DC to null
        end if
      end if               
      PostQuitMessage 0

      case else           
      return DefWindowProc hWnd,wMsg,wParam,lParam
       
    end select

end function ' WndProc



'====================================================================
 
sub LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
  '
  static sys res
  string imgs[1]=""
  'load an image file directly as a new OpenGL texture
  LoadPixImage fn,res,imgs[1],wi[1],ht[1]
 
  'Typical Texture Generation Using Data From The Bitmap
  glBindTexture(GL_TEXTURE_2D, texture[1]);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  glTexImage2D GL_TEXTURE_2D, 0, 4, wi[1], ht[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, strptr imgs[1]
end sub

sub InitGL()
  GDIplus 1

  'Prepare Textures
  '----------------
  glGenTextures 1, texture

  LoadGLTextures

  glEnable(GL_TEXTURE_2D)                            'Enable Texture Mapping
                                                     'All Setup For OpenGL Goes Here
  glShadeModel(GL_SMOOTH)                            'Enable Smooth Shading
  glClearColor(0.0f, 0.0f, 0.0f, 0.5f)               'Black Background
  glClearDepth(1.0f)                                 '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
end sub

sub DrawGLScene ()
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 'Clear Screen And Depth Buffer 
  glLoadIdentity()                    'Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,-5.0f)

    glRotatef(xrot,1.0f,0.0f,0.0f)
    glRotatef(yrot,0.0f,1.0f,0.0f)
    glRotatef(zrot,0.0f,0.0f,1.0f)

    glBindTexture(GL_TEXTURE_2D, texture[1])

    glBegin(GL_QUADS)
        'Front Face
        glTexCoord2f(0.0f, 0.0f) : glVertex3f(-1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f( 1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f( 1.0f,  1.0f,  1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f(-1.0f,  1.0f,  1.0f)
        'Back Face
        glTexCoord2f(1.0f, 0.0f) : glVertex3f(-1.0f, -1.0f, -1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f(-1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f( 1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f( 1.0f, -1.0f, -1.0f)
        'Top Face
        glTexCoord2f(0.0f, 1.0f) : glVertex3f(-1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f(-1.0f,  1.0f,  1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f( 1.0f,  1.0f,  1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f( 1.0f,  1.0f, -1.0f)
        'Bottom Face
        glTexCoord2f(1.0f, 1.0f) : glVertex3f(-1.0f, -1.0f, -1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f( 1.0f, -1.0f, -1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f( 1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f(-1.0f, -1.0f,  1.0f)
        'Right face
        glTexCoord2f(1.0f, 0.0f) : glVertex3f( 1.0f, -1.0f, -1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f( 1.0f,  1.0f, -1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f( 1.0f,  1.0f,  1.0f)
        glTexCoord2f(0.0f, 0.0f) : glVertex3f( 1.0f, -1.0f,  1.0f)
        'Left Face
        glTexCoord2f(0.0f, 0.0f) : glVertex3f(-1.0f, -1.0f, -1.0f)
        glTexCoord2f(1.0f, 0.0f) : glVertex3f(-1.0f, -1.0f,  1.0f)
        glTexCoord2f(1.0f, 1.0f) : glVertex3f(-1.0f,  1.0f,  1.0f)
        glTexCoord2f(0.0f, 1.0f) : glVertex3f(-1.0f,  1.0f, -1.0f)
    glEnd()

    xrot+=0.3f
    yrot+=0.2f
    zrot+=0.4f
end sub

sub ReSizeGLScene (GLsizei width, GLsizei height)    ' Resize And Initialize The GL Window
  if height=0 then              ' Prevent A Divide By Zero By
    height=1                    ' Making Height Equal One
  end if

  glViewport(0,0,width,height)  ' Reset The Current Viewport

  glMatrixMode(GL_PROJECTION)   ' Select The Projection Matrix
  glLoadIdentity()              ' Reset The Projection Matrix

  ' Calculate The Aspect Ratio Of The Window
  gluPerspective(45.0f, width/ height,0.1f,100.0f)

  glMatrixMode(GL_MODELVIEW)    ' Select The Modelview Matrix
  glLoadIdentity()              ' Reset The Modelview Matrix
end sub

sub O2Center(sys hwnd)
    RECT WndRect
    sys x,y
    GetWindowRect(hwnd,&WndRect)
    x = (GetSystemMetrics(SM_CXSCREEN) - (WndRect.Right-WndRect.Left))/2
    y = (GetSystemMetrics(SM_CYSCREEN) - (WndRect.Bottom-WndRect.Top+GetSystemMetrics(SM_CYCAPTION)))/2
    SetWindowPos (hWnd, null, x, y, 0, 0, SWP_NOSIZE or SWP_NOZORDER)
end sub


[attachment deleted by admin]

jcfuller

  • Guest
Re: Problem with using ImgWin.inc
« Reply #5 on: February 16, 2018, 02:23:10 PM »
Roland,
  Are you using the latest OxygenBasic download?
I get this when compiling with uses RTL64
Linker found unidentified names:
::TRUE   level 0

James

Arnold

  • Guest
Re: Problem with using ImgWin.inc
« Reply #6 on: February 17, 2018, 01:11:25 AM »
Hi James,

for developing I used the latest final version of Oxygenbasic (Jan 02/2018) with Oxygen.dll of Jan 13/2018) and slightly modified WinUtil.inc and OpenglSceneFrame.inc which were provided by Charles.
But it also worked for me with OxygenBasicProgress Feb 14. I did not get compile errors. So I am not quite sure what is missing. Oxygen transforms true to -1, so TRUE must be defined somewhere else in the include files.

Roland

Charles Pegge

  • Guest
Re: Problem with using ImgWin.inc
« Reply #7 on: February 17, 2018, 01:20:34 AM »
James,
There is an uppercase TRUE which needs to be lowercase in inc/winutil.inc

Roland,
Some PCs do not have their 60Hz frame rate preset, On my notebook the cube spins at a crazy speed.

You can add this to the start of initgl()
Code: [Select]
  'SYNC FRAME RATE - 60HZ
  sys p=wglgetprocaddress "wglSwapIntervalEXT" : call p 1

Arnold

  • Guest
Re: Problem with using ImgWin.inc
« Reply #8 on: February 17, 2018, 01:30:10 AM »
I think I found the difference. If I use gxo2.exe as compiler I will get the error message which James reported when compiling to an exe file. But using co2.exe should work.
Edit: I was wrong. After restarting my computer there is no difference between using co2.exe and gxo2.exe.

@Charles,
thank you for the hint.  Probably for my next experiment I should also use a timer.

Roland
« Last Edit: February 17, 2018, 01:50:15 AM by Arnold »

Charles Pegge

  • Guest
Re: Problem with using ImgWin.inc
« Reply #9 on: February 17, 2018, 05:41:38 AM »

To make the code compliant with 64bit compiling, I have changed some sys variables into int. Type matching is important when passing variables byref, and int remains 32bit whereas sys becomes 64bit

imgwin.inc goes into the inc folder





[attachment deleted by admin]