Lecteur 3ds en assembleur sous opengl

Contenu du snippet

bonjour,

Voici un bout de code permetant de lire les fichier 3ds et de les incorporer dans vos sources.

Il n'est qu'en BETA, je doit y ajouter qq modifications.

Source / Exemple :


OBJ		STRUCT
	Nb_Poylgon	WORD ?		;nombre de polygon
	textureId	DWORD ?		;Texture ID
	Pt_Vertex	DWORD ?		;pointeur vers zone memoire Vertex 
	Pt_Polygon	DWORD ?		;pointeur vers zone memoire polygon
	Pt_Map_Cord	DWORD ?		;pointeur vers zone memoire des coordonées MAP
OBJ ends

.data
	string_file_not_found	DB "fichier introuvable",0
	Temp_String		DB	255 dup(0)
	N_Texture		DD	0							; numero de texture en cours
	Max_Texture		DD	25							; nombre de texture max
	N_Objets		BYTE 	0
.data?
	End_File	DD ?
	count		dD ?
	bytes_read	dd ?
	Temp_DD		DD ?
	Objets		OBJ <>
.code

File_to_RAM	PROC File_name:DWORD
	push	edi
	push	0
	push	0
	push	OPEN_EXISTING
	push	0
	push	FILE_SHARE_READ
	push	GENERIC_READ
	push	File_name
	call	CreateFile
	test	eax,eax
	jne	@f
		push	MB_ICONERROR
		push	offset string_file_not_found
		push	File_name
		push	0
		call	MessageBox
		xor	eax, eax
		ret		
	@@:
	mov	ebx, eax
	push	ebx
	push	0
	push	ebx
	call	GetFileSize 
	push	0
	push	offset bytes_read
	push	eax
	inc	eax
	push	eax
	push	GPTR
	call	GlobalAlloc	
	mov	edi,eax
	push	eax
	push	ebx
	call	ReadFile
	call	CloseHandle
	mov	eax,edi
	pop	edi
	ret
File_to_RAM endp

Load_3d		proc file:dword
	push	ebp
	push	edi
	push	esi
	invoke	File_to_RAM,file
	test	eax,eax
	jne	@f
		pop	esi
		pop	edi
		pop	ebp
		ret		
	@@:
	mov	ebp,eax
	push	eax
	mov	End_File,eax
	xor	eax, eax
	; lecture du 1er chunk
	movzx	edi,WORD  PTR [ebp]	
	mov	eax,DWORD PTR [ebp+2]
	add	End_File,eax
	add	ebp,6
	test	di,4D4Dh
	jne short @f
		xor	eax, eax	; le fichier n'est pas un fichier 3ds
		pop	esi
		pop	edi
		pop	ebp
		ret
	@@:
	;lecture du fichier
	xor	ebx,ebx
read_chunk:	
	;lecture d'un chunk
	movzx	edi,WORD PTR [ebp]
	movzx	esi,WORD PTR [ebp+2]
	add	ebp,6
	cmp	di,03d3dH;CHUNK_OBJMESH
	je 	short read_chunk
	cmp	di,0A200H;CHUNK_TEXTURE
	je 	short read_chunk
	cmp	di,0AFFFH;CHUNK_MATERIAL
	je 	short read_chunk
	cmp	di,4100H;CHUNK_TRIMESH
	je	short read_chunk		
	cmp	di,4130H;CHUNK_FACEMAT
	je	short read_chunk
	
	cmp	di,4000H;CHUNK_OBJBLOCK
	je 	short CHUNK_OBJBLOCK
	cmp	di,4110H;CHUNK_VERTLIST
	je 	short CHUNK_VERTLIST
	cmp	di,4120H;CHUNK_FACELIST ;
	je	CHUNK_FACELIST
	cmp	di,4140H;CHUNK_MAPLIST
	je	CHUNK_MAPLIST
	cmp	di,0A000H;CHUNK_MATNAME
	je	CHUNK_MATNAME
	cmp	di,0A300H;CHUNK_MAPFILE
	je	CHUNK_MAPFILE	
	; UNKNOW CHUNK
	sub	esi,6
	add	ebp,esi
	cmp	ebp,End_File
	jl	read_chunk
	
	pop	ebx
	push	ebx
	call	GlobalFree
	pop	esi
	pop	edi
	pop	ebp
	ret		
				
CHUNK_OBJBLOCK:
	inc 	N_Objets
	mov	eax, offset Temp_String
	@@:
	mov	bl,BYTE PTR [ebp]
	mov	[eax],bl	
	test	bl,bl
	je	@f
	inc	eax
	inc	ebp
	jmp	short @b
	@@: 
	mov	BYTE PTR [eax],0
	inc	ebp
	;call newObject
	jmp 	read_chunk

CHUNK_VERTLIST:
	push	esi
	push	edi
	mov	esi,ebp
	movzx	eax,WORD PTR[esi] ; nb vertices
	mov	ebx,12		; 3 points par vertices
	mul	ebx	
	push	eax
	push	eax
	push	GPTR
	call	GlobalAlloc
	mov	Objets.Pt_Vertex, eax
	add	esi,2
	mov	edi, Objets.Pt_Vertex
	pop	ecx
	rep	movsb
	pop	edi
	pop	esi
	sub	esi,6
	add	ebp,esi
	jmp 	read_chunk
	
CHUNK_FACELIST:
	push	esi
	push	edi
	mov	esi,ebp
	movzx	eax,WORD PTR[esi]	; nb Polygon
	mov	Objets.Nb_Poylgon,ax	
	mov	ebx,8		        ; 4 points par Polygon (3 + flag)
	mul	ebx	
	push	eax
	push	eax
	push	GPTR
	call	GlobalAlloc
	mov	Objets.Pt_Polygon, eax
	add	esi,2
	mov	edi,Objets.Pt_Polygon
	pop	ecx
	rep	movsb
	pop	edi
	pop	esi
	sub	esi,6
	add	ebp,esi
	jmp 	read_chunk
		
CHUNK_MAPLIST:	
	push	esi
	push	edi
	mov	esi,ebp
	movzx	eax,WORD PTR[esi] ; nb Map coord
	mov	ebx,8		  ; 2 points par map
	mul	ebx	
	push	eax
	push	eax
	push	GPTR
	call	GlobalAlloc
	mov	Objets.Pt_Map_Cord, eax
	add	esi,2
	mov	edi,Objets.Pt_Map_Cord
	pop	ecx
	rep	movsb
	pop	edi
	pop	esi
	sub	esi,6
	add	ebp,esi
	jmp 	read_chunk
	
CHUNK_MAPFILE:
	mov	eax, offset Temp_String
	@@:
	mov	bl,BYTE PTR [ebp]
	mov	[eax],bl	
	test	bl,bl
	je	@f
	inc	eax
	inc	ebp
	jmp	short @b
	@@: 
	mov	BYTE PTR [eax],0
	inc	ebp
	push 	offset Temp_String
	call	LoadGLTexture
	mov 	Objets.textureId,eax
	jmp 	read_chunk
	
CHUNK_MATNAME:
	mov	eax, offset Temp_String
	@@:
	mov	bl,BYTE PTR [ebp]
	mov	[eax],bl	
	test	bl,bl
	je	@f
	inc	eax
	inc	ebp
	jmp	short @b
	@@: 
	mov	BYTE PTR [eax],0
	inc	ebp
	;call newMaterial	
	jmp 	read_chunk

Load_3d endp

LoadGLTexture PROC file_texture:DWORD
	LOCAL	info:		BITMAP
	
	push	edi
	cmp 	N_Texture,0
	jne 	@f
		push	offset texture
		push	Max_Texture
		call	glGenTextures
	@@:
	push	LR_LOADFROMFILE or LR_CREATEDIBSECTION
	push	0
	push	0
	push	IMAGE_BITMAP
	push	file_texture
	push	0
	call	LoadImage
	test	eax,eax
	jne	@f
		push	MB_ICONERROR
		push	offset string_file_not_found
		push	file_texture
		push	0
		call	MessageBox
		pop	edi
		ret
	@@:
	MOV	Temp_DD,eax
	lea	edi,Temp_DD
	MOV	ebx,N_Texture
	push	texture[ebx]
	push	GL_TEXTURE_2D
	call	glBindTexture
	push	GL_REPEAT
	push	GL_TEXTURE_WRAP_S
	push	GL_TEXTURE_2D
	call	glTexParameteri
	push	GL_REPEAT
	push	GL_TEXTURE_WRAP_T
	push	GL_TEXTURE_2D
	call	glTexParameteri
	push	GL_LINEAR
	push	GL_TEXTURE_MAG_FILTER
	push	GL_TEXTURE_2D
	call	glTexParameteri
	push	GL_LINEAR_MIPMAP_NEAREST
	push	GL_TEXTURE_MIN_FILTER
	push	GL_TEXTURE_2D
	call	glTexParameteri
	invoke	GetObject,[edi],sizeof info,addr info
	push	GL_REPLACE
	push	GL_TEXTURE_ENV_MODE
	push	GL_TEXTURE_ENV
	call	glTexEnvi
	push	info.bmBits
	push	GL_UNSIGNED_BYTE
	push	GL_BGR_EXT
	push	0
	push	info.bmHeight
	push	info.bmWidth
	push	4
	push	0
	push	GL_TEXTURE_2D 
	call	glTexImage2D
	push	info.bmBits
	push	GL_UNSIGNED_BYTE
	push	GL_BGR_EXT
	push	info.bmHeight
	push	info.bmWidth
	push	4
	push	GL_TEXTURE_2D
	call	gluBuild2DMipmaps
	push	Temp_DD
	call	DeleteObject
	pop	edi
	MOV	eax,N_Texture
	inc	N_Texture
	ret
LoadGLTexture endp

draw_3d proc
	invoke	glEnable,GL_CULL_FACE
	invoke	glEnable,GL_TEXTURE_2D
	mov	ebx,Objets.textureId
	invoke	glBindTexture,GL_TEXTURE_2D,texture[ebx]
	invoke	glBegin,GL_TRIANGLES

	push	edi	
	push	esi
	push	ebp
	xor	eax,eax
	mov	count,eax
	dec	Objets.Nb_Poylgon
	
	.while (ax < Objets.Nb_Poylgon)	
	; 1 vertex

		shl	eax,3			; 1 poly = 4 pts = 8 octets
		add	eax,Objets.Pt_Polygon
		mov	esi,eax
		
		;1er vertice		
		movzx	eax,WORD PTR[eax]
		mov	ebx,eax
		shl	eax,3		; 2 points par map
		mov	edi,eax
		add	edi,Objets.Pt_Map_Cord
		push	edi
		call	glTexCoord2fv
		mov	eax,ebx
		mov	ebx,12		; 3 points par vertice
		mul	ebx	
		mov	edi,eax
		add	edi,Objets.Pt_Vertex
		push	edi
		call	glVertex3fv
		
		;2nd vertice
		mov	eax,esi
		add	eax,2		
		movzx	eax,WORD PTR[eax]		
		mov	ebx,eax
		shl	eax,3		; 2 points par map
		mov	edi,eax
		add	edi,Objets.Pt_Map_Cord
		push	edi
		call	glTexCoord2fv
		mov	eax,ebx
		mov	ebx,12		; 3 points par vertice
		mul	ebx	
		mov	edi,eax
		add	edi,Objets.Pt_Vertex
		push	edi
		call	glVertex3fv
			
		;3eme vertice
		mov	eax,esi
		add	eax,4
		movzx	eax,WORD PTR[eax]
		mov	ebx,eax
		shl	eax,3		; 2 points par map
		mov	edi,eax
		add	edi,Objets.Pt_Map_Cord
		push	edi
		call	glTexCoord2fv
		mov	eax,ebx
		mov	ebx,12		; 3 points par verice
		mul	ebx
		mov	edi,eax
		add	edi,Objets.Pt_Vertex
		push	edi
		call	glVertex3fv
				
		inc	count
		mov	eax, count
	.endw
	call	glEnd
	push	GL_TEXTURE_2D
	call	glDisable
	push	GL_CULL_FACE	
	call	glDisable			; desactive les faces cachés	
	pop	ebp
	pop	esi
	pop	edi	
	ret
draw_3d endp

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.