简体   繁体   中英

OpenGL 3D model texturing

I have issues with my object loader. It loads the object, but when it comes to texturing the object gets all weird. The texture looks really far from what it should look like.

OBJloader.h:

#include "ShadersPCH.h"
#include <fstream>
#include <sstream>
using namespace std;

static struct Material
{
    string name;
    glm::vec3 Ka, Kd, Ks;
    float d;
    float Ns;
    float illum;
    string map_Kd;
};

static struct MaterialUsage
{
    unsigned int number, textureID, materialIndex;
};

class OBJ
{
    private:

    public:
        vector<glm::vec3> Vertecies;
        vector<glm::vec3> Normals;
        vector<glm::vec2> UVs;
        vector<MaterialUsage> NumberOfVerteciesWihtSameMaterial;
        Material *Materials;
        unsigned int MaterialNumber = 2;
        OBJ(void);
        OBJ(unsigned int);
        ~OBJ();
        bool loadOBJ(const char* filename);
        bool loadMTL(const char* filename);
};

OBJloader.cpp:

#include "OBJloader.h"

OBJ::OBJ(void)
{
    MaterialNumber = 1;
    Materials = new Material[MaterialNumber];
    cout << "OBJ was created with data = " << MaterialNumber << endl;
}
OBJ::OBJ(unsigned int size)
{
    MaterialNumber = size;
    Materials = new Material[MaterialNumber];
    cout << "OBJ was created with data = " << MaterialNumber << endl;
}
OBJ::~OBJ(void)
{
    delete[] Materials;
    cout << "OBJ was deleted" << endl;
}

bool OBJ::loadMTL(const char* filename)
{
    int MaterialIndex = -1;
    ifstream in(filename, ios::in);
    if (!in)
    {
        cout << "Cannot open mtl file.\n";
        return false;
    }
    string line;
    while (!in.eof())
    {
        getline(in, line);
        if (line.substr(0, 18) == "# Material Count: ")
        {
        istringstream s(line.substr(18));
        unsigned int size;
        s >> size;
        continue;
    }
    if (line.substr(0, 7) == "newmtl ")
    {
        MaterialIndex++;
        string s = line.substr(7);
        Materials[MaterialIndex].name = s;
        continue;
    }
    if (line.substr(0, 3) == "Ns ")
    {
        istringstream s(line.substr(3));
        s >> Materials[MaterialIndex].Ns;
        continue;
    }
    if (line.substr(0, 3) == "Ka ")
    {
        istringstream s(line.substr(3));
        s >> Materials[MaterialIndex].Ka.x;
        s >> Materials[MaterialIndex].Ka.y;
        s >> Materials[MaterialIndex].Ka.z;
        continue;
    }
    if (line.substr(0, 3) == "Kd ")
    {
        istringstream s(line.substr(3));
        s >> Materials[MaterialIndex].Kd.x;
        s >> Materials[MaterialIndex].Kd.y;
        s >> Materials[MaterialIndex].Kd.z;
        continue;
    }
    if (line.substr(0, 3) == "Ks ")
    {
        istringstream s(line.substr(3));
        s >> Materials[MaterialIndex].Ks.x;
        s >> Materials[MaterialIndex].Ks.y;
        s >> Materials[MaterialIndex].Ks.z;
        continue;
    }
    if (line.substr(0, 2) == "d ")
    {
        istringstream s(line.substr(2));
        s >> Materials[MaterialIndex].d;
        continue;
    }
    if (line.substr(0, 3) == "Tr ")
    {
        istringstream s(line.substr(3));
        s >> Materials[MaterialIndex].d;
        continue;
    }
    if (line.substr(0, 7) == "map_Kd ")
    {
        Materials[MaterialIndex].map_Kd = line.substr(7);
        continue;
    }
}
return true;
}

and the PCH file is just some includes.

You haven't shown us any of the code to deal with texturing but, at a guess:

OpenGL's origin is in the bottom left. OBJ's is in the top left. Flip all your y coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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