简体   繁体   English

在C ++中构建UV Sphere

[英]Building a UV Sphere in c++

I'm trying to make a UV sphere in C++ using Qt Creator, I want to build the sphere without using the openGL commands. 我正在尝试使用Qt Creator在C ++中制作UV球体,我想在不使用openGL命令的情况下构建球体。 I'm trying trying to add the vertices to lObject and then add the normals and triangles. 我正在尝试将顶点添加到lObject,然后添加法线和三角形。 The sphere will have a radius of 1. First problem is that it doesn't render a sphere when drawn, so maybe I'm not adding the right vertices or maybe I'm not adding the triangles correctly. 球体的半径为1。第一个问题是,绘制时它不会渲染球体,所以也许我没有添加正确的顶点,或者我没有正确添加三角形。 Any help on what I'm doing wrong would be great. 任何对我做错事的帮助都会很棒。

Here's what I've tried: 这是我尝试过的:

NodeObject* ObjectFactory::buildSphere(int slices, int stacks)
{ 
    // Allocate a new node object
    NodeObject* lObject = new NodeObject();

    for(int i=0; i<stacks; i++)
    {
        double lnum1 = 360.0/stacks;
        double lTheta = ((double)i)*(lnum1*(M_PI/180.0));       
        double lNextTheta = ((double)(i+1))*lnum1*(M_PI/180.0);    

        for(int j=0; j<slices; j++)
        {
            double lnum2 = 180.0/slices;
            double lPhi = ((double)i)*(lnum2*(M_PI/180.0));     
            double lNextPhi = ((double)(i+1))*lnum1*(M_PI/180.0);

            lObject->addVertex(0.0, 1.0, 0.0); //Top

            lObject->addVertex(sin(lTheta)*cos(lPhi), sin(lTheta)*sin(lPhi), cos(lTheta));
            lObject->addVertex(sin(lNextTheta)*cos(lNextPhi), sin(lNextTheta)*sin(lNextPhi),    cos(lNextTheta));

            lObject->addVertex(sin(lTheta)*cos(lPhi), -(sin(lTheta)*sin(lPhi)), cos(lTheta));
            lObject->addVertex(sin(lNextTheta)*cos(lNextPhi), -(sin(lNextTheta)*sin(lNextPhi)),              cos(lNextTheta));

            lObject->addVertex(0.0, -1.0, 0.0); //Bottom

            lObject->addNormal(0.0,1.0,0.0);
            lObject->addNormal(0.0,-1.0,0.0);
            lObject->addNormal(sin(lTheta)*cos(lPhi),sin(lTheta)*sin(lPhi), cos(lTheta));

            for(int k=0; k<pSlices*6; k++)
            {
                if(i==0) { lObject->addTriangle(0,1,2,0,0,0); }
                else if(i+1 == stacks) {lObject->addTriangle(2,0,1,0,0,0); }
                else
                {
                    lObject->addTriangle(k, k+1, k+2,k,k+1,k+2);
                }
            }
        }

    }
    return lObject;
}

In you third for loop what is the value for pSlices? 在您的第三个for循环中,pSlices的值是什么? Also why are you adding top and bottom for each stack? 另外,为什么还要为每个堆栈添加顶部和底部?

And for better practice generate the vertices first, then do other stuff. 为了更好的实践,首先生成顶点,然后再进行其他处理。

You can use a simple data structure for holding the data such as, also generate one layer of vertices at the inner loop.: 您可以使用简单的数据结构来保存数据,例如,还可以在内环处生成一层顶点。

QVector3D sphereVertices[stacks][slices];

For first layer fill it with (0.0, 1.0, 0.0); 对于第一层,用(0.0,1.0,0.0)填充; For last layer fill it with (0.0, -1.0, 0.0); 对于最后一层,用(0.0,-1.0,0.0)填充;

Then iterate over the vertices to calculate normals and create triangles in CCW. 然后遍历顶点以计算法线并在CCW中创建三角形。

for ( int i = 0; i < stacks -1; i++){ //-1 is due to we are using the next stack to create the face
    for ( int j = 0; j < slices -1; j++){ //we are also using the next slice
        //Add these vertex indices
        //First triangle indices
        //i*slices + j, (i+1)*slices + j, (i+1)*slices + j + 1
        //Second triangle indices
        //i*slices + j, (i+1)*slices + j + 1, i*slices + j + 1
        //Furthermore you can calculate triangle normal by using these vertices
        //https://www.opengl.org/wiki/Calculating_a_Surface_Normal
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM