format ELF
include "/lib/symbols.inc"

section "codice" executable

public _start
_start:


extrn glViewport
extrn glMatrixMode
extrn glLoadIdentity
extrn glClear
extrn glClearColor
extrn glBegin
extrn glColor3f
extrn glVertex3f
extrn glEnd
extrn glutSwapBuffers
extrn glutInit
extrn glutInitDisplayMode
extrn glutInitWindowSize
extrn glutInitWindowPosition
extrn glutCreateWindow
extrn glutDisplayFunc
extrn glutReshapeFunc
extrn glutKeyboardFunc
extrn glutMainLoop


	push 0
	push 0
	call glutInit
	add esp,8
	
	push 0+2;GLUT_RGB+GLUT_DOUBLE
	call glutInitDisplayMode
	add esp,4
	
	push 480
	push 640
	call glutInitWindowSize
	add esp,8
	
	push 0
	push 0
	call glutInitWindowPosition
	add esp,8
	
	push msg
	call glutCreateWindow
	add esp,4
	
	push disegna
	call glutDisplayFunc
	add esp,4
	
	push resize
	call glutReshapeFunc
	add esp,4
	
	push keyboard
	call glutKeyboardFunc
	add esp,4
	
	call glutMainLoop
_exit:
	mov eax,sys_exit
	xor ebx,ebx
	int 80h	
	
	int3
	
resize: ;resize(larg,alt)
	push ebp
	mov ebp,esp
	
	push dword [ebp+8]
	push dword [ebp+0Ch]
	push 0
	push 0
	call glViewport ;setta il viewport per usare tutta la finestra
	add esp,10h
	
	push 1701h;GL_PROJECTION
	call glMatrixMode
	add esp,4
	
	call glLoadIdentity
	
	push 1700h;GL_MODELVIEW
	call glMatrixMode
	add esp,4
	
	call glLoadIdentity
	
	mov esp,ebp
	pop ebp
	ret 8
	
keyboard: ;(tasto,x,y)
	mov eax,dword [ebp+8]
	cmp al,'q'
	je _exit
	ret
	
disegna:
	push 4000h;GL_COLOR_BUFFER_BIT
	call glClear
	add esp,4
	
	push 0.0f
	push 0.0f
	push 0.0f
	push 0.0f
	call glClearColor
	add esp,10h
	
	;________________________________________
	push 4;GL_TRIANGLES
 	call glBegin
 	add esp,4
 	
 	push 0.0f
 	push 0.0f
 	push 1.0f
 	call glColor3f
 	add esp,0Ch
 	
 	push 0.0f
 	push 0.5f
 	push 0.0f
 	call glVertex3f
 	add esp,0Ch
 	
 	push 0.0f
 	push 1.0f
 	push 0.0f
 	call glColor3f
 	add esp,0Ch
 	
 	push 0.0f
 	push -0.5f
 	push -0.5f
 	call glVertex3f
 	add esp,0Ch
 	
 	push 1.0f
 	push 0.0f
 	push 0.0f
 	call glColor3f
 	add esp,0Ch
 	
 	push 0.0f
 	push -0.5f
 	push 0.5f
 	call glVertex3f
 	add esp,0Ch
 	
	call glEnd;_______________________________
 
	call glutSwapBuffers
	ret
;###########################################################################
section "dati" writeable;###################################################
;###########################################################################
msg db "Prova: Assembly + OpenGL (GLUT)",0