2015-11-01 154 views
-1

我有我的對象加載器的問題。它加載對象,但是當涉及紋理時,對象變得奇怪。紋理看起來應該看起來很遠。OpenGL 3D模型紋理

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文件只是一些包括。

+2

無法重現 - 此代碼不會渲染任何奇怪的東西。事實上,它沒有渲染任何東西。 – immibis

回答

0

你還沒有告訴我們任何的代碼來處理紋理,但在猜測:

OpenGL的原點在左下角。 OBJ's在左上方。翻轉你所有的y座標。

+0

我知道這個問題,但是當我修復它時,我在180度上旋轉紋理,而不是垂直翻轉它,以便在旋轉後水平翻轉。 – pandicacao