void MultiCollide(const Triangle &tri0, const Triangle &tri1, const Triangle &tri2, const Triangle &tri3, ShadowContext &ctx, int sidx, int firstA, int lastA) { //TODO: ulozyc odpowiednio (wektorowo) i liczyc test RayInterval - 4 trojkaty Vec3q tnrm[4], tvec0[4], tvec1[4]; floatq tmul[4], zero(0.0f), one(1.0f); tri0.Prepare(ctx, tnrm[0], tvec0[0], tvec1[0], tmul[0]); tri1.Prepare(ctx, tnrm[1], tvec0[1], tvec1[1], tmul[1]); tri2.Prepare(ctx, tnrm[2], tvec0[2], tvec1[2], tmul[2]); tri3.Prepare(ctx, tnrm[3], tvec0[3], tvec1[3], tmul[3]); for(int q = firstA; q <= lastA; q++) { Vec3q dir = ctx.rayDir[q]; floatq distance = ctx.distance[q]; floatq idet[4] = { Inv(dir | tnrm[0]), Inv(dir | tnrm[1]), Inv(dir | tnrm[2]), Inv(dir | tnrm[3]) }; floatq dist[4] = { idet[0] * tmul[0], idet[1] * tmul[1], idet[2] * tmul[2], idet[3] * tmul[3] }; floatq v[4] = { (dir | tvec0[0]) * idet[0], (dir | tvec0[1]) * idet[1], (dir | tvec0[2]) * idet[2], (dir | tvec0[3]) * idet[3], }; floatq u[4] = { (dir | tvec1[0]) * idet[0], (dir | tvec1[1]) * idet[1], (dir | tvec1[2]) * idet[2], (dir | tvec1[3]) * idet[3], }; f32x4b test[4] = { Min(u[0], v[0]) >= zero && u[0] + v[0] <= one, Min(u[1], v[1]) >= zero && u[1] + v[1] <= one, Min(u[2], v[2]) >= zero && u[2] + v[2] <= one, Min(u[3], v[3]) >= zero && u[3] + v[3] <= one, }; test[0] = test[0] /*&& idet[0] > zero*/ && dist[0] >= zero; test[1] = test[1] /*&& idet[1] > zero*/ && dist[1] >= zero; test[2] = test[2] /*&& idet[2] > zero*/ && dist[2] >= zero; test[3] = test[3] /*&& idet[3] > zero*/ && dist[3] >= zero; f32x4 minDist = distance; minDist = Condition(test[0] && dist[0] < minDist, dist[0], minDist); minDist = Condition(test[1] && dist[1] < minDist, dist[1], minDist); minDist = Condition(test[2] && dist[2] < minDist, dist[2], minDist); minDist = Condition(test[3] && dist[3] < minDist, dist[3], minDist); test[0] = test[0] && dist[0] <= minDist; test[1] = test[1] && dist[1] <= minDist; test[2] = test[2] && dist[2] <= minDist; test[3] = test[3] && dist[3] <= minDist; ctx.distance[q] = minDist; } }
Scene::Scene() : m_AmbientLight(0) { #if 0 #if 1 for (int i=0; i < 100; i++) { DefaultShader *ds = new DefaultShader; Sphere *s = new Sphere(Vector3d(Libvec::Random(-50, 50), Libvec::Random(-50, 50), Libvec::Random(-50, 50)), Libvec::Random(1, 20)); ds->SetDiffuseColor(colorf::RandColor()); ds->SetShinyness(0.0f); s->SetShader(ShaderPtr(ds)); s->Prepare(); m_DrawableList.push_back(s); } #endif Sphere *s = new Sphere(Vector3d(0.0, 0.0, 0.0), 50.0); { DefaultShader *ds = new DefaultShader; ds->SetShinyness(0.0f); s->SetShader(ShaderPtr(ds)); s->Prepare(); } m_DrawableList.push_back(s); Plane *p = new Plane(Ray(Vector3d(0.0, 0.0, -50.0), Vector3d(0.0, 0.0, 1.0))); { DefaultShader *ds = new DefaultShader; ds->SetShinyness(0.2f); p->SetShader(ShaderPtr(ds)); p->Prepare(); } m_DrawableList.push_back(p); p = new Plane(Ray(Vector3d(-50.0, 0.0, 0.0), Vector3d(1.0, 0.0, 0.0))); { DefaultShader *ds = new DefaultShader; ds->SetShinyness(0.0f); p->SetShader(ShaderPtr(ds)); p->Prepare(); } m_DrawableList.push_back(p); m_SimpleLightList.push_back(new SimpleLight(Vector3d(0.0, 0.0, 100.0), colorf(1.0, 0.0, 0.0), 100)); m_SimpleLightList.push_back(new SimpleLight(Vector3d(100.0, 0.0, 5.0), colorf(0.0, 1.0, 0.0), 100)); m_SimpleLightList.push_back(new SimpleLight(Vector3d(0.0, 100.0, 5.0), colorf(0.0, 0.0, 1.0), 100)); m_SimpleLightList.push_back(new SimpleLight(Vector3d(0.0, 60.0, 60.0), colorf(1.0, 1.0, 1.0), 100)); #endif #if 0 m_AmbientLight = colorf(0.0f, 0.0f, 0.0f); { Sphere *s = new Sphere(Vector3d(10.0, 10.0, 10.0), 8.0); { DefaultShader *ds = new DefaultShader; ds->SetShinyness(0.8f); s->SetShader(ShaderPtr(ds)); s->Prepare(); } m_DrawableList.push_back(s); } // bottom { Plane *p = new Plane(Ray(Vector3d(0.0, 0.0, 0.0), Vector3d(0.0, 0.0, 1.0))); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(1.0, 0.2, 0.2)); ds->SetShinyness(0.5f); p->SetShader(ShaderPtr(ds)); p->Prepare(); m_DrawableList.push_back(p); } { Plane *p = new Plane(Ray(Vector3d(0.0, 0.0, 0.0), Vector3d(0.0, 1.0, 0.0))); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(0.2, 1.0, 0.2)); ds->SetShinyness(0.5f); p->SetShader(ShaderPtr(ds)); p->Prepare(); m_DrawableList.push_back(p); } // back { Plane *p = new Plane(Ray(Vector3d(0.0, 0.0, 0.0), Vector3d(1.0, 0.0, 0.0))); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(0.2, 0.2, 1.0)); ds->SetShinyness(0.5f); p->SetShader(ShaderPtr(ds)); p->Prepare(); m_DrawableList.push_back(p); } // m_SimpleLightList.push_back(new SimpleLight(Vector3d(1.0, 1.0, 100.0), colorf(1.0, 0.0, 0.0), 100)); // m_SimpleLightList.push_back(new SimpleLight(Vector3d(100.0, 1.0, 5.0), colorf(0.0, 1.0, 0.0), 100)); // m_SimpleLightList.push_back(new SimpleLight(Vector3d(1.0, 100.0, 5.0), colorf(0.0, 0.0, 1.0), 100)); m_SimpleLightList.push_back(new SimpleLight(Vector3d(20.0, 10.0, 20), colorf(1.0, 1.0, 1.0), 100)); #endif #if 1 m_AmbientLight = colorf(0.0f, 0.0f, 0.2f); if (0) { Triangle *t = new Triangle(Vector3d(0.0, 0.0, 10.0), Vector3d(20.0, 0.0, 0.0), Vector3d(0.0, 20.0, 0.0)); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(0.2, 0.2, 1.0)); ds->SetShinyness(0.5f); t->SetShader(ShaderPtr(ds)); t->Prepare(); m_DrawableList.push_back(t); } if (1) { Plane *p = new Plane(Ray(Vector3d(0.0, 0.0, -3.0), Vector3d(0.0, 0.0, 1.0))); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(1.0, 0.0, 0.0)); ds->SetShinyness(0.2f); p->SetShader(ShaderPtr(ds)); p->Prepare(); m_DrawableList.push_back(p); } // m_SimpleLightList.push_back(new SimpleLight(Vector3d(1.0, 1.0, 100.0), colorf(1.0, 0.0, 0.0), 100)); // m_SimpleLightList.push_back(new SimpleLight(Vector3d(100.0, 1.0, 5.0), colorf(0.0, 1.0, 0.0), 100)); m_SimpleLightList.push_back(new SimpleLight(Vector3d(1.0, 100.0, 5.0), colorf(0.0, 0.0, 1.0), 100)); // m_SimpleLightList.push_back(new SimpleLight(Vector3d(20.0, 10.0, 20), colorf(1.0, 1.0, 1.0), 100)); #endif if (0) { MeshDrawable *md = LoadMesh("resources/cbox.obj"); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(1.0, 1.0, 0.0)); ds->SetShinyness(0.0f); md->SetShader(ShaderPtr(ds)); md->Prepare(); m_DrawableList.push_back(md); } if (1) { MeshDrawable *md = LoadMesh("resources/dude.obj"); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(1.0, 1.0, 0.0)); ds->SetShinyness(0.0f); // DebugShader *ds = new DebugShader; // ds->SetMode(DebugShader::MODE_NORMAL); md->SetShader(ShaderPtr(ds)); md->Prepare(); m_DrawableList.push_back(md); } if (0) { MeshDrawable *md = LoadMesh("resources/plane.obj"); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(0.0, 0.0, 1.0)); ds->SetShinyness(0.0f); md->SetShader(ShaderPtr(ds)); md->Prepare(); m_DrawableList.push_back(md); } if (0) { MeshDrawable *md = LoadMesh("resources/csphere.obj"); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(0.0, 0.0, 1.0)); ds->SetShinyness(0.0f); md->SetShader(ShaderPtr(ds)); md->Prepare(); m_DrawableList.push_back(md); } if (0) { MeshDrawable *md = LoadMesh("resources/blob.obj"); DefaultShader *ds = new DefaultShader; ds->SetDiffuseColor(colorf(1.0, 0.5, 0.5)); ds->SetShinyness(0.0f); md->SetShader(ShaderPtr(ds)); md->Prepare(); m_DrawableList.push_back(md); } if (0) { MeshDrawable *md = LoadMesh("resources/lerp.obj"); DebugShader *ds = new DebugShader; ds->SetMode(DebugShader::MODE_NORMAL); md->SetShader(ShaderPtr(ds)); md->Prepare(); m_DrawableList.push_back(md); } // m_SimpleLightList.push_back(new SimpleLight(Vector3d(0.0, 0.0, 20), colorf(1.0, 1.0, 1.0), 100)); m_Camera = Libvec::Vector3d(2.0f, 2.0f, 2.0f); m_SimpleLightList.push_back(new SimpleLight(Vector3d(1.0f, 4.0f, 7.0f), colorf(0.5, 1.0, 0.5), 100)); // m_Camera = Libvec::Vector3d(80.0f, 80.0f, 10.0f); m_Target = Libvec::Vector3d(0.0f, 0.0f, 0.0f); }
void MultiCollide(const Triangle &tri0, const Triangle &tri1, const Triangle &tri2, const Triangle &tri3, PrimaryContext &ctx, int sidx, int firstA, int lastA) { //TODO: ulozyc odpowiednio (wektorowo) i liczyc test RayInterval - 4 trojkaty Vec3q tnrm[4], tvec0[4], tvec1[4]; floatq tmul[4], zero(0.0f), one(1.0f); i32x4 idx[4] = { sidx, sidx + 1, sidx + 2, sidx + 3 }; /* { Vec3q ttnrm( floatq(tri0.plane.x, tri1.plane.x, tri2.plane.x, tri3.plane.x), floatq(tri0.plane.y, tri1.plane.y, tri2.plane.y, tri3.plane.y), floatq(tri0.plane.z, tri1.plane.z, tri2.plane.z, tri3.plane.z) ); Vec3q tvec = ctx.rayOrigin - Vec3q( floatq(tri0.a.x, tri1.a.x, tri2.a.x, tri3.a.x), floatq(tri0.a.y, tri1.a.y, tri2.a.y, tri3.a.y), floatq(tri0.a.z, tri1.a.z, tri2.a.z, tri3.a.z) ); Vec3q tba( floatq(tri0.ba.x, tri1.ba.x, tri2.ba.x, tri3.ba.x), floatq(tri0.ba.y, tri1.ba.y, tri2.ba.y, tri3.ba.y), floatq(tri0.ba.z, tri1.ba.z, tri2.ba.z, tri3.ba.z) ); Vec3q tca( floatq(tri0.ca.x, tri1.ca.x, tri2.ca.x, tri3.ca.x), floatq(tri0.ca.y, tri1.ca.y, tri2.ca.y, tri3.ca.y), floatq(tri0.ca.z, tri1.ca.z, tri2.ca.z, tri3.ca.z) ); floatq tit0(tri0.it0, tri1.it0, tri2.it0, tri3.it0); Vec3q ttvec0 = (tba ^ tvec) * tit0; Vec3q ttvec1 = (tvec ^ tca) * tit0; floatq ttmul = -(tvec | ttnrm); for(int k = 0; k < 4; k++) { tnrm[k] = Vec3q(ttnrm.x[k], ttnrm.y[k], ttnrm.z[k]); tvec0[k] = Vec3q(ttvec0.x[k], ttvec0.y[k], ttvec0.z[k]); tvec1[k] = Vec3q(ttvec1.x[k], ttvec1.y[k], ttvec1.z[k]); tmul[k] = ttmul[k]; } } */ tri0.Prepare(ctx, tnrm[0], tvec0[0], tvec1[0], tmul[0]); tri1.Prepare(ctx, tnrm[1], tvec0[1], tvec1[1], tmul[1]); tri2.Prepare(ctx, tnrm[2], tvec0[2], tvec1[2], tmul[2]); tri3.Prepare(ctx, tnrm[3], tvec0[3], tvec1[3], tmul[3]); for(int q = firstA; q <= lastA; q++) { Vec3q dir = ctx.rayDir[q]; floatq distance = ctx.distance[q]; i32x4 triIds = ctx.triIds[q]; floatq idet[4] = { Inv(dir | tnrm[0]), Inv(dir | tnrm[1]), Inv(dir | tnrm[2]), Inv(dir | tnrm[3]) }; floatq dist[4] = { idet[0] * tmul[0], idet[1] * tmul[1], idet[2] * tmul[2], idet[3] * tmul[3] }; Vec2q barycentric = ctx.barycentric[q]; Vec3q normals = ctx.normals[q]; floatq v[4] = { (dir | tvec0[0]) * idet[0], (dir | tvec0[1]) * idet[1], (dir | tvec0[2]) * idet[2], (dir | tvec0[3]) * idet[3], }; floatq u[4] = { (dir | tvec1[0]) * idet[0], (dir | tvec1[1]) * idet[1], (dir | tvec1[2]) * idet[2], (dir | tvec1[3]) * idet[3], }; f32x4b test[4] = { Min(u[0], v[0]) >= zero && u[0] + v[0] <= one, Min(u[1], v[1]) >= zero && u[1] + v[1] <= one, Min(u[2], v[2]) >= zero && u[2] + v[2] <= one, Min(u[3], v[3]) >= zero && u[3] + v[3] <= one, }; test[0] = test[0] /*&& idet[0] > zero*/ && dist[0] >= zero; test[1] = test[1] /*&& idet[1] > zero*/ && dist[1] >= zero; test[2] = test[2] /*&& idet[2] > zero*/ && dist[2] >= zero; test[3] = test[3] /*&& idet[3] > zero*/ && dist[3] >= zero; f32x4 minDist = distance; minDist = Condition(test[0] && dist[0] < minDist, dist[0], minDist); minDist = Condition(test[1] && dist[1] < minDist, dist[1], minDist); minDist = Condition(test[2] && dist[2] < minDist, dist[2], minDist); minDist = Condition(test[3] && dist[3] < minDist, dist[3], minDist); test[0] = test[0] && dist[0] <= minDist; test[1] = test[1] && dist[1] <= minDist; test[2] = test[2] && dist[2] <= minDist; test[3] = test[3] && dist[3] <= minDist; ctx.distance[q] = minDist; ctx.normals[q] = Condition(test[0], tnrm[0], Condition(test[1], tnrm[1], Condition(test[2], tnrm[2], Condition(test[3], tnrm[3], normals)))); ctx.triIds[q] = Condition(i32x4b(test[0]), idx[0], Condition(i32x4b(test[1]), idx[1], Condition(i32x4b(test[2]), idx[2], Condition(i32x4b(test[3]), idx[3], triIds)))); ctx.barycentric[q] = Condition(test[0], Vec2q(u[0], v[0]), Condition(test[1], Vec2q(u[1], v[1]), Condition(test[2], Vec2q(u[2], v[2]), Condition(test[3], Vec2q(u[3], v[3]), barycentric)))); } }
void MultiCollide(const Triangle &tri0, const Triangle &tri1, const Triangle &tri2, const Triangle &tri3, SecondaryContext &ctx, int sidx, int firstA, int lastA) { //TODO: ulozyc odpowiednio (wektorowo) i liczyc test RayInterval - 4 trojkaty Vec3q tnrm[4], ta[4], tca[4], tba[4]; floatq tit0[4], zero(0.0f), one(1.0f); i32x4 idx(sidx + 0, sidx + 1, sidx + 2, sidx + 3); tri0.Prepare(ctx, tnrm[0], ta[0], tba[0], tca[0], tit0[0]); tri1.Prepare(ctx, tnrm[1], ta[1], tba[1], tca[1], tit0[1]); tri2.Prepare(ctx, tnrm[2], ta[2], tba[2], tca[2], tit0[2]); tri3.Prepare(ctx, tnrm[3], ta[3], tba[3], tca[3], tit0[3]); for(int q = firstA; q <= lastA; q++) { Vec3q dir = ctx.rayDir[q]; Vec3q origin = ctx.rayOrigin[q]; floatq distance = ctx.distance[q]; i32x4 triIds = ctx.triIds[q]; floatq idet[4] = { Inv(dir | tnrm[0]), Inv(dir | tnrm[1]), Inv(dir | tnrm[2]), Inv(dir | tnrm[3]) }; Vec3q tvec[4] = { origin - ta[0], origin - ta[1], origin - ta[2], origin - ta[3] }; floatq dist[4] = { -idet[0] * (tvec[0] | tnrm[0]), -idet[1] * (tvec[1] | tnrm[1]), -idet[2] * (tvec[2] | tnrm[2]), -idet[3] * (tvec[3] | tnrm[3]) }; Vec2q barycentric = ctx.barycentric[q]; Vec3q normals = ctx.normals[q]; Vec3q tvec0[4] = { tba[0] ^ tvec[0], tba[1] ^ tvec[1], tba[2] ^ tvec[2], tba[3] ^ tvec[3] }; Vec3q tvec1[4] = { tvec[0] ^ tca[0], tvec[1] ^ tca[1], tvec[2] ^ tca[2], tvec[3] ^ tca[3] }; idet[0] *= tit0[0]; idet[1] *= tit0[1]; idet[2] *= tit0[2]; idet[3] *= tit0[3]; floatq v[4] = { (dir | tvec0[0]) * idet[0], (dir | tvec0[1]) * idet[1], (dir | tvec0[2]) * idet[2], (dir | tvec0[3]) * idet[3], }; floatq u[4] = { (dir | tvec1[0]) * idet[0], (dir | tvec1[1]) * idet[1], (dir | tvec1[2]) * idet[2], (dir | tvec1[3]) * idet[3], }; f32x4b test[4] = { Min(u[0], v[0]) >= zero && u[0] + v[0] <= one, Min(u[1], v[1]) >= zero && u[1] + v[1] <= one, Min(u[2], v[2]) >= zero && u[2] + v[2] <= one, Min(u[3], v[3]) >= zero && u[3] + v[3] <= one, }; test[0] = test[0] && /*idet[0] > zero &&*/ dist[0] >= zero; test[1] = test[1] && /*idet[1] > zero &&*/ dist[1] >= zero; test[2] = test[2] && /*idet[2] > zero &&*/ dist[2] >= zero; test[3] = test[3] && /*idet[3] > zero &&*/ dist[3] >= zero; f32x4 minDist = distance; minDist = Condition(test[0] && dist[0] < minDist, dist[0], minDist); minDist = Condition(test[1] && dist[1] < minDist, dist[1], minDist); minDist = Condition(test[2] && dist[2] < minDist, dist[2], minDist); minDist = Condition(test[3] && dist[3] < minDist, dist[3], minDist); test[0] = test[0] && dist[0] <= minDist; test[1] = test[1] && dist[1] <= minDist; test[2] = test[2] && dist[2] <= minDist; test[3] = test[3] && dist[3] <= minDist; ctx.distance[q] = minDist; ctx.normals[q] = Condition(test[0], tnrm[0], Condition(test[1], tnrm[1], Condition(test[2], tnrm[2], Condition(test[3], tnrm[3], normals)))); ctx.triIds[q] = Condition(i32x4b(test[0]), idx[0], Condition(i32x4b(test[1]), idx[1], Condition(i32x4b(test[2]), idx[2], Condition(i32x4b(test[3]), idx[3], triIds)))); ctx.barycentric[q] = Condition(test[0], Vec2q(u[0], v[0]), Condition(test[1], Vec2q(u[1], v[1]), Condition(test[2], Vec2q(u[2], v[2]), Condition(test[3], Vec2q(u[3], v[3]), barycentric)))); } }