程式語言 - MASM32 - OpenGL 1.0 - Draw Triangle



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gamedev-net/nehe-opengl
https://github.com/gammasoft71/Examples_Win32

main.asm

    .386
    .model flat,stdcall
    option casemap:none
   
include c:\masm32\include\windows.inc
include c:\masm32\include\ogl\kernel32.inc
include c:\masm32\include\ogl\user32.inc
include c:\masm32\include\ogl\gdi32.inc
include c:\masm32\include\ogl\gl.def
include c:\masm32\include\ogl\glu.def
include c:\masm32\include\ogl\winextra\winextra.def
   
includelib c:\masm32\lib\gdi32.lib
includelib c:\masm32\lib\glu32.lib
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\opengl32.lib
includelib c:\masm32\lib\kernel32.lib
   
    .data
szCaption   BYTE "main",0
hWin        DWORD 0
hInstance   DWORD 0
pCmdLine    DWORD 0
defWndProc  DWORD 0
hDC         HDC 0
hRC         HGLRC 0
   
    .code
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    .if uMsg == WM_CLOSE
        invoke DestroyWindow, hWnd
        xor eax, eax
        ret
    .elseif uMsg == WM_DESTROY
        invoke PostQuitMessage, 0
        xor eax, eax
        ret
    .endif
    
    invoke CallWindowProc, defWndProc, hWnd, uMsg, wParam, lParam
    ret
WndProc endp
     
WinMain proc hInst:DWORD, hPrevInst:DWORD, CmdLine:DWORD, CmdShow:DWORD
    local w:DWORD
    local h:DWORD
    local msg:MSG
    local fmt:DWORD
    local rc:RECT
    local pfd:PIXELFORMATDESCRIPTOR
      
    mov w, 320
    mov h, 240
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, offset szCaption, WS_OVERLAPPEDWINDOW or WS_VISIBLE, 0, 0, w, h, NULL, NULL, NULL, NULL
    mov hWin, eax
     
    invoke SetWindowLong, hWin, GWL_WNDPROC, WndProc
    mov defWndProc, eax
   
    invoke RtlZeroMemory, addr pfd, sizeof(PIXELFORMATDESCRIPTOR)
    mov pfd.nSize, sizeof PIXELFORMATDESCRIPTOR
    mov pfd.nVersion, 1
    mov pfd.dwFlags, PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER
    mov pfd.iPixelType, PFD_TYPE_RGBA
    mov pfd.cColorBits, 16
    mov pfd.cDepthBits, 16
    mov pfd.dwLayerMask, PFD_MAIN_PLANE
   
    invoke GetDC, hWin
    mov hDC, eax
    invoke ChoosePixelFormat, hDC, addr pfd
    mov fmt, eax
    invoke SetPixelFormat, hDC, fmt, addr pfd
    invoke wglCreateContext, hDC
    mov hRC, eax
    invoke wglMakeCurrent, hDC, hRC

    invoke RtlZeroMemory, addr rc, sizeof RECT
    invoke GetClientRect, hWin, addr rc
    mov eax, rc.right
    sub eax, rc.left
    mov ecx, rc.bottom
    sub ecx, rc.top
    invoke glViewport, 0, 0, eax, ecx
   
@@:
    invoke GetMessage, addr msg, NULL, 0, 0
    cmp eax, 0
    je @f
    invoke DispatchMessage, addr msg
   
    invoke glBegin, GL_TRIANGLES
        invoke glVertex3f, f4(0.0), f4(0.5), f4(0.0)
        invoke glVertex3f, f4(-0.5), f4(-0.5), f4(0.0)
        invoke glVertex3f, f4(0.5), f4(-0.5), f4(0.0)
    invoke glEnd
  
    invoke SwapBuffers, hDC
    jmp @b

@@:
    invoke wglMakeCurrent, NULL, NULL
    invoke wglDeleteContext, hRC
    invoke ReleaseDC, hWin, hDC
  
    mov eax, msg.wParam
    ret
WinMain endp
    
start:
    invoke GetModuleHandle, NULL
    mov hInstance, eax
      
    invoke GetCommandLine
    mov pCmdLine, eax
      
    invoke WinMain, hInstance, NULL, pCmdLine, SW_SHOWDEFAULT
    invoke ExitProcess, 0
end start

完成