;==============================================================================
;	    Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE's	410
;==============================================================================

;==============================================================================
; ==> Part 410 : OpenGL example
;------------------------------------------------------------------------------
; Cheers,
; here is my OpenGL example with a Windows 9x HIGH RESOLUTION TIMER.
; YOU NEED A PI 233 MHZ / WINDOWS 95B (!) OR GREATER (OPENGL32.DLL,GLU32.DLL).
; You also need the Opengl.hlp / Mmedia.hlp reference, a link is on my website.
; The first idea here is to obtain more time for our thread, I hope it works.
; I call API = "GetCurrentThread" to get a pseudo handle.
; Ready to call API = "SetThreadPriority" with THREAD_PRIORITY_HIGHEST = 2h.
; The next idea here is to use a HIGH RESOLUTION TIMER from the multimedia
; library. This timer runs in its own thread !
; API "timeSetEvent" / "timeKillEvent" (Mmedia.hlp) is the function call.
; This timer is much more exact than a standard windows timer.
; This timer could only beat by multimedia applications using MCI services.
; Do you ever try using a standard windows timer with OpenGL, forget it.
; Using this TIMER allows me to program an exact (not realy) OpenGL animation.
; In the window procedure (WP1) reacting to a WM_CREATE message I setup the
; OpenGL window (OpenGL.hlp) and the discussed timer (Mmedia.hlp).
; This timer calls a timer procedure (TP1) on the specified delay time.
; The timer procedure post a WM_PAINT message into the message queue of the 
; main window message loop, so in the end to the main window procedure (WP1).
; With a FLAG I ensure to only post a new message if the last was proceed !
; If the main window procedure (WP1) receives a WM_PAINT message I draw the
; OpenGL stuff.
; Here I also use some simple FPU instructions to add two real values.
; Do not forget to init the FPU, only once (perhaps you did it before ?).
; I set the FLAG to indicate the timer procedure (TP1) that WM_PAINT is proceed
; But after all, it is only a demo ..., where is the next whisky bar ?
; Yeaps

;==============================================================================
; Assembler directives
;------------------------------------------------------------------------------
.386			; specifies the processor our program want run on
.Model Flat ,StdCall	; Flat for Win9x (32 Bit), Calling Convention
option casemap:none	; case sensitive !!!

;==============================================================================
; Include all files where API functins resist you want use, set correct path
;------------------------------------------------------------------------------
include \masm32\include\windows.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\opengl32.lib		;openGL library
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\winmm.lib

;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
;------------------------------------------------------------------------------
GetModuleHandleA	PROTO :DWORD
LoadIconA		PROTO :DWORD,:DWORD
LoadCursorA		PROTO :DWORD,:DWORD
RegisterClassExA	PROTO :DWORD
CreateWindowExA		PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
			      :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ShowWindow		PROTO :DWORD,:DWORD
UpdateWindow		PROTO :DWORD
GetMessageA 		PROTO :DWORD,:DWORD,:DWORD,:DWORD
TranslateMessage 	PROTO :DWORD
DispatchMessageA 	PROTO :DWORD
PostQuitMessage 	PROTO :DWORD
DefWindowProcA 		PROTO :DWORD,:DWORD,:DWORD,:DWORD
ExitProcess		PROTO :DWORD
MessageBoxA		PROTO :DWORD,:DWORD,:DWORD,:DWORD
DestroyWindow 		PROTO :DWORD
GetDC			PROTO :DWORD
ChoosePixelFormat	PROTO :DWORD,:DWORD
SetPixelFormat		PROTO :DWORD,:DWORD,:DWORD
wglCreateContext	PROTO :DWORD
wglMakeCurrent		PROTO :DWORD,:DWORD
glMatrixMode		PROTO :DWORD
glFrustum		PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
			      :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
glTranslatef		PROTO :DWORD,:DWORD,:DWORD
glScalef		PROTO :DWORD,:DWORD,:DWORD
glShadeModel		PROTO :DWORD
glEnable		PROTO :DWORD
GetClientRect		PROTO :DWORD,:DWORD
glViewport		PROTO :DWORD,:DWORD,:DWORD,:DWORD
wglDeleteContext	PROTO :DWORD
ReleaseDC		PROTO :DWORD,:DWORD
glPushMatrix		PROTO
glRotatef		PROTO :DWORD,:DWORD,:DWORD,:DWORD
glClear			PROTO :DWORD
glDisable		PROTO :DWORD
glBegin			PROTO :DWORD
glNormal3f		PROTO :DWORD,:DWORD,:DWORD
glColor3f		PROTO :DWORD,:DWORD,:DWORD
glVertex3f		PROTO :DWORD,:DWORD,:DWORD
glEnd			PROTO
glPopMatrix		PROTO
glFinish		PROTO
SwapBuffers		PROTO :DWORD
PostMessageA 		PROTO :DWORD,:DWORD,:DWORD,:DWORD
timeGetDevCaps		PROTO :DWORD,:DWORD
timeSetEvent		PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
timeKillEvent		PROTO :DWORD
GetCurrentThread	PROTO
SetThreadPriority	PROTO :DWORD,:DWORD

;==============================================================================
; .const 	= the constants area starts here,constants are defined & fixed
;------------------------------------------------------------------------------
.const

;==============================================================================
; .Data		= the data area starts here, datas are defined but not fixed
;   MASM converts real4, real8, real10 values into FPU / IEEE standard.
;------------------------------------------------------------------------------
.Data
IconName		db "TDIcon",0		;icon name in rc file
MenuName		db "TDMenu",0		;menu name in rc file
ClassName		db "TDWinClass",0	;name of windows class
WindowName		db "Test Department - http://surf.to/TestD",0;winname
WM_PAINT_proceed	db 0h			;WM_PAINT flag
speed			dd 0Ch			;default animation speed
gl_Mode			dd 7h			;default gl_Mode, cube=7h
pointervertex		dd 0h			;pointer to vertexArea
pointercolor		dd 0h			;pointer to colorArea
Scale_counter		dd 10h			;default/max scale count value

; - coordinates ( API=glFrustum ) -
_glFrustum_zfar		real8	4.0		;so what ?, QWORD=8 byte !
_glFrustum_znear	real8	1.0		;
_glFrustum_top		real8	0.6		;
_glFrustum_bottom	real8	-0.6		;
_glFrustum_right	real8	0.6		;
_glFrustum_left		real8	-0.6		;

; - normal values ( API=glNormalf ) -
normalArea1		real4	1.0		;normal
normalArea2		real4	-1.0		;
normalArea3		real4	1.0		;
normalArea4		real4	-1.0		;
normalArea5		real4	1.0		;
normalArea6		real4	-1.0		;

; - rotate values ( API=glRotatef ) -
rotateAngel		real4	0.0		;default rotate angel
rotateAdd		real4	2.0		;added to rotate angel
rotate_z1		real4	0.3		;coordinates
rotate_y1		real4	1.0		;
rotate_x1		real4	0.0		;
rotate_z2		real4	1.0		;coordinates
rotate_y2		real4	0.0		;
rotate_x2		real4	0.0		;

; - translate vector values ( API=glTranslatef ) -
transVector_z		real4	-2.0		;z

; - scale values ( API=glScalef ) -
scale_Out		real4	0.9		;scale multiplicator
scale_In		real4	1.1111		;why this ?, you don't know ? 

; - ARRAY of 3 vertex coordinates z, y, x ( API=glVertex3f ) -
; 4 for each of the 6 areas. 12 parameter for each side of the object.
vertexArea1	real4	0.6,0.4,0.6,0.6,0.4,-0.6,0.6,-0.4,-0.6,0.6,-0.4,0.6
vertexArea2	real4	-0.6,-0.4,-0.6,-0.6,0.4,-0.6,-0.6,0.4,0.6,-0.6,-0.4,0.6
vertexArea3	real4	0.6,0.4,0.6,-0.6,0.4,0.6,-0.6,0.4,-0.6,0.6,0.4,-0.6
vertexArea4	real4	-0.6,-0.4,-0.6,-0.6,-0.4,0.6,0.6,-0.4,0.6,0.6,-0.4,-0.6
vertexArea5	real4	0.6,0.4,0.6,0.6,-0.4,0.6,-0.6,-0.4,0.6,-0.6,0.4,0.6
vertexArea6	real4	-0.6,-0.4,-0.6,0.6,-0.4,-0.6,0.6,0.4,-0.6,-0.6,0.4,-0.6

; - ARRAY of 3 color values B, G, R ( API=glColor3f ) -
; 4 for each of the 6 areas. Colors will be associated with the vertex.
colorArea1	real4	0.75,0.2,0.4,-0.5,-0.5,0.5,1.0,0.5,0.0,0.0,-0.5,0.6
colorArea2	real4	1.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,0.4,0.5,0.75
colorArea3	real4	1.0,0.0,0.0,-0.5,0.5,0.5,1.0,-1.0,1.0,0.4,0.2,0.75
colorArea4	real4	0.2,0.3,0.1,-0.5,-0.5,0.5,0.5,-0.5,-0.5,0.4,0.2,0.75
colorArea5	real4	0.5,0.0,0.0,-0.5,-0.5,0.5,0.5,-0.5,-0.5,0.4,0.2,0.75
colorArea6	real4	1.0,0.4,0.0,0.5,-0.5,0.5,0.5,-0.5,-0.5,0.4,0.2,0.75

;==============================================================================
; .Data?	= the data? area starts here, not defined and not fixed
;------------------------------------------------------------------------------
.data?
hdc			dd ?		;handle device context
pixfmtindex		dd ?		;pixel format index
hglrc			dd ?		;handle OpenGL rendering context
timerevent1		dd ?		;handle high resolution timer

; - WndClassEx Structure ( API=RegisterClassExA ) -
cbSize			dd ?		;size in bytes of this structure
style			dd ?		;window style
lpfnWndProc		dd ?		;address of user proc function
cbclsExtra		dd ?		;extra bytes to allocate set to 0
cbWndExtra		dd ?		;extra bytes class directive, rc file
hInstance		dd ?		;program handle(API=GetModuleHandleA)
hIcon			dd ?		;handle of icon (API=LoadIconA)
hcursor			dd ?		;handle of cursor (API=LoadCursor)
hbrBackground		dd ?		;background color, 0=transparent
lpszMenuName 		dd ?		;name of menu class in resource file
lpszClassName		dd ?		;name of windows this window class
hIconSm			dd ?		;iconhandle 0=search in resource file

; - Push Parameter MAIN WINDOW CallBack procedure ( API=RegisterClassExA ) -
WP1_CallBack		dd ?		;return address of calling routine
WP1_hWnd		dd ?		;handle of window who receives message
WP1_uMsg		dd ?		;the message number 
WP1_wParam		dd ?		;extra info about the message 
WP1_lParam		dd ?		;extra info about the message 

; - Push Parameter TIMER CallBack procedure ( API=timeSetEvent ) -
TP1_CallBack		dd ?		;return address of calling routine
TP1_uID			dd ?		;identifier of the timer event
TP1_uMsg		dd ?		;reserved; do not use
TP1_dwUser		dd ?		;the dwUser parameter of timeSetEvent
TP1_dw1			dd ?		;reserved; do not use
TP1_dw2			dd ?		;reserved; do not use

; - Msg Structure ( API=GetMessageA ) - member POINT = POINT structure
hWnd			dd ?		;handle of window who receives message
message			dd ?		;the message number
wParam			dd ?		;extra info about the message
lParam			dd ?		;extra info about the message 
time			dd ?		;time the message was posted 
xpt			dd ?		;cursor x-position, POINT struc
ypt			dd ?		;cursor x-position, POINT struc 

; - PIXELFORMATDESCRIPTOR Structure ( API=ChoosePixelFormat ) -
pfd_nSize		dw ?		;size of this data structure
pfd_nVersion		dw ?		;version, should be set to 1
pfd_dwFlags		dd ?		;bit flags, properties pixel buffer
pfd_iPixelType		db ?		;specifies the type of pixel data
pfd_cColorBits		db ?		;color bitplanes each color buffer
pfd_cRedBits		db ?		;red bitplanes each RGBA color buffer
pfd_cRedShift		db ?		;shift count for red bitplanes
pfd_cGreenBits		db ?		;green bitplanes each RGBA color buffer
pfd_cGreenShift		db ?		;shift count for green bitplanes
pfd_cBlueBits		db ?		;blue bitplanes each RGBA color buffer
pfd_cBlueShift		db ?		;shift count for blue bitplanes
pfd_cAlphaBits		db ?		;alpha bitplanes are not supported
pfd_cAlphaShift		db ?		;alpha bitplanes are not supported
pfd_cAccumBits		db ?		;bitplanes in the accumulation buffer
pfd_cAccumRedBits	db ?		;red bitplanes in accumulation buffer
pfd_cAccumGreenBits	db ?		;green bitplanes in accumulation buffer
pfd_cAccumBlueBits	db ?		;blue bitplanes in accumulation buffer
pfd_cAccumAlphaBits	db ?		;alpha bitplanes in accumulation buffer
pfd_cDepthBits		db ?		;depth of the depth (z-axis) buffer
pfd_cStencilBits	db ?		;depth of the stencil buffer
pfd_cAuxBuffers		db ?		;auxiliary buffers are not supported
pfd_iLayerType		db ?		;ignored
pfd_bReserved		db ?		;number of overlay and underlay planes
pfd_dwLayerMask		dd ?		;ignored
pfd_dwVisibleMask	dd ?		;transparent color/index underlay plane
pfd_dwDamageMask	dd ?		;ignored

; - RECT Structure ( API=GetClientRect ) -
rc_left			dd ?		;x, upper-left corner of the rectangle
rc_top			dd ?		;y, upper-left corner of the rectangle 
rc_right		dd ?		;x, lower-right corner of the rectangle 
rc_bottom		dd ?		;y, lower-right corner of the rectangle

; - TIMECAPS Structure ( API=timeGetDevCaps ) -
wPeriodMin		dd ?		;TIMER minimum supported resolution
wPeriodMax		dd ?		;TIMER maximum supported resolution

;==============================================================================
; .CODE		=our code area starts here	Main=label of our program code
;------------------------------------------------------------------------------
.Code
Main:

;==============================================================================
; Always get your program ID first (API=GetModuleHandleA)
;------------------------------------------------------------------------------
push	0h				;lpModuleHandle, 0=get program handle
call 	GetModuleHandleA		;- API Function -
mov	hInstance,eax			;return value in eax=handle of program

;==============================================================================
; The GetCurrentThread function returns a pseudohandle for the current thread.
;------------------------------------------------------------------------------
call	GetCurrentThread		;- API Function -

;==============================================================================
; SetThreadPriority function sets the priority value for the specified thread.
;------------------------------------------------------------------------------
push	2h				;nPriority, thread priority level 
					;THREAD_PRIORITY_HIGHEST	= 2h
push	eax				;hThread, handle to the thread
call	SetThreadPriority		;- API Function -
cmp	eax,0h				;ERROR ?
je	ExitPrg				;

;==============================================================================
; Init the Floating Point Unit, init it only once !
;------------------------------------------------------------------------------
finit					;init FPU, init coprocessor

;==============================================================================
; The API function "RegisterClassExA" registers a window class.
; This API needs a "WNDCLASSEX" structure so we fill it with correct values.
;------------------------------------------------------------------------------
mov	cbSize,30h			;size in bytes of WNDCLASSEX structure
mov	style,3h			;window style
mov	lpfnWndProc,OFFSET WP1		;address of user lpfnWndProc function
mov	cbclsExtra,0h			;extra bytes to allocate set to 0
mov	cbWndExtra,0h			;class directive in rc file
mov	hbrBackground,2h		;background,1=background(parameter+1)
mov	lpszMenuName,OFFSET MenuName	;menu name in resource file,0=no menu
mov	lpszClassName,OFFSET ClassName	;name of windows class
mov	hIconSm,0h			;iconhandle 0=search in rc file
;------------------------------------------------------------------------------
; API "LoadIconA" loads an icon defined in the resource file and stores the
; handle in the "WNDCLASSEX" structure
;------------------------------------------------------------------------------
push	OFFSET IconName 		;icon-string or icon resource id
push	hInstance			;our program handle
call 	LoadIconA			;- API Function -
mov	hIcon,eax			;store handle of newly loaded icon
;------------------------------------------------------------------------------
; API "LoadCursorA" loads a default system cursor, in this case we must set
; hInstance to 0 and lpCursorName to a default system cursor value, here 32512
; Then we store the cursor handle in the "WNDCLASSEX" structure
;------------------------------------------------------------------------------
push	32512				;lpCursorName, default value in dezimal
push	0h				;hInstance, 0=default system cursor
call	LoadCursorA			;- API Function -
mov	hcursor,eax			;store handle of the cursor
;------------------------------------------------------------------------------
; Now, after filled the "WNDCLASSEX" structure we call API "RegisterClassEx"
;------------------------------------------------------------------------------
push	OFFSET cbSize			;pointer to WNDCLASSEX structure
call 	RegisterClassExA		;- API Function -

;==============================================================================
; API "CreateWindowExA" creates an overlapped, pop-up, or child window with an
; extended style. The return value in EAX is the handle of the new window.
; This API sends a WM_CREATE message to the window procedure (WP1).
;------------------------------------------------------------------------------
push	0h				;lpParam, extra pointer data 0=no data
push	hInstance			;hInstance, handle of our program
push 	0h				;hMenu, handle window menu 0=class menu
push	0h				;hWndParent, handle parent window 0=no
push	00000150h			;intnHeight, window height pixel
push	00000140h			;intnWidth, window width pixel
push	00000010h			;inty, vertical position window
push	00000010h			;intx, horizontal position window
push	04CA0000h			;dwStyle, look into WIN32.HLP
push	OFFSET WindowName		;lpWindowName, pointer to window name
push	OFFSET ClassName		;lpClassName, pointer to class name
push	0300h				;dwExStyle, extra window style 0=no
call	CreateWindowExA			;- API Function -
mov	hWnd,eax			;hwnd,return value=handle of window

;==============================================================================
; API "ShowWindow" function sets the specified window's show state. 
;------------------------------------------------------------------------------
push 	1h				;nCmdShow, show state 1=SW_SHOWNORMAL
push 	hWnd				;hwnd, handle of window
call 	ShowWindow			;- API Function -

;==============================================================================
; API "UpdateWindow" updates the area of the specified window by sending a
; WM_PAINT message to the window if the window's update region is not empty.
;------------------------------------------------------------------------------
push 	hWnd				;hwnd, handle of window
call	UpdateWindow			;- API Function -

LoopGetMessage:
;==============================================================================
; API "GetMessageA" retrieves a message + places it in the specified structure.
;------------------------------------------------------------------------------
push	0h				;wMsgFilterMax, highest message value
push	0h				;wMsgFilterMin, lowest message value
push	0h				;hWnd, handle of window who gets msg.
push	OFFSET hWnd			;lpMsg, pointer to MSG structure
call	GetMessageA			;- API Function -
cmp	eax,0h				;check if return value=0 (exit)
je	ExitPrg				;if return value is 0 goto LABEL

;==============================================================================
; API "TranslateMessage" translates virtual-key messages in character messages
;------------------------------------------------------------------------------
push	OFFSET hWnd			;lpMSG, pointer to msg structure
call	TranslateMessage		;- API Function - keyboard code

;==============================================================================
; API "DispatchMessageA" function dispatches a message to a window procedure.
;------------------------------------------------------------------------------
push	OFFSET hWnd			;lpMSG, pointer to msg structure
call	DispatchMessageA		;- API Function -
jmp	LoopGetMessage			;check for message again, goto LABEL

ExitPrg:
;==============================================================================
; Next we terminate our program (API=ExitProcess)
;------------------------------------------------------------------------------
push	hInstance			;push our programm handle to exit
call	ExitProcess			;- API Function -

;##############################################################################
;==============================================================================
; This is the Window Procedure lpfnWndProc (API=RegisterClassExA) for this
; registered window.
; The WindowProc function is an application-defined callback function that
; processes messages sent to a window. 
; Here our code for checking the receiving messages resist.
; First we POP all parameter given by Windows from the stack and store them in
; variables for future use.
; !!!!! Next we PUSH the WP1_CallBack return address back to the stack !!!!!
; !!! WP1_CallBack must be avaible on the stack in this window procedure !!!
; It is also a good idea to PUSHAD all register, because than we are free to
; use all register in this window procedure.
; Before we leave this subroutine we must POPAD them back. 
;------------------------------------------------------------------------------
WP1:
pop 	WP1_CallBack			;POP return address lpfnWndProc
pop	WP1_hWnd			;handle of window who receives message
pop	WP1_uMsg			;the message number
pop	WP1_wParam			;extra info about the message
pop	WP1_lParam			;extra info about the message
push	WP1_CallBack			;PUSH return address lpfnWndProc !!!!!
pushad					;push all register to the stack

mov	eax,WP1_uMsg			;move the message number to eax
;==============================================================================
; WM_CREATE (value=01h) message received ?
;------------------------------------------------------------------------------
WP1_uMsg_01h:
cmp	eax,1h				;check if WM_CREATE message recieved
jne	WP1_uMsg_02h			;if not goto LABEL
;------------------------------------------------------------------------------
; The GetDC function retrieves a handle of a display device context (DC)
; for the client area of the specified window.
;------------------------------------------------------------------------------
push	WP1_hWnd			;hWnd, handle owner window
call	GetDC				;- API Function -
mov	hdc,eax				;handle device context
;------------------------------------------------------------------------------
; The ChoosePixelFormat function attempts to match an appropriate pixel format
; supported by a device context to a given pixel format specification.
;------------------------------------------------------------------------------
mov	pfd_nSize,28h			;size of structure
mov	pfd_nVersion,1h			;version of this data structure, set 1
mov	pfd_dwFlags,25h			;PFD_SUPPORT_OPENGL	= 00000020h
					;PFD_DOUBLEBUFFER	= 00000001h
					;PFD_DRAW_TO_WINDOW	= 00000004h
mov	pfd_iPixelType,0h		;PFD_TYPE_RGBA		=	 0h
mov	pfd_dwLayerMask,0h		;PFD_MAIN_PLANE		=	 0h
mov	pfd_cColorBits,10h		;# color bitplanes in each colorbuffer
mov	pfd_cDepthBits,10h		;?
mov	pfd_cAlphaBits,0h		;currently not supported
mov	pfd_cAccumBits,0h		;# bitplanes in accumulation buffer
mov	pfd_cStencilBits,0h		;depth of the stencil buffer
mov	pfd_cAuxBuffers,0h		;auxiliary buffers are not supported
push	OFFSET pfd_nSize		;PIXELFORMATDESCRIPTOR structure
push	hdc				;handle device context
call	ChoosePixelFormat		;- API Function -
mov	pixfmtindex,eax			;return value = pixel format index
;------------------------------------------------------------------------------
; The SetPixelFormat function sets the pixel format of the specified device
; context to the format specified by the iPixelFormat index.
;------------------------------------------------------------------------------
push	OFFSET pfd_nSize		;PIXELFORMATDESCRIPTOR structure
push	pixfmtindex			;pixel format index
push	hdc				;device context whose pixel format set
call	SetPixelFormat			;- API Function -
;------------------------------------------------------------------------------
; The wglCreateContext function creates a new OpenGL rendering context, which
; is suitable for drawing on the device referenced by hdc.
; The rendering context has the same pixel format as the device context.
;------------------------------------------------------------------------------
push	hdc				;handle device context
call	wglCreateContext		;- API Function -
mov	hglrc,eax			;handle OpenGL rendering context
;------------------------------------------------------------------------------
; The wglMakeCurrent function makes a specified OpenGL rendering context the
; calling thread's current rendering context.
;------------------------------------------------------------------------------
push	hglrc				;handle OpenGL rendering context
push	hdc				;handle device context
call	wglMakeCurrent			;- API Function -
;------------------------------------------------------------------------------
; The glMatrixMode function specifies which matrix is the current matrix.
;------------------------------------------------------------------------------
push	1701h				;GL_PROJECTION		= 00001701h
call	glMatrixMode			;- API Function -
;------------------------------------------------------------------------------
; The glFrustum function multiplies the current matrix by a perspective matrix.
;------------------------------------------------------------------------------
mov	ebx,OFFSET _glFrustum_zfar	;distances far depth clipping planes
push	[ebx+4]				;
push	[ebx]				;
mov	ebx,OFFSET _glFrustum_znear	;distances near depth clipping planes
push	[ebx+4]				;
push	[ebx]				;
mov	ebx,OFFSET _glFrustum_top	;coord. top horizontal clipping planes
push	[ebx+4]				;
push	[ebx]				;
mov	ebx,OFFSET _glFrustum_bottom	;coord. bottom horiz. clipping planes
push	[ebx+4]				;
push	[ebx]				;
mov	ebx,OFFSET _glFrustum_right	;coord. right vertical clipping planes
push	[ebx+4]				;
push	[ebx]				;
mov	ebx,OFFSET _glFrustum_left	;coord. left vertical clipping planes
push	[ebx+4]				;
push	[ebx]				;
call	glFrustum			;- API Function -
;------------------------------------------------------------------------------
; The glMatrixMode function specifies which matrix is the current matrix.
;------------------------------------------------------------------------------
push	1700h				;GL_MODELVIEW		= 000001700h
call	glMatrixMode			;- API Function -
;------------------------------------------------------------------------------
; The glTranslatef function multiply the current matrix by a translation matrix
;------------------------------------------------------------------------------
push	transVector_z			;z, coordinates translation vector
push	0h				;y, coordinates translation vector
push	0h				;x, coordinates translation vector
call	glTranslatef			;- API Function -
;------------------------------------------------------------------------------
; The glShadeModel function selects flat or smooth shading.
;------------------------------------------------------------------------------
push	1D01h				;GL_SMOOTH		= 000001d01h
call	glShadeModel			;- API Function -
;------------------------------------------------------------------------------
; The glEnable and glDisable functions enable or disable OpenGL capabilities.
;------------------------------------------------------------------------------
push	0B71h				;GL_DEPTH_TEST		= 000000b71h
call	glEnable			;- API Function -
push	0B44h				;GL_CULL_FACE		= 000000b44h
call	glEnable			;- API Function -
;------------------------------------------------------------------------------
; The GetClientRect function retrieves coordinates of a window's client area.
;------------------------------------------------------------------------------
push	OFFSET rc_left			;lpRect, pointer to RECT structure
push	WP1_hWnd			;hWnd, handle owner window
call	GetClientRect			;- API Function -
;------------------------------------------------------------------------------
; The glViewport function sets the viewport.
; If you change the x and y values the object will move on the screen !
;------------------------------------------------------------------------------
push	rc_bottom			;height of the viewport
push	rc_right			;width of the viewport
push	0h				;y, lower-left corner viewport rect.
push	0h				;x, the default in pixel is (0,0)
call	glViewport			;- API Function -
;------------------------------------------------------------------------------
; timeGetDevCaps function queries the timer device to determine its resolution.
; Returned values in the TIMECAPS struc are different from system to system.
;------------------------------------------------------------------------------
push	8h				;cbtc, size in bytes TIMECAPS structure
push	OFFSET wPeriodMin		;ptc, address of a TIMECAPS structure
call	timeGetDevCaps			;- API Function -
;------------------------------------------------------------------------------
; The timeSetEvent function starts a specified timer event. After activated, it
; calls the specified callback function. It runs in its own thread ! 
;------------------------------------------------------------------------------
push	1h				;fuEvent, timer type, TIME_PERIODIC=1h
push	0h				;dwUser, user-supplied callback data 
push	OFFSET TP1			;lpTimeProc, address callback function
push	wPeriodMin			;uResolution, resolution in ms
push	speed				;uDelay, event delay in milliseconds
call	timeSetEvent			;- API Function -
mov	timerevent1,eax			;
jmp	WP1_return			;

;==============================================================================
; WM_DESTROY (value=02h) message received ?
;------------------------------------------------------------------------------
WP1_uMsg_02h:
cmp	eax,2h				;check if value=2h (WM_DESTROY)
jne	WP1_uMsg_0Fh			;if not 2h go to LABEL
;------------------------------------------------------------------------------
; The timeKillEvent function cancels a specified timer event.
;------------------------------------------------------------------------------
push	timerevent1			;uTimerID, ID of timer event to cancel
call	timeKillEvent			;- API Function -
;------------------------------------------------------------------------------
; The wglMakeCurrent function makes a specified OpenGL rendering context the
; calling thread's current rendering context.
;------------------------------------------------------------------------------
push	0h				;hglrc, release = 0h
push	0h				;hdc, ignored if hglrc = 0h
call	wglMakeCurrent			;- API Function -
;------------------------------------------------------------------------------
; The wglDeleteContext function deletes a specified OpenGL rendering context.
;------------------------------------------------------------------------------
push	hglrc				;hglrc, handle OpenGL rendering context
call	wglDeleteContext		;- API Function -
;------------------------------------------------------------------------------
; The ReleaseDC function releases a device context (DC)
;------------------------------------------------------------------------------
push	hdc				;hdc, handle of device context  
push	WP1_hWnd			;hwnd, handle of window 
call	ReleaseDC			;- API Function -
;------------------------------------------------------------------------------
; API "PostQuitMessage" indicates to Windows a request to terminate
;------------------------------------------------------------------------------
popad					;pop all register back from stack
push	0h				;nExitCode, exit code=wParam
call 	PostQuitMessage			;- API Function -
xor	eax,eax				;set eax to 0 to exit our program
ret					;return to lpfnWndProc

;==============================================================================
; WM_PAINT (value=0Fh) message, used to repaint the window area
;------------------------------------------------------------------------------
WP1_uMsg_0Fh:
cmp	eax,0Fh				;check if WM_PAINT message recieved
jne	WP1_uMsg_111h			;if not goto label
;------------------------------------------------------------------------------
; The glPushMatrix functions push the current matrix stack.
;------------------------------------------------------------------------------
call	glPushMatrix			;- API Function -
;------------------------------------------------------------------------------
; The glMatrixMode function specifies which matrix is the current matrix.
;------------------------------------------------------------------------------
push	1700h				;GL_MODELVIEW		= 000001700h
call	glMatrixMode			;- API Function -
;------------------------------------------------------------------------------
; The glRotatef functions multiply the current matrix by a rotation matrix.
;------------------------------------------------------------------------------
push	rotate_z1			;z, coordinates of a vector
push	rotate_y1			;y, coordinates of a vector
push	rotate_x1			;x, coordinates of a vector
push	rotateAngel			;angle of rotation in degrees
call	glRotatef			;- API Function -
fld	rotateAngel			;load real value in TOS
fadd	rotateAdd			;add two real values
fstp	rotateAngel			;store real value and remove from TOS
;------------------------------------------------------------------------------
; The glRotatef functions multiply the current matrix by a rotation matrix.
;------------------------------------------------------------------------------
push	rotate_z2			;z, coordinates of a vector
push	rotate_y2			;y, coordinates of a vector
push	rotate_x2			;x, coordinates of a vector
push	rotateAngel			;angle of rotation in degrees
call	glRotatef			;- API Function -
;------------------------------------------------------------------------------
; The glClear function clears buffers within the viewport.
;------------------------------------------------------------------------------
push	4100h				;GL_COLOR_BUFFER_BIT	= 00004000h
					;GL_DEPTH_BUFFER_BIT	= 00000100h
call	glClear				;- API Function -
;------------------------------------------------------------------------------
; The glEnable and glDisable functions enable or disable OpenGL capabilities.
;------------------------------------------------------------------------------
push	0B50h				;GL_LIGHTING		= 00000B50h
call	glDisable			;- API Function -
;------------------------------------------------------------------------------
; The glBegin and glEnd functions delimit the vertices of a (group) primitive
;------------------------------------------------------------------------------
push	gl_Mode				;GL_QUADS		= 00000007h
call	glBegin				;- API Function -
;------------------------------------------------------------------------------
; API "glNormal3f" sets the current normal vector.
;------------------------------------------------------------------------------
push	normalArea1			;z, coordinates new current normal
push	0h				;y, coordinates new current normal
push	0h				;x, coordinates new current normal
call	glNormal3f			;- API Function -
mov	eax,OFFSET colorArea1
mov	pointercolor,eax
mov	eax,OFFSET vertexArea1
mov	pointervertex,eax
call	My_PaintArea			; - Subroutine -
;------------------------------------------------------------------------------
; API "glNormal3f" sets the current normal vector.
;------------------------------------------------------------------------------
push	normalArea2			;z, coordinates new current normal
push	0h				;y, coordinates new current normal
push	0h				;x, coordinates new current normal
call	glNormal3f			;- API Function -
mov	eax,OFFSET colorArea2
mov	pointercolor,eax
mov	eax,OFFSET vertexArea2
mov	pointervertex,eax
call	My_PaintArea			; - Subroutine -
;------------------------------------------------------------------------------
; API "glNormal3f" sets the current normal vector.
;------------------------------------------------------------------------------
push	0h				;z, coordinates new current normal
push	normalArea3			;y, coordinates new current normal
push	0h				;x, coordinates new current normal
call	glNormal3f			;- API Function -
mov	eax,OFFSET colorArea3
mov	pointercolor,eax
mov	eax,OFFSET vertexArea3
mov	pointervertex,eax
call	My_PaintArea			; - Subroutine -
;------------------------------------------------------------------------------
; API "glNormal3f" sets the current normal vector.
;------------------------------------------------------------------------------
push	0h				;z, coordinates new current normal
push	normalArea4			;y, coordinates new current normal
push	0h				;x, coordinates new current normal
call	glNormal3f			;- API Function -
mov	eax,OFFSET colorArea4
mov	pointercolor,eax
mov	eax,OFFSET vertexArea4
mov	pointervertex,eax
call	My_PaintArea			; - Subroutine -
;------------------------------------------------------------------------------
; API "glNormal3f" sets the current normal vector.
;------------------------------------------------------------------------------
push	0h				;z, coordinates new current normal
push	0h				;y, coordinates new current normal
push	normalArea5			;x, coordinates new current normal
call	glNormal3f			;- API Function -
mov	eax,OFFSET colorArea5
mov	pointercolor,eax
mov	eax,OFFSET vertexArea5
mov	pointervertex,eax
call	My_PaintArea			; - Subroutine -
;------------------------------------------------------------------------------
; API "glNormal3f" sets the current normal vector.
;------------------------------------------------------------------------------
push	0h				;z, coordinates new current normal
push	0h				;y, coordinates new current normal
push	normalArea6			;x, coordinates new current normal
call	glNormal3f			;- API Function -
mov	eax,OFFSET colorArea6
mov	pointercolor,eax
mov	eax,OFFSET vertexArea6
mov	pointervertex,eax
call	My_PaintArea			; - Subroutine -
;------------------------------------------------------------------------------
; The glEnd functions delimit the vertices of a primitive or a group of like.
;------------------------------------------------------------------------------
call	glEnd				;- API Function -
;------------------------------------------------------------------------------
; The glPopMatrix functions pop the current matrix stack.
;------------------------------------------------------------------------------
call	glPopMatrix			;- API Function -
;------------------------------------------------------------------------------
; The glFinish function blocks until all OpenGL execution is complete.
;------------------------------------------------------------------------------
call	glFinish			;- API Function -
;------------------------------------------------------------------------------
; The SwapBuffers function exchanges the front and back buffers if the current
; pixel format for the window referenced by the specified device context
; includes a back buffer. 
;------------------------------------------------------------------------------
push	hdc				;handle device context
call	SwapBuffers			;- API Function -
mov	WM_PAINT_proceed,1h		;set flag, WM_PAINT is proceed ! 
jmp	WP1_return			;

;==============================================================================
; WM_COMMAND (value=111h) message recieved ?
; On WM_COMMAND message we check if it is posted by a menu control ( lParam=0 )
; or by a child window control ( lParam not 0 )!
;
;                 Low word of wParam    High word wParam    lParam
; Menu          - Menu ID               0                   0
; Child window  - Control ID            Notification code   Child Window Handle
;------------------------------------------------------------------------------
WP1_uMsg_111h:
cmp	eax,111h			;check if WM_COMMAND message recieved
jne	WP1_return			;if not goto label
WP1_wParam_01h:
;------------------------------------------------------------------------------
; Check extra message info menu control, "01"-"09" item in menu bar clicked
; The menu bar items ID here are also used to set the gl_Mode !!!
;------------------------------------------------------------------------------
mov	eax,WP1_wParam			;extra info about the message in ax
cmp	ax,9h				;
ja	WP1_wParam_20h			;
cwde					;convert word ax into doubleword eax
mov	gl_Mode,eax			;set gl_Mode
jmp	WP1_return			;

;------------------------------------------------------------------------------
; Check extra message info menu control, "20h" item ID in menu bar clicked
;------------------------------------------------------------------------------
WP1_wParam_20h:
cmp	ax,20h				;
jne	WP1_wParam_21h			;
cmp	speed,20h			;
jae	WP1_return			;
inc	speed				;
inc	speed				;
call	My_HighTimer			; - Subroutine -
jmp	WP1_return			;

;------------------------------------------------------------------------------
; Check extra message info menu control, "21h" item ID in menu bar clicked
;------------------------------------------------------------------------------
WP1_wParam_21h:
cmp	ax,21h				;
jne	WP1_wParam_22h			;
cmp	speed,04h			;
jbe	WP1_return			;
dec	speed				;
dec	speed				;
call	My_HighTimer			; - Subroutine -
jmp	WP1_return			;

;------------------------------------------------------------------------------
; Check extra message info menu control, "22h" item ID in menu bar clicked
;------------------------------------------------------------------------------
WP1_wParam_22h:
cmp	ax,22h				;
jne	WP1_wParam_23h			;
cmp	Scale_counter,0h		;
je	WP1_return			;
dec	Scale_counter			;
;------------------------------------------------------------------------------
; The glScalef function multiply the current matrix by a general scaling matrix
;------------------------------------------------------------------------------
push	scale_Out			;z, coordinates translation vector
push	scale_Out			;y, coordinates translation vector
push	scale_Out			;x, coordinates translation vector
call	glScalef			;- API Function -
jmp	WP1_return

;------------------------------------------------------------------------------
; Check extra message info menu control, "23h" item ID in menu bar clicked
;------------------------------------------------------------------------------
WP1_wParam_23h:
cmp	ax,23h				;
jne	WP1_return			;
cmp	Scale_counter,10h		;
je	WP1_return			;
inc	Scale_counter			;
;------------------------------------------------------------------------------
; The glScalef function multiply the current matrix by a general scaling matrix
;------------------------------------------------------------------------------
push	scale_In			;z, coordinates translation vector
push	scale_In			;y, coordinates translation vector
push	scale_In			;x, coordinates translation vector
call	glScalef			;- API Function -
jmp	WP1_return			;

;==============================================================================
; API "DefWindowProcA" calls the window procedure to provide default processing
; for any window messages that an application does not process.
; This function ensures that every message is processed.
; It is called with the same parameters received by the window procedure.
;------------------------------------------------------------------------------
WP1_return:
popad					;pop all register from stack
push	WP1_lParam			;extra info about the message
push	WP1_wParam			;extra info about the message
push	WP1_uMsg			;the message number
push	WP1_hWnd			;handle of window who receives message
call	DefWindowProcA			;- API Function -
ret					;return to lpfnWndProc (multitask os)
;==============================================================================
;##############################################################################

;##############################################################################
;==============================================================================
; THE timer procedure, you can only call 3 API's here, refer to Mmedia.hlp !
; First we POP all parameter given by Windows from the stack and store them in
; variables for future use.
; !!!!! Next we PUSH the TP1_CallBack return address back to the stack !!!!!
; !!! TP1_CallBack must be avaible on the stack in this TIMER procedure !!!
;------------------------------------------------------------------------------
TP1:
pop 	TP1_CallBack			;POP return address timer procedure
pop	TP1_uID				;identifier of the timer event
pop	TP1_uMsg			;reserved, do not use
pop	TP1_dwUser			;the dwUser parameter of timeSetEvent
pop	TP1_dw1				;reserved, do not use
pop	TP1_dw2				;reserved, do not use
push	TP1_CallBack			;PUSH return address timer procedure !

cmp	WM_PAINT_proceed,1h		;check if last WM_PAINT msg. is proceed
jne	TP1_return			;if not 1h do not post a message
mov	WM_PAINT_proceed,0h		;reset flag
;------------------------------------------------------------------------------
; API "PostMessageA" post (!) a message to the Window Procedure WP1 !
;------------------------------------------------------------------------------
push	0h				;lParam, second message parameter
push	0h				;wParam, first message parameter
push	0Fh				;uMsg, message to post ( WM_PAINT )
push	WP1_hWnd			;hwnd, handle of destination window
call	PostMessageA			;- API Function -

TP1_return:
ret					;
;==============================================================================
;##############################################################################


;******************************************************************************
;==============================================================================
; My own subroutine(s) for a compacter code resist here ...
;------------------------------------------------------------------------------
My_HighTimer:
;------------------------------------------------------------------------------
; The timeKillEvent function cancels a specified timer event.
;------------------------------------------------------------------------------
push	timerevent1			;uTimerID, ID of timer event to cancel
call	timeKillEvent			;- API Function -
;------------------------------------------------------------------------------
; The timeSetEvent function starts a specified timer event. It runs in its own
; thread. After activated, it calls the specified callback function.
;------------------------------------------------------------------------------
push	1h				;fuEvent, timer type, TIME_PERIODIC=1h
push	0h				;dwUser, user-supplied callback data 
push	OFFSET TP1			;lpTimeProc, address callback function
push	wPeriodMin			;uResolution, resolution in ms
push	speed				;uDelay, event delay in milliseconds
call	timeSetEvent			;- API Function -
ret					;

My_PaintArea:
;------------------------------------------------------------------------------
; API "glColor3f" set the current color. You can update the color at any time.
; API "glVertex3f" specify a vertex. Call ONLY inside of a glBegin/glEnd pair !
; Current color, normal and texture coordinates are associated with the vertex.
;------------------------------------------------------------------------------
mov	ebx,pointercolor		;pointer ARRAY, RGB color values
push	[ebx]				;B
push	[ebx+4]				;G
push	[ebx+8]				;R
call	glColor3f			;- API Function -
mov	ebx,pointervertex		;pointer ARRAY, x y z vertex coord.
push	[ebx]				;z
push	[ebx+4]				;y
push	[ebx+8]				;x
call	glVertex3f			;- API Function -

mov	ebx,pointercolor
push	[ebx+12]
push	[ebx+16]
push	[ebx+20]
call	glColor3f			;- API Function -
mov	ebx,pointervertex
push	[ebx+12]
push	[ebx+16]
push	[ebx+20]
call	glVertex3f			;- API Function -

mov	ebx,pointercolor
push	[ebx+24]
push	[ebx+28]
push	[ebx+32]
call	glColor3f			;- API Function -
mov	ebx,pointervertex
push	[ebx+24]
push	[ebx+28]
push	[ebx+32]
call	glVertex3f			;- API Function -

mov	ebx,pointercolor
push	[ebx+36]
push	[ebx+40]
push	[ebx+44]
call	glColor3f			;- API Function -
mov	ebx,pointervertex
push	[ebx+36]
push	[ebx+40]
push	[ebx+44]
call	glVertex3f			;- API Function -
ret					;go out of here
;==============================================================================
;******************************************************************************

;==============================================================================
; end Main	= end of our program code
;------------------------------------------------------------------------------
end Main				;end of our program code, entry point

;==============================================================================
; To create the exe file use this commands with your Microsoft Assembler/Linker
;------------------------------------------------------------------------------
; ml.exe /c /coff td__410.asm					;asm command
; rc.exe /v rsrc.rc						;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td__410.obj rsrc.obj		;link command
;==============================================================================