⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cube.asm

📁 OpenGL Cube, Asm x86. Simple sample, which describes how to use OpenGL GAPI in Assembler. Requires
💻 ASM
字号:
.486P
.Model Flat,StdCall   ; 32 bit memory model
Option Scoped         ; local labels are enabled, global labels inside
                      ; PROC should be defined with double colons (LABEL::)
Option CaseMap:None   ; case sensitive

Include windows.inc
Include user32.inc
Include kernel32.inc
Include gdi32.inc

Include glu32.inc
Include opengl32.inc

IncludeLib user32.lib
IncludeLib kernel32.lib
IncludeLib opengl32.lib
IncludeLib glu32.lib
IncludeLib gdi32.lib

glRed Macro
	invoke glColor3f, val1_0d, val0_0d, val0_0d;	
EndM

glGreen Macro
	invoke glColor3f, val0_0d, val1_0d, val0_0d;	
EndM

glBlue Macro
	invoke glColor3f, val0_0d, val0_0d, val1_0d;	
EndM

glWhite Macro
	invoke glColor3f, val1_0d, val1_0d, val1_0d;	
EndM

glBlack Macro
	invoke glColor3f, val0_0d, val0_0d, val0_0d;	
EndM

glGrey Macro
	invoke glColor3f, val0_5d, val0_5d, val0_5d;	
EndM

; PROTOS ================================================
WinMain Proto :DWORD,:DWORD,:DWORD,:DWORD
WndProc Proto :DWORD,:DWORD,:DWORD,:DWORD
EnableOpenGL Proto :DWORD
DisableOpenGL Proto :DWORD
ResizeGLScene Proto :DWORD,:DWORD

; Read-only data ========================================
.Const

	ClassName BYTE "OGLMainWinClass", 0
	AppName BYTE "Asm + OpenGL Application", 0

	val45_0q QWORD 45.0
	val1_0q QWORD 1.0
	val0_1q QWORD 0.1
	val100_0q QWORD 100.0
	val0_0d DWORD 0.0
	val0_5d DWORD 0.5
	val_8_0d DWORD -8.0
	val1_0d DWORD 1.0
	val_1_0d DWORD -1.0

; Initialized data ======================================
.Data
	widthwnd DWORD 640
	heightwnd DWORD 480
	angle REAL4 0.0
	delta_angle REAL4 0.1
	vangle REAL4 30.0

; Non-initialized data ==================================
.Data?
	hInstance HINSTANCE ?
	hDC HDC ?
	hRC HGLRC ?
	
.Code
Start:

  invoke GetModuleHandle, NULL
  mov hInstance, eax
  invoke WinMain, hInstance, NULL, NULL, SW_SHOWDEFAULT
  
  push 0h
  call ExitProcess

;========================================================
WinMain Proc hInst:HINSTANCE, hPrevInst:HINSTANCE, CmdLine:LPSTR, CmdShow:DWORD
;{
  LOCAL wc:WNDCLASSEX
  LOCAL msg:MSG
  LOCAL hwnd:HWND
  LOCAL bQuit:BOOL

  mov bQuit, FALSE

  mov wc.cbSize, sizeof WNDCLASSEX
  mov wc.style, CS_OWNDC
  mov wc.lpfnWndProc, offset WndProc
  mov wc.cbClsExtra, NULL
  mov wc.cbWndExtra, NULL
  push hInstance
  pop wc.hInstance

  invoke GetStockObject, BLACK_BRUSH
  mov wc.hbrBackground, eax

  mov wc.lpszMenuName, NULL
  mov wc.lpszClassName, offset ClassName

  invoke LoadIcon, NULL, IDI_WINLOGO
  mov wc.hIcon, eax
  mov wc.hIconSm, eax

  invoke LoadCursor,NULL,IDC_ARROW
  mov wc.hCursor,eax

  invoke RegisterClassEx, addr wc
  invoke CreateWindowEx,NULL, addr ClassName, addr AppName,\
                        WS_OVERLAPPEDWINDOW,\
                        CW_USEDEFAULT, CW_USEDEFAULT,\
                        widthwnd, heightwnd, NULL, NULL,\
                        hInst, NULL
  mov hwnd,eax

  invoke ShowWindow, eax, SW_SHOWNORMAL
  invoke UpdateWindow, hwnd
  invoke EnableOpenGL, hwnd

  .while !( bQuit )
  ;{
    invoke PeekMessage, addr msg, NULL, 0, 0, PM_REMOVE
    .if ( eax )
    ;{
      .if ( msg.message == WM_QUIT )
      ;{
        mov bQuit, TRUE
      ;}
      .else
      ;{
        invoke TranslateMessage, addr msg
        invoke DispatchMessage, addr msg
      ;}
      .endif
     ;}
     .else
     ;{
		invoke glClear, GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT ;Clear buffers
		invoke glLoadIdentity  		;//Reset The Current Modelview Matrix
		invoke glTranslatef, val0_0d, val0_0d, val_8_0d
		invoke glRotatef, DWORD PTR vangle, val1_0d, val0_0d, val0_0d
		invoke glRotatef, DWORD PTR angle, val0_0d, val1_0d, val0_0d
		
		invoke glBegin, GL_QUADS	; //Draw A Quad
		
		glRed
		invoke glVertex3f, val1_0d, val1_0d, val_1_0d;					// Top Right Of The Quad (Top)
		glGreen
		invoke glVertex3f, val_1_0d, val1_0d, val_1_0d;					// Top Left Of The Quad (Top)
		glBlue
		invoke glVertex3f, val_1_0d, val1_0d, val1_0d;					// Bottom Left Of The Quad (Top)
		glWhite
		invoke glVertex3f, val1_0d, val1_0d, val1_0d;					// Bottom Right Of The Quad (Top)

		glBlack
		invoke glVertex3f, val1_0d, val_1_0d, val1_0d;					// Top Right Of The Quad (Bottom)
		glGrey
		invoke glVertex3f, val_1_0d, val_1_0d, val1_0d;					// Top Left Of The Quad (Bottom)
		glRed
		invoke glVertex3f, val_1_0d, val_1_0d, val_1_0d;					// Bottom Left Of The Quad (Bottom)
		glGreen
		invoke glVertex3f, val1_0d, val_1_0d, val_1_0d;					// Bottom Right Of The Quad (Bottom)
		
		glBlue
		invoke glVertex3f, val1_0d, val1_0d, val1_0d  ;	// Top Right Of The Quad (Front)
		glWhite
		invoke glVertex3f, val_1_0d, val1_0d, val1_0d  ;	// Top Left Of The Quad (Front)
		glBlack
		invoke glVertex3f, val_1_0d, val_1_0d, val1_0d  ;	// Bottom Left Of The Quad (Front)
		glGrey
		invoke glVertex3f, val1_0d, val_1_0d, val1_0d  ;	// Bottom Right Of The Quad (Front)
		
		glRed
		invoke glVertex3f, val1_0d, val_1_0d, val_1_0d  ;					// Top Right Of The Quad (Back)
		glGreen
		invoke glVertex3f, val_1_0d, val_1_0d, val_1_0d  ;					// Top Left Of The Quad (Back)
		glBlue
		invoke glVertex3f, val_1_0d, val1_0d, val_1_0d  ;					// Bottom Left Of The Quad (Back)
		glWhite
		invoke glVertex3f, val1_0d, val1_0d, val_1_0d  ;					// Bottom Right Of The Quad (Back)
		
		glBlack
		invoke glVertex3f, val_1_0d, val1_0d, val1_0d  ;					// Top Right Of The Quad (Left)
		glGrey
		invoke glVertex3f, val_1_0d, val1_0d, val_1_0d  ;					// Top Left Of The Quad (Left)
		glRed		
		invoke glVertex3f, val_1_0d, val_1_0d, val_1_0d  ;					// Bottom Left Of The Quad (Left)
		glGreen
		invoke glVertex3f, val_1_0d, val_1_0d, val1_0d  ;					// Bottom Right Of The Quad (Left)
		
		glBlue
		invoke glVertex3f, val1_0d, val1_0d, val_1_0d  ;					// Top Right Of The Quad (Right)
		glWhite
		invoke glVertex3f, val1_0d, val1_0d, val1_0d  ;					// Top Left Of The Quad (Right)
		glBlack
		invoke glVertex3f, val1_0d, val_1_0d, val1_0d  ;					// Bottom Left Of The Quad (Right)
		glGrey
		invoke glVertex3f, val1_0d, val_1_0d, val_1_0d  ;					// Bottom Right Of The Quad (Right)
		
		invoke glEnd;
		
		invoke SwapBuffers,hDC
		
	    fld angle
    	fadd delta_angle
    	fstp angle

     ;}
     .endif
  ;}
  .endw

  invoke DisableOpenGL, hwnd
  
mov eax, msg.wParam
ret
;}
WinMain EndP

;========================================================
WndProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
;{
  LOCAL Rect:RECT

  .if ( uMsg == WM_DESTROY )
  ;{
  quit_label:
    invoke PostQuitMessage, NULL
    mov eax, 0
    jmp ret_label
  ;}
  .elseif ( uMsg == WM_CREATE )
  ;{

  ;}
  .elseif ( uMsg == WM_SIZE )
  ;{
  		mov   eax,lParam ;LOWORD lParam
        and   eax,0FFFFh ;LOWORD lParam
  	    mov   ebx,lParam ;HIWORD lParam 
        shr   ebx,16 ;HIWORD lParam
        and   ebx,0FFFFh ;HIWORD lParam
		invoke ResizeGLScene, eax, ebx
		mov eax, 0
		jmp ret_label
  ;}
  .elseif ( uMsg == WM_KEYDOWN )
  ;{
    .if ( wParam == VK_ESCAPE)
    ;{
      jmp quit_label
    ;}
    .endif
  ;}
  .else
  ;{
    invoke DefWindowProc, hWnd, uMsg, wParam, lParam
    jmp ret_label
  ;}
  .endif

  ret_label:
  ret
;}
WndProc EndP

;========================================================
EnableOpenGL Proc hWnd:HWND
;{
  LOCAL iFormat:dword
  LOCAL pfd:PIXELFORMATDESCRIPTOR

;  get the device context (DC)
  invoke GetDC, hWnd
  mov hDC, eax

;  set the pixel format for the DC
  lea edi, pfd
  xor eax, eax
  mov ecx, sizeof pfd / sizeof DWORD
  rep stosd                   ; zero pfd structure

  mov pfd.nSize, sizeof pfd
  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.iLayerType, PFD_MAIN_PLANE
  invoke ChoosePixelFormat, hDC, addr pfd
  mov iFormat, eax
  invoke SetPixelFormat, hDC, iFormat, addr pfd

;  create and enable the render context (RC)
  invoke wglCreateContext, hDC
  mov hRC, eax
  invoke wglMakeCurrent, hDC, hRC 
  invoke ResizeGLScene, widthwnd, heightwnd 
;;;;;;;;;;Init OpenGL
	invoke glShadeModel,GL_SMOOTH ;							// Enable Smooth Shading
	invoke glClearColor,DWORD PTR val0_0d,DWORD PTR val0_0d,DWORD PTR val0_0d, DWORD PTR val0_5d;				// Black Background
	invoke glClearDepth,DWORD PTR val1_0q[0],DWORD PTR val1_0q[4];									// Depth Buffer Setup
	invoke glEnable,GL_DEPTH_TEST;							// Enables Depth Testing
	invoke glDepthFunc,GL_LEQUAL;								// The Type Of Depth Testing To Do
	invoke glHint,GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ;
;;;;;;;;;;/Init OpenGL  
  ret
;}
EnableOpenGL EndP

;========================================================
DisableOpenGL Proc hWnd:HWND
;{
  invoke wglMakeCurrent, NULL, NULL
  invoke wglDeleteContext, hRC
  invoke ReleaseDC, hWnd, hDC
  ret
;}
DisableOpenGL EndP

;========================================================
ResizeGLScene Proc nwidth:DWORD,nheight:DWORD
;{
	LOCAL ratio:REAL8
  	invoke glViewport,0,0,nwidth,nheight;						// Reset The Current Viewport

	invoke glMatrixMode,GL_PROJECTION ;						// Select The Projection Matrix
	invoke glLoadIdentity  ;									// Reset The Projection Matrix

	;// Calculate The Aspect Ratio Of The Window
    fild nwidth
    fild nheight
    fdivp st(1), st(0)
    fstp ratio

	invoke gluPerspective,DWORD PTR val45_0q,DWORD PTR val45_0q[4],\
	DWORD PTR ratio,DWORD PTR ratio[4],DWORD PTR val0_1q,DWORD PTR val0_1q[4],\
	DWORD PTR val100_0q,DWORD PTR val100_0q[4];

	invoke glMatrixMode,GL_MODELVIEW ;							// Select The Modelview Matrix
	invoke glLoadIdentity  ; 
	ret
;}
ResizeGLScene EndP
End Start

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -