-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavefront2v3d.py
203 lines (168 loc) · 7.54 KB
/
wavefront2v3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
#
# Wavefront to Varooom Converter
# Copyright 2024 Nikku4211
#
# Copying and distribution of this file, with or without
# modification, are permitted in any medium without royalty
# provided the copyright notice and this notice are preserved.
# This file is offered as-is, without any warranty.
#
import os
import sys
import math
#thanks gvaliente
def sub(a, b):
result = []
for i in range(len(a)):
result.append(a[i] - b[i])
return result
def cross(a, b):
return [a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]]
def norm(v):
return math.sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]))
def normal(p0, p1, p2):
# cross_product = np.cross(p1 - p0, p2 - p0)
cross_product = cross(sub(p1, p0), sub(p2, p0))
# normal = cross_product / np.linalg.norm(cross_product)
magnitude = norm(cross_product)
return [cross_product[0] / magnitude, cross_product[1] / magnitude, cross_product[2] / magnitude]
def main(argv):
#handle arguments and help
if len(argv) >= 2 and argv[1] in ('--help', '-h'):
print('usage: objinput v3doutput modelname modelscale --recalcnorms')
sys.exit(1)
elif len(argv) < 4:
print('too few arguments, need --help?')
sys.exit(1)
#recalculating vertex normals is optional
recalcvertexnorms = True if len(argv) >= 6 and argv[5] in ('-rn', '--recalcnorms') else False
waveobjfile = open(argv[1],"rt")
waveobjdata = waveobjfile.readlines()
wavemtlfilename = []
#get material file from obj file
for x in range(len(waveobjdata)):
if waveobjdata[x][0:7] == "mtllib ":
wavemtlfilename = os.path.dirname(os.path.abspath(argv[1])) + "/" + waveobjdata[x].split()[1]
wavemtlfile = open(wavemtlfilename,"rt")
wavemtldata = wavemtlfile.readlines()
#wavefront variables
wavevertices = []
wavevertexnorms = []
wavefacels = []
wavecolours = []
wavematerialcurrent = 0
wavematerialindices = []
wavematerialnames = []
v3dfile = open(argv[2], 'wt')
modelname = argv[3]
modelscale = float(argv[4])
v3dfile.write("""
/*
* Generated by Nikku4211's Wavefront to Varooom Converter
* Nikku4211 - github.com/nikku4211/
*
*/
#ifndef FR_MODEL_3D_ITEMS_%s_H
#define FR_MODEL_3D_ITEMS_%s_H
#include "fr_model_3d_item.h"
namespace fr::model_3d_items
{
constexpr inline vertex_3d %s_vertices[] = {\n""" % (modelname,modelname,modelname))
#read vertices as specified in the obj file and then scale them
for x in range(len(waveobjdata)):
if waveobjdata[x][0:2] == "v ":
waveverticesub = []
for i in range(len(waveobjdata[x][2::].split())):
waveverticesub.append(float(waveobjdata[x][2::].split()[i])*modelscale)
wavevertices.append(waveverticesub)
v3dfile.write(' vertex_3d('
+ "".join('%s' % str(float(waveobjdata[x][2::].split()[0])*modelscale))
+ ","
+ "".join('%s' % str(float(waveobjdata[x][2::].split()[1])*modelscale))
+ ","
+ "".join('%s' % str(float(waveobjdata[x][2::].split()[2])*modelscale))
+ '),\n'
)
v3dfile.write(""" };\n""")
v3dfile.write(" constexpr inline bn::color %s_colors[] = {\n" % modelname)
#convert floating point rgb components specified in obj to 5-bit integers
for x in range(len(wavemtldata)):
if wavemtldata[x][0:3] == "Kd ":
if wavemtldata[x][3::] not in wavecolours:
wavecolours.append(wavemtldata[x][3::])
v3dfile.write(' bn::color('
+ "".join('%s' % math.floor(float(wavemtldata[x][3::].split()[0])*31))
+ ","
+ "".join('%s' % math.floor(float(wavemtldata[x][3::].split()[1])*31))
+ ","
+ "".join('%s' % math.floor(float(wavemtldata[x][3::].split()[2])*31))
+ '),\n'
)
wavematerialnames.append(wavemtldata[x-3][7::])
wavematerialnames.append(wavemtldata[x][3::])
#print(wavematerialnames)
v3dfile.write(" };\n")
#give these colours names
for x in range(len(wavecolours)):
v3dfile.write(' constexpr inline int %s_color_%d = ' % (modelname, x)
+ "".join('%d' % x)
+ ";\n"
)
v3dfile.write("""
constexpr inline face_3d %s_faces_full[] = {\n""" % (modelname))
#if we're not recalculating the vertex normals, take them as specified in the obj
#and in either case, take the face vertex indices as specified in the obj
for x in range(len(waveobjdata)):
if waveobjdata[x][0:3] == "vn " and recalcvertexnorms == False:
wavevertexnormssub = []
for i in range(len(waveobjdata[x][3::].split())):
wavevertexnormssub.append(waveobjdata[x][3::].split()[i])
wavevertexnorms.append(wavevertexnormssub)
if waveobjdata[x][0:2] == "f ":
wavefacelnormindices = []
for i in range(len(waveobjdata[x][2::].split())):
if waveobjdata[x][2::].find('/'):
wavefacelnormindices.append(str(int(waveobjdata[x][2::].split()[i].split("/")[0])-1))
else:
wavefacelnormindices.append(str(int(waveobjdata[x][2::].split()[i])-1))
wavefacels.append(wavefacelnormindices)
if waveobjdata[x-1][0:7] == "usemtl " and wavematerialnames[wavematerialnames.index(waveobjdata[x-1][7::])+1] not in wavematerialnames[:(wavematerialnames.index(waveobjdata[x-1][7::]))]:
wavematerialcurrent += 1
#print(wavematerialcurrent)
wavematerialindices.append(wavematerialcurrent)
#print(wavefacels)
#if we're recalculating the vertex normals, take the sum of the cross product of all the vertices
if recalcvertexnorms == True:
for x in range(len(wavefacels)):
p0 = wavevertices[int(wavefacels[x][0])]
p1 = wavevertices[int(wavefacels[x][1])]
p2 = wavevertices[int(wavefacels[x][2])]
#print(wavevertices[int(wavefacels[x][0])])
#print(wavevertices[int(wavefacels[x][1])])
#print(wavevertices[int(wavefacels[x][2])])
wavevertexnormssub = normal(p0, p1, p2)
wavevertexnorms.append(wavevertexnormssub)
#print(wavevertexnorms[x])
#then put all the vertex normals, face indices, and material indices together in the v3d header file
for x in range(len(wavevertexnorms)):
v3dfile.write(' face_3d(%s_vertices, vertex_3d(' % modelname
+ "".join("%s" % wavevertexnorms[x][0])
+ ","
+ "".join("%s" % wavevertexnorms[x][1])
+ ","
+ "".join("%s" % wavevertexnorms[x][2])
+ "),"
+ ",".join(wavefacels[x])
+ ","
+ "".join("%d" % wavematerialindices[x])
+ ','
+ "0"
+ '),\n'
)
v3dfile.write(""" };\n""")
v3dfile.write(""" constexpr inline model_3d_item %s_full(%s_vertices, %s_faces_full);\n""" % (modelname,modelname,modelname))
v3dfile.write(""" };\n""")
v3dfile.write("#endif")
if __name__=='__main__':
main(sys.argv)