簡體   English   中英

加載的2D紋理未將自身綁定到QUAD

[英]Loaded 2D texture is not binding itself to a QUAD

我已經將紋理的ID加載到tex變量中,但是當我在繪制QUAD之前將紋理與該ID綁定時(在下面的代碼中被xxxxxxxxx...包圍的renderScene函數中看到),沒有紋理被應用到QUAD; 它仍然是無色的。 也沒有錯誤消息。

我已經在程序中包含了所有與紋理相關的內容。

我還需要添加/更改什么?

#include<windows.h>
#include <GL/glut.h>
#include<iostream>
#include <stdlib.h>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>

// (...)



GLuint loadBMP_custom(const char * imagepath);
GLuint tex = loadBMP_custom("texture.bmp");



GLuint loadBMP_custom( const char * imagepath )
{

   GLuint texture;

   int width, height;

   unsigned char * data;

   FILE * file;

   file = fopen( imagepath, "rb" );

   if ( file == NULL ) return 0;

   width = 256;
   height = 256;
   data = (unsigned char *)malloc( width * height * 3 );

   fread( data, width * height * 3, 1, file );
   fclose( file );

  for(int i = 0; i < width * height ; ++i)
  {
    int index = i*3;
    unsigned char B,R;
    B = data[index];
    R = data[index+2];

    data[index] = R;
    data[index+2] = B;

  }



  glGenTextures( 1, &texture );
  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, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
  free( data );

  return texture;
}



// (...)



void renderScene(void)
{

    glMatrixMode(GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(  posX, 1.0f, posZ,
            posX+vlx, posY+vly,  posZ+vlz,
            0.0f, 1.0f,  0.0f);




    // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    glBindTexture(GL_TEXTURE_2D, tex);

    glBegin(GL_QUADS);
        glTexCoord2f (0.0, 0.0);
        glVertex3f(-100.0f, -0.02f, -100.0f);
        glTexCoord2f (0.0, 1.0);
        glVertex3f(-100.0f, -0.02f,  100.0f);
        glTexCoord2f (1.0, 1.0);
        glVertex3f( 100.0f, -0.02f,  100.0f);
        glTexCoord2f (1.0, 0.0);
        glVertex3f( 100.0f, -0.02f, -100.0f);
    glEnd();

    // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


    // [Also display 3D blocks in space besides the above QUAD, which serves as ground]

    glFlush();
    glutSwapBuffers();

}

//(...)

int main(int argc, char **argv)
{

    // (...)

    glEnable (GL_TEXTURE_2D) ;

    glutMainLoop();
    return 1;
}

如評論中所述,加載BMP文件的方法不正確:即使您知道圖像的大小(256x256),仍然需要跳到像素數據部分(通常為54個字節,這是通常的標頭的大小)。

因此,現在您可以通過閱讀此處的官方規范來編寫自己的進口商: https : //en.wikipedia.org/wiki/BMP_file_format

...或者在網絡上也找到許多實現,例如此問題中提到的實現: C ++:讀取位圖錯誤

除非您找到一些易於插入的僅標頭的庫,否則我個人會反對使用一個庫專門處理BMP文件,因為它引入了額外的依賴關系,使某些東西可以很容易地自己滾動(並且同時時間,學習如何實現簡單的文件加載器)。

暫無
暫無

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

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