簡體   English   中英

OpenGL C ++:紋理代碼使用相同的紋理對所有模型進行紋理化

[英]Opengl C++: texture code textures all models with the same texture

我創建了一個類來保存我的模型信息。 我必須對模型進行正確的渲染並正確地包裝紋理,但是由於某些原因,如果我有多個模型,它將僅用1個紋理對我的所有模型進行紋理化,如您在此圖中看到的: http : //imgur.com/d0glIwF

任何想法為什么會發生這種情況?

這是我的代碼:

struct BitMapFile
{
   int sizeX;
   int sizeY;
   unsigned char *data;
};


// Routine to read a bitmap file.
// Works only for uncompressed bmp files of 24-bit color.
BitMapFile *getBMPData(string filename)
{
   BitMapFile *bmp = new BitMapFile;
   unsigned int size, offset, headerSize;

   // Read input file name.
   ifstream infile(filename.c_str(), ios::binary);

   // Get the starting point of the image data.
   infile.seekg(10);
   infile.read((char *) &offset, 4); 

   // Get the header size of the bitmap.
   infile.read((char *) &headerSize,4);

   // Get width and height values in the bitmap header.
   infile.seekg(18);
   infile.read( (char *) &bmp->sizeX, 4);
   infile.read( (char *) &bmp->sizeY, 4);

   // Allocate buffer for the image.
   size = bmp->sizeX * bmp->sizeY * 24;
   bmp->data = new unsigned char[size];

   // Read bitmap data.
   infile.seekg(offset);
   infile.read((char *) bmp->data , size);

   // Reverse color from bgr to rgb.
   int temp;
   for (int i = 0; i < size; i += 3)
   { 
      temp = bmp->data[i];
      bmp->data[i] = bmp->data[i+2];
      bmp->data[i+2] = temp;
   }

   return bmp;
}

class Model
{
public:
    Model(string modelFilename, string textureFilename);

   float getCenterX() { return m_CenterX; }
   float getCenterY() { return m_CenterY; }
   float getCenterZ() { return m_CenterZ; }
   void SetCenterX(float x) { m_CenterX = x; }
   void SetCenterY(float y) { m_CenterY = y; }
   void SetCenterZ(float z) { m_CenterZ = z; }

    void LoadTexture(string fileName);
    //load model function
    void Draw();
private:

    float m_CenterX, m_CenterY, m_CenterZ, m_Width, m_Height, m_Depth;

    string m_ModelFilename;

    int m_Texture;
        string m_TextureName;
};
Model::Model(string modelFilename, string textureFilename)
{
    m_ModelFilename = modelFilename;
    m_TextureName = textureFilename;

    //load model function//
    LoadTexture(m_TextureName);
}

void Model::LoadTexture(string TextureName)         
{
   // Local storage for bmp image data.
   BitMapFile *image[1];

        string filename = TextureName;
        filename.append(".bmp");

       // Load the texture.
       image[0] = getBMPData(filename);

       // Bind grass image to texture index[i]. 
       glBindTexture(GL_TEXTURE_2D, m_Texture); //makes room for our texture
       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);
       glTexImage2D(GL_TEXTURE_2D, //always GL_TEXTURE_2D
           0,                       //0 for now
           GL_RGB,                  //format opengl uses to read textures
           image[0]->sizeX,         //width
           image[0]->sizeY,         //height
           0,                       //the border of the image
           GL_RGB,                  //GL_RGB because pixels are stored in RGB format
           GL_UNSIGNED_BYTE,        //GL_UNSIGNED_BYTE because pixels are stored as unsigned numbers
           image[0]->data);         //actual pixel data
}

void Model::Draw()
{
        glPushMatrix();
        glTranslatef(m_CenterX, m_CenterY, m_CenterZ);

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, m_Texture);

        glBegin(GL_TRIANGLES);
        //my code for drawing the model to the screen. it isn't the problem so i removed it
        glEnd();

        glDisable(GL_TEXTURE_2D);
        glPopMatrix();
}

Model model;
Model model1;

// Drawing routine.
void drawScene(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glLoadIdentity();

   model.SetCenterX(0);
   model.SetCenterY(0); 
   model.SetCenterZ(12);
   model.Draw();

   model1.SetCenterX(12);
   model1.SetCenterY(10);
   model1.SetCenterZ(0);
   model1.Draw();

   glutSwapBuffers();
}

void setup(void) 
{
    glClearColor(0.0, 0.0, 0.0, 0.0); 

    //model = Model("monkey.obj", "launch");
    model = Model("cube.obj", "launch");

    model1 = Model("cube.obj", "grass");

   // Specify how texture values combine with current surface color values.
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
}

先感謝您。

問題是您沒有創建紋理id。 您可以使用glGenTextures函數執行此操作。 在您的情況下,我將其放在LoadTexture方法的開頭-只需向其要求1個紋理ID,然后將它返回的內容保存到m_Texture

請記住,就像使用glGen*創建的所有內容一樣,使用glDelete* (在本例中為glDeleteTextures )完成該操作后,也應將其刪除。

另外,考慮使用着色器和頂點陣列遷移到更現代的OpenGL。 不幸的是,這是一個非常廣泛的話題。 我從OpenGL Superbible中學到了很多教程和書籍,盡管我聽說有些人不太喜歡它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM