2013-11-01 4 views
-1

obj 로더를 작성하고 모든 로딩 작업을 올바르게 말할 수있는 한 텍스처를 그리려고 할 때 작동하지 않습니다.OpenGL, Devil, 텍스처가있는 obj 로더

나는 악마를 들여다 보았다.로드 후 아무런 오류가 없다. 모든 데이터가 있지만, 그릴 때 이미지가 검은 색으로 표시되지 않는다.

나는 여러분이 내가 을 들여다 보길 권할만한 것은 무엇이든지 이미 몇 가지를 살펴 보았지만 해결책을 찾지 못했습니다. 내가 일을하는 방법에

나는 질감

Loader.h

#pragma once 
#include "typedef.h" 
#include <string> 
#include <vector> 
#include <fstream> 

#include "MTL.h" 
#include "../freeglut.h" 

#include <iostream> // this is just for testing, you can remove this with all the cout's 

class Loader 
{ 
public: 
    /** 
    * \brief string location of the obj file 
    * 
    * In Order for you to Load the 3D object you need to specify its location 
    * in a string. 
    * 
    * 
    */ 
    Loader(std::string input); 

    ~Loader(void); 


    void draw(); // this function takes the obj file and draws it 

    //this can be ignored, it does not make any changes to the problem 
    void move(float x, float y, float z); 
    void getPos(); 
    void setPos(float x, float y, float z); // set position 
    void find_box(float &maxX,float &minX,float &maxY, float &minY, float &maxZ,float &minZ); 

private: 




    std::ifstream m_inFile; 

    // the list of vectors that i will be using 
    std::vector<points> m_points; 
    std::vector<normal> m_normals; 
    std::vector<coordinate> m_coords; 

    std::vector<MTL> m_mtl; 

    std::vector<face> m_faces; 

    void process(std::string input); 
    void processMTL(std::string input); 

    void inputPoints(points temp); 
    void inputNormals(normal temp); 
    void inputCoordinates(coordinate temp); 
    void createFaces(face temp); 


}; 

Loader.cpp

void Loader::process(std::string input) 
{ 
    std::string identifier; //used to identify where the input should go 


    points temppoint; 
    normal tempnormal; 
    coordinate tempcoord; 
    face tempface; 


    std::string read;  // used to read the curent line 

    int readNum; // this is the number that has just been read in 
    char skip; // a char to skip thr/
    int i; 
    int count= 1; 

    //other things to take into account 
    //**************************************** 
    std::string l_name; 
    bool firstRun = true; 
    std::string parse; 
    //************************************** 


    m_inFile.open(input); 



    //creation of the reading loop 

    m_inFile >> identifier; 

    do { 
     // check to see what the opening is 

     if (identifier =="#") 
     { 
      getline(m_inFile,read); // use this to read the whole line 

     } 
     else if(identifier == "v") 
     { 
      m_inFile >> temppoint.x >> temppoint.y >> temppoint.z; 
      inputPoints(temppoint); 
     } 
     else if(identifier == "vn") 
     { 
      m_inFile >> tempnormal.vn[0] >> tempnormal.vn[1] >> tempnormal.vn[2]; 
      inputNormals(tempnormal); 
     } 
     else if (identifier == "vt") 
     { 
      m_inFile >> tempcoord.vt[0] >> tempcoord.vt[1] >> tempcoord.vt[2]; 
      inputCoordinates(tempcoord); 
     } 
     else if(identifier == "f") 
     { 


      for(i =0; i < 3; i++) 
      { 
       count++; 


       m_inFile >> readNum; 
       if(readNum == 0) 
        break; 
       readNum--; 
       tempface.p[i].x = m_points[readNum].x; 
       tempface.p[i].y = m_points[readNum].y; 
       tempface.p[i].z = m_points[readNum].z; 


       m_inFile >> skip >> readNum; 
       readNum--; 
       tempface.coord[i].vt[0] = m_coords[readNum].vt[0]; 
       tempface.coord[i].vt[1] = m_coords[readNum].vt[1]; 
       tempface.coord[i].vt[2] = m_coords[readNum].vt[2]; 


       m_inFile >> skip >> readNum; 
       readNum--; 
       tempface.norm[i].vn[0] = m_normals[readNum].vn[0]; 
       tempface.norm[i].vn[1] = m_normals[readNum].vn[1]; 
       tempface.norm[i].vn[2] = m_normals[readNum].vn[2]; 



      } 

      tempface.name = l_name; // this gives the mtl name to the face so it can be compared and applied later 

      createFaces(tempface); 

     } 
     else if(identifier =="mtllib") 
     { 
      m_inFile >> identifier; 
      identifier = "test/" + identifier; 
      processMTL(identifier); 
     } 

     else if(identifier == "usemtl") 
     { 
     // chnages the mtl that is being applied 
      m_inFile >> read; 

      l_name = read; // so the face can have 

     } 
     else 
     { 
      getline(m_inFile,read); 
      std::cout << "Not Processed From Loader" << identifier << " " << read <<std::endl; 
     } 

     m_inFile >> identifier; 

    } while (!m_inFile.eof()); 
    //m_inFile.close(); 
    //m_inFile.~basic_ifstream(); 
} 



void Loader::draw() 
{ 
    int i; 
    int j; 
    int k; 

    std::string currentTex; 

    glEnable(GL_TEXTURE_2D); 
    //glDisable(GL_TEXTURE_2D); 
    //glBegin(GL_TRIANGLES); 

    for (i=0; i < m_faces.size();i++) 
    { 
     //bind a certain image 
     for (k=0; k<m_mtl.size();k++) 
     { 
      if (m_mtl[k].compare(m_faces[i].name) == true) 
      { 
       //these next two lines do the same thing 
       //m_mtl[k].draw(); 
       glBindTexture(GL_TEXTURE_2D,m_mtl[k].getGLID()); 
       //break; // break out of the loop 
      } 

     } 

     glBegin(GL_TRIANGLES); 

     for(j = 0 ; j < 3; j++) 
     { 
      glNormal3f(m_faces[i].norm[j].vn[0],m_faces[i].norm[j].vn[1],m_faces[i].norm[j].vn[2]); 
      glTexCoord2f(m_faces[i].coord[j].vt[0],m_faces[i].coord[j].vt[1]); 
      glVertex3f(m_faces[i].p[j].x,m_faces[i].p[j].y,m_faces[i].p[j].z); 
     } 
     glEnd(); 

    } 

} 





void Loader::processMTL(std::string input) 
{ 

    std::string identifier; 
    std::string read; //for reading whole lines 

    MTL mtlTemp; 
    std::ifstream l_inMtl; 

    //for reading in numbers to give to the mtl 
    float ka0, ka1, ka2; 
    float kd0, kd1, kd2; 
    float ks0, ks1, ks2; 

    //so that an empty mtl is not pushed back into the vector 
    bool firstRun = true; 

    l_inMtl.open(input); 
    l_inMtl >> identifier; 

    do{ 
     if(identifier == "#") 
     { 
      getline(l_inMtl,read); 
     } 
     else if(identifier == "newmtl") 
     { 
      //checks to see if it has run before if it has not run before don't push back the last read 
      if (firstRun == false) 
       m_mtl.push_back(mtlTemp); 
      else 
       firstRun = false; 


      l_inMtl >> identifier; 
      mtlTemp.setName(identifier); 
     } 
     else if(identifier == "Ka") 
     { 
      l_inMtl >>ka0 >> ka1 >> ka2; 
      mtlTemp.setKa(ka0,ka1,ka2); 
     } 
     else if(identifier == "Kd") 
     { 
      l_inMtl >> kd0 >>kd1 >> kd2; 
      mtlTemp.setKd(kd0,kd1,kd2); 
     } 
     else if(identifier =="Ks") 
     { 
      l_inMtl >> ks0 >> ks1 >> ks2; 
      mtlTemp.setKs(ks0, ks1, ks2); 
     } 
     else if(identifier == "map_Ka") 
     { 
      getline(l_inMtl,identifier); 
      //l_inMtl >> identifier; 
      mtlTemp.setLoc(identifier); 
     } 
     else 
     { 
      getline(l_inMtl,read); 
      std::cout << "Not Processed MTL" << identifier << " " << read << std::endl; 
     } 
     l_inMtl >> identifier; 
    }while(!l_inMtl.eof()); 

    l_inMtl.close(); 
    m_mtl.push_back(mtlTemp); // so the last one is pushed back 

} 

MTL.h

#pragma once 


#include "../freeglut.h" 

#include <string> 
#include <fstream> 
#include "typedef.h" 
#include "../lib/IL/il.h" 
#include <iostream> 
//#include <IL/il.h> 

class MTL 
{ 
public: 
    MTL(); //name of the file 
    ~MTL(void); 

    void bind(std::string bindType);// somehow set the bind type eg tile stretch ect 

    void setVar(float ka0, float ka1, float ka2,float kd0, float kd1, float kd2, float ks0, float ks1, float ks2); // takes in all the inputs and sets them to the class 
    void setLoc(std::string inLocation); // set the location 
    void setName(std::string inName); 

    void setKa(float ka0, float ka1, float ka2); 
    void setKd(float kd0, float kd1, float kd2); 
    void setKs(float ks0, float ks1, float ks2); 

    void setNs(float inNs); 

    bool compare(std::string inName); // compare the string with another 
    void draw(); // change the binds 
    GLuint getGLID() { return texture;}; 

private: 

    std::string name; 
    std::string location; // the location of the image 

    float ka[3]; 
    float kd[3]; 
    float ks[3]; 


    // must have freeglut in order to use this 
    BYTE *pixmap; // contains the data (not yet set) 
    GLuint texture; 

    //two functions that you need to load and display 
    void LoadThing(); 
    void createTex(int width, int height, int chan); 

}; 
당 만든 MTL이

MTL.cpp

#include "MTL.h" 

MTL::MTL()/
{ 
} 
MTL::~MTL() 
{ 

} 

void MTL::setName(std::string inName) 
{ 
    name = inName; 
} 







void MTL::setLoc(std::string inLocation) 
{ 
    location = inLocation; 

    LoadThing(); 
} 
bool MTL::compare(std::string inName) // compare the string with another 
{ 
    //return true if they are the same 
    if (name == inName) 
     return true; 
    else 
     return false; 

} 
void MTL::draw() // change the binds 
{ 
    glBindTexture(GL_TEXTURE_2D, texture); 

} 

void MTL::setKa(float ka0, float ka1, float ka2) 
{ 
    ka[0] = ka0; 
    ka[1] = ka1; 
    ka[2] = ka2; 
} 

void MTL::setKd(float kd0, float kd1, float kd2) 
{ 
    kd[0] = kd0; 
    kd[1] = kd1; 
    kd[2] = kd2; 
} 

void MTL::setKs(float ks0, float ks1, float ks2) 
{ 
    ks[0] = ks0; 
    ks[1] = ks1; 
    ks[2] = ks2; 
} 

void MTL::LoadThing() 
{ 
    ILuint devImg; 
    ilGenImages(1,&devImg); 
    ilBindImage(devImg); 

    int error = ilLoadImage("marble_floor2.png"); 

    int e = ilGetError(); 

    pixmap = ilGetData(); 
    int width = ilGetInteger(IL_IMAGE_WIDTH); 
    int height = ilGetInteger(IL_IMAGE_HEIGHT); 
    int chan = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); 

    createTex(width,height,chan); 
} 

void MTL::createTex(int width, int height, int chan) 
{ 
    glEnable(GL_TEXTURE_2D); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    glGenTextures(1, &texture); 


    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glBindTexture(GL_TEXTURE_2D, texture); 

    glTexImage2D(GL_TEXTURE_2D, 0, chan, width, height, 0, (chan == 4)?GL_RGBA: GL_RGB, GL_UNSIGNED_BYTE, pixmap); 

    //no clue 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

} 

편집 : 아래로, 저를 투표 실현 걸 렸어요하지만 난 너희들이 지금의이 감소되어 볼 필요하지 않은 기능을 많이 한 사람에게 먼저 감사를드립니다.

장소 나는 MTL에서 로더 및 그리기() LoadThings() createTex()에서

편집 무승부() 과정() 될 문제를 포함 할 수 있다고 생각 : 괜찮아들 내가 텍스처를 표시 할 때 해결책을 발견, 어떤 이유로 OpenGL을위한 판명이 glColor 함수를 사용하기 때문에하고 있던 무슨이었다 ... OpenGL을

솔루션 : 이 당신을 glBegin (? 폴리 삼각형) 이상이 줄을 추가 텍스처를 표시 할 때 glColor3f (1.0F, 1.0F, 1.0F)

가 내가 해결책을 발견
+0

http://25.media.tumblr.com/a3e313c0b2dee60e1c0575be4e136e09/tumblr_mvl7wjR0Uz1r22o1uo1_500.png 내가 추가하는 것을 잊었다 이상이 줄을 추가 이게 내가 조명을 사용할 수 없었을지라도 얻을 수 있지만 코드가 단지 색상 정점을 사용하여 코드를 바꿀 때 잘 동작합니다. –

+0

조명이 비활성화되었고 GL_BLEND가 테스트되었습니다. 아무것도 바꾸어 라. –

답변

0

확실히 사람이, 어떤 이유로 OpenGL을위한 판명이 glColor 함수를 사용하기 때문에하고 있던 무슨이었다 ... OpenGL을

솔루션 : 당신의의 glBegin (? 폴리 삼각형) glColor3f (1.0F, 1.0F, 1.0F)