public Color getRadiance(ShadingState state) { // don't use these - gather lights for sphere of directions // gather lights state.initLightSamples(); state.initCausticSamples(); Vector3 v = state.getRay().getDirection(); v.negate(); Vector3 h = new Vector3(); Vector3 t = state.getBasis().transform(new Vector3(0, 1, 0)); Color diff = Color.black(); Color spec = Color.black(); foreach (LightSample ls in state) { Vector3 l = ls.getShadowRay().getDirection(); float dotTL = Vector3.dot(t, l); float sinTL = (float)Math.Sqrt(1 - dotTL * dotTL); // float dotVL = Vector3.dot(v, l); diff.madd(sinTL, ls.getDiffuseRadiance()); Vector3.add(v, l, h); h.normalize(); float dotTH = Vector3.dot(t, h); float sinTH = (float)Math.Sqrt(1 - dotTH * dotTH); float s = (float)Math.Pow(sinTH, 10.0f); spec.madd(s, ls.getSpecularRadiance()); } Color c = Color.add(diff, spec, new Color()); // transparency return Color.blend(c, state.traceTransparency(), state.getV(), new Color()); }
public static OrthoNormalBasis makeFromW(Vector3 w) { OrthoNormalBasis onb = new OrthoNormalBasis(); w.normalize(onb.w); if ((Math.Abs(onb.w.x) < Math.Abs(onb.w.y)) && (Math.Abs(onb.w.x) < Math.Abs(onb.w.z))) { onb.v.x = 0; onb.v.y = onb.w.z; onb.v.z = -onb.w.y; } else if (Math.Abs(onb.w.y) < Math.Abs(onb.w.z)) { onb.v.x = onb.w.z; onb.v.y = 0; onb.v.z = -onb.w.x; } else { onb.v.x = onb.w.y; onb.v.y = -onb.w.x; onb.v.z = 0; } Vector3.cross(onb.v.normalize(), onb.w, onb.u); return onb; }
public static OrthoNormalBasis makeFromWV(Vector3 w, Vector3 v) { OrthoNormalBasis onb = new OrthoNormalBasis(); w.normalize(onb.w); Vector3.cross(v, onb.w, onb.u).normalize(); Vector3.cross(onb.w, onb.u, onb.v); return onb; }
public DirectionalSpotlight() { src = new Point3(0, 0, 0); dir = new Vector3(0, 0, -1); dir.normalize(); basis = OrthoNormalBasis.makeFromW(dir); r = 1; r2 = r * r; radiance = Color.WHITE; }
public Quaternion(float theta, float x, float y, float z) { data = new float[4]; float sinAngle; theta = theta * 0.5f; Vector3 vn = new Vector3(x, y, z); vn.normalize(); sinAngle = (float)Math.Sin(theta); data[1] = (vn.X * sinAngle); data[2] = (vn.Y * sinAngle); data[3] = (vn.Z * sinAngle); data[0] = (float)Math.Cos(theta); }
public bool update(ParameterList pl, SunflowAPI api) { src = pl.getPoint("source", src); dir = pl.getVector("dir", dir); dir.normalize(); r = pl.getFloat("radius", r); basis = OrthoNormalBasis.makeFromW(dir); r2 = r * r; radiance = pl.getColor("radiance", radiance); return true; }
protected void configBillboard(Vector3 position) { //Shamelessly stolen from http://www.lighthouse3d.com/opengl/billboarding/index.php?billSphe if (LockType == BillboardLockType.Cylindrical || LockType == BillboardLockType.Spherical) { float[] modelView = new float[16]; Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glPushMatrix(); //Gl.glLoadIdentity(); Vector3 camera = CameraManager.Current.position; Vector3 difference = new Vector3(camera.X - position.X, 0, camera.Z - position.Z); Vector3 lookAt = new Vector3(0,0,1); difference.normalize(); Vector3 up = lookAt.cross(difference); float angleCosine = lookAt.dot(difference); if ((angleCosine < 0.99990) && (angleCosine > -0.9999)) Gl.glRotatef((float)(Math.Acos(angleCosine) * 180/Math.PI), up.X, up.Y, up.Z); if (LockType == BillboardLockType.Spherical) { Vector3 difference3d = camera - position; difference3d.normalize(); angleCosine = difference3d.dot(difference); if ((angleCosine < 0.99990) && (angleCosine > -0.9999)) { if (difference3d.Y < 0) Gl.glRotatef((float)(Math.Acos(angleCosine) * 180 / Math.PI), 1, 0, 0); else Gl.glRotatef((float)(Math.Acos(angleCosine) * 180 / Math.PI), -1, 0, 0); } } } }
private void initSunSky() { // perform all the required initialization of constants sunDirWorld.normalize(); sunDir = basis.untransform(sunDirWorld, new Vector3()); sunDir.normalize(); sunTheta = (float)Math.Acos(MathUtils.clamp(sunDir.z, -1, 1)); if (sunDir.z > 0) { sunSpectralRadiance = computeAttenuatedSunlight(sunTheta, turbidity); // produce color suitable for rendering sunColor = RGBSpace.SRGB.convertXYZtoRGB(sunSpectralRadiance.toXYZ().mul(1e-4f)).constrainRGB(); } else { sunSpectralRadiance = new ConstantSpectralCurve(0); } // sunSolidAngle = (float) (0.25 * Math.PI * 1.39 * 1.39 / (150 * 150)); float theta2 = sunTheta * sunTheta; float theta3 = sunTheta * theta2; float T = turbidity; float T2 = turbidity * turbidity; double chi = (4.0 / 9.0 - T / 120.0) * (Math.PI - 2.0 * sunTheta); zenithY = (4.0453 * T - 4.9710) * Math.Tan(chi) - 0.2155 * T + 2.4192; zenithY *= 1000; /* conversion from kcd/m^2 to cd/m^2 */ zenithx = (0.00165 * theta3 - 0.00374 * theta2 + 0.00208 * sunTheta + 0) * T2 + (-0.02902 * theta3 + 0.06377 * theta2 - 0.03202 * sunTheta + 0.00394) * T + (0.11693 * theta3 - 0.21196 * theta2 + 0.06052 * sunTheta + 0.25885); zenithy = (0.00275 * theta3 - 0.00610 * theta2 + 0.00316 * sunTheta + 0) * T2 + (-0.04212 * theta3 + 0.08970 * theta2 - 0.04153 * sunTheta + 0.00515) * T + (0.15346 * theta3 - 0.26756 * theta2 + 0.06669 * sunTheta + 0.26688); perezY[0] = 0.17872 * T - 1.46303; perezY[1] = -0.35540 * T + 0.42749; perezY[2] = -0.02266 * T + 5.32505; perezY[3] = 0.12064 * T - 2.57705; perezY[4] = -0.06696 * T + 0.37027; perezx[0] = -0.01925 * T - 0.25922; perezx[1] = -0.06651 * T + 0.00081; perezx[2] = -0.00041 * T + 0.21247; perezx[3] = -0.06409 * T - 0.89887; perezx[4] = -0.00325 * T + 0.04517; perezy[0] = -0.01669 * T - 0.26078; perezy[1] = -0.09495 * T + 0.00921; perezy[2] = -0.00792 * T + 0.21023; perezy[3] = -0.04405 * T - 1.65369; perezy[4] = -0.01092 * T + 0.05291; int w = 32, h = 32; imageHistogram = new float[w][]; for (int i = 0; i < imageHistogram.Length; i++) imageHistogram[i] = new float[h]; colHistogram = new float[w]; float du = 1.0f / w; float dv = 1.0f / h; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { float u = (x + 0.5f) * du; float v = (y + 0.5f) * dv; Color c = getSkyRGB(getDirection(u, v)); imageHistogram[x][y] = c.getLuminance() * (float)Math.Sin(Math.PI * v); if (y > 0) imageHistogram[x][y] += imageHistogram[x][y - 1]; } colHistogram[x] = imageHistogram[x][h - 1]; if (x > 0) colHistogram[x] += colHistogram[x - 1]; for (int y = 0; y < h; y++) imageHistogram[x][y] /= imageHistogram[x][h - 1]; } for (int x = 0; x < w; x++) colHistogram[x] /= colHistogram[w - 1]; jacobian = (float)(2 * Math.PI * Math.PI) / (w * h); }
private Color getSkyRGB(Vector3 dir) { if (dir.z < 0) return Color.BLACK; if (dir.z < 0.001f) dir.z = 0.001f; dir.normalize(); double theta = Math.Acos(MathUtils.clamp(dir.z, -1, 1)); double gamma = Math.Acos(MathUtils.clamp(Vector3.dot(dir, sunDir), -1, 1)); double x = perezFunction(perezx, theta, gamma, zenithx); double y = perezFunction(perezy, theta, gamma, zenithy); double Y = perezFunction(perezY, theta, gamma, zenithY) * 1e-4; XYZColor c = ChromaticitySpectrum.get((float)x, (float)y); // XYZColor c = new ChromaticitySpectrum((float) x, (float) y).toXYZ(); float X = (float)(c.getX() * Y / c.getY()); float Z = (float)(c.getZ() * Y / c.getY()); return RGBSpace.SRGB.convertXYZtoRGB(X, (float)Y, Z); }
private Vector3 getTangent(int line, int v0, float v) { Vector3 vcurr = new Vector3(points[v0 + 3] - points[v0 + 0], points[v0 + 4] - points[v0 + 1], points[v0 + 5] - points[v0 + 2]); vcurr.normalize(); if (line == 0 || line == numSegments - 1) return vcurr; if (v <= 0.5f) { // get previous segment Vector3 vprev = new Vector3(points[v0 + 0] - points[v0 - 3], points[v0 + 1] - points[v0 - 2], points[v0 + 2] - points[v0 - 1]); vprev.normalize(); float t = v + 0.5f; float s = 1 - t; float vx = vprev.x * s + vcurr.x * t; float vy = vprev.y * s + vcurr.y * t; float vz = vprev.z * s + vcurr.z * t; return new Vector3(vx, vy, vz); } else { // get next segment v0 += 3; Vector3 vnext = new Vector3(points[v0 + 3] - points[v0 + 0], points[v0 + 4] - points[v0 + 1], points[v0 + 5] - points[v0 + 2]); vnext.normalize(); float t = 1.5f - v; float s = 1 - t; float vx = vnext.x * s + vcurr.x * t; float vy = vnext.y * s + vcurr.y * t; float vz = vnext.z * s + vcurr.z * t; return new Vector3(vx, vy, vz); } }
public Vector3 transformVector(Vector3 vec) { Vector3 vn = new Vector3(ref vec); vn.normalize(); Quaternion vecQuat = new Quaternion(); Quaternion resQuat = new Quaternion(); vecQuat.X = vn.X; vecQuat.Y = vn.Y; vecQuat.Z = vn.Z; vecQuat.W = 0.0f; resQuat = vecQuat * conjugate(); resQuat = this * resQuat; return new Vector3(resQuat.X, resQuat.Y, resQuat.Z); }
public TriangleLight(int tri, TriangleMeshLight meshlight) { tri3 = 3 * tri; this.meshlight = meshlight; int a = meshlight.triangles[tri3 + 0]; int b = meshlight.triangles[tri3 + 1]; int c = meshlight.triangles[tri3 + 2]; Point3 v0p = meshlight.getPoint(a); Point3 v1p = meshlight.getPoint(b); Point3 v2p = meshlight.getPoint(c); ng = Point3.normal(v0p, v1p, v2p); area = 0.5f * ng.Length(); ng.normalize(); }
public void getSamples(ShadingState state) { if (meshlight.numSamples == 0) return; Vector3 n = state.getNormal(); Point3 p = state.getPoint(); // vector towards each vertex of the light source Vector3 p0 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 0]), p, new Vector3()); // cull triangle if it is facing the wrong way if (Vector3.dot(p0, ng) >= 0) return; Vector3 p1 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 1]), p, new Vector3()); Vector3 p2 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 2]), p, new Vector3()); // if all three vertices are below the hemisphere, stop if (Vector3.dot(p0, n) <= 0 && Vector3.dot(p1, n) <= 0 && Vector3.dot(p2, n) <= 0) return; p0.normalize(); p1.normalize(); p2.normalize(); float dot = Vector3.dot(p2, p0); Vector3 h = new Vector3(); h.x = p2.x - dot * p0.x; h.y = p2.y - dot * p0.y; h.z = p2.z - dot * p0.z; float hlen = h.Length(); if (hlen > 1e-6f) h.div(hlen); else return; Vector3 n0 = Vector3.cross(p0, p1, new Vector3()); float len0 = n0.Length(); if (len0 > 1e-6f) n0.div(len0); else return; Vector3 n1 = Vector3.cross(p1, p2, new Vector3()); float len1 = n1.Length(); if (len1 > 1e-6f) n1.div(len1); else return; Vector3 n2 = Vector3.cross(p2, p0, new Vector3()); float len2 = n2.Length(); if (len2 > 1e-6f) n2.div(len2); else return; float cosAlpha = MathUtils.clamp(-Vector3.dot(n2, n0), -1.0f, 1.0f); float cosBeta = MathUtils.clamp(-Vector3.dot(n0, n1), -1.0f, 1.0f); float cosGamma = MathUtils.clamp(-Vector3.dot(n1, n2), -1.0f, 1.0f); float alpha = (float)Math.Acos(cosAlpha); float beta = (float)Math.Acos(cosBeta); float gamma = (float)Math.Acos(cosGamma); float area = alpha + beta + gamma - (float)Math.PI; float cosC = MathUtils.clamp(Vector3.dot(p0, p1), -1.0f, 1.0f); float salpha = (float)Math.Sin(alpha); float product = salpha * cosC; // use lower sampling depth for diffuse bounces int samples = state.getDiffuseDepth() > 0 ? 1 : meshlight.numSamples; Color c = Color.mul(area / samples, meshlight.radiance); for (int i = 0; i < samples; i++) { // random offset on unit square double randX = state.getRandom(i, 0, samples); double randY = state.getRandom(i, 1, samples); float phi = (float)randX * area - alpha + (float)Math.PI; float sinPhi = (float)Math.Sin(phi); float cosPhi = (float)Math.Cos(phi); float u = cosPhi + cosAlpha; float v = sinPhi - product; float q = (-v + cosAlpha * (cosPhi * -v + sinPhi * u)) / (salpha * (sinPhi * -v - cosPhi * u)); float q1 = 1.0f - q * q; if (q1 < 0.0f) q1 = 0.0f; float sqrtq1 = (float)Math.Sqrt(q1); float ncx = q * p0.x + sqrtq1 * h.x; float ncy = q * p0.y + sqrtq1 * h.y; float ncz = q * p0.z + sqrtq1 * h.z; dot = p1.dot(ncx, ncy, ncz); float z = 1.0f - (float)randY * (1.0f - dot); float z1 = 1.0f - z * z; if (z1 < 0.0f) z1 = 0.0f; Vector3 nd = new Vector3(); nd.x = ncx - dot * p1.x; nd.y = ncy - dot * p1.y; nd.z = ncz - dot * p1.z; nd.normalize(); float sqrtz1 = (float)Math.Sqrt(z1); Vector3 result = new Vector3(); result.x = z * p1.x + sqrtz1 * nd.x; result.y = z * p1.y + sqrtz1 * nd.y; result.z = z * p1.z + sqrtz1 * nd.z; // make sure the sample is in the right hemisphere - facing in // the right direction if (Vector3.dot(result, n) > 0 && Vector3.dot(result, state.getGeoNormal()) > 0 && Vector3.dot(result, ng) < 0) { // compute intersection with triangle (if any) Ray shadowRay = new Ray(state.getPoint(), result); if (!intersectTriangleKensler(shadowRay)) continue; LightSample dest = new LightSample(); dest.setShadowRay(shadowRay); // prepare sample dest.setRadiance(c, c); dest.traceShadow(state); state.addSample(dest); } } }
private void GenerateMesh() { List<Vector3> curvePoints = new List<Vector3>(); List<Vector3> attractorPoints = new List<Vector3>(); pipeFunction.InitParameters(); attractorPoints.Capacity = pipeSegments + 2; curvePoints.Capacity = (pipeSegments + 2) * 5; int circleSegments = outlinePoints.Count; float tDelta = 1.0f/(knotsPerPipeSegment-1); attractorPoints.Add(startPosition); Vector3 temp = new Vector3(); temp.set(startPosition); // Console.WriteLine(temp); for (int i =0; i<pipeSegments; i++) { pipeFunction.GetNextPosition(temp); Vector3 nextPoint = new Vector3(); nextPoint.set(temp); attractorPoints.Add(nextPoint); } // special case the first point int lastItem = attractorPoints.Count-1; if (attractorPoints[0] == attractorPoints[lastItem]) { // it loops attractorPoints.RemoveAt(lastItem); Vector3 tmp = attractorPoints[ attractorPoints.Count-1]; attractorPoints.Add(attractorPoints[0]); attractorPoints.Insert(0, tmp); } else { // it does not loop Vector3 diff= new Vector3(); diff = Vector3.sub(attractorPoints[0], attractorPoints[1], diff); diff = Vector3.add(attractorPoints[0], diff, diff); Vector3 diff2 = new Vector3(); diff2 = Vector3.sub(attractorPoints[lastItem], attractorPoints[lastItem-1], diff2); diff2 = Vector3.add(attractorPoints[lastItem], diff2, diff2); attractorPoints.Add(diff2); attractorPoints.Insert(0,diff ); } // Console.WriteLine("attractorPoints: {0}" , attractorPoints.Count); int knotIndex = 0; Vector3 splinePoint = new Vector3(); Vector3 tangent = new Vector3(); Vector3 normal = new Vector3(); Vector3 up = new Vector3(0f,1f,0f); Point3 zero = new Point3(0f,0f,0f); Point3 tangentAsPoint = new Point3(); Point3 rotatedPoint = new Point3(); Vector3 oldSplinePoint = new Vector3(); Matrix4 rotateToTangent ; int pipeIndex = 0; int quadIndex = 0; int circleIndex = 0; int normalIndex = 0; float t=0f; foreach ( Vector3 tempv in attractorPoints) { tempv.mul(4.0f); // Console.WriteLine("attractorPoint: {0}" , tempv); } oldSplinePoint.set(attractorPoints[0]); for (int i=0; i<=pipeSegments-1; i++) { while (t <= 1.0f) { // Console.WriteLine("t : {0}", t); // Console.WriteLine("knotIndex : {0} - {1}", knotIndex, knotIndex+3); // Console.WriteLine(attractorPoints[knotIndex+1]); // Console.WriteLine(attractorPoints[knotIndex+2]); splinePoint.x = CatmullRomSpline(t, attractorPoints[knotIndex].x, attractorPoints[knotIndex+1].x, attractorPoints[knotIndex+2].x, attractorPoints[knotIndex+3].x); splinePoint.y = CatmullRomSpline(t, attractorPoints[knotIndex].y, attractorPoints[knotIndex+1].y, attractorPoints[knotIndex+2].y, attractorPoints[knotIndex+3].y); splinePoint.z = CatmullRomSpline(t, attractorPoints[knotIndex].z, attractorPoints[knotIndex+1].z, attractorPoints[knotIndex+2].z, attractorPoints[knotIndex+3].z); t += tDelta; tangent = Vector3.sub(splinePoint, oldSplinePoint, tangent).normalize(); tangentAsPoint.set(tangent.x, tangent.y, tangent.z); // normal = Vector3.cross(tangent,up,normal).normalize(); oldSplinePoint.set(splinePoint); // Matrix4 rotateAlongTangent = Matrix4.rotate(tangent.x, tangent.y, tangent.z, (float)thetaDelta); rotateToTangent = Matrix4.lookAt(zero, tangentAsPoint, up);//.inverse(); if(circleIndex == 0) { for (int circleSegement = 0; circleSegement < circleSegments ; circleSegement++) { // pointOnOutline.set (pipeRadius * (float)Math.Cos(theta) ,pipeRadius * (float)Math.Sin(theta), 0f); rotatedPoint = rotateToTangent.transformP(outlinePoints[circleSegement]); bb.include(rotatedPoint.x + splinePoint.x, rotatedPoint.y + splinePoint.y, rotatedPoint.z + splinePoint.z); points[pipeIndex++] = rotatedPoint.x + splinePoint.x; points[pipeIndex++] = rotatedPoint.y + splinePoint.y; points[pipeIndex++] = rotatedPoint.z + splinePoint.z; if(smooth) { normal.x = rotatedPoint.x; normal.y = rotatedPoint.y; normal.z = rotatedPoint.z; normal.normalize(); normals.data[normalIndex++] = normal.x; normals.data[normalIndex++] = normal.y; normals.data[normalIndex++] = normal.z; } } } else { // go round it a circle. int circleSegement; for (circleSegement = 0; circleSegement < circleSegments; circleSegement++) { rotatedPoint = rotateToTangent.transformP(outlinePoints[circleSegement]); bb.include(rotatedPoint.x + splinePoint.x, rotatedPoint.y + splinePoint.y, rotatedPoint.z + splinePoint.z); points[pipeIndex++] = rotatedPoint.x + splinePoint.x; points[pipeIndex++] = rotatedPoint.y + splinePoint.y; points[pipeIndex++] = rotatedPoint.z + splinePoint.z; if(smooth) { normal.x = rotatedPoint.x; normal.y = rotatedPoint.y; normal.z = rotatedPoint.z; normal.normalize(); normals.data[normalIndex++] = normal.x; normals.data[normalIndex++] = normal.y; normals.data[normalIndex++] = normal.z; } if (circleSegement + 1 < circleSegments) { // Console.WriteLine("quadIndex : {0}", quadIndex); quads[quadIndex++] = circleSegement + ((circleIndex - 1) * circleSegments) ; quads[quadIndex++] = circleSegement + ((circleIndex - 1) * circleSegments) + 1; quads[quadIndex++] = circleSegement + (circleIndex * circleSegments) + 1; quads[quadIndex++] = circleSegement + (circleIndex * circleSegments); } } // joint it back to first points quads[quadIndex++] = (circleSegement - 1) + ((circleIndex - 1) * circleSegments) ; quads[quadIndex++] = ((circleIndex - 1) * circleSegments); quads[quadIndex++] = (circleIndex * circleSegments) ; quads[quadIndex++] = (circleSegement - 1) + (circleIndex * circleSegments); } circleIndex++; } t = tDelta; knotIndex++; } }
public bool Update(ParameterList pl, SunflowAPI api) { level = MathUtils.clamp(pl.getInt("level", level), 0, 20); axis = pl.getVector("axis", axis); axis.normalize(); baseRadius = Math.Abs(pl.getFloat("radius", baseRadius)); return true; }
private TriangleMesh generate(int[] tris, float[] verts, bool smoothNormals) { ParameterList pl = new ParameterList(); pl.addIntegerArray("triangles", tris); pl.addPoints("points", ParameterList.InterpolationType.VERTEX, verts); if (smoothNormals) { float[] normals = new float[verts.Length]; // filled with 0's Point3 p0 = new Point3(); Point3 p1 = new Point3(); Point3 p2 = new Point3(); Vector3 n = new Vector3(); for (int i3 = 0; i3 < tris.Length; i3 += 3) { int v0 = tris[i3 + 0]; int v1 = tris[i3 + 1]; int v2 = tris[i3 + 2]; p0.set(verts[3 * v0 + 0], verts[3 * v0 + 1], verts[3 * v0 + 2]); p1.set(verts[3 * v1 + 0], verts[3 * v1 + 1], verts[3 * v1 + 2]); p2.set(verts[3 * v2 + 0], verts[3 * v2 + 1], verts[3 * v2 + 2]); Point3.normal(p0, p1, p2, n); // compute normal // add face normal to each vertex // note that these are not normalized so this in fact weights // each normal by the area of the triangle normals[3 * v0 + 0] += n.x; normals[3 * v0 + 1] += n.y; normals[3 * v0 + 2] += n.z; normals[3 * v1 + 0] += n.x; normals[3 * v1 + 1] += n.y; normals[3 * v1 + 2] += n.z; normals[3 * v2 + 0] += n.x; normals[3 * v2 + 1] += n.y; normals[3 * v2 + 2] += n.z; } // normalize all the vectors for (int i3 = 0; i3 < normals.Length; i3 += 3) { n.set(normals[i3 + 0], normals[i3 + 1], normals[i3 + 2]); n.normalize(); normals[i3 + 0] = n.x; normals[i3 + 1] = n.y; normals[i3 + 2] = n.z; } pl.addVectors("normals", ParameterList.InterpolationType.VERTEX, normals); } TriangleMesh m = new TriangleMesh(); if (m.update(pl, null)) return m; // something failed in creating the mesh, the error message will be // printed by the mesh itself - no need to repeat it here return null; }
public void prepareShadingState(ShadingState state) { state.init(); Instance parent = state.getInstance(); int primID = state.getPrimitiveID(); float u = state.getU(); float v = state.getV(); state.getRay().getPoint(state.getPoint()); int quad = 4 * primID; int index0 = quads[quad + 0]; int index1 = quads[quad + 1]; int index2 = quads[quad + 2]; int index3 = quads[quad + 3]; Point3 v0p = getPoint(index0); Point3 v1p = getPoint(index1); Point3 v2p = getPoint(index2); Point3 v3p = getPoint(index3); float tanux = (1 - v) * (v1p.x - v0p.x) + v * (v2p.x - v3p.x); float tanuy = (1 - v) * (v1p.y - v0p.y) + v * (v2p.y - v3p.y); float tanuz = (1 - v) * (v1p.z - v0p.z) + v * (v2p.z - v3p.z); float tanvx = (1 - u) * (v3p.x - v0p.x) + u * (v2p.x - v1p.x); float tanvy = (1 - u) * (v3p.y - v0p.y) + u * (v2p.y - v1p.y); float tanvz = (1 - u) * (v3p.z - v0p.z) + u * (v2p.z - v1p.z); float nx = tanuy * tanvz - tanuz * tanvy; float ny = tanuz * tanvx - tanux * tanvz; float nz = tanux * tanvy - tanuy * tanvx; Vector3 ng = new Vector3(nx, ny, nz); ng = state.transformNormalObjectToWorld(ng); ng.normalize(); state.getGeoNormal().set(ng); float k00 = (1 - u) * (1 - v); float k10 = u * (1 - v); float k01 = (1 - u) * v; float k11 = u * v; switch (normals.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getNormal().set(ng); break; } case ParameterList.InterpolationType.VERTEX: { int i30 = 3 * index0; int i31 = 3 * index1; int i32 = 3 * index2; int i33 = 3 * index3; float[] normals1 = this.normals.data; state.getNormal().x = k00 * normals1[i30 + 0] + k10 * normals1[i31 + 0] + k11 * normals1[i32 + 0] + k01 * normals1[i33 + 0]; state.getNormal().y = k00 * normals1[i30 + 1] + k10 * normals1[i31 + 1] + k11 * normals1[i32 + 1] + k01 * normals1[i33 + 1]; state.getNormal().z = k00 * normals1[i30 + 2] + k10 * normals1[i31 + 2] + k11 * normals1[i32 + 2] + k01 * normals1[i33 + 2]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * quad; float[] normals1 = this.normals.data; state.getNormal().x = k00 * normals1[idx + 0] + k10 * normals1[idx + 3] + k11 * normals1[idx + 6] + k01 * normals1[idx + 9]; state.getNormal().y = k00 * normals1[idx + 1] + k10 * normals1[idx + 4] + k11 * normals1[idx + 7] + k01 * normals1[idx + 10]; state.getNormal().z = k00 * normals1[idx + 2] + k10 * normals1[idx + 5] + k11 * normals1[idx + 8] + k01 * normals1[idx + 11]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } } float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0, uv30 = 0, uv31 = 0; switch (uvs.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getUV().x = 0; state.getUV().y = 0; break; } case ParameterList.InterpolationType.VERTEX: { int i20 = 2 * index0; int i21 = 2 * index1; int i22 = 2 * index2; int i23 = 2 * index3; float[] uvs1 = this.uvs.data; uv00 = uvs1[i20 + 0]; uv01 = uvs1[i20 + 1]; uv10 = uvs1[i21 + 0]; uv11 = uvs1[i21 + 1]; uv20 = uvs1[i22 + 0]; uv21 = uvs1[i22 + 1]; uv20 = uvs1[i23 + 0]; uv21 = uvs1[i23 + 1]; break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = quad << 1; float[] uvs1 = this.uvs.data; uv00 = uvs1[idx + 0]; uv01 = uvs1[idx + 1]; uv10 = uvs1[idx + 2]; uv11 = uvs1[idx + 3]; uv20 = uvs1[idx + 4]; uv21 = uvs1[idx + 5]; uv30 = uvs1[idx + 6]; uv31 = uvs1[idx + 7]; break; } } if (uvs.interp != ParameterList.InterpolationType.NONE) { // get exact uv coords and compute tangent vectors state.getUV().x = k00 * uv00 + k10 * uv10 + k11 * uv20 + k01 * uv30; state.getUV().y = k00 * uv01 + k10 * uv11 + k11 * uv21 + k01 * uv31; float du1 = uv00 - uv20; float du2 = uv10 - uv20; float dv1 = uv01 - uv21; float dv2 = uv11 - uv21; Vector3 dp1 = Point3.sub(v0p, v2p, new Vector3()), dp2 = Point3.sub(v1p, v2p, new Vector3()); float determinant = du1 * dv2 - dv1 * du2; if (determinant == 0.0f) { // create basis in world space state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); } else { float invdet = 1 / determinant; // Vector3 dpdu = new Vector3(); // dpdu.x = (dv2 * dp1.x - dv1 * dp2.x) * invdet; // dpdu.y = (dv2 * dp1.y - dv1 * dp2.y) * invdet; // dpdu.z = (dv2 * dp1.z - dv1 * dp2.z) * invdet; Vector3 dpdv = new Vector3(); dpdv.x = (-du2 * dp1.x + du1 * dp2.x) * invdet; dpdv.y = (-du2 * dp1.y + du1 * dp2.y) * invdet; dpdv.z = (-du2 * dp1.z + du1 * dp2.z) * invdet; dpdv = state.transformVectorObjectToWorld(dpdv); // create basis in world space state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), dpdv)); } } else state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); int shaderIndex = faceShaders == null ? 0 : (faceShaders[primID] & 0xFF); state.setShader(parent.getShader(shaderIndex)); state.setModifier(parent.getModifier(shaderIndex)); }