Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

OpenGL DllCalls


  • Please log in to reply
14 replies to this topic
Zippo
  • Members
  • 56 posts
  • Last active: Jan 24 2009 05:18 AM
  • Joined: 21 Apr 2006
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:
;	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.
____________________

Zippo()
  • Guests
  • Last active:
  • Joined: --
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:
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. :(

Zippo
  • Members
  • 56 posts
  • Last active: Jan 24 2009 05:18 AM
  • Joined: 21 Apr 2006
*laces out*
____________________

IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007
wow, realy amazing ;)

I've changed your code, so the Triangle and the Quad rotating :D


;/////////////////////////////////////////////////////////////////////////////////////////////
#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

Z Gecko
  • Guests
  • Last active:
  • Joined: --
really, really cool! :O

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

IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007
rotating 3d pyramid :D

;/////////////////////////////////////////////////////////////////////////////////////////////
#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. 8)



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]

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

}





Z Gecko
  • Guests
  • Last active:
  • Joined: --
you´re really turned on by this, IsNull, aren´t you? :lol:

Me too! 8)

Next example:
;/////////////////////////////////////////////////////////////////////////////////////////////
#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


IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007

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

definitely :D
Well, ago 1 year, I tried to use the openGL dll too. But I failed. But then, a realy good solution crop up, u know... 8)

Z Gecko
  • Guests
  • Last active:
  • Joined: --
yeah, i noticed. :D

Z Gecko
  • Guests
  • Last active:
  • Joined: --
btw i found some nice OpenGL tutorials, they might help.
http://docs.mandrago...troduction.html
http://nehe.gamedev....on.asp?index=01
http://www.codeworx....opengl_tuts.php this on is in german language.

Rhys
  • Members
  • 761 posts
  • Last active: Aug 09 2013 04:53 PM
  • Joined: 17 Apr 2007
Amazing! This will hopefully open some very interesting doors for us.

holomind
  • Members
  • 341 posts
  • Last active: Aug 23 2015 03:27 PM
  • Joined: 11 Mar 2006
Hi somebody pointed me to this thread, from my expose-clone-thread...

looks nice what you did with your implementation. this means i can backport my experiments on : <!-- m -->http://community.thi... ... 0.msg12301<!-- m -->
and do it in AHK again ;)

some suggestion for your code, you should define all DllCalls as separate functions (Wrapper) which makes the code much more readable, and in best case you can even nearly copy/paste basic or c-code from other examples.

with the help of thinbasic, i learned enough opengl. to do it now in any language ;)

eg. similar to this, and you can even #include the file similar to gdi_plus_helper.ahk, you can mapp all GLU functions.

-- mfc wrapper
GetDC( hw ) {
   return DLLCall("GetDC", UInt, hw )
}

CreateDC( driver,device,output,mode  ) {
   return DLLCall("GetDC", UInt, driver, UInt, device, UInt, output, UInt, mode )
}

SetStretchBltMode( hdc , value ) {
     return DllCall("gdi32.dll\SetStretchBltMode", UInt,hdc, "int",value)
}

CreateCompatibleDC( hdc ) {
   return DllCall("gdi32.dll\CreateCompatibleDC", UInt,hdc)
}

CreateCompatibleBitmap( hdc , w, h ) {
     return DllCall("gdi32.dll\CreateCompatibleBitmap", UInt,hdc, Int,w, Int,h)
}
Greetings
Holomind.

IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007

with the help of thinbasic, i learned enough opengl. to do it now in any language Wink

great :D

Can you explain, how to create and atach a texture for a 3d volume?

In fact, I tried this tutorial: <!-- m -->http://www.codeworx....opengl_tut6.php<!-- m -->

But the part with the Bitmap loading, I doesn t understand. There is a Funtion who returns a pointer. Ok. After that, these calls follow:
glGenTextures(1, &texture[0]); 
glBindTexture(GL_TEXTURE_2D, texture[0]);
In the first call the param is a pointer to the other pointer!? And in the second call, the param is our pointer to the texture data... so I don't understand this step...

AUX_RGBImageRec *LoadBMP(char *Filename) // Lädt ein BMP
{
  FILE *File=NULL; // Das File Handle

  if (!Filename) 
  // Ist ein Dateiname übergeben worden?

  {
    return NULL; // offenbar nicht... Abbruch
  }

  File=fopen(Filename,"r"); 
  // Versuch die Datei zu öffnen

  if (File) // Existiert die Datei?
  {
    fclose(File); // Datei schließen
    return auxDIBImageLoad(Filename); 
    // BMP laden und Zeiger (Pointer) zurückgeben, 
    // der auf die Bilddaten verweist
  }
  return NULL; // Laden hat nicht geklappt
}

int LoadGLTextures() // Bitmaps laden und konvertieren
{
  int Status=FALSE; 
  // Status ersteinmal auf FALSE setzen

  AUX_RGBImageRec *TextureImage[1]; 
  // Speicherplatz für die Textur schaffen

  memset(TextureImage,0,sizeof(void *)*1); 
  //Vorher sichergehen das dieser auch leer ist

Das Bitmap "Data/codeworx.bmp" wird mit Hilfe von LoadBMP geladen und konvertiert. Falls das geklappt hat, wird Status auf TRUE gesetzt.

  if (TextureImage[0]=LoadBMP("Data/codeworx.bmp"))
  // Bitmap laden
  {
    Status=TRUE; // hat geklappt.

Aus den gespeicherten Bilddaten in TextureImage[0] wird jetzt mit Hilfe von glGenTextures() in texture[0] eine Textur erstellt. glBlindTexture() teilt OpenGL mit, das es sich bei texture[0] um eine 2D-Textur handelt.

    glGenTextures(1, &texture[0]); // Textur wird erstellt

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    //Es handelt sich um eine 2D Textur

Wirklich fertig ist die Textur leider noch nicht, wieder muss "mitgeteilt" werden, das es sich um eine 2D-Textur handelt, der übergebene Wert 0 ist vorerst unwichtig, repräsentiert den Grad derDetails, 3 steht für die 3 Farbkanäle Rot, Grün und Blau in denen das Bild als BMP vorliegt. Die Abmessungen werden automtisch mit sizeX und sizeY ermittelt, die nächste Null gibt die Rahmenbreite an, was ersteinmal vernachläßigt werden kann, der Farmodus ist RGB, die Bildinformationen bestehen aus unsigned Bytes und mit TextureImage[0]->data werden die Bilddaten letztendlich ausgelesen.

    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, 
                 TextureImage[0]->sizeY, 0, GL_RGB, 
                 GL_UNSIGNED_BYTE, TextureImage[0]->data);

Die nächsten Zeilen legen fest, welche Filter benutzt werden sollen um die Textur zu verzerren. GL_TEXTURE_MIN_FILTER steht für den Filter der verwendet werden soll wenn die Textur zusammengedrückt werden soll, GL_TEXTURE_MAG_FILTER wird genutzt wenn die Textur auseinandergezerrt wird. Die Filtermethode GL_LINEAR sieht sehr schön aus, belastet aber den Prozessor recht stark. GL_NEAREST kann ebenfalls genutzt werden, was zwar Ressourcen spart, leider aber recht pixelig wirkt.

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    // GL_LINEAR wird für GL_TEXTURE_MIN_FILTER genutzt
   
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    // GL_LINEAR wird für GL_TEXTURE_MAG_FILTER genutzt
  }

Abschließend sollte der durch TextureImage[0] belegte RAM wieder freigegeben werden

  if (TextureImage[0]) // Existiert TextureImage[0]?
  {
    if (TextureImage[0]->data) // enthält sie Bilddaten?
    {
      free(TextureImage[0]->data); 
      // wenn ja, dann diese Bilddaten löschen
    }

    free(TextureImage[0]); 
    // Und auch die Struktur an sich löschen.
  }

Und der Status wird zurückgegen, ist dieser TRUE, hat alles geklappt.

  return Status; // Status zurückgeben
}
sry for german comments...

And btw, wrapping this dllcalls would be great. Maby I will try it and share here :wink:

holomind
  • Members
  • 341 posts
  • Last active: Aug 23 2015 03:27 PM
  • Joined: 11 Mar 2006
Hi IsNull, your posted code looks "obvious" to me, what is your question?

I try what i understand. (no problem with the german comments, as i am native german speaking)
glGenTextures(1, &texture[0]); 
glBindTexture(GL_TEXTURE_2D, texture[0]);

glGenTextures( -number of texture- , -texture data)
loads a string or stream of data (pixels) into a opengl-"variable" and stores it into position=1.
then the Texture is "mapped" "linked" "applied" onto the next polygones until glDraw... is called. the &...

the thinbasic code for this example can be found in my flip3d-example and is perhaps more understandable. (see links in the previous post to thinbasic).

Gen-Texture normally only returns a "Number" of a Texture, so i am wondering why this is a pointer. (which means this is a region in the memory where the texture-pixels are stored).

Bind-Texture only says the open-gl engine to apply or copy the texture-data for position x to the vectors.

you first call bindtexture and this will be applied to the next "polygon drawing" commands.

perhaps you should read the thinbasic code with the flip3d experiment, for me the code is quite readable in thinbasic and also the code is working. (you only need to install the free thinbasic -programm/ide to run it, you find the software at <!-- m -->http://www.thinbasic.com<!-- m --> ).

i also got a notice from psch at the thinbasic forum, who supports the efforts. (and he has way more experience in opengl, and is really helpful with questions). he managed to build a function to convert a hdc into a string and then load this into a opengl texture, which means to load the contents of a normal windows-window into a texture.

but if you only want to load a bmp into a texture there is a simple function (at least in thinbasic TBGL , but i guess this is only a dll call) TBGL_loadtexture or similar.

the only tricky part is to handle returnvalues, but opengl in nature is not objectoriented but works with simple function-calls and behaves more like (c and not c++) so returnvalues should not be too complex.

there will be ways to find a syntax we can copy/paste from TBGL (or simple DLLCalls) in thinbasic vs. AHK.

In essence, opengl with dllcalls are only Windows-API calls (or similar) and are therefore exactly the same in thinbasic or AHK (or c++ or c or VBasic or c# ) if you will. basic or AHK is only the GLUE to these DLL-Calls. This also means even if AHK or thinbasic are interpreted the programm will still be fast, because there is way more time spent in the dll-call than the basic or AHK program.

I see some fun coming, programming AHK and thinbasic in parallel with mostly compatible code (copy/paste). Combining the users and community of AHK with the knowledge of not so many but smart people in thinbasic.

you should try the TBGL (thinBasicGL) demos on the thinbasic webpage, and you will learn to programm OpenGL quickly whithout the hassle C or C++ involve.

I will also use the AHK- DllCall Wrapper functions, and name it in a way it will be matching with the existing functions in thinbasic. either with TBGL_-Prefix or als real DLL-Calls there , which is also possible.

Psch gives the right comments at the right time. fabulous ;)

<!-- m -->http://community.thi... ... icseen#new<!-- m -->

Perhaps this example also helps. ( simply execute the thinbasic code to see what happens, thinbasic is as simple to use as ahk...)
<!-- m -->http://community.thi... ... 90#msg6490<!-- m -->

Hope this gives some hints.
Greeting
Holomind.

Zippo()
  • Guests
  • Last active:
  • Joined: --
Glad to see others with an interest in OpenGL. Overall much nicer interface than what DirectX has become IMO :)

But that script above is very buggy... please don't use it as it is :D

some suggestion for your code, you should define all DllCalls as separate functions (Wrapper) which makes the code much more readable, and in best case you can even nearly copy/paste basic or c-code from other examples.

Erm, I don't have a bit of problem reading the code. That's what AHK code looks like. You're already calling a high-performance graphics library from a scripting language. I can't see wrapping all the calls and making performance worse (an extra function call for every DllCall?).

You can make it a little better by calling something like glGenTextures := DllCall("GetProcAddress", UInt, hOpenGL, Str, "glGenTextures"), and then you can use it like DllCall(glGenTextures, UInt, 1, UInt, &texture).

That's just my opinion. Do what you want.

And I'll clean it up here in a while and post in the Scripts and Functions section, unless a mod wouldn't mind sticking it there now?

One last thing: can anyone explain why the first script has a memory leak and the modified ones do not?

Thanks.