Commit 4b89db5a authored by Gauvain Roussel-Tarbouriech's avatar Gauvain Roussel-Tarbouriech
Browse files

Rewriting using vectors and making this actually readable

parent 029687f6
......@@ -3,6 +3,7 @@
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
#include <limits>
......@@ -14,32 +15,17 @@ int max(int x, int y, int z) {
return flagx;
}
void get_uv(std::ifstream in, std::ofstream dsm, int uv){
in.clear();
in.seekg(0, std::ios::beg);
int cnt=0;
std::string line;
while (getline(in, line))
{
if (line.substr(0,3) == "vt ")
{
cnt++;
if(cnt==uv){
std::istringstream s(line.substr(3));
float u, v;
s >> u; s >> v;
// todo: get to uv off and write
dsm << ".short " << int(round(u*4095)) << ", " << int(round(v*4095)) << "\n";
}
}
}
}
int main(int argc, char* argv[]){
long ap, vp, hp;
// max uv buffer, should be enough for an entire set of obj vertices
int uvs[8192];
int uv_count=0;
int vertex_count=0;
int face_count=0;
int vertices_arg=0;
int vertex_affiliation=0;
int junk=1;
printf("obj2kh2v\n-- If you don't know what a VIF packet be prepared to be driven to insanity\n\n");
if(argc<2){printf("Usage: obj2kh2v model.obj"); return -1;}
......@@ -47,26 +33,38 @@ int main(int argc, char* argv[]){
std::string dsmname = std::string(argv[1]).substr(0,std::string(argv[1]).find_last_of('.'))+".dsm";
std::string kh2vname = std::string(argv[1]).substr(0,std::string(argv[1]).find_last_of('.'))+".kh2v";
std::ofstream dsm (dsmname);
dsm << ".align 0\n;" << argv[1] << "\n;Automatically generated by obj2kh2v\n;DO NOT EDIT IF YOU DON'T KNOW WHAT YOU ARE DOING\n\nstcycl 01, 01; We write code to memory without skips/overwrite\n\nunpack[r] V4_32, 0, *; Model Part Header\n.int 1, 0, 0, 0;type 1 Model\n";
hp = dsm.tellp();
//Can't insert if not at eof using ofstream so I put 10 spaces to fill up, size of INT_MAX and no \n to avoid linefeed not beeing rewritten
//It's so hacky but I'm fed up with this shit already
dsm << " " << " , , , ";
dsm << "\n.EndUnpack\n\nstcycl 01, 01; We write code to memory without skips/overwrite\n\nunpack[r] V2_16, 4, *; UV definition\n";
int vi =0;
int ti =0;
int face_count=0;
int vert_count=0;
// max uv buffer, should be enough for an entire set of obj vertices
int uvs[8192];
std::string line;
std::vector<std::string> dsm_mem;
dsm_mem.push_back(".align 0");
line = ";";
line+=argv[1];
dsm_mem.push_back(line);
dsm_mem.push_back(";Automatically generated by obj2kh2v");
dsm_mem.push_back(";DO NOT EDIT IF YOU DON'T KNOW WHAT YOU ARE DOING");
dsm_mem.push_back("");
dsm_mem.push_back("stcycl 01, 01; We write code to memory without skips/overwrite");
dsm_mem.push_back("");
dsm_mem.push_back("unpack[r] V4_32, 0, * ;Model Part Header");
dsm_mem.push_back(".int 1, 0, 0, 0 ;type 1 Model");
// 3 MPHeader lines we will fix at the end, Vector is 11 here
int tmpheader = dsm_mem.size();
dsm_mem.push_back("");
dsm_mem.push_back("");
dsm_mem.push_back("");
dsm_mem.push_back(".EndUnpack");
dsm_mem.push_back("");
dsm_mem.push_back("stcycl 01, 01; We write code to memory without skips/overwrite");
dsm_mem.push_back("");
dsm_mem.push_back("unpack[r] V2_16, 4, *; UV definition");
if (dsm.is_open()){
std::string line;
while (getline(input, line))
{
if (line.substr(0,2) == "v ")
{
vi++;
vertex_count++;
}
}
input.clear();
......@@ -81,7 +79,6 @@ dsm << "\n.EndUnpack\n\nstcycl 01, 01; We write code to memory without skips/ove
input.clear();
input.seekg(0, std::ios::beg);
int y=0;
while (getline(input, line))
{
if (line.substr(0,3) == "vt ")
......@@ -90,29 +87,33 @@ dsm << "\n.EndUnpack\n\nstcycl 01, 01; We write code to memory without skips/ove
float u, v;
s >> u; s >> v;
printf("tx= %f, ty= %f, txr= %d, tyr=%d\n", u, v, int(round(u*4095)), int(round(v*4095)));
uvs[y]=int(round(u*4095));
uvs[y+1]=int(round(v*4095));
y+=2;
uvs[uv_count]=int(round(u*4095));
uvs[uv_count+1]=int(round(v*4095));
uv_count+=2;
}
}
input.clear();
input.seekg(0, std::ios::beg);
long uv_pos = dsm.tellp();
// get uv pos here
// making temporary entries for uvs to insert later on
int uv_pos=dsm_mem.size();
for(int i=0; i<face_count;i++){
// temporary uvs to rewrite below
dsm << " ";
dsm << " ";
dsm << " ";
dsm_mem.push_back("");
dsm_mem.push_back("");
dsm_mem.push_back("");
}
input.clear();
input.seekg(0, std::ios::beg);
dsm_mem.push_back(".EndUnpack");
dsm_mem.push_back("");
dsm_mem.push_back("stmask 0xcfcfcfcf; Sets mask register(3303, check EEUSER_E)");
dsm_mem.push_back("stcycl 01, 01; We write code to memory without skips/overwrite");
dsm_mem.push_back("");
dsm_mem.push_back("unpack[mr] S_8, 4, *; Vertex indices");
dsm << "\n.EndUnpack\n\nstmask 0xcfcfcfcf; Sets mask register(3303, check EEUSER_E)\nstcycl 01, 01; We write code to memory without skips/overwrite\n\nunpack[mr] S_8, 4, *; Vertex indices\n";
while (getline(input, line))
{
if (line.substr(0,2) == "f ")
......@@ -126,144 +127,101 @@ dsm << "\n.EndUnpack\n\nstcycl 01, 01; We write code to memory without skips/ove
inn = std::stoi(n.substr(0, n.find("/")));
iu = std::stoi(u.substr(0, u.find("/")));
printf("i1: %d i2: %d i3: %d\n", ii, inn, iu);
dsm << std::flush;
dsm << ".byte " << ii-1 << "\n";
cur_pos=dsm.tellp();
#if (_WIN32)
dsm.seekp(uv_pos+10);
#else
dsm.seekp(uv_pos);
#endif
dsm << std::flush;
dsm << ".short " << uvs[(ii-1)*2] << ", " << uvs[((ii-1)*2)+1] << std::endl;
uv_pos=dsm.tellp();
#if (_WIN32)
dsm.seekp(cur_pos+10);
#else
dsm.seekp(cur_pos);
#endif
dsm << std::flush;
dsm << ".byte " << inn-1 << std::endl;
cur_pos=dsm.tellp();
#if (_WIN32)
dsm.seekp(uv_pos+10);
#else
dsm.seekp(uv_pos);
#endif
dsm << std::flush;
dsm << ".short " << uvs[(inn-1)*2] << ", " << uvs[((inn-1)*2)+1] << std::endl;
uv_pos=dsm.tellp();
#if (_WIN32)
dsm.seekp(cur_pos+10);
#else
dsm.seekp(cur_pos);
#endif
dsm << std::flush;
dsm << ".byte " << iu-1 << std::endl;
cur_pos=dsm.tellp();
#if (_WIN32)
dsm.seekp(uv_pos+10);
#else
dsm.seekp(uv_pos);
#endif
dsm << std::flush;
dsm << ".short " << uvs[(iu-1)*2] << ", " << uvs[((iu-1)*2)+1] << std::endl;
uv_pos=dsm.tellp();
#if (_WIN32)
dsm.seekp(cur_pos+10);
#else
dsm.seekp(cur_pos);
#endif
line=".byte " + std::to_string(ii-1);
dsm_mem.push_back(line);
line=".short " + std::to_string(uvs[(ii-1)*2]) + ", " + std::to_string(uvs[((ii-1)*2)+1]);
dsm_mem[uv_pos]=line;
uv_pos++;
line=".byte " + std::to_string(inn-1);
dsm_mem.push_back(line);
line=".short " + std::to_string(uvs[(inn-1)*2]) + ", " + std::to_string(uvs[((inn-1)*2)+1]);
dsm_mem[uv_pos]=line;
uv_pos++;
line=".byte " + std::to_string(iu-1);
dsm_mem.push_back(line);
line=".short " + std::to_string(uvs[(iu-1)*2]) + ", " + std::to_string(uvs[((iu-1)*2)+1]);
dsm_mem[uv_pos]=line;
uv_pos++;
printf("bis i1: %d i2: %d i3: %d\n", (ii-1)*2, (inn-1)*2, (iu-1)*2);
vert_count+=3;
}
}
input.clear();
input.seekg(0, std::ios::beg);
dsm << ".EndUnpack\n\nstmask 0x3f3f3f3f; Sets mask register(3330, check EEUSER_E)\nstcycl 01, 01; We write code to memory without skips/overwrite\n\nunpack[mr] S_8, 4, *; Flags\n";
for(int i=0; i<face_count; i++){
long pos = dsm.tellp();
dsm << ".byte 0x10; stock\n";
dsm << ".byte 0x10; stock\n";
dsm << ".byte 0x20; draw triangle\n";
}
dsm << ".EndUnpack\n\nstcol 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000; We set garbage data to 1(float) so even if nothing is referenced game doesn't go crazy\nstmask 0x80808080; Sets mask register(0002, check EEUSER_E)\nstcycl 01, 01; We write code to memory without skips/overwrite\n\nunpack[mr] V3_32,";
vp = dsm.tellp();
dsm << " , *; Vertex definition\n";
input.clear();
input.seekg(0, std::ios::beg);
dsm_mem.push_back(".EndUnpack");
dsm_mem.push_back("");
dsm_mem.push_back("stmask 0x3f3f3f3f; Sets mask register(3330, check EEUSER_E)");
dsm_mem.push_back("stcycl 01, 01; We write code to memory without skips/overwrite");
dsm_mem.push_back("");
dsm_mem.push_back("unpack[mr] S_8, 4, *; Flags");
for(int i=0; i<face_count; i++){
dsm_mem.push_back(".byte 0x10; stock");
dsm_mem.push_back(".byte 0x10; stock");
dsm_mem.push_back(".byte 0x20; draw triangle");
}
dsm_mem.push_back(".EndUnpack");
dsm_mem.push_back("");
dsm_mem.push_back("stcol 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000; We set garbage data to 1(float) so even if nothing is referenced game doesn't go crazy");
dsm_mem.push_back("stmask 0x80808080; Sets mask register(0002, check EEUSER_E)");
dsm_mem.push_back("stcycl 01, 01; We write code to memory without skips/overwrite");
dsm_mem.push_back("");
dsm_mem.push_back("unpack[mr] V3_32, , *; Vertex definition");
vertices_arg = dsm_mem.size()-1;
while (getline(input, line))
while (getline(input, line))
{
if (line.substr(0,2) == "v ")
{
std::istringstream s(line.substr(2));
float x, y, z, w;
s >> x; s >> y; s >> z; w = 1.0f;
dsm << ".float " << x << ", " << y << ", " << z << "\n";
}
}
if (line.substr(0,2) == "v ")
{
std::istringstream s(line.substr(2));
float x, y, z, w;
s >> x; s >> y; s >> z; w = 1.0f;
line=".float " + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z);
dsm_mem.push_back(line);
}
}
dsm_mem.push_back(".EndUnpack");
dsm_mem.push_back("");
dsm_mem.push_back("stcycl 01, 01; We write code to memory without skips/overwrite");
dsm_mem.push_back("");
dsm_mem.push_back("unpack[r] V4_32, ,*; Vertex affiliation header");
vertex_affiliation = dsm_mem.size()-1;
line = ".int " + std::to_string(vertex_count) + ", 0, 0, 0";
dsm_mem.push_back(line);
dsm_mem.push_back(".EndUnpack");
dsm_mem.push_back("vifnop");
dsm_mem.push_back("vifnop; We wait for data to be kicked in");
dsm << ".EndUnpack\n\nstcycl 01, 01; We write code to memory without skips/overwrite\n\nunpack[r] V4_32,";
ap = dsm.tellp();
dsm << " \n.int " << vi << ", 0, 0, 0\n.EndUnpack\nvifnop\nvifnop; We wait for data to be kicked in\n";
}
printf("h1: %i, h2: 4, h3: %i, h4: %i\nj1: %i, j2: %i, j3: 0, j4: 1\n",ti, 4+ti+vi, 4+ti+vi+1,vi, 4+ti);
}
printf("h1: %i, h2: 4, h3: %i, h4: %i\nj1: %i, j2: %i, j3: 0, j4: 1\n",0, 4+0+vertex_count, 4+0+vertex_count+1,vertex_count, 4+0);
//TODO: Stop hardcode Header size(5type-line exist) and Vert/Array
//h3: 1 not because 1 value but 1 array of 4, padding needs to be
//checked!
#if (_WIN32)
dsm << std::flush;
dsm.seekp(hp+20);
#else
dsm.seekp(hp);
#endif
dsm << std::flush;
dsm << ".int " << face_count*3 << ", 4, " << 4+(face_count*3)+vi << ", " << 4+(face_count*3)+vi+1 << "; Number of u+v+flag+index, their offset, offset of vertex affiliation header, offset of mat definition(end)\n";
dsm << ".int 0, 0, 0, 0; Nobody care about vertices merging and colors\n";
dsm << ".int " << vi << ", " << 4+(face_count*3) << ", 0, 1; Number of vertices, their offset, reserved and number of array attribution\n";
#if (_WIN32)
dsm << std::flush;
dsm.seekp(vp+10);
#else
dsm.seekp(vp);
#endif
dsm << std::flush;
dsm << 4+(face_count*3);
#if (_WIN32)
dsm << std::flush;
dsm.seekp(ap+30);
#else
dsm.seekp(ap);
#endif
dsm << std::flush;
dsm << 4+(face_count*3)+vi << ", *; Vertex affiliation header" << std::endl ;
dsm.close();
#if (_WIN32)
if(system(("dvp-as \""+dsmname+"\" -o junk.o").c_str()) != 0){printf("Could not proceed, please install homebrew ps2 sdk!\n"); return -1;}
line=".int " + std::to_string(face_count*3) + ", 4, " + std::to_string(4+(face_count*3)+vertex_count) + ", " + std::to_string(4+(face_count*3)+vertex_count+1) + "; Number of u+v+flag+index, their offset, offset of vertex affiliation header, offset of mat definiti on(end)";
dsm_mem[tmpheader]=line;
dsm_mem[tmpheader+1]=".int 0, 0, 0, 0; Nobody care about vertices merging and colors";
line=".int " + std::to_string(vertex_count) + ", " + std::to_string(4+(face_count*3)) + ", 0, 1; Number of vertices, their offset, reserved and number of array attribution";
dsm_mem[tmpheader+2]=line;
line="unpack[mr] V3_32, " + std::to_string(4+(face_count*3)) + ", *; Vertex definition";
dsm_mem[vertices_arg]=line;
line="unpack[r] V4_32, " + std::to_string(4+(face_count*3)+vertex_count) + " ,*; Vertex affiliation header";
dsm_mem[vertex_affiliation]=line;
for(int i=0; i<dsm_mem.size(); i++){
dsm << dsm_mem[i] << std::endl;
}
dsm.close();
if(system(("dvp-as \""+dsmname+"\" -o junk.o").c_str()) != 0){printf("Could not proceed, please install the homebrew ps2 sdk!\n"); return -1;}
if(system(("dvp-objcopy -O binary junk.o \""+kh2vname+"\"").c_str()) != 0){printf("Your homebrew ps2 sdk installation seems to be broken, please reinstall\n"); return -1;}
#else
if(system(("dvp-as "+dsmname+" -o junk.o > /dev/null").c_str()) != 0){printf("Could not proceed, please install homebrew ps2 sdk!\n"); return -1;}
if(system(("dvp-objcopy -O binary junk.o "+kh2vname+" > /dev/null").c_str()) != 0){printf("Your homebrew ps2 sdk installation seems to be broken, please reinstall\n"); return -1;}
#endif
remove("junk.o");
if(!junk){remove(dsmname.c_str());}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment