News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Lightwave OBJ Parser

Started by Farabi, November 09, 2011, 08:13:02 AM

Previous topic - Next topic

Farabi

Obviously, if I keep it on my harddisk, no benefit I'll gain.

EDITED:
Miss calculated the stack
Wrong copy at each of end of the indices


;Created By Onan Farabi. At least You had most of the valuable data even without the material detail
;

fOBJ struct
lptext dd 0
nVertex dd 0
lpvertex3f dd 0
nNormal dd 0
lpnormal3f dd 0
nTextC dd 0
lpTextC dd 0
nGroup dd 0
lpgroup dd 0
fOBJ ends

.code

Word_Count proc uses esi edi lptz:dword,nSize:dword
LOCAL stts,sizech,p:dword

mov esi,lptz
mov ecx,nSize

xor eax,eax
mov stts,eax
mov edx,eax

cmp nSize,0
jnz @f
xor eax,eax
ret
@@:

loop_each_word:
mov al,[esi]
cmp al,040h ; 39 is '1'
ja not_number
cmp al,021h
jl not_number
; mov eax,stts
; cmp eax,1
; jz not_number
mov sizech,esi
mov stts,1
jmp not_ilegal_char
not_number:

mov al,[esi]
cmp al,05fh ; 059h is 'Z'
ja not_char_up
cmp al,041h
jl not_char_up
; mov eax,stts
; cmp eax,1
; jz not_char_up
mov sizech,esi
mov stts,1
jmp not_ilegal_char
not_char_up:

mov al,[esi]
cmp al,07ah ; 07ah is 'z'
ja not_char_low
cmp al,061h
jl not_char_low
; cmp stts,1
; jz not_char_low
mov sizech,esi
mov stts,1
jmp not_ilegal_char
not_char_low:

mov eax,stts
cmp eax,1
jnz not_ilegal_char
inc edx
dec eax
mov stts,eax
sub sizech,esi
xchg edx,sizech
sub edx,esi
xchg edx,sizech

not_ilegal_char:
inc esi
dec ecx
jnz loop_each_word

cmp stts,1
jnz @f
inc edx
@@:

mov eax,edx

ret
Word_Count endp

Word_position proc uses esi edi nPos:dword,lptz:dword,nSize:dword
LOCAL stts,sizech,p,ttc:dword

mov esi,lptz
mov ecx,nSize

xor eax,eax
mov stts,eax
mov sizech,eax
mov edx,eax

cmp nSize,0
jnz @f
xor eax,eax
ret
@@:

loop_each_word:
mov al,[esi]

cmp al,'a'
jnae not_low
cmp al,'z'
jnle not_low
cmp stts,1
jz point_jmp
mov stts,1
mov sizech,esi
jmp point_jmp
not_low:

cmp al,'A'
jnae not_high
cmp al,060h
jnle not_high
cmp stts,1
jz point_jmp
mov stts,1
mov sizech,esi
jmp point_jmp
not_high:

cmp al,021h
jnae not_number
cmp al,040h
jnle not_number
cmp stts,1
jz point_jmp
mov stts,1
mov sizech,esi
jmp point_jmp
not_number:

cmp stts,1
jnz point_jmp
inc edx
mov stts,0
cmp edx,nPos
jz done
point_jmp:
inc esi
dec ecx
jnz loop_each_word

; On the latest version the last word is not detected.
cmp stts,1
jnz failed
inc edx
mov stts,0
cmp edx,nPos
jz done
failed:
xor eax,eax
ret

done:
mov edx,sizech ; address
mov eax,sizech ; size of the word
sub esi,eax
mov eax,esi ;

ret
Word_position endp

wReplace proc uses esi edi lptxt:dword,charsub:byte

mov esi,lptxt
xor ecx,ecx
loop_replace:
push ecx
mov al,[esi+ecx]
.if al==charsub
mov byte ptr[esi+ecx],0
.endif
pop ecx
inc ecx
cmp al,0
jnz loop_replace

ret
wReplace endp

fLoadOBJ proc uses esi edi lpName:dword
LOCAL pmem:dword
LOCAL fsz:dword
LOCAL tzrow:dword
LOCAL tblrow:dword
LOCAL cline:dword
LOCAL buff[256]:dword
LOCAL fobj,vc,pface,offb,facec:dword
LOCAL q:qword

invoke filesize,lpName
.if eax!=-1
add eax,4
mov fsz,eax
invoke Alloc,fsz
mov pmem,eax
invoke read_disk_file,lpName,addr pmem,addr fsz
.else
xor eax,eax
dec eax
ret
.endif

invoke Alloc,sizeof fOBJ
mov fobj,eax

invoke szWcnt,pmem,CADD(0dh,0ah)
mov tzrow,eax

mov ecx,tzrow
shl ecx,2
invoke Alloc,ecx
mov tblrow,eax

mov ecx,fsz
mov esi,pmem
loop_replace:
push ecx
mov al,[esi+ecx]
.if al==0Dh
mov byte ptr[esi+ecx],0
.elseif al==0Ah
mov byte ptr[esi+ecx],0
.endif
pop ecx
dec ecx
jnz loop_replace


mov ecx,tzrow
mov esi,pmem
mov edi,tblrow
mov cline,1
mov [edi],esi
loop_create_row_hash:
push ecx
invoke lstrlen,esi
add esi,eax
add esi,2
;inc esi

mov ecx,cline
mov [edi+ecx*4],esi

inc cline
pop ecx
dec ecx
jnz loop_create_row_hash

;Count Vertex
xor ecx,ecx
mov vc,ecx
loop_cvertex:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("v ")
.if eax!=0
inc vc
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_cvertex

mov ecx,vc
shl ecx,4
invoke Alloc,ecx
mov ecx,fobj
mov [ecx].fOBJ.lpvertex3f,eax
push vc
pop [ecx].fOBJ.nVertex

; Load all Vertex
xor ecx,ecx
mov vc,ecx
loop_vertex:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("v ")
.if eax!=0
mov ecx,[esp]
mov eax,[edi+ecx*4]
inc ecx
mov edx,[edi+ecx*4]
dec ecx
sub edx,eax
mov fsz,edx

invoke Word_position,2,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpvertex3f
mov ecx,vc
fstp dword ptr[eax+ecx]
.endif

mov ecx,[esp]
invoke Word_position,3,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpvertex3f
mov ecx,vc
fstp dword ptr[eax+ecx+4]
.endif

mov ecx,[esp]
invoke Word_position,4,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpvertex3f
mov ecx,vc
fstp dword ptr[eax+ecx+8]
.endif

add vc,12
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_vertex
;--------------------------------------------------------------------------------

;Count Vertex
xor ecx,ecx
mov vc,ecx
loop_cnormal:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("vn ")
.if eax!=0
inc vc
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_cnormal

mov ecx,vc
shl ecx,4
invoke Alloc,ecx
mov ecx,fobj
mov [ecx].fOBJ.lpnormal3f,eax
push vc
pop [ecx].fOBJ.nNormal

xor ecx,ecx
mov vc,ecx
loop_normal:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("vn ")
.if eax!=0
mov ecx,[esp]
mov eax,[edi+ecx*4]
inc ecx
mov edx,[edi+ecx*4]
dec ecx
sub edx,eax
mov fsz,edx

invoke Word_position,2,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpnormal3f
mov ecx,vc
fstp dword ptr[eax+ecx]
.endif

mov ecx,[esp]
invoke Word_position,3,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpnormal3f
mov ecx,vc
fstp dword ptr[eax+ecx+4]
.endif

mov ecx,[esp]
invoke Word_position,4,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpnormal3f
mov ecx,vc
fstp dword ptr[eax+ecx+8]
.endif

add vc,12
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_normal

;--------------------------------------------------------------------------------

;Count Vertex
xor ecx,ecx
mov vc,ecx
loop_ctextc:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("vt ")
.if eax!=0
inc vc
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_ctextc

mov ecx,vc
shl ecx,4
invoke Alloc,ecx
mov ecx,fobj
mov [ecx].fOBJ.lpTextC,eax
push vc
pop [ecx].fOBJ.nTextC

xor ecx,ecx
mov vc,ecx
loop_textc:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("vt ")
.if eax!=0
mov ecx,[esp]
mov eax,[edi+ecx*4]
inc ecx
mov edx,[edi+ecx*4]
dec ecx
sub edx,eax
mov fsz,edx

invoke Word_position,2,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpTextC
mov ecx,vc
fstp dword ptr[eax+ecx]
.endif

mov ecx,[esp]
invoke Word_position,3,[edi+ecx*4],fsz
.if eax!=0
pushad
invoke StrToFloat,edx,addr q
popad
fld q
mov eax,fobj
mov eax,[eax].fOBJ.lpTextC
mov ecx,vc
fstp dword ptr[eax+ecx+4]
.endif

add vc,8
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_textc

;--------------------------------------------------------------------------------


xor ecx,ecx
mov vc,ecx
loop_cgroup:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("g ")
.if eax!=0
inc vc
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_cgroup

mov ecx,vc
shl ecx,2
invoke Alloc,ecx
mov ecx,fobj
mov [ecx].fOBJ.lpgroup,eax
push vc
pop [ecx].fOBJ.nGroup


xor ecx,ecx
mov vc,ecx
loop_group:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("g ")
.if eax!=0
pushad
invoke memfill,addr buff,1024,0
popad
mov ecx,[esp]
mov eax,[edi+ecx*4]
inc ecx
mov edx,[edi+ecx*4]
dec ecx
sub edx,eax
mov fsz,edx
pushad
invoke Word_position,2,[edi+ecx*4],fsz
add eax,2
invoke lstrcpyn,addr buff,edx,eax
popad

xor ecx,ecx
mov fc,ecx
mov ecx,[esp]
inc ecx
loop_cface:
push ecx
invoke szWcnt,[edi+ecx*4],CADD("f ")
.if eax!=0
inc fc
.else
pop ecx
jmp done
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_cface
done:


mov ecx,fc
shl ecx,4
add ecx,128+4
invoke Alloc,ecx
mov ecx,fobj


mov edx,[ecx].fOBJ.lpgroup
mov ecx,vc
mov [edx+ecx*4],eax
pushad
mov edx,eax
invoke lstrcat,edx,addr buff
popad
push eax
xor edx,edx
mov ecx,12
mov eax,fc
mul ecx
mov fc,eax
pop eax
push fc
pop [eax].fObjGroup.nFace
add eax,128+4
mov pface,eax


xor ecx,ecx
mov fc,ecx
mov ecx,[esp]
inc ecx
loop_face:
push ecx
invoke szWcnt ,[edi+ecx*4],CADD("f ")
.if eax!=0
mov ecx,[esp]
mov eax,[edi+ecx*4]
inc ecx
mov edx,[edi+ecx*4]
dec ecx
sub edx,eax
mov fsz,edx

mov ecx,2
loop_facel:
push ecx
mov ecx,[esp+4]
mov eax,[esp]
invoke Word_position,eax,[edi+ecx*4],fsz
pushad
invoke memfill,addr buff,1024,0
popad
inc eax
invoke lstrcpyn,addr buff,edx,eax
invoke wReplace,addr buff,"/"

invoke atodw,addr buff
mov edx,pface
mov [edx],eax
invoke lstrlen,addr buff
lea ecx,buff
inc eax
add ecx,eax
mov offb,ecx
invoke atodw,offb
mov edx,pface
mov [edx+4],eax
invoke lstrlen,offb
inc eax
add offb,eax
invoke atodw,offb
mov edx,pface
mov [edx+8],eax
add pface,12
pop ecx
inc ecx
cmp ecx,6
jl loop_facel

.else
pop ecx
jmp done2
.endif
pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_face
done2:

inc vc
.endif

pop ecx
inc ecx
cmp ecx,tzrow
jnz loop_group


invoke memfill,addr buff,1024,0
invoke lstrcat,addr buff,CADD("Vertext: ")
mov ecx,fobj
invoke dw2a,[ecx].fOBJ.nVertex, addr buff+512
invoke lstrcat,addr buff,addr buff+512

invoke lstrcat,addr buff,CADD(" Normals: ")
mov ecx,fobj
invoke dw2a,[ecx].fOBJ.nNormal, addr buff+512
invoke lstrcat,addr buff,addr buff+512

invoke lstrcat,addr buff,CADD(" TextC: ")
mov ecx,fobj
invoke dw2a,[ecx].fOBJ.nTextC, addr buff+512
invoke lstrcat,addr buff,addr buff+512

invoke lstrcat,addr buff,CADD(" nGroup: ")
mov ecx,fobj
invoke dw2a,[ecx].fOBJ.nGroup, addr buff+512
invoke lstrcat,addr buff,addr buff+512

invoke lstrcat,addr buff,CADD(" GName: ")
mov ecx,fobj
mov edx,[ecx].fOBJ.nGroup
dec edx
mov ecx,[ecx].fOBJ.lpgroup
mov ecx,[ecx+edx*4]
invoke lstrcat,addr buff,ecx

invoke lstrcat,addr buff,CADD(" Row: ")
mov ecx,fobj
invoke dw2a,tzrow, addr buff+512
invoke lstrcat,addr buff,addr buff+512

; invoke MessageBox,0,addr buff,0,0
mov eax,fobj

ret
fLoadOBJ endp


Tell me if some structure are missing.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

To Lightwave Developer,

Im sorry, Im just a poor hobyist programmer, this is my career somehow. I cant afford to buy your application, but I respect your bussiness. If you think this pubication will damage your bussiness, I will remove this. Dont worry, we are on the same side on bussines.  :U
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"