【问题标题】:Multiple Textures OpenGL GLUT C++多纹理 OpenGL GLUT C++
【发布时间】:2011-04-15 20:18:33
【问题描述】:

好吧,还有一些问题,这是我目前为止的:

Bitmap Display::m_HeightMap;
unsigned int Display:: textures;

我的初始化方法:

 glEnable(GL_TEXTURE_2D);

Bitmap image[2];
GLuint *textures = new GLuint[2];
glGenTextures(1, textures);
glGenTextures(2, textures);
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myOtherTexture.bmp");  

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);

上面一行报错:左边的.data must have class/struct/union

glDisable(GL_TEXTURE_2D);

绘制方法:

void Draw()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glBindTexture(GL_TEXTURE_2D, textures[0]);
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
//glColor3d(0.5,0.40,0.05);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0.3, 2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-4.5, 0.3, 2.5);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2.5);//top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(4.5, 0.3, 2);//top left
glEnd();
glDisable(GL_TEXTURE_2D);
}

这里唯一的问题是纹理未定义。

希望最后一件事!

void loadTexture(GLuint texture, const char* filename)
{
   Bitmap image;

   Bitmap image[2];

   image[0].loadBMP("myTexture.bmp");  <=== error 
   image[1].loadBMP("myTexture2.bmp"); <=== error


   glBindTexture(GL_TEXTURE_2D, texture);

   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, 

图像.数据); }

当我尝试加载多个位图时,出现 7 个错误,

错误 C2040:图像:“位图 [2]”在“位图”的间接级别上不同
错误 C2088: '[' 类非法(两次)
错误 C2228:.BMP 左侧必须有类/结构/联合(两次)
没有运算符“[]”匹配这些操作数(两次)

【问题讨论】:

    标签: c++ opengl textures


    【解决方案1】:

    所以。 glGenTextures 有两个参数。一个int 和一个GLuint*int 告诉 GL 要生成多少纹理,GLuint*GLuints 的数组(在哪里生成纹理)。您执行以下操作的原因...

    GLuint m_TextureID
    glGenTextures(1, &m_TextureID)
    

    是因为你只有一种纹理。如果你有多个,你会这样做:

    // Substitute 'n' for some const number
    GLuint *textures = new GLuint[n];
    glGenTextures(n, textures);
    

    这样,你告诉 GL 我想生成 n 个纹理,这是一个为至少那么多纹理分配空间的数组。

    假设您想在绘制循环中同时使用它们,您可以这样实现:

    void draw()
    {
       glBindTexture(GL_TEXTURE_2D, textures[0]); // Tell GL to use the first texture
       // Any drawing here will use first texture
    
       glBindTexture(GL_TEXTURE_2D, textures[1]); // Tell GL to use the second textures
       // Any drawing here will use second texture
    
       glBindTexture(GL_TEXTURE_2D, 0); // Set the GL texture to NULL, standard cleanup
    }
    

    确保在程序结束时delete textures; 在分配该空间后正确清理。

    还有一些方法可以不必绑定单独的纹理。您可以使用所谓的“纹理图集”。基本上,这是一个包含多个子图像的位图。因此,您只需生成并绑定一个位图,并使用它的不同部分。

    要处理多个位图,请执行以下操作:

    Bitmap image[2];
    image[0].loadBMP("myTexture.bmp");
    image[1].loadBMP("myOtherTexture.bmp");
    

    然后按照流程为两个位图生成一个位图。


    此行下方的所有内容都是对您更新后的问题的回应。

    这几乎可以满足您的要求。

    // Global variable
    GLuint textures[2];
    
    // init function
    void init()
    {
       textures = new GLuint[2]; 
       glGenTextures(2, textures);
    
       loadTexture(textures[0], "texture1.bmp");
       loadTexture(textures[1], "texture2.bmp");
    
    }
    
    void loadTexture(GLuint texture, const char* filename)
    {
       Bitmap image; 
       image.loadBMP(filename); 
    
       glBindTexture(GL_TEXTURE_2D, texture);
    
       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    
       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
       gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
    }
    
    // draw function
    void draw()
    {
       glBegin(GL_QUADS); //TOP
       glBindTexture(GL_TEXTURE_2D, textures[0]);
       glNormal3f(0,1,0);
       glColor4f(1,1,1,0);
       glTexCoord2f(0.0f,0.0f); glVertex3f(-4.5, 0.3, 2); //bottom left
       glTexCoord2f(1.0f,0.0f); glVertex3f(-4.5, 0.3, 2.5); //bottom right
       glTexCoord2f(1.0f,1.0f); glVertex3f(4.5, 0.3, 2.5); //top right
       glTexCoord2f(0.0f,1.0f); glVertex3f(4.5, 0.3, 2); //top left
       glEnd();
    }
    
    // cleanup function
    void cleanup()
    {
       delete textures;
    }
    

    您指出的一些问题与 OpenGL 不完全相关,它们与 C/C++ 更相关。我知道这不是您要寻找的答案,但它可能会帮助您了解 C 函数、指针、数组等,并在移动之前花一个月的时间与函数/指针/数组密切合作像 OpenGL 这样的东西,它需要对 C 有相当程度的理解。

    【讨论】:

    • 这不会按原样工作,您的“纹理”指针需要分配给“新”数组。
    • 关于纹理图集的优点:使用这些图集通常可以获得性能优势。
    • 好的,谢谢!我需要改变吗:image.loadBMP("myTexture.bmp"); ?到一个变量还是我对不同的图像重复这个?
    • 位图图像[2]; image[0].loadBMP("myTexture1.bmp"); image[1].loadBMP("myTexture2.bmp");等等……有意义吗?
    • 在我的帖子底部回答了您的问题。看起来更漂亮:)
    【解决方案2】:

    const GLsizei n = (这里的纹理数量);

    GLuint textureIDs = new GLuint[ n ];

    glGenTextures(n, textureIDs);

    【讨论】:

      【解决方案3】:

      我假设您的意思是要生成多个纹理并将 id 存储在一个数组中?你可以这样做:

      GLsizei num_textures = 5;
      GLuint textures[num_textures];
      
      glGenTextures(num_textures, textures);
      

      完成此操作后,只需遍历纹理并为每个纹理进行绑定、设置参数、加载图像数据等。

      您可能希望在堆上创建纹理数组,以便稍后访问纹理 ID:

      GLuint* textures = new GLuint[num_textures];
      

      请确保稍后删除该数组:

      delete textures;
      

      【讨论】:

      • 数组的大小必须是一个整数常量表达式。在您动态分配的数组中,您需要delete []
      猜你喜欢
      • 1970-01-01
      • 2014-11-19
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多