Example #1
0
        // The ambientCache is on the stack, so we don't want to leave a reference to it that would try to be freed later.  Create the ambientCache immediately.
        static void R_FinishDeform(DrawSurf drawSurf, SrfTriangles newTri, DrawVert *ac)
        {
            if (newTri == null)
            {
                return;
            }

            // generate current normals, tangents, and bitangents We might want to support the possibility of deform functions generating
            // explicit normals, and we might also want to allow the cached deformInfo optimization for these.
            // FIXME: this doesn't work, because the deformed surface is just the ambient one, and there isn't an opportunity to generate light interactions
            if (drawSurf.material.ReceivesLighting)
            {
                newTri.verts = ac; R_DeriveTangents(newTri, false); newTri.verts = null;
            }
            newTri.ambientCache = vertexCache.AllocFrameTemp(ac, newTri.numVerts * DrawVert.SizeOf, false);

            fixed(void *newTri_indexes_ = newTri.indexes) newTri.indexCache = vertexCache.AllocFrameTemp(newTri_indexes_, newTri.numIndexes * sizeof(GlIndex), true);

            drawSurf.geoFrontEnd  = newTri;
            drawSurf.ambientCache = newTri.ambientCache;
            drawSurf.indexCache   = newTri.indexCache;
            drawSurf.numIndexes   = newTri.numIndexes;
            drawSurf.numShadowIndexesNoFrontCaps = newTri.numShadowIndexesNoFrontCaps;
            drawSurf.numShadowIndexesNoCaps      = newTri.numShadowIndexesNoCaps;
            drawSurf.shadowCapPlaneBits          = newTri.shadowCapPlaneBits;
        }
Example #2
0
        // Special transform to make the mesh seem fat or skinny.  May be used for zombie deaths
        void TransformScaledVerts(DrawVert *verts, JointMat *joints, float scale)
        {
            var scaledWeights = stackalloc Vector4[numWeights + Vector4.ALLOC16]; scaledWeights = _alloca16(scaledWeights);

            Simd.Mul(&scaledWeights[0].x, scale, &scaledWeights[0].x, numWeights * 4);

            fixed(int *weightIndexI = weightIndex)
            Simd.TransformVerts(verts, texCoords.Length, joints, scaledWeights, weightIndexI, numWeights);
        }
Example #3
0
        public static unsafe void render(BasicEffect effect)
        {
            IO io = ImGui.GetIO();

            ImGui.Render();

            DrawData *draw_data      = ImGui.GetDrawData();
            var       graphicsDevice = _game.GraphicsDevice;

            graphicsDevice.DepthStencilState = DepthStencilState.None;
            graphicsDevice.RasterizerState   = new RasterizerState()
            {
                ScissorTestEnable = true, CullMode = CullMode.None
            };
            graphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
            graphicsDevice.BlendState       = BlendState.AlphaBlend;

            // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
            ImGui.ScaleClipRects(draw_data, io.DisplayFramebufferScale);

            //var effect = Assets.defaultEffect;
            //effect.Texture = Assets.dummyTexture;
            effect.World      = Matrix.Identity;
            effect.View       = Matrix.Identity;// camera.viewMatrix3D;
            effect.Projection =
                Matrix.CreateOrthographicOffCenter(
                    0.0f,
                    io.DisplaySize.X / io.DisplayFramebufferScale.X,
                    io.DisplaySize.Y / io.DisplayFramebufferScale.Y,
                    0.0f,
                    -1.0f,
                    1.0f
                    );

            // Render command lists

            for (int n = 0; n < draw_data->CmdListsCount; n++)
            {
                NativeDrawList *cmd_list       = draw_data->CmdLists[n];
                DrawVert *      vtx_buffer     = (DrawVert *)cmd_list->VtxBuffer.Data;
                ushort *        idx_buffer     = (ushort *)cmd_list->IdxBuffer.Data;
                var             vertexElements = cmd_list->VtxBuffer.Size;
                var             indexElements  = cmd_list->IdxBuffer.Size;
                var             idxPos         = 0;

                if (vertices.Length < vertexElements)
                {
                    vertices = new ImGuiVertex[vertexElements / 2 * 3];
                    _vertexBuffer.Dispose();
                    _vertexBuffer = new VertexBuffer(graphicsDevice, ImGuiVertex.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
                }

                for (int i = 0; i < vertexElements; i++)
                {
                    DrawVert vert = *vtx_buffer++;
                    vertices[i].x     = vert.pos.X;
                    vertices[i].y     = vert.pos.Y;
                    vertices[i].tx    = vert.uv.X;
                    vertices[i].ty    = vert.uv.Y;
                    vertices[i].color = vert.col;
                }

                _vertexBuffer.SetData(vertices, 0, vertexElements);

                if (indices.Length < indexElements)
                {
                    indices = new short[indexElements / 2 * 3];
                    _indexBuffer.Dispose();
                    _indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.WriteOnly);
                }

                for (int i = 0; i < indexElements; i++)
                {
                    indices[i] = (short)idx_buffer[i];
                }
                _indexBuffer.SetData(indices, 0, indexElements);

                graphicsDevice.Indices = _indexBuffer;
                graphicsDevice.SetVertexBuffer(_vertexBuffer);

                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
                {
                    DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]);
                    if (pcmd->UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        graphicsDevice.ScissorRectangle = new Rectangle((int)pcmd->ClipRect.X, (int)pcmd->ClipRect.Y, (int)(pcmd->ClipRect.Z - pcmd->ClipRect.X), (int)(pcmd->ClipRect.W - pcmd->ClipRect.Y));
                        if (pcmd->TextureId.ToInt32() == fontTexture.GetHashCode())
                        {
                            effect.Texture = fontTexture;
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }

                        var primivites = (int)pcmd->ElemCount / 3;
                        foreach (var pass in effect.CurrentTechnique.Passes)
                        {
                            pass.Apply();
                            graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, idxPos, primivites);
                            //graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertexElements, indices, 0, indexElements / 3);
                        }
                    }
                    idxPos += (int)pcmd->ElemCount;
                }
            }
        }
Example #4
0
 void TransformVerts(DrawVert *verts, JointMat *joints)
 {
     fixed(Vector4 *scaledWeightsV = scaledWeights)
     fixed(int *weightIndexI = weightIndex)
     Simd.TransformVerts(verts, texCoords.Length, joints, scaledWeightsV, weightIndexI, numWeights);
 }
Example #5
0
        private unsafe void ImGuiRenderDraw(DrawData *drawData)
        {
            if (drawData == null)
            {
                return;
            }
            var io = ImGui.GetIO();

            if (io.DisplaySize.X <= 0.0f || io.DisplaySize.Y <= 0.0f)
            {
                return;
            }
            var st = new StateBlock(device, StateBlockType.All);
            var vp = new Viewport();

            vp.X                = vp.Y = 0;
            vp.Width            = (int)io.DisplaySize.X;
            vp.Height           = (int)io.DisplaySize.Y;
            vp.MinDepth         = 0.0f;
            vp.MaxDepth         = 1.0f;
            device.Viewport     = vp;
            device.PixelShader  = null;
            device.VertexShader = null;
            device.SetRenderState(RenderState.CullMode, Cull.None);
            device.SetRenderState(RenderState.Lighting, false);
            device.SetRenderState(RenderState.ZEnable, false);
            device.SetRenderState(RenderState.AlphaBlendEnable, true);
            device.SetRenderState(RenderState.AlphaTestEnable, false);
            device.SetRenderState(RenderState.BlendOperation, BlendOperation.Add);
            device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
            device.SetRenderState(RenderState.DestinationBlend, Blend.BothInverseSourceAlpha);
            device.SetRenderState(RenderState.ScissorTestEnable, true);
            device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Modulate);
            device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
            device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.Diffuse);
            device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate);
            device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture);
            device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.Diffuse);
            device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
            device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear);
            // Setup orthographic projection matrix
            {
                const float L            = 0.5f;
                float       R            = io.DisplaySize.X + 0.5f;
                const float T            = 0.5f;
                float       B            = io.DisplaySize.Y + 0.5f;
                RawMatrix   mat_identity = new Matrix(1.0f, 0.0f, 0.0f, 0.0f,
                                                      0.0f, 1.0f, 0.0f, 0.0f,
                                                      0.0f, 0.0f, 1.0f, 0.0f,
                                                      0.0f, 0.0f, 0.0f, 1.0f);
                RawMatrix mat_projection = new Matrix(
                    2.0f / (R - L), 0.0f, 0.0f, 0.0f,
                    0.0f, 2.0f / (T - B), 0.0f, 0.0f,
                    0.0f, 0.0f, 0.5f, 0.0f,
                    (L + R) / (L - R), (T + B) / (B - T), 0.5f, 1.0f);
                device.SetTransform(TransformState.World, ref mat_identity);
                device.SetTransform(TransformState.View, ref mat_identity);
                device.SetTransform(TransformState.Projection, ref mat_projection);
            }
            using (device.VertexDeclaration = new VertexDeclaration(device, GuiVertex.VertexElements))
            {
                for (var n = 0; n < drawData->CmdListsCount; n++)
                {
                    NativeDrawList *cmdList    = drawData->CmdLists[n];
                    DrawVert *      vtx_buffer = (DrawVert *)cmdList->VtxBuffer.Data;
                    ushort *        idx_buffer = (ushort *)cmdList->IdxBuffer.Data;

                    var myCustomVertices = new GuiVertex[cmdList->VtxBuffer.Size];
                    for (var i = 0; i < myCustomVertices.Length; i++)
                    {
                        var cl = (vtx_buffer[i].col & 0xFF00FF00) | ((vtx_buffer[i].col & 0xFF0000) >> 16) | ((vtx_buffer[i].col & 0xFF) << 16);
                        myCustomVertices[i] =
                            new GuiVertex(vtx_buffer[i].pos.X, vtx_buffer[i].pos.Y, vtx_buffer[i].uv.X, vtx_buffer[i].uv.Y, cl);
                    }

                    for (var i = 0; i < cmdList->CmdBuffer.Size; i++)
                    {
                        DrawCmd *pcmd = &((DrawCmd *)cmdList->CmdBuffer.Data)[i];
                        if (pcmd->UserCallback != IntPtr.Zero)
                        {
                            throw new NotImplementedException();
                        }
                        else
                        {
                            device.SetTexture(0, new Texture(pcmd->TextureId));
                            device.ScissorRect = new RectangleF((int)pcmd->ClipRect.X,
                                                                (int)pcmd->ClipRect.Y,
                                                                (int)(pcmd->ClipRect.Z - pcmd->ClipRect.X),
                                                                (int)(pcmd->ClipRect.W - pcmd->ClipRect.Y));
                            ushort[] indices = new ushort[pcmd->ElemCount];
                            for (int j = 0; j < indices.Length; j++)
                            {
                                indices[j] = idx_buffer[j];
                            }

                            device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, myCustomVertices.Length, (int)(pcmd->ElemCount / 3), indices, Format.Index16, myCustomVertices);
                        }
                        idx_buffer += pcmd->ElemCount;
                    }
                }
            }
            st.Apply();
            st.Dispose();
        }