参数化3D模型

时间:2014-11-05 14:01:17

标签: opengl 3d texture-mapping 3d-model

我正在与一个小组合作开发一个捕获人类2D图像并将其转换为3D模型的应用程序。我们已经使用stasm库来执行ASM http://www.milbo.org/stasm-files/stasm4.pdf,我们正在制定一个有限的时间计划,我们需要在11月底之前完成这个项目,因此了解我们想要通过OpenGL做什么并不是我们最好的想法,因为它有一个非常先进的学习曲线

我们要做的是根据来自stasm的数据来参数化面部的特征,所以我们将这个模型分类为我的鼻子作为搅拌器上的一个对象,所以obj文件我们在关键点上面添加了注释在鼻子里

然后我们开发了一个解析器

int str2num(string s){

    if (s.length() > 2){
        string str= s.substr((s.length()-2), s.find(' '));
        int value = (int)atof(str.c_str());
        return value;
    }
    else
        return 0;

}


void rokko::objToTxt(const string aInFilename,const string aOutFilename,bool aVerbose)
{
    if(aVerbose) cout << "Loading OBJ file <"
                      << aInFilename << ">" << endl;

    // Open file
    ifstream objFile(aInFilename.c_str());

    if(objFile.fail())
    {
        cout << "Error: could not open file <"
             << aInFilename << ">" << endl;
        exit(1);
    }


    // Extract verts, normals, textures, and faces
    vector<int> verts, norms, textures;
    vector<int> faces;
    // map<int,int> faceHash;

    vector<int> finalVerts, finalNorms, finalTextures;
    vector<int> finalFaces;

    string line;
    int line_size=0;
    int hashIndex = 0;

    if(aVerbose) cout << "Extracting values from file" << endl;

    // Visit each line of the obj file
    while(getline(objFile, line))
    {
        // Extract vertex
        // Line starts with v[space]...
        if(line[0] == 'v' && line[1] == ' ')
        {
            string lineVals = line.substr(2);
            float val;

            string val0 = lineVals.substr(0, lineVals.find(' '));
            val = (float)atof(val0.c_str());
            verts.push_back(val);
            line_size = val0.size();
            string val1 = lineVals.substr(val0.length() + 1,
                                          lineVals.find(' '));
            val = (float)atof(val1.c_str());
            verts.push_back(val);


            string val2 = lineVals.substr(lineVals.find_last_of(' ') + 1);
            val = (float)atof(val2.c_str());
            verts.push_back(val);
        }


        // Extract textures
        // Line starts with vt[space]...
        else if(line[0] == 'v' && line[1] == 't' && line[2] == ' ')
        {
            string lineVals = line.substr(3);
            float val;

            string val0 = lineVals.substr(0, lineVals.find(' '));
            val = (float)atof(val0.c_str());
            textures.push_back(val);

            string val1 = lineVals.substr(val0.length() + 1,
                                          lineVals.find(' '));
            val = (float)atof(val1.c_str());
            textures.push_back(val);
        }


        // Extract normals
        // Line starts with vn[space]...

        else if(line[0] == 'v' && line[1] == 'n' && line[2] == ' ')
        {
            string lineVals = line.substr(3);
            float val;

            string val0 = lineVals.substr(0, lineVals.find(' '));
            val = (float)atof(val0.c_str());
            norms.push_back(val);

            string val1 = lineVals.substr(val0.length() + 1,
                                          lineVals.find(' '));
            val = (float)atof(val1.c_str());
            norms.push_back(val);

            string val2 = lineVals.substr(lineVals.find_last_of(' ') + 1);
            val = (float)atof(val2.c_str());
            norms.push_back(val);
        }


    }

    for (int i=0; i<verts.size();i++){
        cout << "verts " << i << ": " << verts[i] << endl;}

    for (int i=0; i<verts.size();i++)
    {

        verts[i] = i * 2;
        cout << "verts_new " << i << ": " << verts[i] << endl;}



    // Parametrizing the nose part



    ifstream model ("test.obj");
    ofstream out ("out.obj");


    int test = 1;

    string str;
    string line1, line2,line3;
    int replace_size = (line_size *3) + 2;

    string lineV;
    float val_x, val_y, val_z;
    string val0, val1, val2;

    while(!model.eof())
    {


        getline(model,line1);

        out<<line1<<endl;

        while(line1 =="o N")
        {
            getline(model,line2);


            if (line2 =="o H"){

                line1 = "o H";
                out<<line2<<endl;
            }
            else
            {

                string compare = line2.substr(0, line2.find(' '));

                switch(str2num(compare)){



                case 48: // "#nose_48

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x+0.1<<" "<<val_y<<" "<<val_z<<endl;

                    break;

                case 49: // "#nose_49

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x<<" "<<val_y<<" "<<val_z<<endl;


                    break;

                case 50: // "#nose_50

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x-0.1<<" "<<val_y<<" "<<val_z<<endl;


                    break;

                case 51: // "#nose_51

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x<<" "<<val_y-0.1<<" "<<val_z<<endl;



                    break;

                case 52: // "#nose_52


                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x<<" "<<val_y<<" "<<val_z<<endl;



                    break;

                case 53: // "#nose_53


                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x<<" "<<val_y-0.1<<" "<<val_z<<endl;



                    break;

                case 54: // "#nose_54

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x+0.1<<" "<<val_y<<" "<<val_z<<endl;



                    break;



                case 55: // "#nose_55

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x+0.1<<" "<<val_y<<" "<<val_z<<endl;



                    break;



                case 56: // "#nose_56

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x<<" "<<val_y-0.2<<" "<<val_z<<endl;



                    break;


                case 57: // "#nose_57

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x-0.1<<" "<<val_y<<" "<<val_z<<endl;



                    break;


                case 58: // "#nose_58

                    getline(model,line3);

                    lineV = line3.substr(2);


                    val0 = lineV.substr(0, lineV.find(' '));
                    val_x = (float)atof(val0.c_str());


                    line_size = val0.size();

                    val1 = lineV.substr(val0.length() + 1,lineV.find(' '));
                    val_y= (float)atof(val1.c_str());


                    val2 = lineV.substr(lineV.find_last_of(' ') + 1);
                    val_z= (float)atof(val2.c_str());

                    out<<"v "<<val_x-0.1<<" "<<val_y<<" "<<val_z<<endl;



                    break;


                    //

                default:

                    out<<line2<<endl;



                    break;
                }

            }



        }


    }

    model.close();
    out.close(); 

}

我们似乎无法做到正确

在不使用难以学习的库的情况下,替代方法的任何帮助。

0 个答案:

没有答案