/// <summary> /// /// </summary> /// <param name="VertexInfo"></param> /// <param name="VertexType"></param> private void PutVertex(ref VertexInfo VertexInfo, ref VertexTypeStruct VertexType) { /* * if (GpuState->ClearingMode) * { * Console.WriteLine(VertexInfo); * } */ //Console.WriteLine(VertexInfo); //Console.WriteLine(VertexInfo); //Console.WriteLine(VertexInfo); if (VertexType.Color != VertexTypeStruct.ColorEnum.Void) { GL.Color4((float)VertexInfo.R, (float)VertexInfo.G, (float)VertexInfo.B, (float)VertexInfo.A); } if (VertexType.Texture != VertexTypeStruct.NumericEnum.Void) { //Console.WriteLine("{0}, {1}", VertexInfo.U, VertexInfo.V); GL.TexCoord2(VertexInfo.U, VertexInfo.V); } if (VertexType.Normal != VertexTypeStruct.NumericEnum.Void) { GL.Normal3(VertexInfo.NX, VertexInfo.NY, VertexInfo.NZ); } if (VertexType.Position != VertexTypeStruct.NumericEnum.Void) { GL.Vertex3(VertexInfo.PX, VertexInfo.PY, VertexInfo.PZ); } }
private void _CapturePutVertex(ref VertexInfo vertexInfo) { if (_pspWavefrontObjWriter != null) { lock (this) _pspWavefrontObjWriter.PutVertex(ref vertexInfo); } }
/// <summary> /// </summary> /// <param name="vertexInfo"></param> private void PutVertex(VertexInfo vertexInfo) { _CapturePutVertex(ref vertexInfo); PutVertexIndex(_verticesPosition.Length); _verticesPosition.Add(new VertexInfoVector3F(vertexInfo.Position)); _verticesNormal.Add(new VertexInfoVector3F(vertexInfo.Normal)); _verticesTexcoords.Add(new VertexInfoVector3F(vertexInfo.Texture)); _verticesColors.Add(new VertexInfoColor(vertexInfo.Color)); _verticesWeights.Add(new VertexInfoWeights(vertexInfo)); }
/// <summary> /// </summary> /// <param name="vertexInfo"></param> private void PutVertex(VertexInfo vertexInfo) { _CapturePutVertex(ref vertexInfo); PutVertexIndex(_verticesPosition.Length); _verticesPosition.Add(vertexInfo.Position.ToRVector3()); _verticesNormal.Add(vertexInfo.Normal.ToRVector3()); _verticesTexcoords.Add(vertexInfo.Texture.ToRVector3()); _verticesColors.Add(new RgbaFloat(vertexInfo.Color)); _verticesWeights.Add(new VertexInfoWeights(vertexInfo)); }
/* void ReadVertex(int Index, VertexInfo* VertexInfo) { if (VertexType.Index == VertexTypeStruct.IndexEnum.Byte) { *VertexInfo = Vertices[IndexListByte[Index]]; } else if (VertexType.Index == VertexTypeStruct.IndexEnum.Short) { *VertexInfo = Vertices[IndexListShort[Index]]; } else *VertexInfo = Vertices[Index]; } */ void ReadVertex_Byte(int Index, VertexInfo* VertexInfo) { *VertexInfo = Vertices[IndexListByte[Index]]; }
/// <summary> /// /// </summary> /// <param name="VertexInfo"></param> /// <param name="VertexType"></param> private void PutVertex(ref VertexInfo _VertexInfo, ref VertexTypeStruct VertexType) { VertexInfo VertexInfo = PerformSkinning(_VertexInfo); _CapturePutVertex(ref VertexInfo); //Console.WriteLine(VertexType); //Console.WriteLine(VertexInfo); /* if (GpuState->ClearingMode) { Console.WriteLine(VertexInfo); } */ //Console.WriteLine(VertexInfo); //Console.WriteLine(VertexInfo); //Console.WriteLine(VertexInfo); #if DEBUG_VERTEX_TYPE if (OutputVertexInfoStream != null) OutputVertexInfoStream.WriteBytes(Encoding.UTF8.GetBytes(String.Format("{0}\n", VertexInfo))); #endif #if false GL.Color4(1.0f, 1.0f, 1.0f, 1.0f); GL.Vertex3(VertexInfo.PX, VertexInfo.PY, 0.5f); #else if (VertexType.Color != VertexTypeStruct.ColorEnum.Void) { GL.Color4(VertexInfo.Color.R, VertexInfo.Color.G, VertexInfo.Color.B, VertexInfo.Color.A); } if (VertexType.Texture != VertexTypeStruct.NumericEnum.Void) { /* if (VertexType.Texture == VertexTypeStruct.NumericEnum.Short) { var Texture = CurrentTexture; Console.WriteLine( "U={0}, V={1}, OFFSET({2}, {3}), SCALE({4}, {5}) : SIZE({6},{7}) : {8}", VertexInfo.U, VertexInfo.V, GpuState->TextureMappingState.TextureState.OffsetU, GpuState->TextureMappingState.TextureState.OffsetV, GpuState->TextureMappingState.TextureState.ScaleU, GpuState->TextureMappingState.TextureState.ScaleV, Texture.Width, Texture.Height, GpuState->TextureMappingState.TextureState ); //return; } */ //Console.WriteLine("{0}, {1}", VertexInfo.U, VertexInfo.V); //GL.TexCoord2(VertexInfo.TX, VertexInfo.TY); GL.TexCoord3(VertexInfo.Texture.X, VertexInfo.Texture.Y, VertexInfo.Texture.Z); } //Console.Write(",{0}", VertexInfo.PZ); if (VertexType.Normal != VertexTypeStruct.NumericEnum.Void) { #if false if (VertexType.ReversedNormal) { GL.Normal3(-VertexInfo.Normal.X, -VertexInfo.Normal.Y, -VertexInfo.Normal.Z); } else #endif { GL.Normal3(VertexInfo.Normal.X, VertexInfo.Normal.Y, VertexInfo.Normal.Z); } } if (VertexType.Position != VertexTypeStruct.NumericEnum.Void) { GL.Vertex3(VertexInfo.Position.X, VertexInfo.Position.Y, VertexInfo.Position.Z); } #endif }
/// <summary> /// /// </summary> /// <param name="GpuState"></param> override public unsafe void Prim(GpuStateStruct *GpuState, PrimitiveType PrimitiveType, ushort VertexCount) { this.GpuState = GpuState; if (!IsCurrentWindow) { IsCurrentWindow = true; GraphicsContext.MakeCurrent(NativeWindow.WindowInfo); } //Console.WriteLine("--------------------------------------------------------"); VertexType = GpuState->VertexState.Type; VertexReader.SetVertexTypeStruct(VertexType, (byte *)Memory.PspAddressToPointerSafe(GpuState->VertexAddress)); //IndexReader.SetVertexTypeStruct(VertexType, VertexCount, (byte*)Memory.PspAddressToPointerSafe(GpuState->IndexAddress)); int TotalVerticesWithoutMorphing = VertexCount; void *IndexAddress = Memory.PspAddressToPointerSafe(GpuState->IndexAddress); switch (VertexType.Index) { case VertexTypeStruct.IndexEnum.Void: break; case VertexTypeStruct.IndexEnum.Byte: Marshal.Copy(new IntPtr(IndexAddress), IndexListByte, 0, VertexCount); TotalVerticesWithoutMorphing = IndexListByte.Take(VertexCount).Max() + 1; break; case VertexTypeStruct.IndexEnum.Short: Marshal.Copy(new IntPtr(IndexAddress), IndexListShort, 0, VertexCount); TotalVerticesWithoutMorphing = IndexListShort.Take(VertexCount).Max() + 1; break; default: throw (new NotImplementedException()); } //Console.WriteLine(TotalVerticesWithoutMorphing); int MorpingVertexCount = (int)VertexType.MorphingVertexCount + 1; int z = 0; VertexInfo TempVertexInfo; float *Morphs = &GpuState->MorphingState.MorphWeight0; //for (int n = 0; n < MorpingVertexCount; n++) Console.Write("{0}, ", Morphs[n]); Console.WriteLine(""); fixed(VertexInfo *VerticesPtr = Vertices) { for (int n = 0; n < TotalVerticesWithoutMorphing; n++) { //Console.WriteLine(MorpingVertexCount); if (MorpingVertexCount == 1) { VertexReader.ReadVertex(z++, &TempVertexInfo); //Console.WriteLine(TempVertexInfo); VerticesPtr[n] = TempVertexInfo; } else { var ComponentsIn = (float *)&TempVertexInfo; var ComponentsOut = (float *)&VerticesPtr[n]; for (int cc = 0; cc < 20; cc++) { ComponentsOut[cc] = 0; } for (int m = 0; m < MorpingVertexCount; m++) { VertexReader.ReadVertex(z++, &TempVertexInfo); for (int cc = 0; cc < 20; cc++) { ComponentsOut[cc] += ComponentsIn[cc] * Morphs[m]; } } } } } //VertexType.Texture == VertexTypeStruct.TextureEnum.Byte //return; //PrepareRead(GpuState); if (GpuState->ClearingMode) { //return; //GL.ClearColor(1, 1, 0, 0); // @TODO: Fake /* */ //PrepareState(GpuState); //return; //Console.WriteLine(VertexCount); drawBeginClear(GpuState); /* * GL.ClearColor(0, 0, 0, 1); * GL.ClearDepth(0); * GL.ClearStencil(0); * GL.ClearAccum(0, 0, 0, 0); * GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit | ClearBufferMask.AccumBufferBit); * return; */ } else { PrepareState(GpuState); } // DRAW BEGIN COMMON { if (GpuState->VertexState.Type.Transform2D) { GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 512, 272, 0, -0x7FFF, +0x7FFF); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); } else { GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.MultMatrix(GpuState->VertexState.ProjectionMatrix.Values); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.MultMatrix(GpuState->VertexState.ViewMatrix.Values); GL.MultMatrix(GpuState->VertexState.WorldMatrix.Values); if (GpuState->VertexState.WorldMatrix.Values[0] == float.NaN) { throw (new Exception("Invalid WorldMatrix")); } //GpuState->VertexState.ViewMatrix.Dump(); //GpuState->VertexState.WorldMatrix.Dump(); //Console.WriteLine("NO Transform2D"); } } // DRAW ACTUALLY { uint VertexSize = GpuState->VertexState.Type.GetVertexSize(); byte *VertexPtr = (byte *)Memory.PspAddressToPointerSafe(GpuState->VertexAddress); //Console.WriteLine(VertexSize); var BeginMode = default(BeginMode); switch (PrimitiveType) { case PrimitiveType.Lines: BeginMode = BeginMode.Lines; break; case PrimitiveType.LineStrip: BeginMode = BeginMode.LineStrip; break; case PrimitiveType.Triangles: BeginMode = BeginMode.Triangles; break; case PrimitiveType.Points: BeginMode = BeginMode.Points; break; case PrimitiveType.TriangleFan: BeginMode = BeginMode.TriangleFan; break; case PrimitiveType.TriangleStrip: BeginMode = BeginMode.TriangleStrip; break; case PrimitiveType.Sprites: BeginMode = BeginMode.Quads; break; default: throw (new NotImplementedException("Not implemented PrimitiveType:'" + PrimitiveType + "'")); } //Console.WriteLine(BeginMode); GL.Begin(BeginMode); { if (PrimitiveType == PrimitiveType.Sprites) { GL.Disable(EnableCap.CullFace); for (int n = 0; n < VertexCount; n += 2) { VertexInfo VertexInfoTopLeft; VertexInfo VertexInfoTopRight; VertexInfo VertexInfoBottomRight; VertexInfo VertexInfoBottomLeft; ReadVertex(n + 0, &VertexInfoTopLeft); ReadVertex(n + 1, &VertexInfoBottomRight); //if (GpuState->ClearingMode) Console.WriteLine("{0} - {1}", VertexInfoTopLeft, VertexInfoBottomRight); float R = VertexInfoBottomRight.R, G = VertexInfoBottomRight.G, B = VertexInfoBottomRight.B, A = VertexInfoBottomRight.A; float PZ = VertexInfoTopLeft.PZ; float NZ = VertexInfoTopLeft.NZ; VertexInfoTopRight = new VertexInfo() { U = VertexInfoBottomRight.U, V = VertexInfoTopLeft.V, PX = VertexInfoBottomRight.PX, PY = VertexInfoTopLeft.PY, NX = VertexInfoBottomRight.NX, NY = VertexInfoTopLeft.NY, }; VertexInfoBottomLeft = new VertexInfo() { U = VertexInfoTopLeft.U, V = VertexInfoBottomRight.V, PX = VertexInfoTopLeft.PX, PY = VertexInfoBottomRight.PY, NX = VertexInfoTopLeft.NX, NY = VertexInfoBottomRight.NY, }; VertexInfoBottomLeft.R = VertexInfoBottomRight.R = VertexInfoTopRight.R = VertexInfoTopLeft.R = R; VertexInfoBottomLeft.G = VertexInfoBottomRight.G = VertexInfoTopRight.G = VertexInfoTopLeft.G = G; VertexInfoBottomLeft.B = VertexInfoBottomRight.B = VertexInfoTopRight.B = VertexInfoTopLeft.B = B; VertexInfoBottomLeft.A = VertexInfoBottomRight.A = VertexInfoTopRight.A = VertexInfoTopLeft.A = R; VertexInfoBottomLeft.PZ = VertexInfoBottomRight.PZ = VertexInfoTopRight.PZ = VertexInfoTopLeft.PZ = PZ; VertexInfoBottomLeft.NZ = VertexInfoBottomRight.NZ = VertexInfoTopRight.NZ = VertexInfoTopLeft.NZ = NZ; PutVertex(ref VertexInfoTopLeft, ref VertexType); PutVertex(ref VertexInfoTopRight, ref VertexType); PutVertex(ref VertexInfoBottomRight, ref VertexType); PutVertex(ref VertexInfoBottomLeft, ref VertexType); } } else { for (int n = 0; n < VertexCount; n++) { VertexInfo VertexInfo; ReadVertex(n, &VertexInfo); PutVertex(ref VertexInfo, ref VertexType); } } } GL.End(); GL.Flush(); } //Console.WriteLine(VertexCount); //PrepareWrite(GpuState); }
public override void DrawCurvedSurface(GlobalGpuState GlobalGpuState, GpuStateStruct* GpuStateStruct, VertexInfo[,] Patch, int UCount, int VCount) { //GpuState->TextureMappingState.Enabled = true; //ResetState(); PrepareStateCommon(GpuState); PrepareStateDraw(GpuState); PrepareStateMatrix(GpuState); PrepareState_Texture_Common(GpuState); PrepareState_Texture_3D(GpuState); GL.ActiveTexture(TextureUnit.Texture0); /* GL.Enable(EnableCap.Texture2D); GL.Disable(EnableCap.Lighting); GL.Disable(EnableCap.CullFace); //GL.Disable(EnableCap.Texture2D); //var CurrentTexture = TextureCache.Get(GpuState); var CurrentTexture = new Texture(this).Load("Bezier2.png"); CurrentTexture.Bind(); var Mipmap0 = &GpuStateStruct->TextureMappingState.TextureState.Mipmap0; int MipmapWidth = Mipmap0->BufferWidth; int MipmapHeight = Mipmap0->TextureHeight; */ /* GL.Scale( 1.0f * Mipmap0->BufferWidth, 1.0f * Mipmap0->TextureHeight, 1.0f ); */ //GL.LoadIdentity(); //var CurrentTexture = TextureCache.Get(GpuState); //CurrentTexture.Save("Bezier.png"); var VertexType = GpuStateStruct->VertexState.Type; //GL.Color3(Color.White); #if true GL.Begin(BeginMode.Triangles); for (int t = 0; t < Patch.GetLength(1) - 1; t++) { for (int s = 0; s < Patch.GetLength(0) - 1; s++) { var VertexInfo1 = Patch[s + 0, t + 0]; var VertexInfo2 = Patch[s + 0, t + 1]; var VertexInfo3 = Patch[s + 1, t + 1]; var VertexInfo4 = Patch[s + 1, t + 0]; /* VertexInfo1.Texture.X = (s + 0) * MipmapWidth / Patch.GetLength(0); VertexInfo1.Texture.Y = (t + 0) * MipmapWidth / Patch.GetLength(1); VertexInfo2.Texture.X = (s + 0) * MipmapWidth / Patch.GetLength(0); VertexInfo2.Texture.Y = (t + 1) * MipmapWidth / Patch.GetLength(1); VertexInfo3.Texture.X = (s + 1) * MipmapWidth / Patch.GetLength(0); VertexInfo3.Texture.Y = (t + 1) * MipmapWidth / Patch.GetLength(1); VertexInfo4.Texture.X = (s + 1) * MipmapWidth / Patch.GetLength(0); VertexInfo4.Texture.Y = (t + 0) * MipmapWidth / Patch.GetLength(1); */ PutVertex(ref VertexInfo1, ref VertexType); PutVertex(ref VertexInfo2, ref VertexType); PutVertex(ref VertexInfo3, ref VertexType); PutVertex(ref VertexInfo1, ref VertexType); PutVertex(ref VertexInfo3, ref VertexType); PutVertex(ref VertexInfo4, ref VertexType); //GL.Color3(Color.White); //Console.WriteLine("{0}, {1} : {2}", s, t, VertexInfo1); } } GL.End(); #else GL.Begin(BeginMode.TriangleStrip); //GL.Begin(BeginMode.LineStrip); //GL.Begin(BeginMode.Points); for (int t = 0; t < Patch.GetLength(1) - 1; t++) { for (int s = 0; s < Patch.GetLength(0); s++) { var VertexInfo1 = Patch[s, t]; var VertexInfo2 = Patch[s, t + 1]; PutVertex(ref VertexInfo1, ref VertexType); PutVertex(ref VertexInfo2, ref VertexType); //GL.Color3(Color.White); Console.WriteLine("{0}, {1} : {2}", s, t, VertexInfo1); } } GL.End(); #endif }
VertexInfo PerformSkinning(VertexInfo VertexInfo) { int SkinningWeightCount = VertexType.RealSkinningWeightCount; if (SkinningWeightCount == 0) return VertexInfo; var OutputPosition = default(Vector3F); var OutputNormal = default(Vector3F); var InputPosition = VertexInfo.Position; var InputNormal = VertexInfo.Normal; var BoneMatrices = &GpuState->SkinningState.BoneMatrix0; for (int m = 0; m < SkinningWeightCount; m++) { var BoneMatrix = BoneMatrices[m]; float Weight = VertexInfo.Weights[m]; if (Weight != 0) { OutputPosition.X += (InputPosition.X * BoneMatrix.Values[0] + InputPosition.Y * BoneMatrix.Values[4] + InputPosition.Z * BoneMatrix.Values[8] + 1 * BoneMatrix.Values[12]) * Weight; OutputPosition.Y += (InputPosition.X * BoneMatrix.Values[1] + InputPosition.Y * BoneMatrix.Values[5] + InputPosition.Z * BoneMatrix.Values[9] + 1 * BoneMatrix.Values[13]) * Weight; OutputPosition.Z += (InputPosition.X * BoneMatrix.Values[2] + InputPosition.Y * BoneMatrix.Values[6] + InputPosition.Z * BoneMatrix.Values[10] + 1 * BoneMatrix.Values[14]) * Weight; OutputNormal.X += (InputNormal.X * BoneMatrix.Values[0] + InputNormal.Y * BoneMatrix.Values[4] + InputNormal.Z * BoneMatrix.Values[8] + 0 * BoneMatrix.Values[12]) * Weight; OutputNormal.Y += (InputNormal.X * BoneMatrix.Values[1] + InputNormal.Y * BoneMatrix.Values[5] + InputNormal.Z * BoneMatrix.Values[9] + 0 * BoneMatrix.Values[13]) * Weight; OutputNormal.Z += (InputNormal.X * BoneMatrix.Values[2] + InputNormal.Y * BoneMatrix.Values[6] + InputNormal.Z * BoneMatrix.Values[10] + 0 * BoneMatrix.Values[14]) * Weight; } } VertexInfo.Position = OutputPosition; VertexInfo.Normal = OutputNormal; return VertexInfo; }
private void _CapturePutVertex(ref VertexInfo VertexInfo) { if (PspWavefrontObjWriter != null) { lock (this) { if (PspWavefrontObjWriter != null) PspWavefrontObjWriter.PutVertex(ref VertexInfo); } } }
private void ReadVertex_Short(int index, out VertexInfo vertexInfo) => vertexInfo = Vertices[indexListShort[index]];
private void ReadVertex_Byte(int index, out VertexInfo vertexInfo) => vertexInfo = Vertices[indexListByte[index]];
private void ReadVertex_Void(int index, out VertexInfo vertexInfo) => vertexInfo = Vertices[index];
void ReadVertex_Short(int Index, VertexInfo* VertexInfo) { *VertexInfo = Vertices[IndexListShort[Index]]; }
/// <summary> /// /// </summary> /// <param name="GpuState"></param> public unsafe override void Prim(GpuStateStruct* GpuState, PrimitiveType PrimitiveType, ushort VertexCount) { this.GpuState = GpuState; if (!IsCurrentWindow) { IsCurrentWindow = true; GraphicsContext.MakeCurrent(NativeWindow.WindowInfo); } //Console.WriteLine("--------------------------------------------------------"); VertexType = GpuState->VertexState.Type; VertexReader.SetVertexTypeStruct(VertexType, (byte*)Memory.PspAddressToPointerSafe(GpuState->VertexAddress)); //IndexReader.SetVertexTypeStruct(VertexType, VertexCount, (byte*)Memory.PspAddressToPointerSafe(GpuState->IndexAddress)); int TotalVerticesWithoutMorphing = VertexCount; void* IndexAddress = Memory.PspAddressToPointerSafe(GpuState->IndexAddress); switch (VertexType.Index) { case VertexTypeStruct.IndexEnum.Void: break; case VertexTypeStruct.IndexEnum.Byte: Marshal.Copy(new IntPtr(IndexAddress), IndexListByte, 0, VertexCount); TotalVerticesWithoutMorphing = IndexListByte.Take(VertexCount).Max() + 1; break; case VertexTypeStruct.IndexEnum.Short: Marshal.Copy(new IntPtr(IndexAddress), IndexListShort, 0, VertexCount); TotalVerticesWithoutMorphing = IndexListShort.Take(VertexCount).Max() + 1; break; default: throw (new NotImplementedException()); } //Console.WriteLine(TotalVerticesWithoutMorphing); int MorpingVertexCount = (int)VertexType.MorphingVertexCount + 1; int z = 0; VertexInfo TempVertexInfo; float* Morphs = &GpuState->MorphingState.MorphWeight0; //for (int n = 0; n < MorpingVertexCount; n++) Console.Write("{0}, ", Morphs[n]); Console.WriteLine(""); fixed (VertexInfo* VerticesPtr = Vertices) { for (int n = 0; n < TotalVerticesWithoutMorphing; n++) { //Console.WriteLine(MorpingVertexCount); if (MorpingVertexCount == 1) { VertexReader.ReadVertex(z++, &TempVertexInfo); //Console.WriteLine(TempVertexInfo); VerticesPtr[n] = TempVertexInfo; } else { var ComponentsIn = (float*)&TempVertexInfo; var ComponentsOut = (float*)&VerticesPtr[n]; for (int cc = 0; cc < 20; cc++) ComponentsOut[cc] = 0; for (int m = 0; m < MorpingVertexCount; m++) { VertexReader.ReadVertex(z++, &TempVertexInfo); for (int cc = 0; cc < 20; cc++) ComponentsOut[cc] += ComponentsIn[cc] * Morphs[m]; } } } } //VertexType.Texture == VertexTypeStruct.TextureEnum.Byte //return; //PrepareRead(GpuState); if (GpuState->ClearingMode) { //return; //GL.ClearColor(1, 1, 0, 0); // @TODO: Fake /* */ //PrepareState(GpuState); //return; //Console.WriteLine(VertexCount); drawBeginClear(GpuState); /* GL.ClearColor(0, 0, 0, 1); GL.ClearDepth(0); GL.ClearStencil(0); GL.ClearAccum(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit | ClearBufferMask.AccumBufferBit); return; */ } else { PrepareState(GpuState); } // DRAW BEGIN COMMON { if (GpuState->VertexState.Type.Transform2D) { GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 512, 272, 0, -0x7FFF, +0x7FFF); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); } else { GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.MultMatrix(GpuState->VertexState.ProjectionMatrix.Values); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.MultMatrix(GpuState->VertexState.ViewMatrix.Values); GL.MultMatrix(GpuState->VertexState.WorldMatrix.Values); if (GpuState->VertexState.WorldMatrix.Values[0] == float.NaN) { throw (new Exception("Invalid WorldMatrix")); } //GpuState->VertexState.ViewMatrix.Dump(); //GpuState->VertexState.WorldMatrix.Dump(); //Console.WriteLine("NO Transform2D"); } } // DRAW ACTUALLY { uint VertexSize = GpuState->VertexState.Type.GetVertexSize(); byte* VertexPtr = (byte*)Memory.PspAddressToPointerSafe(GpuState->VertexAddress); //Console.WriteLine(VertexSize); var BeginMode = default(BeginMode); switch (PrimitiveType) { case PrimitiveType.Lines: BeginMode = BeginMode.Lines; break; case PrimitiveType.LineStrip: BeginMode = BeginMode.LineStrip; break; case PrimitiveType.Triangles: BeginMode = BeginMode.Triangles; break; case PrimitiveType.Points: BeginMode = BeginMode.Points; break; case PrimitiveType.TriangleFan: BeginMode = BeginMode.TriangleFan; break; case PrimitiveType.TriangleStrip: BeginMode = BeginMode.TriangleStrip; break; case PrimitiveType.Sprites: BeginMode = BeginMode.Quads; break; default: throw (new NotImplementedException("Not implemented PrimitiveType:'" + PrimitiveType + "'")); } //Console.WriteLine(BeginMode); GL.Begin(BeginMode); { if (PrimitiveType == PrimitiveType.Sprites) { GL.Disable(EnableCap.CullFace); for (int n = 0; n < VertexCount; n += 2) { VertexInfo VertexInfoTopLeft; VertexInfo VertexInfoTopRight; VertexInfo VertexInfoBottomRight; VertexInfo VertexInfoBottomLeft; ReadVertex(n + 0, &VertexInfoTopLeft); ReadVertex(n + 1, &VertexInfoBottomRight); //if (GpuState->ClearingMode) Console.WriteLine("{0} - {1}", VertexInfoTopLeft, VertexInfoBottomRight); float R = VertexInfoBottomRight.R, G = VertexInfoBottomRight.G, B = VertexInfoBottomRight.B, A = VertexInfoBottomRight.A; float PZ = VertexInfoTopLeft.PZ; float NZ = VertexInfoTopLeft.NZ; VertexInfoTopRight = new VertexInfo() { U = VertexInfoBottomRight.U, V = VertexInfoTopLeft.V, PX = VertexInfoBottomRight.PX, PY = VertexInfoTopLeft.PY, NX = VertexInfoBottomRight.NX, NY = VertexInfoTopLeft.NY, }; VertexInfoBottomLeft = new VertexInfo() { U = VertexInfoTopLeft.U, V = VertexInfoBottomRight.V, PX = VertexInfoTopLeft.PX, PY = VertexInfoBottomRight.PY, NX = VertexInfoTopLeft.NX, NY = VertexInfoBottomRight.NY, }; VertexInfoBottomLeft.R = VertexInfoBottomRight.R = VertexInfoTopRight.R = VertexInfoTopLeft.R = R; VertexInfoBottomLeft.G = VertexInfoBottomRight.G = VertexInfoTopRight.G = VertexInfoTopLeft.G = G; VertexInfoBottomLeft.B = VertexInfoBottomRight.B = VertexInfoTopRight.B = VertexInfoTopLeft.B = B; VertexInfoBottomLeft.A = VertexInfoBottomRight.A = VertexInfoTopRight.A = VertexInfoTopLeft.A = R; VertexInfoBottomLeft.PZ = VertexInfoBottomRight.PZ = VertexInfoTopRight.PZ = VertexInfoTopLeft.PZ = PZ; VertexInfoBottomLeft.NZ = VertexInfoBottomRight.NZ = VertexInfoTopRight.NZ = VertexInfoTopLeft.NZ = NZ; PutVertex(ref VertexInfoTopLeft, ref VertexType); PutVertex(ref VertexInfoTopRight, ref VertexType); PutVertex(ref VertexInfoBottomRight, ref VertexType); PutVertex(ref VertexInfoBottomLeft, ref VertexType); } } else { for (int n = 0; n < VertexCount; n++) { VertexInfo VertexInfo; ReadVertex(n, &VertexInfo); PutVertex(ref VertexInfo, ref VertexType); } } } GL.End(); GL.Flush(); } //Console.WriteLine(VertexCount); //PrepareWrite(GpuState); }
void ReadVertex_Void(int Index, VertexInfo* VertexInfo) { *VertexInfo = Vertices[Index]; }
/// <summary> /// /// </summary> /// <param name="VertexInfo"></param> /// <param name="VertexType"></param> private void PutVertex(ref VertexInfo VertexInfo, ref VertexTypeStruct VertexType) { /* if (GpuState->ClearingMode) { Console.WriteLine(VertexInfo); } */ //Console.WriteLine(VertexInfo); //Console.WriteLine(VertexInfo); //Console.WriteLine(VertexInfo); if (VertexType.Color != VertexTypeStruct.ColorEnum.Void) { GL.Color4((float)VertexInfo.R, (float)VertexInfo.G, (float)VertexInfo.B, (float)VertexInfo.A); } if (VertexType.Texture != VertexTypeStruct.NumericEnum.Void) { //Console.WriteLine("{0}, {1}", VertexInfo.U, VertexInfo.V); GL.TexCoord2(VertexInfo.U, VertexInfo.V); } if (VertexType.Normal != VertexTypeStruct.NumericEnum.Void) { GL.Normal3(VertexInfo.NX, VertexInfo.NY, VertexInfo.NZ); } if (VertexType.Position != VertexTypeStruct.NumericEnum.Void) { GL.Vertex3(VertexInfo.PX, VertexInfo.PY, VertexInfo.PZ); } }
/// <summary> /// /// </summary> /// <param name="GpuState"></param> private unsafe void _Prim(GpuStateStruct* GpuState, GuPrimitiveType PrimitiveType, ushort VertexCount) { //if (PrimitiveType == GuPrimitiveType.TriangleStrip) VertexCount++; //Console.WriteLine("Prim: {0}, {1}", PrimitiveType, VertexCount); this.GpuState = GpuState; //Console.WriteLine("--------------------------------------------------------"); VertexType = GpuState->VertexState.Type; ReadVertexDelegate ReadVertex = ReadVertex_Void; VertexReader.SetVertexTypeStruct( VertexType, (byte*)Memory.PspAddressToPointerSafe(GpuState->GetAddressRelativeToBaseOffset(GpuState->VertexAddress), 0) ); #if DEBUG_VERTEX_TYPE try { if (!File.Exists("VertexType_" + VertexType.Value)) { File.WriteAllBytes( "VertexType_" + VertexType.Value, PointerUtils.PointerToByteArray((byte*)Memory.PspAddressToPointerSafe(GpuState->VertexAddress), 16 * 1024) ); File.WriteAllText( "VertexType_" + VertexType.Value + "_str", VertexCount + "," + PrimitiveType + "\n" + VertexType.ToString() ); OutputVertexInfoStream = File.OpenWrite("VertexType_" + VertexType.Value + "_list"); } } catch { } #endif //IndexReader.SetVertexTypeStruct(VertexType, VertexCount, (byte*)Memory.PspAddressToPointerSafe(GpuState->IndexAddress)); uint TotalVerticesWithoutMorphing = VertexCount; //Console.Error.WriteLine("GpuState->IndexAddress: {0:X}", GpuState->IndexAddress); // Invalid /* if (GpuState->IndexAddress == 0xFFFFFFFF) { //Debug.Fail("Invalid IndexAddress"); throw (new Exception("Invalid IndexAddress == 0xFFFFFFFF")); } */ void* IndexPointer = null; if (VertexType.Index != VertexTypeStruct.IndexEnum.Void) { IndexPointer = Memory.PspAddressToPointerSafe(GpuState->GetAddressRelativeToBaseOffset(GpuState->IndexAddress), 0); } //Console.Error.WriteLine(VertexType.Index); switch (VertexType.Index) { case VertexTypeStruct.IndexEnum.Void: break; case VertexTypeStruct.IndexEnum.Byte: ReadVertex = ReadVertex_Byte; IndexListByte = (byte *)IndexPointer; TotalVerticesWithoutMorphing = 0; for (int n = 0; n < VertexCount; n++) { if (TotalVerticesWithoutMorphing < IndexListByte[n]) TotalVerticesWithoutMorphing = IndexListByte[n]; } break; case VertexTypeStruct.IndexEnum.Short: ReadVertex = ReadVertex_Short; IndexListShort = (ushort*)IndexPointer; TotalVerticesWithoutMorphing = 0; //VertexCount--; for (int n = 0; n < VertexCount; n++) { //Console.Error.WriteLine(IndexListShort[n]); if (TotalVerticesWithoutMorphing < IndexListShort[n]) TotalVerticesWithoutMorphing = IndexListShort[n]; } break; default: throw (new NotImplementedException()); } TotalVerticesWithoutMorphing++; //Console.WriteLine(TotalVerticesWithoutMorphing); int MorpingVertexCount = (int)VertexType.MorphingVertexCount + 1; int z = 0; VertexInfo TempVertexInfo; float* Morphs = &GpuState->MorphingState.MorphWeight0; //for (int n = 0; n < MorpingVertexCount; n++) Console.Write("{0}, ", Morphs[n]); Console.WriteLine(""); //int VertexInfoFloatCount = (sizeof(Color4F) + sizeof(Vector3F) * 3) / sizeof(float); int VertexInfoFloatCount = (sizeof(VertexInfo)) / sizeof(float); fixed (VertexInfo* VerticesPtr = Vertices) { #if true if (MorpingVertexCount == 1) { VertexReader.ReadVertices(0, VerticesPtr, (int)TotalVerticesWithoutMorphing); } else { var ComponentsIn = (float*)&TempVertexInfo; for (int n = 0; n < TotalVerticesWithoutMorphing; n++) { var ComponentsOut = (float*)&VerticesPtr[n]; for (int cc = 0; cc < VertexInfoFloatCount; cc++) ComponentsOut[cc] = 0; for (int m = 0; m < MorpingVertexCount; m++) { VertexReader.ReadVertex(z++, &TempVertexInfo); for (int cc = 0; cc < VertexInfoFloatCount; cc++) ComponentsOut[cc] += ComponentsIn[cc] * Morphs[m]; } VerticesPtr[n].Normal = VerticesPtr[n].Normal.Normalize(); } } #else var ComponentsIn = (float*)&TempVertexInfo; for (int n = 0; n < TotalVerticesWithoutMorphing; n++) { if (MorpingVertexCount == 1) { VertexReader.ReadVertex(z++, &TempVertexInfo); VerticesPtr[n] = TempVertexInfo; //VertexReader.ReadVertices(0, VerticesPtr, TotalVerticesWithoutMorphing); } else { var ComponentsOut = (float*)&VerticesPtr[n]; for (int cc = 0; cc < VertexInfoFloatCount; cc++) ComponentsOut[cc] = 0; for (int m = 0; m < MorpingVertexCount; m++) { VertexReader.ReadVertex(z++, &TempVertexInfo); for (int cc = 0; cc < VertexInfoFloatCount; cc++) ComponentsOut[cc] += ComponentsIn[cc] * Morphs[m]; } } } #endif } //VertexType.Texture == VertexTypeStruct.TextureEnum.Byte //return; //PrepareRead(GpuState); PrepareStateCommon(GpuState); if (GpuState->ClearingMode) { PrepareStateClear(GpuState); } else { PrepareStateDraw(GpuState); } PrepareStateMatrix(GpuState); //GL.Enable(EnableCap.Blend); /* if (CurrentTexture != null) { if (CurrentTexture.TextureHash == 0x2202293873) { Console.Error.WriteLine(CurrentTexture); Console.Error.WriteLine(VertexCount); } } */ _CaptureStartPrimitive(PrimitiveType, GpuState->GetAddressRelativeToBaseOffset(GpuState->VertexAddress), VertexCount, ref VertexType); // DRAW ACTUALLY { //uint VertexSize = GpuState->VertexState.Type.GetVertexSize(); //byte* VertexPtr = (byte*)Memory.PspAddressToPointerSafe(GpuState->VertexAddress); //Console.WriteLine(VertexSize); var BeginMode = default(BeginMode); switch (PrimitiveType) { case GuPrimitiveType.Lines: BeginMode = BeginMode.Lines; break; case GuPrimitiveType.LineStrip: BeginMode = BeginMode.LineStrip; break; case GuPrimitiveType.Triangles: BeginMode = BeginMode.Triangles; break; case GuPrimitiveType.Points: BeginMode = BeginMode.Points; break; case GuPrimitiveType.TriangleFan: BeginMode = BeginMode.TriangleFan; break; case GuPrimitiveType.TriangleStrip: BeginMode = BeginMode.TriangleStrip; break; case GuPrimitiveType.Sprites: BeginMode = BeginMode.Quads; break; default: throw (new NotImplementedException("Not implemented PrimitiveType:'" + PrimitiveType + "'")); } if (PrimitiveType == GuPrimitiveType.Sprites) { GL.Disable(EnableCap.CullFace); } //Console.WriteLine(BeginMode); //lock (GpuLock) { //Console.Error.WriteLine("GL.Begin : Thread : {0}", Thread.CurrentThread.ManagedThreadId); GL.Begin(BeginMode); { if (PrimitiveType == GuPrimitiveType.Sprites) { GL.Disable(EnableCap.CullFace); for (int n = 0; n < VertexCount; n += 2) { VertexInfo V1, V2, V3, V4; ReadVertex(n + 0, &V1); ReadVertex(n + 1, &V3); { //if (GpuState->ClearingMode) Console.WriteLine("{0} - {1}", VertexInfoTopLeft, VertexInfoBottomRight); var Color = V3.Color; var TZ = V1.Texture.Z; var PZ = V1.Position.Z; var NZ = V1.Normal.Z; V2 = new VertexInfo() { Texture = new Vector3F(V3.Texture.X, V1.Texture.Y, TZ), Position = new Vector3F(V3.Position.X, V1.Position.Y, PZ), Normal = new Vector3F(V3.Normal.X, V1.Normal.Y, NZ), }; V4 = new VertexInfo() { Texture = new Vector3F(V1.Texture.X, V3.Texture.Y, TZ), Position = new Vector3(V1.Position.X, V3.Position.Y, PZ), Normal = new Vector3F(V1.Normal.X, V3.Normal.Y, NZ), }; V4.Color = V3.Color = V2.Color = V1.Color = Color; V4.Position.Z = V3.Position.Z = V2.Position.Z = V1.Position.Z = PZ; V4.Normal.Z = V3.Normal.Z = V2.Normal.Z = V1.Normal.Z = NZ; V4.Texture.Z = V3.Texture.Z = V2.Texture.Z = V1.Texture.Z = NZ; } PutVertex(ref V1, ref VertexType); PutVertex(ref V2, ref VertexType); PutVertex(ref V3, ref VertexType); PutVertex(ref V4, ref VertexType); } } else { VertexInfo VertexInfo; //Console.Error.WriteLine("{0} : {1} : {2}", BeginMode, VertexCount, VertexType.Index); for (int n = 0; n < VertexCount; n++) { ReadVertex(n, &VertexInfo); PutVertex(ref VertexInfo, ref VertexType); } } } GL.End(); } } _CaptureEndPrimitive(); //Console.WriteLine(VertexCount); //PrepareWrite(GpuState); #if DEBUG_VERTEX_TYPE if (OutputVertexInfoStream != null) { OutputVertexInfoStream.Close(); OutputVertexInfoStream = null; } #endif }
void ReadVertex(int Index, VertexInfo* VertexInfo) { if (VertexType.Index == VertexTypeStruct.IndexEnum.Byte) { *VertexInfo = Vertices[IndexListByte[Index]]; } else if (VertexType.Index == VertexTypeStruct.IndexEnum.Short) { *VertexInfo = Vertices[IndexListShort[Index]]; } else *VertexInfo = Vertices[Index]; }
VertexInfo PerformSkinning(VertexInfo VertexInfo) { if (VertexType.RealSkinningWeightCount == 0) return VertexInfo; //return VertexInfo; var SkinnedVertexState = VertexInfo; SkinnedVertexState.Position = Vector3.Zero; SkinnedVertexState.Normal = Vector3.Zero; var BoneMatrices = &GpuState->SkinningState.BoneMatrix0; //Console.WriteLine(VertexType.SkinningWeightCount); int SkinningWeightCount = VertexType.RealSkinningWeightCount; for (int m = 0; m < SkinningWeightCount; m++) { var BoneMatrix = BoneMatrices[m].Matrix4; //BoneMatrix.Transpose(); //Console.WriteLine("{0}", VertexInfo.Weights[m]); SkinnedVertexState.Position += Vector3.Transform(VertexInfo.Position, BoneMatrix) * VertexInfo.Weights[m]; SkinnedVertexState.Normal += Vector3.Transform(VertexInfo.Normal, BoneMatrix) * VertexInfo.Weights[m]; } return SkinnedVertexState; /* if (!shouldPerformSkin) return vertexState; //writefln("%s", gpu.state.boneMatrix[0]); VertexState skinnedVertexState = vertexState; (cast(float *)&skinnedVertexState.px)[0..3] = 0.0; (cast(float *)&skinnedVertexState.nx)[0..3] = 0.0; */ /* float[3] p, n; for (int m = 0; m < vertexType.skinningWeightCount; m++) { multiplyVectorPerMatrix!(true)( p, (cast(float *)&vertexState.px)[0..3], boneMatrix[m], vertexState.weights[m] ); multiplyVectorPerMatrix!(false)( n, (cast(float *)&vertexState.nx)[0..3], boneMatrix[m], vertexState.weights[m] ); //writefln("%s", p); (cast(float *)&skinnedVertexState.px)[0..3] += p[]; (cast(float *)&skinnedVertexState.nx)[0..3] += n[]; } return skinnedVertexState; */ }
public void ReadVertex(int Index, VertexInfo* VertexInfo) { this.Pointer = &BasePointer[VertexSize * Index]; this.VertexInfo = VertexInfo; // Vertex has to be aligned to the maxium size of any component. VertexAlignment(); ReadWeights(); ReadTextureCoordinates(); ReadColor(); ReadNormal(); ReadPosition(); }