|
|
|
@ -31,6 +31,17 @@ struct triangle |
|
|
|
|
struct mesh |
|
|
|
|
{ |
|
|
|
|
std::vector<triangle> tris; |
|
|
|
|
Decal*texture; |
|
|
|
|
|
|
|
|
|
void Parse(std::string str,int&v,int&uv) { |
|
|
|
|
std::cout<<str<<"\n"; |
|
|
|
|
std::stringstream s(str.substr(0,str.find("/")+1)); |
|
|
|
|
s>>v; |
|
|
|
|
str.erase(0,str.find("/")+1); |
|
|
|
|
std::stringstream s2(str.substr(0,str.find("/")+1)); |
|
|
|
|
s2>>uv; |
|
|
|
|
//std::cout<<" "<<v<<"/"<<uv<<"\n";
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool LoadFromObjectFile(std::string sFilename) |
|
|
|
|
{ |
|
|
|
@ -42,67 +53,33 @@ struct mesh |
|
|
|
|
std::vector<vec3d> verts; |
|
|
|
|
std::vector<vec2d> uvs; |
|
|
|
|
|
|
|
|
|
while (!f.eof()) |
|
|
|
|
{ |
|
|
|
|
char line[128]; |
|
|
|
|
f.getline(line, 128); |
|
|
|
|
|
|
|
|
|
std::strstream s; |
|
|
|
|
s << line; |
|
|
|
|
|
|
|
|
|
char junk; |
|
|
|
|
|
|
|
|
|
if (line[0] == 'v') |
|
|
|
|
{ |
|
|
|
|
if (line[1]=='t') { |
|
|
|
|
vec2d v; |
|
|
|
|
s >> junk >> junk >> v.u >> v.v; |
|
|
|
|
uvs.push_back(v); |
|
|
|
|
//std::cout<<"Line: "<<line<<"\n";
|
|
|
|
|
//std::cout<<"Tex coords: "<<v.u<<","<<v.v<<"\n";
|
|
|
|
|
} else |
|
|
|
|
if (line[1] != 'n') |
|
|
|
|
{ |
|
|
|
|
vec3d v; |
|
|
|
|
s >> junk >> v.x >> v.y >> v.z; |
|
|
|
|
verts.push_back(v); |
|
|
|
|
//std::cout<<"Line: "<<line<<"\n";
|
|
|
|
|
//std::cout<<"Vertex: "<<v.x<<","<<v.y<<","<<v.z<<"\n";
|
|
|
|
|
} |
|
|
|
|
std::string data; |
|
|
|
|
while (f.good()) { |
|
|
|
|
f>>data; |
|
|
|
|
if (data=="v") { |
|
|
|
|
float x,y,z; |
|
|
|
|
f>>x>>y>>z; |
|
|
|
|
verts.push_back({x,y,z}); |
|
|
|
|
//std::cout<<x<<" "<<y<<" "<<z<<"\n";
|
|
|
|
|
} else |
|
|
|
|
if (data=="vt") { |
|
|
|
|
float u,v; |
|
|
|
|
f>>u>>v; |
|
|
|
|
uvs.push_back({u,v}); |
|
|
|
|
//std::cout<<u<<" "<<v<<"\n";
|
|
|
|
|
} else
|
|
|
|
|
if (data=="f") { |
|
|
|
|
//std::cout<<"face\n";
|
|
|
|
|
std::string t1,t2,t3; |
|
|
|
|
f>>t1>>t2>>t3; |
|
|
|
|
int v1,v2,v3,uv1,uv2,uv3; |
|
|
|
|
Parse(t1,v1,uv1); |
|
|
|
|
Parse(t2,v2,uv2); |
|
|
|
|
Parse(t3,v3,uv3); |
|
|
|
|
tris.push_back({verts[v1-1],verts[v2-1],verts[v3-1], |
|
|
|
|
uvs[uv1-1],uvs[uv2-1],uvs[uv3-1]}); |
|
|
|
|
} |
|
|
|
|
if (line[0] == 'f') |
|
|
|
|
{ |
|
|
|
|
s >> junk; |
|
|
|
|
|
|
|
|
|
std::string tokens[9]; |
|
|
|
|
int nTokenCount = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (!s.eof()) |
|
|
|
|
{ |
|
|
|
|
char c = s.get(); |
|
|
|
|
if (c == ' ' || c == '/') |
|
|
|
|
nTokenCount++; |
|
|
|
|
else |
|
|
|
|
tokens[nTokenCount].append(1, c); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tokens[nTokenCount].pop_back(); |
|
|
|
|
triangle newtri{ verts[stoi(tokens[0]) - 1], verts[stoi(tokens[3]) - 1], verts[stoi(tokens[6]) - 1], |
|
|
|
|
uvs[stoi(tokens[1]) - 1], uvs[stoi(tokens[4]) - 1], uvs[stoi(tokens[7]) - 1]}; |
|
|
|
|
if (newtri.p[0].y>16||newtri.p[1].y>16||newtri.p[2].y>16) { |
|
|
|
|
newtri.col=WHITE; |
|
|
|
|
} else
|
|
|
|
|
if (newtri.p[0].y>10||newtri.p[1].y>10||newtri.p[2].y>10) { |
|
|
|
|
newtri.col=DARK_GREEN; |
|
|
|
|
} else
|
|
|
|
|
if (newtri.p[0].y>6||newtri.p[1].y>6||newtri.p[2].y>6) { |
|
|
|
|
newtri.col=Pixel(56, 38, 39); |
|
|
|
|
} else { |
|
|
|
|
newtri.col=Pixel(0,100,255); |
|
|
|
|
} |
|
|
|
|
tris.push_back(newtri); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
@ -461,8 +438,8 @@ private: |
|
|
|
|
public: |
|
|
|
|
bool OnUserCreate() override |
|
|
|
|
{ |
|
|
|
|
//texture = new Decal(new Sprite("Body.png"));
|
|
|
|
|
meshCube.LoadFromObjectFile("obj_1.obj"); |
|
|
|
|
texture = new Decal(new Sprite("High.png")); |
|
|
|
|
meshCube.LoadFromObjectFile("Artisans Hub.obj"); |
|
|
|
|
|
|
|
|
|
matProj=Matrix_MakeProjection(90.0f,(float)ScreenHeight() / (float)ScreenWidth(),0.1f,1000.0f); |
|
|
|
|
|
|
|
|
@ -477,7 +454,7 @@ public: |
|
|
|
|
if (GetKey(olc::UP).bHeld) { |
|
|
|
|
pitch+=1*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
vec3d vForward=Vector_Mul(vLookDir,8*fElapsedTime); |
|
|
|
|
vec3d vForward=Vector_Mul(vLookDir,20*fElapsedTime); |
|
|
|
|
if (GetKey(olc::W).bHeld) { |
|
|
|
|
vCamera=Vector_Add(vCamera,vForward); |
|
|
|
|
} |
|
|
|
@ -534,10 +511,10 @@ public: |
|
|
|
|
vec3d vCameraRay=Vector_Sub(triTransformed.p[0],vCamera); |
|
|
|
|
|
|
|
|
|
if (Vector_DotProduct(normal,vCameraRay)<0) { |
|
|
|
|
vec3d light_dir=Vector_Mul(vCameraRay,-1); |
|
|
|
|
vec3d light_dir=Vector_Mul(vLookDir,-1); |
|
|
|
|
light_dir=Vector_Normalise(light_dir); |
|
|
|
|
|
|
|
|
|
float dp = std::max(0.1f,Vector_DotProduct(light_dir,normal)); |
|
|
|
|
float dp = std::max(0.7f,Vector_DotProduct(light_dir,normal)); |
|
|
|
|
|
|
|
|
|
triViewed.p[0]=Matrix_MultiplyVector(matView,triTransformed.p[0]); |
|
|
|
|
triViewed.p[1]=Matrix_MultiplyVector(matView,triTransformed.p[1]); |
|
|
|
@ -545,7 +522,7 @@ public: |
|
|
|
|
triViewed.uv[0]=triTransformed.uv[0]; |
|
|
|
|
triViewed.uv[1]=triTransformed.uv[1]; |
|
|
|
|
triViewed.uv[2]=triTransformed.uv[2]; |
|
|
|
|
triViewed.col=Pixel(tri.col.r*dp*dp,tri.col.g*dp*dp,tri.col.b*dp*dp); |
|
|
|
|
triViewed.col=Pixel(255*dp*dp,255*dp*dp,255*dp*dp); |
|
|
|
|
|
|
|
|
|
int nClippedTriangles = 0; |
|
|
|
|
triangle clipped[2]; |
|
|
|
@ -647,7 +624,7 @@ public: |
|
|
|
|
// Rasterize triangle
|
|
|
|
|
SetDecalStructure(DecalStructure::LIST); |
|
|
|
|
SetDecalMode(DecalMode::NORMAL); |
|
|
|
|
DrawPolygonDecal(nullptr,{ |
|
|
|
|
DrawPolygonDecal(texture,{ |
|
|
|
|
{t.p[0].x, t.p[0].y}, |
|
|
|
|
{t.p[1].x, t.p[1].y}, |
|
|
|
|
{t.p[2].x, t.p[2].y} |
|
|
|
|