0
\$\begingroup\$

I have the following rendering method written in C# and apparently using OpenGL immediate mode, and i'm trying to port it to C++ and DirectX:

protected override void RenderFace(int faceIndex)
{
        var face = Map.Faces[faceIndex];

        if (Map.Faces[faceIndex].Styles[0] == 0xFF) // Skip sky faces
            return;


        GL.Color3(Map.FaceColors[faceIndex]);

        BeginMode mode;
        switch (face.NumEdges)
        {
            case 3:
                mode = BeginMode.Triangles;
                break;
            case 4:
                mode = BeginMode.Quads;
                break;
            default:
                mode = BeginMode.Polygon;
                break;
        }

        GL.Begin(mode);
        for (int i = 0; i < Map.Faces[faceIndex].NumEdges; i++)
        {
            var vNormal = Map.Planes[face.PlaneId].Normal;
            var normal = new Vector3(vNormal[0], vNormal[1], vNormal[2]);

            if (Map.Faces[faceIndex].PlaneSide != 0)
                normal = normal * -1;

            int iEdge = Map.SurfEdges[face.FirstEdge + i]; // This gives the index into the edge lump

            if (iEdge > 0)
            {
                var edge = Map.Edges[iEdge];
                var point = Map.Vertices[edge.Vertices[0]].Point;
                var vertex = new Vector3(point[0], point[1], point[2]);

                GL.Vertex3(vertex.X, vertex.Y, vertex.Z);
            }
            else
            {
                iEdge *= -1;

                var edge = Map.Edges[iEdge];
                var point = Map.Vertices[edge.Vertices[1]].Point;
                var vertex = new Vector3(point[0], point[1], point[2]);

                GL.Vertex3(vertex.X, vertex.Y, vertex.Z);
            }
        }
        GL.End();
}

I have no experience of immediate mode using DirectX (and i'm not very experienced in DirectX either...). How would you port this code using vertex buffers? I was thinking about using an ID3D11Buffer for every Face, does it make sense?

Thanks

\$\endgroup\$
  • 2
    \$\begingroup\$ Assuming this code is used during runtime rendering, I would recommend to NOT port this code to DirectX, but rather to dump the output to disk (e.g. in OBJ format) and use this output to fill (vertex) buffers while loading your scene. It is very inefficient to render your models like in the code you've showed. rastertek.com is a really good resource if you want to start with using DirectX. \$\endgroup\$ – Krienie Dec 24 '19 at 10:29
  • \$\begingroup\$ Thanks, that's exactly the website i've been following until now. And that's the reason why i couldnt understand how to port this code using buffers. \$\endgroup\$ – rekotc Dec 24 '19 at 11:02
  • 1
    \$\begingroup\$ Using a separate buffer for each face would be incredibly inefficient. The idea if to have a single large buffer containing all of your faces, and do as much work with as few draw calls as possible. This is as true of OpenGL (with buffer objects) as it is of Direct 3D. \$\endgroup\$ – Maximus Minimus Dec 24 '19 at 17:00
  • 1
    \$\begingroup\$ @rekotc - look at the parameters of your draw call, either glDrawArrays or ID3D11DeviceContext::Draw, they both have start and count parameters, meaning they can be used to draw a subrange of a buffer; there's nothing in either API requiring you to draw the entire buffer with each draw call. \$\endgroup\$ – Maximus Minimus Dec 26 '19 at 20:20
  • 1
    \$\begingroup\$ @rekotc - this is actually the fault of tutorial writers; you'll see the very same problem with OpenGL buffer object tutorials. \$\endgroup\$ – Maximus Minimus Dec 27 '19 at 15:50
0
\$\begingroup\$

PrimitiveBatch in XNA Game Studio was designed for this style of "immediate mode" rendering. There is an implementation of this class for C++ in the DirectX Tool Kit for DX11 / DX12.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Sorry, i had to be more specific in my question, i'm trying to port the code to C++, and i've learnt to use ID3D11Buffers everywhere, so i'm a bit stuck with this C# implementation... (i'm an hobbyist CG developer). I updated the question. \$\endgroup\$ – rekotc Dec 24 '19 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.