AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

OpenGL DllCalls
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Zippo



Joined: 21 Apr 2006
Posts: 56
Location: East Coast, USA

PostPosted: Tue Apr 22, 2008 10:05 pm    Post subject: OpenGL DllCalls Reply with quote

EDIT 4/26/08: Cleaned up the script a bit.

After much headache I finally tracked down most of the problems.

For anyone interested, I've updated the script and 2 include files below (be sure to grab the newest include files below or you will be missing some needed constants if you want to test it).

It is now a working example of OpenGL in AHK. The code is a mess and all. If I ever do anything useful with it, I'll clean it up and put it in the Scripts section.

To test it you'll need:
GL.ahk
WINDERS.ahk

Here is the script:
Code:
;   This script is based off of Nehe's First Polygon Tutrorial, which can be found at
;   http://nehe.gamedev.net


;/////////////////////////////////////////////////////////////////////////////////////////////
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.   ;//
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.   ;//
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.                    ;//
;/////////////////////////////////////////////////////////////////////////////////////////////

#include WINDERS.ahk
#include GL.ahk

OnExit, Exit

hOpenGL := DllCall("LoadLibrary", Str, "opengl32.dll")
hGlu32  := DllCall("LoadLibrary", Str, "glu32.dll")
hGdi32  := DllCall("LoadLibrary", Str, "gdi32.dll")

hInstance := 0
hWnd := 0
ATOM := 0
hDC := 0
hRC := 0
F1 := 0
active := 1
fullscreen := 1
lpszClassName := "OpenGL"

   WinMain()

; ==============================================================================================

ReSizeGLScene()
{

   Global

   If height = 0               ;Prevent divide by 0
      height := 1

   DllCall("opengl32.dll\glViewport", Int, 0, Int, 0, Int, width, Int, height)
   DllCall("opengl32.dll\glMatrixMode", UInt, GL_PROJECTION)
   DllCall("opengl32.dll\glLoadIdentity")

   ; Calculate aspect ratio of the window
   DllCall("glu32.dll\gluPerspective", Double, 45.0, Double, width/height, Double, 0.1, Double, 100.0)

   DllCall("opengl32.dll\glMatrixMode", UInt, GL_MODELVIEW)
   DllCall("opengl32.dll\glLoadIdentity")

   Return

}




InitGL()
{

   Global

   DllCall("opengl32.dll\glShadeModel", UInt, GL_SMOOTH)
   DllCall("opengl32.dll\glClearColor", Float, 0.0, Float, 0.0, Float, 0.0, Float, 0.5)
   DllCall("opengl32.dll\glClearDepth", Double, 1.0)
   DllCall("opengl32.dll\glEnable", UInt, GL_DEPTH_TEST)
   DllCall("opengl32.dll\glDepthFunc", UInt, GL_LEQUAL)
   DllCall("opengl32.dll\glHint", UInt, GL_PERSPECTIVE_CORRECTION_HINT, UInt, GL_NICEST)

   Return 1

}




DrawGLScene()
{

   Global

   DllCall("opengl32.dll\glClear", Int, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   DllCall("opengl32.dll\glLoadIdentity")

   DllCall("opengl32.dll\glTranslatef", Float, -1.5, Float, 0.0, Float, -6.0)
   DllCall("opengl32.dll\glBegin", UInt, GL_TRIANGLES)

      DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0)

   DllCall("opengl32.dll\glEnd")

   DllCall("opengl32.dll\glTranslatef", Float, 3.0, Float, 0.0, Float, 0.0)
   DllCall("opengl32.dll\glBegin", UInt, GL_QUADS)

      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0)

   DllCall("opengl32.dll\glEnd")


   DllCall("gdi32.dll\SwapBuffers", UInt, hDC)

   Return 1

}




KillGLWindow()
{

   Global

   If fullscreen
   {
      DllCall("ChangeDisplaySettings", UInt, 0, UInt, 0)
      DllCall("ShowCursor", Int, 1)
   }

   If hRC
   {
      If !DllCall("opengl32.dll\wglMakeCurrent", UInt, 0, UInt, 0)
         MsgBox, Release of DC and RC failed.`nError: %A_LastError%

      If !DllCall("opengl32.dll\wglDeleteContext", UInt, hRC)
         MsgBox, Release rendering context failed.`nError: %A_LastError%
   }

   If hDC
   {
      If !DllCall("ReleaseDC", UInt, hWnd, UInt, hDC)
         MsgBox, Release device context failed.`nError: %A_LastError%
   }

   If hWnd
   {
      If !DllCall("DestroyWindow", UInt, hWnd)
         MsgBox, Could not release hWnd.`nError: %A_LastError%
   }

   If ATOM
   {
      If !DllCall("UnregisterClass", UInt, &lpszClassName, UInt, hInstance)
         MsgBox, Could not unregister class.`nError: %A_LastError%
   }

   hInstance := 0
   hWnd := 0
   ATOM := 0
   hDC := 0
   hRC := 0
}




CreateGLWindow(title, width, height, bits)
{

   Global

   VarSetCapacity(wc, 40, 0)
   VarSetCapacity(WindowRect, 16, 0)
   VarSetCapacity(pfd, 40, 0)

   NumPut(0, WindowRect, 0, "Int")
   NumPut(0, WindowRect, 4, "Int")
   NumPut(width, WindowRect, 8, "Int")
   NumPut(height, WindowRect, 12, "Int")

   Style := CS_HREDRAW | CS_VREDRAW | CS_OWNDC
   lpfnWndProc := RegisterCallback("WndProc", "", 4)
   cbClsExtra := 0
   cbWndExtra := 0
   hInstance := DllCall("GetModuleHandle", UInt, 0)
   hbrBackground := 0
   hCursor := DllCall("LoadCursor", UInt, 0, UInt, IDC_ARROW)
   hIcon := DllCall("LoadIcon", UInt, 0, UInt, IDI_APPLICATION)
   lpszMenuName := 0

   NumPut(Style, wc, 0, "UInt")
   NumPut(lpfnWndProc, wc, 4, "UInt")
   NumPut(cbClsExtra, wc, 8, "UInt")
   NumPut(cbWndExtra, wc, 12, "UInt")
   NumPut(hInstance, wc, 16, "UInt")
   NumPut(hIcon, wc, 20, "UInt")
   NumPut(hCursor, wc, 24, "UInt")
   NumPut(hbrBackground, wc, 28, "UInt")
   NumPut(&lpszMenuName, wc, 32, "UInt")
   NumPut(&lpszClassName, wc, 36, "UInt")

   ATOM := DllCall("RegisterClass", UInt, &wc)
   If !ATOM
   {
      MsgBox, Failed to register class.`nError: %A_LastError%
      ExitApp
   }

   If fullscreen
   {
      ; DEVMODE size: 156 ANSI, 220 UNICODE
      VarSetCapacity(dmScreenSettings, 156, 0)

      dmFields := DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT

      NumPut(156, dmScreenSettings, 0, "UShort")

      Result := DllCall("EnumDisplaySettings", UInt, 0, UInt, 0, UInt, &dmScreenSettings)

      NumPut(width, dmScreenSettings, 108, "UInt")
      NumPut(height, dmScreenSettings, 112, "UInt")
      NumPut(bits, dmScreenSettings, 104, "UInt") ;bits
      NumPut(dmFields, dmScreenSettings, 40, "UInt")

      Result := DllCall("ChangeDisplaySettings", UInt, &dmScreenSettings, UInt, CDS_FULLSCREEN)
      If Result != %DISP_CHANGE_SUCCESSFUL%
      {
         MsgBox,4, Error, The requested fullscreen mode is not supported by`nyour video card. Use windowed mode instead?
         IfMsgBox Yes
            fullscreen := 0
         Else
         {
            ; Let the user know the program is closing
            MsgBox, The program will now close.
            ExitApp
         }
      }
   }

   If fullscreen
   {
      dwExStyle := WS_EX_APPWINDOW
      dwStyle := WS_POPUP
      DllCall("ShowCursor", Int, 0)
   }
   Else
   {
      dwExStyle := WS_EX_APPWINDOW | WS_EX_WINDOWEDGE
      dwStyle := WS_OVERLAPPEDWINDOW
   }

   DllCall("AdjustWindowRectEx", UInt, &WindowRect, UInt, dwStyle, UInt, 0, UInt, dwExStyle)

   hWnd := DllCall("CreateWindowEx", UInt, dwExStyle
               , UInt, &lpszClassName
               , "Str", title
               , UInt, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN
               , Int, 0
               , Int, 0
               , Int, NumGet(WindowRect, 8, "Int")-NumGet(WindowRect, 0, "Int")
               , Int, NumGet(WindowRect, 12, "Int")-NumGet(WindowRect, 4, "Int")
               , UInt, 0
               , UInt, 0
               , UInt, hInstance
               , UInt, 0
               , UInt)
   If !hWnd
   {
      MsgBox, Window creation error.`nError: %A_LastError%
      ExitApp
   }

   dwFlags := PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER

   NumPut(40, pfd, 0, "UShort")
   NumPut(1, pfd, 2, "UShort")
   NumPut(dwFlags, pfd, 4, "UInt")
   NumPut(PFD_TYPE_RGBA, pfd, 8, "UChar")
   NumPut(bits, pfd, 9, "UChar")
   NumPut(16, pfd, 23, "UChar")
   NumPut(PFD_MAIN_PLANE, pfd, 26, "UChar")

   hDC := DllCall("GetDC", UInt, hWnd)
   If !hDC
   {
      MsgBox, Can't create GL device context.`nError: %A_LastError%
      ExitApp
   }

   PixelFormat := DllCall("gdi32.dll\ChoosePixelFormat", UInt, hDC, UInt, &pfd)
   If !PixelFormat
   {
      MsgBox, Can't find a suitable pixel format.`nError: %A_LastError%
      ExitApp
   }

   If !DllCall("gdi32.dll\SetPixelFormat", UInt, hDC, UInt, PixelFormat, UInt, &pfd)
   {
      MsgBox, Can't set the pixel format.`nError: %A_LastError%
      ExitApp
   }

   hRC := DllCall("opengl32.dll\wglCreateContext", UInt, hDC)
   If !hRC
   {
      MsgBox, Can't create GL rendering context.`nError: %A_LastError%
      ExitApp
   }

   If !DllCall("opengl32.dll\wglMakeCurrent", UInt, hDC, UInt, hRC)
   {
      MsgBox, Can't activate the GL rendering context.`nError: %A_LastError%
      ExitApp
   }

   DllCall("ShowWindow", UInt, hWnd, Int, SW_SHOW)
   DllCall("SetForegroundWindow", UInt, hWnd)
   DllCall("SetFocus", UInt, hWnd)
   ReSizeGLScene()

   If !InitGL()
   {
      MsgBox, Initialization failed.
      ExitApp
   }

   Return 1

}




WndProc(hwnd, uMsg, wParam, lParam)
{

   Global

   If uMsg = %WM_ACTIVATE%
   {
      If !(wParam >> 16)
         active := 1
      Else
         active := 0

      Return 0
   }

   If uMsg = %WM_SYSCOMMAND%
   {
      If wParam = %SC_SCREENSAVE%
         Return 0

      If wParam = %SC_MONITORPOWER%
         Return 0
   }

   If uMsg = %WM_CLOSE%
      ExitApp

   If uMsg = %WM_SIZE%
   {
      width := lParam & 0x0ffff
      height := lParam >> 16
      ReSizeGLScene()
      Return 0
   }

   Return DllCall("DefWindowProc", UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam)

}




WinMain()
{

   Global

   MsgBox, 4, Start Fullscreen?, Would you like to run in fullscreen mode?
   IfMsgBox No
      fullscreen := 0

   CreateGLWindow("Nehe's First Polygon Tutorial", 640, 480, 16)

   Loop
   {
      If F1
      {
         KillGLWindow()
         F1 := 0
         fullscreen := !fullscreen
         CreateGLWindow("Nehe's First Polygon Tutorial", 640, 480, 16)
      }

      If active
      If !DrawGLScene()
         Break
   }

   ExitApp

}

; ==============================================================================================

Esc::ExitApp
F1::F1++

Exit:
KillGLWindow()

If hOpenGL
   DllCall("FreeLibrary", UInt, hOpenGL)
If hGlu32
   DllCall("FreeLibrary", UInt, hGlu32)
If hGdi32
   DllCall("FreeLibrary", UInt, hGdi32)
ExitApp


F1 toggles fullscreen, Escape to exit.
_________________
____________________


Last edited by Zippo on Sat Apr 26, 2008 7:47 am; edited 2 times in total
Back to top
View user's profile Send private message
Zippo()
Guest





PostPosted: Wed Apr 23, 2008 6:15 pm    Post subject: Reply with quote

Well, thinking the thread created by SetTimer might be causing problems, I removed the timer and used a loop in its place. No change.

I went back and checked the PIXELFORMATDESCRIPTOR structure to verify the sizes, looks right to me:
Code:
Struct: PIXELFORMATDESCRIPTOR
Base: 1569792

nSize: 1569792
nVersion: 1569794
dwFlags: 1569796
iPixelType: 1569800
cColorBits: 1569801
cRedBits: 1569802
cRedShift: 1569803
cGreenBits: 1569804
cGreenShift: 1569805
cBlueBits: 1569806
cBlueShift: 1569807
cAlphaBits: 1569808
cAlphaShift: 1569809
cAccumBits: 1569810
cAccumRedBits: 1569811
cAccumGreenBits: 1569812
cAccumBlueBits: 1569813
cAccumAlphaBits: 1569814
cDepthBits: 1569815
cStencilBits: 1569816
cAuxBuffers: 1569817
iLayerType: 1569818
bReserved: 1569819
dwLayerMask: 1569820
dwVisible: 1569824
dwDamageMask: 1569828

Total Size: 40


Nothing I do with the cColorBits member makes any difference. Windows should choose the closest match for the entire structure when I call ChoosePixelFormat, so I don't think it really matters.

I've tried handling the WM_PAINT messages and returning 0. I don't see any special handling in the message loop or WndProc function in any of the tutorials I've read that would account for the problem.

Guess I'll have to break down and watch how AHK is handling the floats and doubles. I've tried playing with the padding but I'm a little lost on what excatly OpenGL is expecting. I don't get any parameter errors on any of the calls. I've tried padding width/height before sending it to gluPerspective and it didn't work.

Bah I guess I'll end up wrapping it in a dll. I started to do that in the first place but it looks simple enough to pull off with AHK alone. Sad
Back to top
Zippo



Joined: 21 Apr 2006
Posts: 56
Location: East Coast, USA

PostPosted: Thu Apr 24, 2008 6:25 am    Post subject: Reply with quote

*laces out*
_________________
____________________
Back to top
View user's profile Send private message
IsNull



Joined: 10 May 2007
Posts: 63
Location: .switzerland

PostPosted: Thu Apr 24, 2008 10:31 am    Post subject: Reply with quote

wow, realy amazing Wink

I've changed your code, so the Triangle and the Quad rotating Very Happy


Code:

;/////////////////////////////////////////////////////////////////////////////////////////////
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.   ;//
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.   ;//
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.                    ;//
;/////////////////////////////////////////////////////////////////////////////////////////////

#include %A_ScriptDir%\WINDERS.ahk
#include %A_ScriptDir%\GL.ahk

hInstance := 0
hWnd := 0
hDC := 0
hRC := 0
hOpenGL := 0
hGlu32 := 0
hGdi32 := 0
Escape := 0
F1 := 0
active := 1
fullscreen := 1
lpszClassName := "OpenGL"



;-----------------------------------------
;mod to rotate
fRotation := 10.0
;-----------------------------------------


   WinMain()
   ExitApp

; ==============================================================================================

ReSizeGLScene()
{

   Global
   If height = 0               ;Prevent divide by 0
   {
      height := 1
   }

    DllCall("opengl32.dll\glViewport", Int, 0, Int, 0, Int, width, Int, height)
   DllCall("opengl32.dll\glMatrixMode", UInt, GL_PROJECTION)
   DllCall("opengl32.dll\glLoadIdentity")

   ; Calculate aspect ratio of the window
   DllCall("glu32.dll\gluPerspective", Double, 45.0, Double, width/height, Double, 0.1, Double, 100.0)

   DllCall("opengl32.dll\glMatrixMode", UInt, GL_MODELVIEW)
   DllCall("opengl32.dll\glLoadIdentity")

}




InitGL()
{

   Global

   DllCall("opengl32.dll\glShadeModel", UInt, GL_SMOOTH)
   DllCall("opengl32.dll\glClearColor", Float, 0.0, Float, 0.0, Float, 0.0, Float, 0.5)
   DllCall("opengl32.dll\glClearDepth", Double, 1.0)
   DllCall("opengl32.dll\glEnable", UInt, GL_DEPTH_TEST)
   DllCall("opengl32.dll\glDepthFunc", UInt, GL_LEQUAL)
   DllCall("opengl32.dll\glHint", UInt, GL_PERSPECTIVE_CORRECTION_HINT, UInt, GL_NICEST)

   Return 1

}




DrawGLScene()
{

   Global ;hDC, fRotation
   
;-----------------------------------------
;mod to rotate
   SetFormat, float, 0.2
   fRotation -= 0.4
;-----------------------------------------

   DllCall("opengl32.dll\glClear", Int, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   DllCall("opengl32.dll\glLoadIdentity")

   DllCall("opengl32.dll\glTranslatef", Float, -1.5, Float, 0.0, Float, -6.0)

;-----------------------------------------
;mod to rotate
   DllCall("opengl32.dll\glRotatef",Float,fRotation,Float,0.0,Float,1.0,Float,0.0) 
;-----------------------------------------


   DllCall("opengl32.dll\glBegin", Int, GL_TRIANGLES)

      DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0)
   DllCall("opengl32.dll\glEnd")



   DllCall("opengl32.dll\glTranslatef", Float, 3.0, Float, 0.0, Float, 0.0)
   DllCall("opengl32.dll\glBegin", Int, GL_QUADS)

      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0)

   DllCall("opengl32.dll\glEnd")


/********** doesn't work yet... *********
;-----------------------------------------
;mod to rotate -
   DllCall("opengl32.dll\glLoadIdentity")
;-----------------------------------------
*/

   DllCall("gdi32.dll\SwapBuffers", UInt, hDC)

   Return 1

}




KillGLWindow()
{

   Global ;lpszClassName, hDC, hRC, hWnd, hInstance

   If fullscreen
   {
      DllCall("ChangeDisplaySettings", UInt, 0, UInt, 0)
      DllCall("ShowCursor", Int, 1)
   }

   If hRC
   {
      If !DllCall("opengl32.dll\wglMakeCurrent", UInt, 0, UInt, 0)
      {
         MsgBox, Release of DC and RC failed.
      }

      If !DllCall("opengl32.dll\wglDeleteContext", UInt, hRC)
      {
         MsgBox, Release rendering context failed.
      }

      hRC := 0
   }

   If hDC
   {
      If !DllCall("ReleaseDC", UInt, hWnd, UInt, hDC)
      {
         MsgBox, Release device context failed.
      }

      hDC := 0
   }

   If hWnd
   {
      If !DllCall("DestroyWindow", UInt, hWnd)
      {
         MsgBox, Could not release hWnd.
      }

      hWnd := 0
   }

   If !DllCall("UnregisterClass", UInt, &lpszClassName, UInt, hInstance)
   {
      MsgBox, Could not unregister class. %A_LastError%
   }

   hInstance := 0
}




CreateGLWindow(title, width, height, bits, fullscreenflag)
{

   Global ;lpszClassName, hDC, hRC, hInstance, hWnd, fullscreen
   ;Static pfd

   VarSetCapacity(wc, 40, 0)
   VarSetCapacity(WindowRect, 16, 0)
   VarSetCapacity(pfd, 40, 0)

   NumPut(0, WindowRect, 0, "Int")
   NumPut(0, WindowRect, 4, "Int")
   NumPut(width, WindowRect, 8, "Int")
   NumPut(height, WindowRect, 12, "Int")

   Style := CS_HREDRAW | CS_VREDRAW | CS_OWNDC
   lpfnWndProc := RegisterCallback("WndProc", "", 4)
   cbClsExtra := 0
   cbWndExtra := 0
   hInstance := DllCall("GetModuleHandle", UInt, 0)
   hbrBackground := 0
   hCursor := DllCall("LoadCursor", UInt, 0, UInt, IDC_ARROW)
   hIcon := DllCall("LoadIcon", UInt, 0, UInt, IDI_APPLICATION)
   lpszMenuName := 0

   NumPut(Style, wc, 0, "UInt")
   NumPut(lpfnWndProc, wc, 4, "UInt")
   NumPut(cbClsExtra, wc, 8, "UInt")
   NumPut(cbWndExtra, wc, 12, "UInt")
   NumPut(hInstance, wc, 16, "UInt")
   NumPut(hIcon, wc, 20, "UInt")
   NumPut(hCursor, wc, 24, "UInt")
   NumPut(hbrBackground, wc, 28, "UInt")
   NumPut(&lpszMenuName, wc, 32, "UInt")
   NumPut(&lpszClassName, wc, 36, "UInt")

   If !DllCall("RegisterClass", UInt, &wc)
   {
      MsgBox, Failed to register class.
      Return 0
   }

   If fullscreen
   {
      ; DEVMODE size: 156 ANSI, 220 UNICODE
      VarSetCapacity(dmScreenSettings, 156, 0)

      dmFields := DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT

      NumPut(156, dmScreenSettings, 0, "UShort")

      Result := DllCall("EnumDisplaySettings", UInt, 0, UInt, 0, UInt, &dmScreenSettings)

      NumPut(width, dmScreenSettings, 108, "UInt")
      NumPut(height, dmScreenSettings, 112, "UInt")
      NumPut(bits, dmScreenSettings, 104, "UInt") ;bits
      NumPut(dmFields, dmScreenSettings, 40, "UInt")

      Result := DllCall("ChangeDisplaySettings", UInt, &dmScreenSettings, UInt, CDS_FULLSCREEN)
      If Result != %DISP_CHANGE_SUCCESSFUL%
      {
         MsgBox,4,, The requested fullscreen mode is not supported by`nyour video card. Use windowed mode instead?
         IfMsgBox Yes
         {
            fullscreen := 0
         }
         Else
         {
            ; Let the user know the program is closing
            MsgBox, The program will now close.
            Return 0
         }
      }
   }

   If fullscreen
   {
      dwExStyle := WS_EX_APPWINDOW
      dwStyle := WS_POPUP
      DllCall("ShowCursor", UInt, 0)
   }
   Else
   {
      dwExStyle := WS_EX_APPWINDOW | WS_EX_WINDOWEDGE
      dwStyle := WS_OVERLAPPEDWINDOW
   }

   DllCall("AdjustWindowRectEx", UInt, &WindowRect, UInt, dwStyle, UInt, 0, UInt, dwExStyle)

   hWnd := DllCall("CreateWindowEx", UInt, dwExStyle
               , UInt, &lpszClassName
               , "Str", title
               , UInt, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN
               , Int, 0
               , Int, 0
               , Int, NumGet(WindowRect, 8, "Int")-NumGet(WindowRect, 0, "Int")
               , Int, NumGet(WindowRect, 12, "Int")-NumGet(WindowRect, 4, "Int")
               , UInt, 0
               , UInt, 0
               , UInt, hInstance
               , UInt, 0
               , UInt)
   If !hWnd
   {
      KillGLWindow()
      MsgBox, Window creation error. %A_LastError% : %ErrorLevel%
      Return 0
   }

   dwFlags := PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER

   NumPut(40, pfd, 0, "UShort")
   NumPut(1, pfd, 2, "UShort")
   NumPut(dwFlags, pfd, 4, "UInt")
   NumPut(PFD_TYPE_RGBA, pfd, 8, "UChar")
   NumPut(bits, pfd, 9, "UChar")
   NumPut(16, pfd, 23, "UChar")
   NumPut(PFD_MAIN_PLANE, pfd, 26, "UChar")

   hDC := DllCall("GetDC", UInt, hWnd)
   If !hDC
   {
      KillGLWindow()
      MsgBox, Can't create GL device context.
      Return 0
   }

   PixelFormat := DllCall("gdi32.dll\ChoosePixelFormat", UInt, hDC, UInt, &pfd)
   If !PixelFormat
   {
      KillGLWindow()
      MsgBox, Can't find a suitable pixel format.
      Return 0
   }

   If !DllCall("gdi32.dll\SetPixelFormat", UInt, hDC, UInt, PixelFormat, UInt, &pfd)
   {
      KillGLWindow()
      MsgBox, Can't set the pixel format.
      Return 0
   }

   hRC := DllCall("opengl32.dll\wglCreateContext", UInt, hDC, Int)
   If !hRC
   {
      KillGLWindow()
      MsgBox, Can't create GL rendering context.
      Return 0
   }

   If !DllCall("opengl32.dll\wglMakeCurrent", UInt, hDC, UInt, hRC)
   {
      KillGLWindow()
      MsgBox, Can't activate the GL rendering context.
      Return 0
   }

   DllCall("ShowWindow", UInt, hWnd, Int, SW_SHOW)
   ;DllCall("UpdateWindow", UInt, hWnd)
   DllCall("SetForegroundWindow", UInt, hWnd)
   DllCall("SetFocus", UInt, hWnd)
   ReSizeGLScene()

   If !InitGL()
   {
      KillGLWindow()
      MsgBox, Initialization failed.
      Return 0
   }

   Return 1

}




WndProc(hwnd, uMsg, wParam, lParam)
{

   Global
   VarSetCapacity(ps, 64, 0)

   If uMsg = %WM_ACTIVATE%
   {
      If !(wParam >> 16)
      {
         active := 1
      }
      Else
      {
         active := 0
      }

      Return 0
   }

   If uMsg = %WM_SYSCOMMAND%
   {
      If (wParam = %SC_SCREENSAVE% or wParam = %SC_MONITORPOWER%)
      {
         Return 0
      }
   }

   If uMsg = %WM_PAINT%
   {
      DllCall("BeginPaint", UInt, hwnd, UInt, &ps)
      DllCall("EndPaint", UInt, hwnd, UInt, &ps)
      Return 0
   }

   If uMsg = %WM_CLOSE%
   {
      DllCall("PostQuitMessage", UInt, 0)
      Return 0
   }

   If uMsg = %WM_SIZE%
   {
      width := lParam & 0x0ffff
      height := lParam >> 16
      ReSizeGLScene() ;lParam & 0x0ffff, lParam >> 16)
      Return 0
   }

   Return DllCall("DefWindowProc", UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam)

}




WinMain()
{

   Global ;fullscreen, F1, Escape

   ; Load required libs
   hOpenGl := DllCall("LoadLibrary", Str, "opengl32.dll")
   If !hOpenGl
   {
      MsgBox, OpenGl32.dll could not be loaded.
      ExitApp
   }

   hGlu32  := DllCall("LoadLibrary", Str, "glu32.dll")
   If !hGlu32
   {
      MsgBox, Glu32.dll could not be loaded.
      ExitApp
   }

   hGdi32  := DllCall("LoadLibrary", Str, "gdi32.dll")
   If !hGlu32
   {
      MsgBox, Gdi32.dll could not be loaded.
      ExitApp
   }

   MsgBox, 4, Start Fullscreen?, Would you like to run in fullscreen mode?
   IfMsgBox No
   {
      fullscreen := 0
   }

   If CreateGLWindow("Nehe's First Poylgon Tutorial", 640, 480, 16, fullscreen)
   {

      Loop
      {
         If Escape
         {
            Break
         }

         If F1
         {
            F1 := !F1
            fullscreen := !fullscreen
            KillGLWindow()
            If !CreateGLWindow("Nehe's First Poylgon Tutorial", 640, 480, 16, fullscreen)
            {
               Break
            }
         }

         If active
         {
            If !DrawGLScene()
            {
               Break
            }
         }

         Sleep, 50
      }
   }

   KillGLWindow()

   If hOpenGL
   {
      DllCall("FreeLibrary", UInt, OpenGL)
   }

   If hGlu32
   {
      DllCall("FreeLibrary", UInt, hGlu32)
   }

   If Gdi32
   {
      DllCall("FreeLibrary", UInt, 0)
   }

   Return 0

}

; ==============================================================================================

Esc::Escape := !Escape
F1::F1 := !F1

Are there any other working AHK Examples of OpenGL? I will try to create 3d volumes...

regards
IsNull
_________________
http://securityvision.ch
Back to top
View user's profile Send private message
Z Gecko
Guest





PostPosted: Thu Apr 24, 2008 10:49 am    Post subject: Reply with quote

really, really cool! Surprised

I strongly recommend that you put these scripts in the Scripts&Functions section NOW, no matter if it´s "a mess" or not!
Back to top
IsNull



Joined: 10 May 2007
Posts: 63
Location: .switzerland

PostPosted: Thu Apr 24, 2008 11:10 am    Post subject: Reply with quote

rotating 3d pyramid Very Happy

Code:

;/////////////////////////////////////////////////////////////////////////////////////////////
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.   ;//
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.   ;//
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.                    ;//
;/////////////////////////////////////////////////////////////////////////////////////////////

#include %A_ScriptDir%\WINDERS.ahk
#include %A_ScriptDir%\GL.ahk

hInstance := 0
hWnd := 0
hDC := 0
hRC := 0
hOpenGL := 0
hGlu32 := 0
hGdi32 := 0
Escape := 0
F1 := 0
active := 1
fullscreen := 1
lpszClassName := "OpenGL"



;-----------------------------------------
;mod to rotate
fRotation := 10.0
;-----------------------------------------


   WinMain()
   ExitApp

; ==============================================================================================

ReSizeGLScene()
{

   Global
   If height = 0               ;Prevent divide by 0
   {
      height := 1
   }

    DllCall("opengl32.dll\glViewport", Int, 0, Int, 0, Int, width, Int, height)
   DllCall("opengl32.dll\glMatrixMode", UInt, GL_PROJECTION)
   DllCall("opengl32.dll\glLoadIdentity")

   ; Calculate aspect ratio of the window
   DllCall("glu32.dll\gluPerspective", Double, 45.0, Double, width/height, Double, 0.1, Double, 100.0)

   DllCall("opengl32.dll\glMatrixMode", UInt, GL_MODELVIEW)
   DllCall("opengl32.dll\glLoadIdentity")

}




InitGL()
{

   Global

   DllCall("opengl32.dll\glShadeModel", UInt, GL_SMOOTH)
   DllCall("opengl32.dll\glClearColor", Float, 0.0, Float, 0.0, Float, 0.0, Float, 0.5)
   DllCall("opengl32.dll\glClearDepth", Double, 1.0)
   DllCall("opengl32.dll\glEnable", UInt, GL_DEPTH_TEST)
   DllCall("opengl32.dll\glDepthFunc", UInt, GL_LEQUAL)
   DllCall("opengl32.dll\glHint", UInt, GL_PERSPECTIVE_CORRECTION_HINT, UInt, GL_NICEST)

   Return 1

}




DrawGLScene()
{

   Global ;hDC, fRotation
   
;-----------------------------------------
;mod to rotate - IsNulll 
   SetFormat, float, 0.2
   fRotation -= 0.4
;-----------------------------------------

   DllCall("opengl32.dll\glClear", Int, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)


   DllCall("opengl32.dll\glTranslatef", Float, -1.5, Float, -0.8, Float, -6.0)


;mod to rotate
   DllCall("opengl32.dll\glRotatef",Float,fRotation,Float,0.0,Float,1.0,Float,0.0) 

   DllCall("opengl32.dll\glBegin", Int, GL_TRIANGLES)


      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 0.0, Float, 1.0) ;blau
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (vorderes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 1.0) ;links (vorderes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 1.0) ;rechts (vorderes Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 1.0, Float, 0.0, Float, 0.0) ;ROT
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (rechtes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 1.0) ;links (rechtes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, -1.0) ;rechts (rechtes Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 1.0, Float, 0.0) ;Grün
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (hinteres Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, -1.0) ;links (hinteres Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, -1.0) ;rechts (hinteres Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 1.0, Float, 1.0, Float, 0.0) ;GELB
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (linkes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, -1.0) ;links (linkes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 1.0) ;rechts (linkes Dreieck)


   DllCall("opengl32.dll\glEnd")
   DllCall("opengl32.dll\glLoadIdentity")

;************************************


   DllCall("gdi32.dll\SwapBuffers", UInt, hDC)

   Return 1

}




KillGLWindow()
{

   Global ;lpszClassName, hDC, hRC, hWnd, hInstance

   If fullscreen
   {
      DllCall("ChangeDisplaySettings", UInt, 0, UInt, 0)
      DllCall("ShowCursor", Int, 1)
   }

   If hRC
   {
      If !DllCall("opengl32.dll\wglMakeCurrent", UInt, 0, UInt, 0)
      {
         MsgBox, Release of DC and RC failed.
      }

      If !DllCall("opengl32.dll\wglDeleteContext", UInt, hRC)
      {
         MsgBox, Release rendering context failed.
      }

      hRC := 0
   }

   If hDC
   {
      If !DllCall("ReleaseDC", UInt, hWnd, UInt, hDC)
      {
         MsgBox, Release device context failed.
      }

      hDC := 0
   }

   If hWnd
   {
      If !DllCall("DestroyWindow", UInt, hWnd)
      {
         MsgBox, Could not release hWnd.
      }

      hWnd := 0
   }

   If !DllCall("UnregisterClass", UInt, &lpszClassName, UInt, hInstance)
   {
      MsgBox, Could not unregister class. %A_LastError%
   }

   hInstance := 0
}




CreateGLWindow(title, width, height, bits, fullscreenflag)
{

   Global ;lpszClassName, hDC, hRC, hInstance, hWnd, fullscreen
   ;Static pfd

   VarSetCapacity(wc, 40, 0)
   VarSetCapacity(WindowRect, 16, 0)
   VarSetCapacity(pfd, 40, 0)

   NumPut(0, WindowRect, 0, "Int")
   NumPut(0, WindowRect, 4, "Int")
   NumPut(width, WindowRect, 8, "Int")
   NumPut(height, WindowRect, 12, "Int")

   Style := CS_HREDRAW | CS_VREDRAW | CS_OWNDC
   lpfnWndProc := RegisterCallback("WndProc", "", 4)
   cbClsExtra := 0
   cbWndExtra := 0
   hInstance := DllCall("GetModuleHandle", UInt, 0)
   hbrBackground := 0
   hCursor := DllCall("LoadCursor", UInt, 0, UInt, IDC_ARROW)
   hIcon := DllCall("LoadIcon", UInt, 0, UInt, IDI_APPLICATION)
   lpszMenuName := 0

   NumPut(Style, wc, 0, "UInt")
   NumPut(lpfnWndProc, wc, 4, "UInt")
   NumPut(cbClsExtra, wc, 8, "UInt")
   NumPut(cbWndExtra, wc, 12, "UInt")
   NumPut(hInstance, wc, 16, "UInt")
   NumPut(hIcon, wc, 20, "UInt")
   NumPut(hCursor, wc, 24, "UInt")
   NumPut(hbrBackground, wc, 28, "UInt")
   NumPut(&lpszMenuName, wc, 32, "UInt")
   NumPut(&lpszClassName, wc, 36, "UInt")

   If !DllCall("RegisterClass", UInt, &wc)
   {
      MsgBox, Failed to register class.
      Return 0
   }

   If fullscreen
   {
      ; DEVMODE size: 156 ANSI, 220 UNICODE
      VarSetCapacity(dmScreenSettings, 156, 0)

      dmFields := DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT

      NumPut(156, dmScreenSettings, 0, "UShort")

      Result := DllCall("EnumDisplaySettings", UInt, 0, UInt, 0, UInt, &dmScreenSettings)

      NumPut(width, dmScreenSettings, 108, "UInt")
      NumPut(height, dmScreenSettings, 112, "UInt")
      NumPut(bits, dmScreenSettings, 104, "UInt") ;bits
      NumPut(dmFields, dmScreenSettings, 40, "UInt")

      Result := DllCall("ChangeDisplaySettings", UInt, &dmScreenSettings, UInt, CDS_FULLSCREEN)
      If Result != %DISP_CHANGE_SUCCESSFUL%
      {
         MsgBox,4,, The requested fullscreen mode is not supported by`nyour video card. Use windowed mode instead?
         IfMsgBox Yes
         {
            fullscreen := 0
         }
         Else
         {
            ; Let the user know the program is closing
            MsgBox, The program will now close.
            Return 0
         }
      }
   }

   If fullscreen
   {
      dwExStyle := WS_EX_APPWINDOW
      dwStyle := WS_POPUP
      DllCall("ShowCursor", UInt, 0)
   }
   Else
   {
      dwExStyle := WS_EX_APPWINDOW | WS_EX_WINDOWEDGE
      dwStyle := WS_OVERLAPPEDWINDOW
   }

   DllCall("AdjustWindowRectEx", UInt, &WindowRect, UInt, dwStyle, UInt, 0, UInt, dwExStyle)

   hWnd := DllCall("CreateWindowEx", UInt, dwExStyle
               , UInt, &lpszClassName
               , "Str", title
               , UInt, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN
               , Int, 0
               , Int, 0
               , Int, NumGet(WindowRect, 8, "Int")-NumGet(WindowRect, 0, "Int")
               , Int, NumGet(WindowRect, 12, "Int")-NumGet(WindowRect, 4, "Int")
               , UInt, 0
               , UInt, 0
               , UInt, hInstance
               , UInt, 0
               , UInt)
   If !hWnd
   {
      KillGLWindow()
      MsgBox, Window creation error. %A_LastError% : %ErrorLevel%
      Return 0
   }

   dwFlags := PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER

   NumPut(40, pfd, 0, "UShort")
   NumPut(1, pfd, 2, "UShort")
   NumPut(dwFlags, pfd, 4, "UInt")
   NumPut(PFD_TYPE_RGBA, pfd, 8, "UChar")
   NumPut(bits, pfd, 9, "UChar")
   NumPut(16, pfd, 23, "UChar")
   NumPut(PFD_MAIN_PLANE, pfd, 26, "UChar")

   hDC := DllCall("GetDC", UInt, hWnd)
   If !hDC
   {
      KillGLWindow()
      MsgBox, Can't create GL device context.
      Return 0
   }

   PixelFormat := DllCall("gdi32.dll\ChoosePixelFormat", UInt, hDC, UInt, &pfd)
   If !PixelFormat
   {
      KillGLWindow()
      MsgBox, Can't find a suitable pixel format.
      Return 0
   }

   If !DllCall("gdi32.dll\SetPixelFormat", UInt, hDC, UInt, PixelFormat, UInt, &pfd)
   {
      KillGLWindow()
      MsgBox, Can't set the pixel format.
      Return 0
   }

   hRC := DllCall("opengl32.dll\wglCreateContext", UInt, hDC, Int)
   If !hRC
   {
      KillGLWindow()
      MsgBox, Can't create GL rendering context.
      Return 0
   }

   If !DllCall("opengl32.dll\wglMakeCurrent", UInt, hDC, UInt, hRC)
   {
      KillGLWindow()
      MsgBox, Can't activate the GL rendering context.
      Return 0
   }

   DllCall("ShowWindow", UInt, hWnd, Int, SW_SHOW)
   ;DllCall("UpdateWindow", UInt, hWnd)
   DllCall("SetForegroundWindow", UInt, hWnd)
   DllCall("SetFocus", UInt, hWnd)
   ReSizeGLScene()

   If !InitGL()
   {
      KillGLWindow()
      MsgBox, Initialization failed.
      Return 0
   }

   Return 1

}




WndProc(hwnd, uMsg, wParam, lParam)
{

   Global
   VarSetCapacity(ps, 64, 0)

   If uMsg = %WM_ACTIVATE%
   {
      If !(wParam >> 16)
      {
         active := 1
      }
      Else
      {
         active := 0
      }

      Return 0
   }

   If uMsg = %WM_SYSCOMMAND%
   {
      If (wParam = %SC_SCREENSAVE% or wParam = %SC_MONITORPOWER%)
      {
         Return 0
      }
   }

   If uMsg = %WM_PAINT%
   {
      DllCall("BeginPaint", UInt, hwnd, UInt, &ps)
      DllCall("EndPaint", UInt, hwnd, UInt, &ps)
      Return 0
   }

   If uMsg = %WM_CLOSE%
   {
      DllCall("PostQuitMessage", UInt, 0)
      Return 0
   }

   If uMsg = %WM_SIZE%
   {
      width := lParam & 0x0ffff
      height := lParam >> 16
      ReSizeGLScene() ;lParam & 0x0ffff, lParam >> 16)
      Return 0
   }

   Return DllCall("DefWindowProc", UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam)

}




WinMain()
{

   Global ;fullscreen, F1, Escape

   ; Load required libs
   hOpenGl := DllCall("LoadLibrary", Str, "opengl32.dll")
   If !hOpenGl
   {
      MsgBox, OpenGl32.dll could not be loaded.
      ExitApp
   }

   hGlu32  := DllCall("LoadLibrary", Str, "glu32.dll")
   If !hGlu32
   {
      MsgBox, Glu32.dll could not be loaded.
      ExitApp
   }

   hGdi32  := DllCall("LoadLibrary", Str, "gdi32.dll")
   If !hGlu32
   {
      MsgBox, Gdi32.dll could not be loaded.
      ExitApp
   }

   MsgBox, 4, Start Fullscreen?, Would you like to run in fullscreen mode?
   IfMsgBox No
   {
      fullscreen := 0
   }

   If CreateGLWindow("Nehe's First Poylgon Tutorial", 640, 480, 16, fullscreen)
   {

      Loop
      {
         If Escape
         {
            Break
         }

         If F1
         {
            F1 := !F1
            fullscreen := !fullscreen
            KillGLWindow()
            If !CreateGLWindow("Nehe's First Poylgon Tutorial", 640, 480, 16, fullscreen)
            {
               Break
            }
         }

         If active
         {
            If !DrawGLScene()
            {
               Break
            }
         }

         Sleep, 50
      }
   }

   KillGLWindow()

   If hOpenGL
   {
      DllCall("FreeLibrary", UInt, OpenGL)
   }

   If hGlu32
   {
      DllCall("FreeLibrary", UInt, hGlu32)
   }

   If Gdi32
   {
      DllCall("FreeLibrary", UInt, 0)
   }

   Return 0

}

; ==============================================================================================

Esc::Escape := !Escape
F1::F1 := !F1

A collection of these scriptlets would be great. Cool



EDIT: 2 Pyramdys who rotate on diffrent places on diffrent Axes.: (Only the important Draw function. To run, take the code above and replace this function with the old one)[/b]

Code:

DrawGLScene()
{

   Global ;hDC, fRotation
   
;-----------------------------------------
;mod to rotate - IsNulll 
   SetFormat, float, 0.2
   fRotation -= 0.4
;-----------------------------------------

   DllCall("opengl32.dll\glClear", Int, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   DllCall("opengl32.dll\glLoadIdentity")




;************************************ Start OF 1th Pyramid *********************************************
;*******************************************************************************************************

DllCall("opengl32.dll\glTranslatef", Float, +1.5, Float, -1.0, Float, -6.0)
;mod to rotate
   DllCall("opengl32.dll\glRotatef",Float,fRotation,Float,0.0,Float,1.0,Float,0.0) 

   DllCall("opengl32.dll\glBegin", Int, GL_TRIANGLES)


      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 0.0, Float, 1.0) ;blau
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (vorderes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 1.0) ;links (vorderes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 1.0) ;rechts (vorderes Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 1.0, Float, 0.0, Float, 0.0) ;ROT
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (rechtes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 1.0) ;links (rechtes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, -1.0) ;rechts (rechtes Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 1.0, Float, 0.0) ;Grün
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (hinteres Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, -1.0) ;links (hinteres Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, -1.0) ;rechts (hinteres Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 1.0, Float, 1.0, Float, 0.0) ;GELB
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (linkes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, -1.0) ;links (linkes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 1.0) ;rechts (linkes Dreieck)


   DllCall("opengl32.dll\glEnd")
   DllCall("opengl32.dll\glLoadIdentity")

;************************************ END OF 1th Pyramid ***********************************************


;************************************ Start OF 2th Pyramid *********************************************
;*******************************************************************************************************
DllCall("opengl32.dll\glTranslatef", Float, -1.5, Float, 0.0, Float, -6.0)

;mod to rotate
   DllCall("opengl32.dll\glRotatef",Float,fRotation,Float,0.0,Float,0.0,Float,1.0) 

   DllCall("opengl32.dll\glBegin", Int, GL_TRIANGLES)



      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 0.0, Float, 1.0) ;blau
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (vorderes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 1.0) ;links (vorderes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 1.0) ;rechts (vorderes Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 1.0, Float, 0.0, Float, 0.0) ;ROT
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (rechtes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 1.0) ;links (rechtes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, -1.0) ;rechts (rechtes Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 1.0, Float, 0.0) ;Grün
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (hinteres Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, -1.0) ;links (hinteres Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, -1.0) ;rechts (hinteres Dreieck)

      DllCall("opengl32.dll\glColor3f",  Float, 1.0, Float, 1.0, Float, 0.0) ;GELB
   DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) ;oben (linkes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, -1.0) ;links (linkes Dreieck)
   DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 1.0) ;rechts (linkes Dreieck)


   DllCall("opengl32.dll\glEnd")
   DllCall("opengl32.dll\glLoadIdentity")

;************************************ END OF 2th Pyramid ***********************************************


   DllCall("gdi32.dll\SwapBuffers", UInt, hDC)

   Return 1

}




_________________
http://securityvision.ch
Back to top
View user's profile Send private message
Z Gecko
Guest





PostPosted: Thu Apr 24, 2008 12:05 pm    Post subject: Reply with quote

you´re really turned on by this, IsNull, aren´t you? Laughing

Me too! Cool

Next example:
Code:

;/////////////////////////////////////////////////////////////////////////////////////////////
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.   ;//
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.   ;//
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.                    ;//
;/////////////////////////////////////////////////////////////////////////////////////////////

#include %A_ScriptDir%\WINDERS.ahk
#include %A_ScriptDir%\GL.ahk

hInstance := 0
hWnd := 0
hDC := 0
hRC := 0
hOpenGL := 0
hGlu32 := 0
hGdi32 := 0
Escape := 0
F1 := 0
active := 1
fullscreen := 1
lpszClassName := "OpenGL"



;-----------------------------------------
;mod to rotate
f1Rotation := 0.0
b1Rotation := 0.0
f2Rotation := 0.0
b2Rotation := 0.0
;-----------------------------------------


   WinMain()
   ExitApp

; ==============================================================================================

ReSizeGLScene()
{

   Global
   If height = 0               ;Prevent divide by 0
   {
      height := 1
   }

    DllCall("opengl32.dll\glViewport", Int, 0, Int, 0, Int, width, Int, height)
   DllCall("opengl32.dll\glMatrixMode", UInt, GL_PROJECTION)
   DllCall("opengl32.dll\glLoadIdentity")

   ; Calculate aspect ratio of the window
   DllCall("glu32.dll\gluPerspective", Double, 45.0, Double, width/height, Double, 0.1, Double, 100.0)

   DllCall("opengl32.dll\glMatrixMode", UInt, GL_MODELVIEW)
   DllCall("opengl32.dll\glLoadIdentity")

}




InitGL()
{

   Global

   DllCall("opengl32.dll\glShadeModel", UInt, GL_SMOOTH)
   DllCall("opengl32.dll\glClearColor", Float, 0.0, Float, 0.0, Float, 0.0, Float, 0.5)
   DllCall("opengl32.dll\glClearDepth", Double, 1.0)
   DllCall("opengl32.dll\glEnable", UInt, GL_DEPTH_TEST)
   DllCall("opengl32.dll\glDepthFunc", UInt, GL_LEQUAL)
   DllCall("opengl32.dll\glHint", UInt, GL_PERSPECTIVE_CORRECTION_HINT, UInt, GL_NICEST)

   Return 1

}




DrawGLScene()
{

   Global ;hDC, fRotation
   
;-----------------------------------------
;mod to rotate
   SetFormat, float, 0.2
   f1Rotation -= 0.8
   b1Rotation += 0.8
   f2Rotation -= 1.2
   b2Rotation += 1.2
;-----------------------------------------

   DllCall("opengl32.dll\glClear", Int, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   DllCall("opengl32.dll\glLoadIdentity")


   DllCall("opengl32.dll\glBegin", Int, GL_QUADS)   
      DllCall("opengl32.dll\glColor3f",  Float, 0.8, Float, 0.3, Float, 0.1)
      DllCall("opengl32.dll\glVertex3f", Float, -15.0, Float, -1.2, Float, -15.0)
      DllCall("opengl32.dll\glVertex3f", Float, -15.0, Float, -1.5, Float, 15.0)
      DllCall("opengl32.dll\glVertex3f", Float, 15.0, Float, -1.5, Float, 15.0)
      DllCall("opengl32.dll\glVertex3f", Float, 15.0, Float, -1.2, Float, -15.0)
   DllCall("opengl32.dll\glEnd")

   
   DllCall("opengl32.dll\glBegin", Int, GL_QUADS)   
      DllCall("opengl32.dll\glColor3f",  Float, 0.4, Float, 0.5, Float, 1.0)
      DllCall("opengl32.dll\glVertex3f", Float, -15.0, Float, -15.0, Float, -15.0)
      DllCall("opengl32.dll\glVertex3f", Float, -15.0, Float, 15.0, Float, -15.0)
      DllCall("opengl32.dll\glVertex3f", Float, 15.0, Float,  15.0, Float, -15.0)
      DllCall("opengl32.dll\glVertex3f", Float, 15.0, Float,  -15.0, Float, -15.0)
   DllCall("opengl32.dll\glEnd")     
   
     
   DllCall("opengl32.dll\glTranslatef", Float, -1.5, Float, 0.0, Float, -6.0)

   DllCall("opengl32.dll\glRotatef",Float,f1Rotation,Float,0.0,Float,1.0,Float,0.0) 

   DllCall("opengl32.dll\glBegin", Int, GL_TRIANGLES)
      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 0.0, Float, 1.0)   
      DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0)
   DllCall("opengl32.dll\glEnd")

   DllCall("opengl32.dll\glRotatef",Float,b1Rotation,Float,0.0,Float,1.0,Float,0.0) 


   DllCall("opengl32.dll\glTranslatef", Float, 3.0, Float, 0.0, Float, 0.0)

   DllCall("opengl32.dll\glRotatef",Float,f2Rotation,Float,0.0,Float,1.0,Float,0.0) 
   
   DllCall("opengl32.dll\glBegin", Int, GL_QUADS)   
      DllCall("opengl32.dll\glColor3f",  Float, 0.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, 1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0)
      DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0)
   DllCall("opengl32.dll\glEnd")

   DllCall("opengl32.dll\glRotatef",Float,b2Rotation,Float,0.0,Float,1.0,Float,0.0) 



   DllCall("gdi32.dll\SwapBuffers", UInt, hDC)

   Return 1

}




KillGLWindow()
{

   Global ;lpszClassName, hDC, hRC, hWnd, hInstance

   If fullscreen
   {
      DllCall("ChangeDisplaySettings", UInt, 0, UInt, 0)
      DllCall("ShowCursor", Int, 1)
   }

   If hRC
   {
      If !DllCall("opengl32.dll\wglMakeCurrent", UInt, 0, UInt, 0)
      {
         MsgBox, Release of DC and RC failed.
      }

      If !DllCall("opengl32.dll\wglDeleteContext", UInt, hRC)
      {
         MsgBox, Release rendering context failed.
      }

      hRC := 0
   }

   If hDC
   {
      If !DllCall("ReleaseDC", UInt, hWnd, UInt, hDC)
      {
         MsgBox, Release device context failed.
      }

      hDC := 0
   }

   If hWnd
   {
      If !DllCall("DestroyWindow", UInt, hWnd)
      {
         MsgBox, Could not release hWnd.
      }

      hWnd := 0
   }

   If !DllCall("UnregisterClass", UInt, &lpszClassName, UInt, hInstance)
   {
      MsgBox, Could not unregister class. %A_LastError%
   }

   hInstance := 0
}




CreateGLWindow(title, width, height, bits, fullscreenflag)
{

   Global ;lpszClassName, hDC, hRC, hInstance, hWnd, fullscreen
   ;Static pfd

   VarSetCapacity(wc, 40, 0)
   VarSetCapacity(WindowRect, 16, 0)
   VarSetCapacity(pfd, 40, 0)

   NumPut(0, WindowRect, 0, "Int")
   NumPut(0, WindowRect, 4, "Int")
   NumPut(width, WindowRect, 8, "Int")
   NumPut(height, WindowRect, 12, "Int")

   Style := CS_HREDRAW | CS_VREDRAW | CS_OWNDC
   lpfnWndProc := RegisterCallback("WndProc", "", 4)
   cbClsExtra := 0
   cbWndExtra := 0
   hInstance := DllCall("GetModuleHandle", UInt, 0)
   hbrBackground := 0
   hCursor := DllCall("LoadCursor", UInt, 0, UInt, IDC_ARROW)
   hIcon := DllCall("LoadIcon", UInt, 0, UInt, IDI_APPLICATION)
   lpszMenuName := 0

   NumPut(Style, wc, 0, "UInt")
   NumPut(lpfnWndProc, wc, 4, "UInt")
   NumPut(cbClsExtra, wc, 8, "UInt")
   NumPut(cbWndExtra, wc, 12, "UInt")
   NumPut(hInstance, wc, 16, "UInt")
   NumPut(hIcon, wc, 20, "UInt")
   NumPut(hCursor, wc, 24, "UInt")
   NumPut(hbrBackground, wc, 28, "UInt")
   NumPut(&lpszMenuName, wc, 32, "UInt")
   NumPut(&lpszClassName, wc, 36, "UInt")

   If !DllCall("RegisterClass", UInt, &wc)
   {
      MsgBox, Failed to register class.
      Return 0
   }

   If fullscreen
   {
      ; DEVMODE size: 156 ANSI, 220 UNICODE
      VarSetCapacity(dmScreenSettings, 156, 0)

      dmFields := DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT

      NumPut(156, dmScreenSettings, 0, "UShort")

      Result := DllCall("EnumDisplaySettings", UInt, 0, UInt, 0, UInt, &dmScreenSettings)

      NumPut(width, dmScreenSettings, 108, "UInt")
      NumPut(height, dmScreenSettings, 112, "UInt")
      NumPut(bits, dmScreenSettings, 104, "UInt") ;bits
      NumPut(dmFields, dmScreenSettings, 40, "UInt")

      Result := DllCall("ChangeDisplaySettings", UInt, &dmScreenSettings, UInt, CDS_FULLSCREEN)
      If Result != %DISP_CHANGE_SUCCESSFUL%
      {
         MsgBox,4,, The requested fullscreen mode is not supported by`nyour video card. Use windowed mode instead?
         IfMsgBox Yes
         {
            fullscreen := 0
         }
         Else
         {
            ; Let the user know the program is closing
            MsgBox, The program will now close.
            Return 0
         }
      }
   }

   If fullscreen
   {
      dwExStyle := WS_EX_APPWINDOW
      dwStyle := WS_POPUP
      DllCall("ShowCursor", UInt, 0)
   }
   Else
   {
      dwExStyle := WS_EX_APPWINDOW | WS_EX_WINDOWEDGE
      dwStyle := WS_OVERLAPPEDWINDOW
   }

   DllCall("AdjustWindowRectEx", UInt, &WindowRect, UInt, dwStyle, UInt, 0, UInt, dwExStyle)

   hWnd := DllCall("CreateWindowEx", UInt, dwExStyle
               , UInt, &lpszClassName
               , "Str", title
               , UInt, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN
               , Int, 0
               , Int, 0
               , Int, NumGet(WindowRect, 8, "Int")-NumGet(WindowRect, 0, "Int")
               , Int, NumGet(WindowRect, 12, "Int")-NumGet(WindowRect, 4, "Int")
               , UInt, 0
               , UInt, 0
               , UInt, hInstance
               , UInt, 0
               , UInt)
   If !hWnd
   {
      KillGLWindow()
      MsgBox, Window creation error. %A_LastError% : %ErrorLevel%
      Return 0
   }

   dwFlags := PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER

   NumPut(40, pfd, 0, "UShort")
   NumPut(1, pfd, 2, "UShort")
   NumPut(dwFlags, pfd, 4, "UInt")
   NumPut(PFD_TYPE_RGBA, pfd, 8, "UChar")
   NumPut(bits, pfd, 9, "UChar")
   NumPut(16, pfd, 23, "UChar")
   NumPut(PFD_MAIN_PLANE, pfd, 26, "UChar")

   hDC := DllCall("GetDC", UInt, hWnd)
   If !hDC
   {
      KillGLWindow()
      MsgBox, Can't create GL device context.
      Return 0
   }

   PixelFormat := DllCall("gdi32.dll\ChoosePixelFormat", UInt, hDC, UInt, &pfd)
   If !PixelFormat
   {
      KillGLWindow()
      MsgBox, Can't find a suitable pixel format.
      Return 0
   }

   If !DllCall("gdi32.dll\SetPixelFormat", UInt, hDC, UInt, PixelFormat, UInt, &pfd)
   {
      KillGLWindow()
      MsgBox, Can't set the pixel format.
      Return 0
   }

   hRC := DllCall("opengl32.dll\wglCreateContext", UInt, hDC, Int)
   If !hRC
   {
      KillGLWindow()
      MsgBox, Can't create GL rendering context.
      Return 0
   }

   If !DllCall("opengl32.dll\wglMakeCurrent", UInt, hDC, UInt, hRC)
   {
      KillGLWindow()
      MsgBox, Can't activate the GL rendering context.
      Return 0
   }

   DllCall("ShowWindow", UInt, hWnd, Int, SW_SHOW)
   ;DllCall("UpdateWindow", UInt, hWnd)
   DllCall("SetForegroundWindow", UInt, hWnd)
   DllCall("SetFocus", UInt, hWnd)
   ReSizeGLScene()

   If !InitGL()
   {
      KillGLWindow()
      MsgBox, Initialization failed.
      Return 0
   }

   Return 1

}




WndProc(hwnd, uMsg, wParam, lParam)
{

   Global
   VarSetCapacity(ps, 64, 0)

   If uMsg = %WM_ACTIVATE%
   {
      If !(wParam >> 16)
      {
         active := 1
      }
      Else
      {
         active := 0
      }

      Return 0
   }

   If uMsg = %WM_SYSCOMMAND%
   {
      If (wParam = %SC_SCREENSAVE% or wParam = %SC_MONITORPOWER%)
      {
         Return 0
      }
   }

   If uMsg = %WM_PAINT%
   {
      DllCall("BeginPaint", UInt, hwnd, UInt, &ps)
      DllCall("EndPaint", UInt, hwnd, UInt, &ps)
      Return 0
   }

   If uMsg = %WM_CLOSE%
   {
      DllCall("PostQuitMessage", UInt, 0)
      Return 0
   }

   If uMsg = %WM_SIZE%
   {
      width := lParam & 0x0ffff
      height := lParam >> 16
      ReSizeGLScene() ;lParam & 0x0ffff, lParam >> 16)
      Return 0
   }

   Return DllCall("DefWindowProc", UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam)

}




WinMain()
{

   Global ;fullscreen, F1, Escape

   ; Load required libs
   hOpenGl := DllCall("LoadLibrary", Str, "opengl32.dll")
   If !hOpenGl
   {
      MsgBox, OpenGl32.dll could not be loaded.
      ExitApp
   }

   hGlu32  := DllCall("LoadLibrary", Str, "glu32.dll")
   If !hGlu32
   {
      MsgBox, Glu32.dll could not be loaded.
      ExitApp
   }

   hGdi32  := DllCall("LoadLibrary", Str, "gdi32.dll")
   If !hGlu32
   {
      MsgBox, Gdi32.dll could not be loaded.
      ExitApp
   }

   MsgBox, 4, Start Fullscreen?, Would you like to run in fullscreen mode?
   IfMsgBox No
   {
      fullscreen := 0
   }

   If CreateGLWindow("Nehe's First Poylgon Tutorial", 640, 480, 16, fullscreen)
   {

      Loop
      {
         If Escape
         {
            Break
         }

         If F1
         {
            F1 := !F1
            fullscreen := !fullscreen
            KillGLWindow()
            If !CreateGLWindow("Nehe's First Poylgon Tutorial", 640, 480, 16, fullscreen)
            {
               Break
            }
         }

         If active
         {
            If !DrawGLScene()
            {
               Break
            }
         }

         Sleep, 50
      }
   }

   KillGLWindow()

   If hOpenGL
   {
      DllCall("FreeLibrary", UInt, OpenGL)
   }

   If hGlu32
   {
      DllCall("FreeLibrary", UInt, hGlu32)
   }

   If Gdi32
   {
      DllCall("FreeLibrary", UInt, 0)
   }

   Return 0

}

; ==============================================================================================

Esc::Escape := !Escape
F1::F1 := !F1
Back to top
IsNull



Joined: 10 May 2007
Posts: 63
Location: .switzerland

PostPosted: Thu Apr 24, 2008 12:13 p