程式語言 - JWasm - OpenGL 1.0 - Interpolate Color



參考資訊:
https://www.masm32.com/
https://github.com/gamedev-net/nehe-opengl

main.S

    .386
    .model flat,stdcall
    option casemap : none
  
include /opt/wadk/masm32/include/windows.inc
include /opt/wadk/jwasm/include/ogl/kernel32.inc
include /opt/wadk/jwasm/include/ogl/user32.inc
include /opt/wadk/jwasm/include/ogl/gdi32.inc
include /opt/wadk/jwasm/include/ogl/gl.def
include /opt/wadk/jwasm/include/ogl/glu.def

    .data
szCaption   db "main", 0
hWin        dd 0
hInstance   dd 0
CommandLine dd 0
defWndProc  dd 0
hDC         HDC 0
hRC         HGLRC 0
nearPlane   real8 0.1
farPlane    real8 100.0
angle       real8 45.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 pfd : PIXELFORMATDESCRIPTOR
    local top : real8
    local right : real8
    local mtop : real8
    local mright : real8
  
    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
 
    fld angle
    fldpi
    fmulp st(1), st(0)
 
    mov eax, 360
    push eax
    fild dword ptr [esp]
    add esp, 4
 
    fdivp st(1), st(0)
 
    fptan
    fstp st(0)
 
    fmul nearPlane
    fstp top
 
    fld top
 
    push w
    fild dword ptr [esp]
    add esp, 4
 
    push h
    fild dword ptr [esp]
    add esp, 4
 
    fdivp st(1), st(0)
    fmulp st(1), st(0)
    fstp right
 
    fld top
    fchs
    fstp mtop
 
    fld right
    fchs
    fstp mright
 
    invoke glViewport, 0, 0, w, h
    invoke glMatrixMode, GL_PROJECTION
    invoke glFrustum, \
        dword ptr [mright + 0], \
        dword ptr [mright + 4], \
        dword ptr [right + 0], \
        dword ptr [right + 4], \
        dword ptr [mtop + 0], \
        dword ptr [mtop + 4], \
        dword ptr [top + 0], \
        dword ptr [top + 4], \
        dword ptr [nearPlane + 0], \
        dword ptr [nearPlane + 4], \
        dword ptr [farPlane + 0], \
        dword ptr [farPlane + 4]
 
    invoke glMatrixMode, GL_MODELVIEW
    invoke glLoadIdentity
 
@@:
    invoke GetMessage, addr msg, NULL, 0, 0
    cmp eax, 0
    je @f
    invoke DispatchMessage, addr msg
 
    invoke glLoadIdentity
    invoke glTranslatef, fpc(-1.0), fpc(0.0), fpc(-5.0)
    invoke glBegin, GL_TRIANGLES
        invoke glColor3f, fpc(1.0), fpc(0.0), fpc(0.0)
        invoke glVertex3f, fpc(0.0), fpc(0.5), fpc(0.0)
   
        invoke glColor3f, fpc(0.0), fpc(1.0), fpc(0.0)
        invoke glVertex3f, fpc(-0.5), fpc(-0.5), fpc(0.0)
   
        invoke glColor3f, fpc(0.0), fpc(0.0), fpc(1.0)
        invoke glVertex3f, fpc(0.5), fpc(-0.5), fpc(0.0)
    invoke glEnd
   
    invoke glLoadIdentity
    invoke glTranslatef, fpc(1.0), fpc(0.0), fpc(-5.0)
    invoke glBegin, GL_QUADS
        invoke glColor3f, fpc(1.0), fpc(0.0), fpc(0.0)
        invoke glVertex3f, fpc(1.0), fpc(1.0), fpc(0.0)
   
        invoke glColor3f, fpc(0.0), fpc(1.0), fpc(0.0)
        invoke glVertex3f, fpc(-1.0), fpc(1.0), fpc(0.0)
   
        invoke glColor3f, fpc(0.0), fpc(0.0), fpc(1.0)
        invoke glVertex3f, fpc(-1.0), fpc(-1.0), fpc(0.0)
   
        invoke glColor3f, fpc(1.0), fpc(1.0), fpc(0.0)
        invoke glVertex3f, fpc(1.0), fpc(-1.0), fpc(0.0)
    invoke glEnd
 
    invoke SwapBuffers, hDC
    jmp @b

@@:
    mov eax, msg.wParam
    ret
WinMain endp
  
start:
    invoke GetModuleHandle, NULL
    mov hInstance, eax
    
    invoke GetCommandLine
    mov CommandLine, eax
    
    invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
    invoke ExitProcess, 0
end start

完成