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
'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]