OpenGL 3D模型纹理

时间:2015-11-01 23:22:37

标签: c++ opengl

我的对象加载器存在问题。它加载对象,但是当涉及到纹理化时,对象变得非常奇怪。纹理看起来与它的外观相差甚远。

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;
}

和PCH文件只是一些包含。

1 个答案:

答案 0 :(得分:0)

您还没有向我们展示任何处理纹理的代码,但是,猜测:

OpenGL的起源位于左下方。 OBJ位于左上方。翻转所有y坐标。