public static void showVector3(CVector3D v) { if (isEnable) { Console.WriteLine("[{0},{1},{2},{3}]", v.x, v.y, v.z, v.w); } }
/// <summary> /// 背面消隐 /// </summary> /// <returns>是否通关背面消隐测试</returns> private bool BackFaceCulling(CVertex p1, CVertex p2, CVertex p3) { CVector3D v1 = p2.point - p1.point; CVector3D v2 = p3.point - p2.point; CVector3D normal = CVector3D.Cross(v1, v2); CVector3D viewDir = new CVector3D(0, 0, 1);//由于在剪裁空间,视线一点是这个方向 if (CVector3D.Dot(normal, viewDir) > 0) { return(true); } return(false); }
public SFoliageInstance(CR2WFile cr2w) : base(cr2w) { position = new CVector3D(cr2w) { Name = "Position" }; Yaw = new CFloat(cr2w); Yaw.Name = "Yaw"; Pitch = new CFloat(cr2w); Pitch.Name = "Pitch"; Roll = new CFloat(cr2w); Roll.Name = "Roll"; }
public static void Test() { CVector3D a = new CVector3D(1, 2, 1, 1); CVector3D b = new CVector3D(5, 6, 0, 1); CVector3D c = new CVector3D(1, 2, 3, 1); float r1 = CVector3D.Dot(a, b); CVector3D r2 = a - b; CVector3D r3 = CVector3D.Cross(a, b); Console.WriteLine("a dot b:{0}", r1); Console.WriteLine("a - b:({0},{1},{2},{3})", r2.x, r2.y, r2.z, r2.w); Console.WriteLine("a X b:({0},{1},{2},{3})", r3.x, r3.y, r3.z, r3.w); // CMatrix4x4 mat1 = new CMatrix4x4(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 0, 0, 0, 1); CMatrix4x4 mat2 = new CMatrix4x4(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4); CMatrix4x4 mat3 = new CMatrix4x4(); mat3.Identity(); CMatrix4x4 mat4 = new CMatrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1); CMatrix4x4 matr1 = mat1 * mat3; Console.WriteLine("mat1 * mat3:"); showMat(matr1); CMatrix4x4 matr2 = mat1 * mat2; Console.WriteLine("mat1 * mat2:"); showMat(matr2); CVector3D r4 = a * mat1; Console.WriteLine("a * mat1:({0},{1},{2},{3})", r4.x, r4.y, r4.z, r4.w); CVector3D r5 = a * mat4; Console.WriteLine("a * mat4:({0},{1},{2},{3})", r5.x, r5.y, r5.z, r5.w); }
/// <summary> /// Glut:マウスクリックイベントハンドラ /// </summary> /// <param name="button"></param> /// <param name="state"></param> /// <param name="x"></param> /// <param name="y"></param> private void myGlutMouse(int button, int state, int x, int y) { int[] viewport = new int[4]; Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport); int winW = viewport[2]; int winH = viewport[3]; MovBeginX = (2.0 * x - winW) / winW; MovBeginY = (winH - 2.0 * y) / winH; Modifier = Glut.glutGetModifiers(); if (state == Glut.GLUT_DOWN && Modifier == 0) { int sizeBuffer = 2048; DelFEM4NetCom.View.DrawerGlUtility.PickSelectBuffer pickSelectBuffer = null; DelFEM4NetCom.View.DrawerGlUtility.PickPre((uint)sizeBuffer, out pickSelectBuffer, (uint)x, (uint)y, 5, 5, Camera); DrawerAry.DrawSelection(); List<DelFEM4NetCom.View.SSelectedObject> aSelecObj = (List<DelFEM4NetCom.View.SSelectedObject>)DelFEM4NetCom.View.DrawerGlUtility.PickPost(pickSelectBuffer, (uint)x, (uint)y, Camera); /* uint[] select_buffer = pickSelectBuffer.ToArray(); foreach (uint buf in select_buffer) { Console.Write("[" + buf + "]"); } Console.WriteLine(); */ DrawerAry.ClearSelected(); if (aSelecObj.Count > 0) { DrawerAry.AddSelected(aSelecObj[0].name); if( aSelecObj[0].name[1] == 3 ) { Id_loop_selected = (uint)aSelecObj[0].name[2]; PickedPos = aSelecObj[0].picked_pos; } else { Id_loop_selected = 0; } MousePos = PickedPos; } } if(state == Glut.GLUT_UP && Modifier == 0) { if( ModeDv == 1 ) { if( Cad3D.IsElemID(CAD_ELEM_TYPE.LOOP, Id_loop_selected) ) { CLoop3D l = Cad3D.GetLoop(Id_loop_selected); CVector2D v0 = l.Project(PickedPos); CVector2D v1 = l.Project(MousePos); Cad3D.AddRectLoop(Id_loop_selected,v0,v1); Id_loop_selected = 0; DrawerAry.Clear(); DrawerAry.PushBack( new CDrawer_Cad3D(Cad3D) ); } } } }
/// <summary> /// Glut:マウスドラッグ(マウスボタンを押したまま移動)イベントハンドラ /// </summary> /// <param name="x"></param> /// <param name="y"></param> private void myGlutMotion(int x, int y) { int[] viewport = new int[4]; Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport); int winW = viewport[2]; int winH = viewport[3]; double movEndX = (2.0 * x - winW) / winW; double movEndY = (winH - 2.0 * y) / winH; if ((Modifier & Glut.GLUT_ACTIVE_CTRL) == Glut.GLUT_ACTIVE_CTRL) { Camera.MouseRotation(MovBeginX, MovBeginY, movEndX, movEndY); } else if ((Modifier & Glut.GLUT_ACTIVE_SHIFT) == Glut.GLUT_ACTIVE_SHIFT) { Camera.MousePan(MovBeginX, MovBeginY, movEndX, movEndY); } else if( Cad3D.IsElemID(CAD_ELEM_TYPE.LOOP, Id_loop_selected) ) { if( ModeDv == 0 ) { if( Cad3D.IsElemID(CAD_ELEM_TYPE.LOOP, Id_loop_selected) ) { CVector3D nv = Cad3D.GetLoop(Id_loop_selected).normal; double[] n = new double[3] { nv.x, nv.y, nv.z }; double[] r = null;// new double[9]; Camera.RotMatrix33(out r); double[] nr = new double[3] { r[0] * n[0] + r[1] * n[1] + r[2] * n[2], r[3] * n[0] + r[4] * n[1] + r[5] * n[2], r[6] * n[0] + r[7] * n[1] + r[8] * n[2] }; double[] del = new double[2] { movEndX-MovBeginX, movEndY-MovBeginY }; nr[0] /= Camera.GetHalfViewHeight() * Camera.GetWindowAspect(); nr[1] /= Camera.GetHalfViewHeight(); if( nr[0]*nr[0] + nr[1]*nr[1] > 1.0e-5 ) { double rr = (nr[0] * del[0] + nr[1] * del[1]) / (nr[0] * nr[0] + nr[1] * nr[1]); Cad3D.LiftLoop(Id_loop_selected, nv * rr); DrawerAry.Clear(); DrawerAry.PushBack( new CDrawer_Cad3D(Cad3D) ); } } } else if( ModeDv == 1 ) { if( Cad3D.IsElemID(CAD_ELEM_TYPE.LOOP, Id_loop_selected) ) { CLoop3D l = Cad3D.GetLoop(Id_loop_selected); CVector3D o0 = l.org; CVector3D n0 = l.normal; CMatrix3 r = Camera.RotMatrix33(); CVector3D d1 = r.MatVecTrans( new CVector3D(0,0,-1) ); double hvh = Camera.GetHalfViewHeight(); double asp = Camera.GetWindowAspect(); CVector3D cp = - 1.0 * Camera.GetCenterPosition() + new CVector3D(hvh * asp * movEndX, hvh * movEndY, 0); CVector3D o1 = r.MatVecTrans( cp ) + Camera.GetObjectCenter(); double tmp0 = CVector3D.Dot(d1, n0); double tmp1 = CVector3D.Dot(o0 - o1, n0); double tmp2 = tmp1 / tmp0; MousePos = o1 + d1 * tmp2; } } } MovBeginX = movEndX; MovBeginY = movEndY; Glut.glutPostRedisplay(); }
/// <summary> /// Read animations and animbuffers data. /// </summary> public void SelectAnimation(CR2WFile animFile, int selectedAnimIdx) { // *************** READ ANIMATION DATA *************** if (animFile != null && selectedAnimIdx != -1) { positions.Clear(); positionsKeyframes.Clear(); orientations.Clear(); orientKeyframes.Clear(); scales.Clear(); scalesKeyframes.Clear(); foreach (var chunk in animFile.chunks) { if (chunk.Type == "CAnimationBufferBitwiseCompressed" && chunk.ChunkIndex == AnimationNames[selectedAnimIdx].Value) { uint numFrames = (chunk.GetVariableByName("numFrames") as CUInt32).val; float animDuration = (chunk.GetVariableByName("duration") as CFloat)?.val ?? 1.0f; animationSpeed = numFrames / animDuration; uint keyFrame = 0; byte[] data; var deferredData = chunk.GetVariableByName("deferredData") as CInt16; var streamingOption = (chunk.GetVariableByName("streamingOption") as CVariable); if (deferredData != null && deferredData.val != 0) { if (streamingOption != null && streamingOption.ToString() == "ABSO_PartiallyStreamable") { data = ConvertAnimation.Combine((chunk.GetVariableByName("data") as CByteArray).Bytes, File.ReadAllBytes(animFile.FileName + "." + deferredData.val + ".buffer")); } else { data = File.ReadAllBytes(animFile.FileName + "." + deferredData.val + ".buffer"); } } else { data = (chunk.GetVariableByName("data") as CByteArray).Bytes; } using (MemoryStream ms = new MemoryStream(data)) using (BinaryReader br = new BinaryReader(ms)) { foreach (CVector bone in (chunk.GetVariableByName("bones") as CArray).array) { List <uint> currkeyframe = new List <uint>(); List <Quaternion> currorient = new List <Quaternion>(); br.BaseStream.Position = ((bone.GetVariableByName("orientation") as CVector).GetVariableByName("dataAddr") as CUInt32).val; int orientNumFrames = ((bone.GetVariableByName("orientation") as CVector).GetVariableByName("numFrames") as CUInt16).val; for (uint idx = 0; idx < orientNumFrames; idx++) { keyFrame = idx; //keyFrame += numFrames; currkeyframe.Add(keyFrame); //bone.GetVariableByName("position"); string cm = chunk.GetVariableByName("orientationCompressionMethod")?.ToString() ?? ""; if (cm.Contains("ABOCM_PackIn48bitsW")) { byte[] odata = br.ReadBytes(6); ulong bits = (ulong)odata[0] << 40 | (ulong)odata[1] << 32 | (ulong)odata[2] << 24 | (ulong)odata[3] << 16 | (ulong)odata[4] << 8 | odata[5]; ushort[] orients = new ushort[4]; float[] quart = new float[4]; orients[0] = (ushort)((bits & 0x0000FFF000000000) >> 36); orients[1] = (ushort)((bits & 0x0000000FFF000000) >> 24); orients[2] = (ushort)((bits & 0x0000000000FFF000) >> 12); orients[3] = (ushort)((bits & 0x0000000000000FFF)); for (int i = 0; i < orients.Length; i++) { float fVal = (2047.0f - orients[i]) * (1 / 2048.0f); quart[i] = fVal; } quart[3] = -quart[3]; currorient.Add(new Quaternion(quart[0], quart[1], quart[2], quart[3])); } else { byte[] odata = br.ReadBytes(8); ushort[] plain = new ushort[4]; float[] quart = new float[4]; plain[0] = (ushort)BitConverter.ToInt16(odata, 0); plain[1] = (ushort)BitConverter.ToInt16(odata, 2); plain[2] = (ushort)BitConverter.ToInt16(odata, 4); plain[3] = (ushort)BitConverter.ToInt16(odata, 6); for (int i = 0; i < plain.Length; i++) { float fVal = (32767.0f - plain[i]) * (1 / 32768.0f); quart[i] = fVal; } quart[3] = -quart[3]; currorient.Add(new Quaternion(quart[0], quart[1], quart[2], quart[3])); } } orientKeyframes.Add(currkeyframe); orientations.Add(currorient); // TODO: Refactor List <Vector3Df> currposition = new List <Vector3Df>(); currkeyframe = new List <uint>(); int compression = 0; var compr = (bone.GetVariableByName("position") as CVector).GetVariableByName("compression") as CInt8; if (compr != null) { compression = compr.val; } var addr = (bone.GetVariableByName("position") as CVector).GetVariableByName("dataAddr") as CUInt32; if (addr != null) { br.BaseStream.Position = addr.val; } else { br.BaseStream.Position = 0; } var posNumFrames = ((bone.GetVariableByName("position") as CVector).GetVariableByName("numFrames") as CUInt16).val; for (uint idx = 0; idx < posNumFrames; idx++) { keyFrame = idx; //keyFrame += numFrames; currkeyframe.Add(keyFrame); var vec = new CVector3D(); vec.Read(br, compression); Vector3Df pos = new Vector3Df(vec.x.val, vec.y.val, vec.z.val); currposition.Add(pos); } positionsKeyframes.Add(currkeyframe); positions.Add(currposition); List <Vector3Df> currscale = new List <Vector3Df>(); currkeyframe = new List <uint>(); compression = 0; compr = (bone.GetVariableByName("scale") as CVector).GetVariableByName("compression") as CInt8; if (compr != null) { compression = compr.val; } addr = (bone.GetVariableByName("scale") as CVector).GetVariableByName("dataAddr") as CUInt32; if (addr != null) { br.BaseStream.Position = addr.val; } else { br.BaseStream.Position = 0; } var scaleNumFrames = ((bone.GetVariableByName("scale") as CVector).GetVariableByName("numFrames") as CUInt16).val; for (uint idx = 0; idx < scaleNumFrames; idx++) { keyFrame = idx; //keyFrame += numFrames; currkeyframe.Add(keyFrame); var vec = new CVector3D(); vec.Read(br, compression); Vector3Df scale = new Vector3Df(vec.x.val, vec.y.val, vec.z.val); currscale.Add(scale); } scalesKeyframes.Add(currkeyframe); scales.Add(currscale); } } break; } } } }
public void readBuffer(CR2WExportWrapper chunk, int selectedAnimIdx, CR2WFile animFile) { string dataAddrVar = "dataAddr"; uint numFrames = (chunk.GetVariableByName("numFrames") as CUInt32).val; float animDuration = (chunk.GetVariableByName("duration") as CFloat)?.val ?? 1.0f; animationSpeed = numFrames / animDuration; uint keyFrame = 0; byte[] data; //data = (chunk.GetVariableByName("fallbackData") as CByteArray).Bytes; currentAnimName = AnimationNames[selectedAnimIdx].Key; exportData.name = AnimationNames[selectedAnimIdx].Key; exportData.duration = animDuration; exportData.numFrames = numFrames; exportData.dt = (chunk.GetVariableByName("dt") as CFloat)?.val ?? 0.03333333f; var deferredData = chunk.GetVariableByName("deferredData") as CInt16; var streamingOption = (chunk.GetVariableByName("streamingOption") as CVariable); if (deferredData != null && deferredData.val != 0) { if (streamingOption != null && streamingOption.ToString() == "ABSO_PartiallyStreamable") { data = ConvertAnimation.Combine((chunk.GetVariableByName("data") as CByteArray).Bytes, File.ReadAllBytes(animFile.FileName + "." + deferredData.val + ".buffer")); } else { data = File.ReadAllBytes(animFile.FileName + "." + deferredData.val + ".buffer"); } } else { data = (chunk.GetVariableByName("data") as CByteArray).Bytes; } using (MemoryStream ms = new MemoryStream(data)) using (BinaryReader br = new BinaryReader(ms)) { foreach (CVector bone in (chunk.GetVariableByName("bones") as CArray).array) { List <uint> currkeyframe = new List <uint>(); List <Quaternion> currorient = new List <Quaternion>(); List <Vector3Df> currorientEuler = new List <Vector3Df>(); currentBones.Add(bone); br.BaseStream.Position = ((bone.GetVariableByName("orientation") as CVector).GetVariableByName(dataAddrVar) as CUInt32).val; int orientNumFrames = ((bone.GetVariableByName("orientation") as CVector).GetVariableByName("numFrames") as CUInt16).val; for (uint idx = 0; idx < orientNumFrames; idx++) { keyFrame = idx; //keyFrame += numFrames; currkeyframe.Add(keyFrame); //bone.GetVariableByName("position"); byte[] odata = br.ReadBytes(6); ulong bits = (ulong)odata[0] << 40 | (ulong)odata[1] << 32 | (ulong)odata[2] << 24 | (ulong)odata[3] << 16 | (ulong)odata[4] << 8 | odata[5]; ushort[] orients = new ushort[4]; float[] quart = new float[4]; orients[0] = (ushort)((bits & 0x0000FFF000000000) >> 36); orients[1] = (ushort)((bits & 0x0000000FFF000000) >> 24); orients[2] = (ushort)((bits & 0x0000000000FFF000) >> 12); orients[3] = (ushort)((bits & 0x0000000000000FFF)); for (int i = 0; i < orients.Length; i++) { float fVal = (2047.0f - orients[i]) * (1 / 2048.0f); quart[i] = fVal; } quart[3] = -quart[3]; Quaternion orientation = new Quaternion(quart[0], quart[1], quart[2], quart[3]); currorient.Add(orientation); Vector3Df euler = orientation.ToEuler(); currorientEuler.Add(euler); //Console.WriteLine("Euler : x=%f, y=%f, z=%f", euler.X, euler.Y, euler.Z); } orientKeyframes.Add(currkeyframe); orientations.Add(currorient); orientationsEuler.Add(currorientEuler); // TODO: Refactor List <Vector3Df> currposition = new List <Vector3Df>(); currkeyframe = new List <uint>(); int compression = 0; var compr = (bone.GetVariableByName("position") as CVector).GetVariableByName("compression") as CInt8; if (compr != null) { compression = compr.val; } var addr = (bone.GetVariableByName("position") as CVector).GetVariableByName(dataAddrVar) as CUInt32; if (addr != null) { br.BaseStream.Position = addr.val; } else { br.BaseStream.Position = 0; } var posNumFrames = ((bone.GetVariableByName("position") as CVector).GetVariableByName("numFrames") as CUInt16).val; for (uint idx = 0; idx < posNumFrames; idx++) { keyFrame = idx; //keyFrame += numFrames; currkeyframe.Add(keyFrame); var vec = new CVector3D(); vec.Read(br, compression); Vector3Df pos = new Vector3Df(vec.x.val, vec.y.val, vec.z.val); currposition.Add(pos); } positionsKeyframes.Add(currkeyframe); positions.Add(currposition); List <Vector3Df> currscale = new List <Vector3Df>(); currkeyframe = new List <uint>(); compression = 0; compr = (bone.GetVariableByName("scale") as CVector).GetVariableByName("compression") as CInt8; if (compr != null) { compression = compr.val; } addr = (bone.GetVariableByName("scale") as CVector).GetVariableByName(dataAddrVar) as CUInt32; if (addr != null) { br.BaseStream.Position = addr.val; } else { br.BaseStream.Position = 0; } var scaleNumFrames = ((bone.GetVariableByName("scale") as CVector).GetVariableByName("numFrames") as CUInt16).val; for (uint idx = 0; idx < scaleNumFrames; idx++) { keyFrame = idx; //keyFrame += numFrames; currkeyframe.Add(keyFrame); var vec = new CVector3D(); vec.Read(br, compression); Vector3Df scale = new Vector3Df(vec.x.val, vec.y.val, vec.z.val); currscale.Add(scale); } scalesKeyframes.Add(currkeyframe); scales.Add(currscale); } } }