OpenGL – 如何将3D管道绘制为Windows屏幕保护程序

前端之家收集整理的这篇文章主要介绍了OpenGL – 如何将3D管道绘制为Windows屏幕保护程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定一组3D点和直径大小,我想绘制一个与 Windows管道屏幕保护程序 http://answers.yahoo.com/question/index?qid=20080919090035AAO55Qv相同的3D管道.

我正在使用C和OpenGL.任何人都可以帮我找到可以告诉我如何实现这一目标的资源吗?
经过一番调查后,似乎我们必须做额外的工作才能使转折点看起来光滑.如果可能的话,我正在寻找一些示例代码来说明如何实现它

谢谢

我会告诉我如何做这样的屏幕保护程序.我的管道适合3d网格,每个单元格都是一个立方体:
Y^
               |_______ 
               /  2   /|            0 vs 3
              /______/ |            1 vs 4
              |      |1|            2 vs 5
              |  3   | /
              |______|/ -->X
             /
          -Z/

每个单元中的管道配置由六位描述 – 每侧一位. 0表示单元格为空,63表示有6个段位于中心.如果将组合分解为类型,则会很少:

>短棍,x6
>长棍,x3(0-3,1-4,2-5)
>拱门,x12(2 – 1,1 – 3,2 – 3)
> T形交叉口,x12(1 – 3 – 4)
>角,x8(2 – 1 – 3,2 – 3 – 4)
>十字架,x3(3 – 1 – 2 – 4)
>四段式集群,x12(1 – 2 – 3 – 4)
>五段集群,x6
>六段星,x1.

我使用圆环四分之一用于拱门(自我合成),球形三角形补丁用于角落和圆柱体用于其他一切.星形,十字形和五段在我的模型中只是相交的圆柱体.

编辑:C#中的一些代码(我希望它在某种程度上有用).

现场的所有内容都由三种模型组合而成 – 拱形,圆柱形和球形三角形贴片,旋转和渲染多次.所有模型都在顶点数组中.很快我遇到了性能问题并实现了简单的基于距离的LOD来对抗它.所以,我生成的不是每种模型,而是一系列具有不同细分数的模型.

/// <summary>
    /// Generates full row of arch models and lod map
    /// to render them.
    /// </summary>
    /// <param name="radius">Pipe radius</param>
    /// <returns>Model with lod</returns>
    Model GenerateArches(double radius)
    {
        //Determine total number of vertices for full row
        LodEntry[] lod = new LodEntry[slicesLod.Length];

        int totalVertices = 0;
        int totalIndices = 0;
        for (int level = 0; level < slicesLod.Length; ++level)
        {
            int sl = slicesLod[level];
            int st = archStacksLod[level];
            if (st < 3) st = 3;
            int vertices = (sl + 1)*(st + 1);
            int indices = ((sl + 1)*2 + 4)*(st) - 4;
            lod[level].start = totalIndices;
            totalVertices += vertices;
            totalIndices += indices;
            lod[level].count = indices;
        }

        int[] indexArray = new int[totalIndices];
        VertexAttributes[] va = new VertexAttributes[totalVertices];

        int vCounter = 0; //index for vertices
        int iCounter = 0; //indices counter

        for (int level = 0; level < slicesLod.Length; ++level)
        {
            int iOffset = vCounter;
            int slices = slicesLod[level];
            int stacks = archStacksLod[level];
            if (stacks < 3) stacks = 3;

            for (int st = 0; st <= stacks; ++st)
            {
                double a = Math.PI*0.5*st/stacks;
                float texCoordS = st/(float) stacks;

                for (int sl = 0; sl <= slices; ++sl)
                {
                    double b = Math.PI*2*sl/slices;

                    float texCoordT = sl/(float) slices;
                    va[vCounter].S = texCoordS;
                    va[vCounter].T = texCoordT;

                    //point on central arch
                    double x0 = 0.5*Math.Sin(a);
                    double y0 = 0.5*Math.Cos(a);
                    const double z0 = 0;

                    //point displacement
                    double rx = radius*Math.Sin(a)*Math.Sin(b);
                    double ry = radius*Math.Cos(a)*Math.Sin(b);
                    double rz = radius*Math.Cos(b);

                    //normal factor
                    double nf = 1.0/Math.Sqrt(rx*rx + ry*ry + rz*rz);
                    va[vCounter].NX = (float)(rx * nf);
                    va[vCounter].NY = (float)(ry * nf);
                    va[vCounter].NZ = (float)(rz * nf);

                    //position
                    va[vCounter].X = (float) (x0 + rx);
                    va[vCounter].Y = (float) (y0 + ry);
                    va[vCounter].Z = (float) (z0 + rz);
                    ++vCounter;
                }
            }

            for (int stack = 0; stack < stacks; ++stack)
            {
                for (int slice = 0; slice <= slices; ++slice)
                {
                    indexArray[iCounter++] = iOffset + stack * slices + slice + stack;
                    indexArray[iCounter++] = iOffset + (stack + 1) * slices + slice + 1 + stack;
                }
                if (stack < stacks - 1)
                {
                    indexArray[iCounter++] = iOffset + stack * slices + slices + stack;
                    indexArray[iCounter++] = iOffset + stack * slices + slices + stack;
                    indexArray[iCounter++] = iOffset + (stack + 1) * slices + slices + 2 * (stack + 1) - stack;
                    indexArray[iCounter++] = iOffset + (stack + 1) * slices + slices + 2 * (stack + 1) - stack;
                }
            }
        }
        return new Model(va,indexArray,lod);
    }

    /// <summary>
    /// Generates indices for rendering of vertex array,/// representing a cylinder section.
    /// Vertices assumed to be stored slice by slice:
    /// 0 1 2 3 ................... cylStacks-1,/// cylStacks .................. 2*cylStacks-1,/// ....................................,/// (cylSlices-1)*cylStacks .. cylSlices*cylStacks-1.
    /// </summary>
    /// <param name="radius"></param>
    private Model GenerateCylinders(double radius)
    {
        LodEntry[] lod = new LodEntry[slicesLod.Length];

        int totalVertices = 0;
        int totalIndices = 0;
        for (int level = 0; level < slicesLod.Length; ++level)
        {
            int sl = slicesLod[level];
            int st = cylStacksLod[level];
            int vertices = (sl + 1)*(st + 1);
            int indices = ((sl+1)*2 + 4)*st - 4;
            lod[level].start = totalIndices;
            totalVertices += vertices;
            totalIndices += indices;
            lod[level].count = indices;
        }

        int[] indexArray = new int[totalIndices];
        VertexAttributes[] va = new VertexAttributes[totalVertices];

        int vCounter = 0; //index for vertex attributes
        int iCounter = 0; //indices counter

        for (int level = 0; level < slicesLod.Length; ++level)
        {
            int iOffset = vCounter;
            int slices = slicesLod[level];
            int stacks = cylStacksLod[level];

            for (int st = 0; st <= stacks; ++st)
            {
                double i = 0.5 - 0.5 * st / stacks;
                float texCoordS = st / (float)stacks;
                for (int sl = 0; sl <= slices; ++sl)
                {
                    double b = Math.PI * 2 * sl / slices;

                    //tex coords
                    float texCoordT = sl / (float)slices;
                    va[vCounter].S = 0.5f * texCoordS;
                    va[vCounter].T = texCoordT;

                    //point on central axis
                    const double x0 = 0;
                    const double y0 = 0;
                    double z0 = i;

                    //point displacement
                    double rx = radius*Math.Cos(b);
                    double ry = radius*Math.Sin(b);
                    const double rz = 0;

                    //normal factor
                    double nf = 1.0/Math.Sqrt(ry*ry + rx*rx);
                    va[vCounter].NX = (float)(rx * nf);
                    va[vCounter].NY = (float)(ry * nf);
                    va[vCounter].NZ = 0.0f;

                    va[vCounter].X = (float)(x0 + rx);
                    va[vCounter].Y = (float)(y0 + ry);
                    va[vCounter].Z = (float)(z0 + rz);

                    ++vCounter;
                }
            }

            for (int stack = 0; stack < stacks; ++stack)
            {
                for (int slice = 0; slice <= slices; ++slice)
                {
                    indexArray[iCounter++] = iOffset + stack*slices + slice + stack;
                    indexArray[iCounter++] = iOffset + (stack + 1)*slices + slice + 1 + stack;
                }
                if (stack < stacks - 1)
                {
                    indexArray[iCounter++] = iOffset + stack * slices + slices + stack;
                    indexArray[iCounter++] = iOffset + stack * slices + slices + stack;
                    indexArray[iCounter++] = iOffset + (stack + 1) * slices + slices + 2 * (stack + 1) - stack;
                    indexArray[iCounter++] = iOffset + (stack + 1) * slices + slices + 2 * (stack + 1) - stack;
                }
            }
        }
        return new Model(va,lod);
    }

    static int R0(int _slices,int _level)
    {
        return _level * (_slices+2) - (int)(0.5 * _level * (_level + 1));
    }

    static int RL(int _slices,int _level)
    {
        return _slices - _level + 1;
    }

    private Model GenerateSphereSegment(double radius)
    {
        //Determine total number of vertices for full row
        LodEntry[] lod = new LodEntry[slicesLod.Length];

        int totalVertices = 0;
        int totalIndices = 0;
        for (int level = 0; level < slicesLod.Length; ++level)
        {
            int sl = slicesLod[level] >> 2;
            int vertices = (((2 + sl) * (sl + 1)) >> 1);
            int indices = sl * (sl + 3);
            lod[level].start = totalIndices;
            totalVertices += vertices;
            totalIndices += indices;
            lod[level].count = indices;
        }

        int[] indexArray = new int[totalIndices];
        VertexAttributes[] va = new VertexAttributes[totalVertices];

        int vCounter = 0; //index for vertices
        int iCounter = 0; //indices counter

        for (int level = 0; level < slicesLod.Length; ++level)
        {
            int sphSlices = slicesLod[level]>>2;
            int iOffset = vCounter;  //index offset for level
            for (int sl = 0; sl <= sphSlices; ++sl)
            {
                double a = Math.PI*sl*0.5/sphSlices;
                double Y = radius*Math.Sin(a);
                double Ry = radius*Math.Cos(a);
                for (int st = 0; st <= sphSlices - sl; ++st)
                {
                    double X,Z,b;
                    if (sphSlices > sl)
                    {
                        b = Math.PI*0.5*st/(sphSlices - sl);
                        X = Ry*Math.Sin(b);
                        Z = Ry*Math.Cos(b);
                    }
                    else
                    {
                        X = 0;
                        Z = 0;
                        b = 0;
                    }
                    va[vCounter].S = (float)(0.5 / 3 * a);
                    va[vCounter].T = (float)(0.14 * b);
                    double coeff = 1/Math.Sqrt(X*X + Y*Y + Z*Z);
                    va[vCounter].NX = (float)(X * coeff);
                    va[vCounter].NY = (float)(Y * coeff);
                    va[vCounter].NZ = (float)(Z * coeff);
                    va[vCounter].X = (float)(va[vCounter].NX * radius);
                    va[vCounter].Y = (float)(va[vCounter].NY * radius);
                    va[vCounter].Z = (float)(va[vCounter].NZ * radius);
                    ++vCounter;
                }
            }

            for (int k = 0; k < sphSlices; ++k)
            {
                int lastS = RL(sphSlices,k);
                for (int s = 0; s < lastS - 1; ++s)
                {
                    int c0 = R0(sphSlices,k) + s;
                    int cn = R0(sphSlices,k) + s + RL(sphSlices,k);
                    indexArray[iCounter++] = cn + iOffset;
                    indexArray[iCounter++] = c0 + iOffset;
                }
                int tail = R0(sphSlices,k) + lastS - 1;
                indexArray[iCounter++] = tail + iOffset;
                indexArray[iCounter++] = tail + iOffset;
            }
        }
        return new Model(va,lod);
    }
原文链接:https://www.f2er.com/windows/364251.html

猜你在找的Windows相关文章