【问题标题】:Compiling NeHe (or other 32 bit oriented) examples on Linux-64在 Linux-64 上编译 NeHe(或其他面向 32 位的)示例
【发布时间】:2014-02-03 20:18:23
【问题描述】:

我有多个 NeHe OpenGL 教程,它们是面向 Linux/GLX 的,我有 Linux 64 位。问题是许多教程都假设是 32 位系统。特别是它们将无法加载文件,因为sizeof(int) 不是 4(32 位)并且sizeof(short) 不是 2(16 位)。有没有办法告诉编译器使int 32 位?或者如何在不重新安装操作系统的情况下编译和运行示例?

【问题讨论】:

  • 那是彻头彻尾的糟糕设计。如果教程使用 GLintGLshort 代替,这永远不会成为问题。在任何实现 OpenGL 的系统上,它们都保证具有特定的位宽(分别为 32 位和 16 位)。另一种甚至更常见的方法是在建立文件结构时使用标准类型,例如 uint32_t (C99),甚至是像 DWORD(始终为 32 位的古老 Windows 标准类型)这样令人讨厌的东西。
  • 也就是说,针对 64 位 Linux 通常不会更改 shortint 的大小。它主要影响较大的整数类型,如long,和数据指针,如void*,大到足以存储连续地址范围的类型,如intptr_t等。
  • 我不知道你为什么期望sizeof (int) == 32sizeof 运算符返回 char 对象的单位,而不是位。

标签: c++ linux opengl 64-bit


【解决方案1】:

如果您在 Linux 上,并且使用 gcc(并使用 multilib 运行),您可以将 -m32 选项传递给 gcc,它将针对“32 位”架构进行编译。

【讨论】:

    【解决方案2】:

    GCC 的手册页告诉我们,

    -m32
    -m64
    

    为 32 位或 64 位环境生成代码。 32位环境设置int, long 和指向 32 位的指针,并生成在任何 i386 系统上运行的代码。这 64 位环境将 int 设置为 32 位和 long 并将指针设置为 64 位和 为 AMD 的 x86-64 架构生成代码。

    【讨论】:

      【解决方案3】:

      原来是LoadBMP()函数是教程示例中唯一的问题。在这里,我发布了此函数的修改版本,以便您可以运行示例。只需将 LoadBMP 替换为:

      /* simple loader for 24bit bitmaps (data is in rgb-format) */
      int loadBMP(char *filename, textureImage *texture)
      {
          FILE *file;
      /*    unsigned short int bfType;
          long int bfOffBits;
          short int biPlanes;
          short int biBitCount;
          long int biSizeImage;
          int i;
          unsigned char temp;*/
          uint16_t bfType;
          int32_t bfOffBits;
          short int biPlanes;
          short int biBitCount;
          long int biSizeImage;
          int i;
          unsigned char temp;
          /* make sure the file is there and open it read-only (binary) */
          if ((file = fopen(filename, "rb")) == NULL)
          {
              printf("File not found : %s\n", filename);
              return 0;
          }
          if(!fread(&bfType, sizeof(uint16_t), 1, file))
          {
              printf("Error reading file!\n");
              return 0;
          }
          /* check if file is a bitmap */
          if (bfType != 19778)
          {
              printf("Not a Bitmap-File!\n");
              return 0;
          }        
          /* get the file size */
          /* skip file size and reserved fields of bitmap file header */
          fseek(file, 8, SEEK_CUR);
          /* get the position of the actual bitmap data */
          if (!fread(&bfOffBits, sizeof(int32_t), 1, file))
          {
              printf("Error reading file!\n");
              return 0;
          }
          printf("Data at Offset: %ld\n", bfOffBits);
          /* skip size of bitmap info header */
          fseek(file, 4, SEEK_CUR);
          /* get the width of the bitmap */
          fread(&texture->width, sizeof(int), 1, file);
          printf("Width of Bitmap: %d\n", texture->width);
          /* get the height of the bitmap */
          fread(&texture->height, sizeof(int), 1, file);
          printf("Height of Bitmap: %d\n", texture->height);
          /* get the number of planes (must be set to 1) */
          fread(&biPlanes, sizeof(short int), 1, file);
          if (biPlanes != 1)
          {
              printf("Error: number of Planes not 1!\n");
              return 0;
          }
          /* get the number of bits per pixel */
          if (!fread(&biBitCount, sizeof(short int), 1, file))
          {
              printf("Error reading file!\n");
              return 0;
          }
          printf("Bits per Pixel: %d\n", biBitCount);
          if (biBitCount != 24)
          {
              printf("Bits per Pixel not 24\n");
              return 0;
          }
          /* calculate the size of the image in bytes */
          biSizeImage = texture->width * texture->height * 3;
          printf("Size of the image data: %ld\n", biSizeImage);
          texture->data = malloc(biSizeImage);
          /* seek to the actual data */
          fseek(file, bfOffBits, SEEK_SET);
          if (!fread(texture->data, biSizeImage, 1, file))
          {
              printf("Error loading file!\n");
              return 0;
          }
          /* swap red and blue (bgr -> rgb) */
          for (i = 0; i < biSizeImage; i += 3)
          {
              temp = texture->data[i];
              texture->data[i] = texture->data[i + 2];
              texture->data[i + 2] = temp;
          }
          return 1;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        • 2017-01-21
        • 1970-01-01
        • 2013-04-08
        • 2012-01-05
        • 1970-01-01
        • 2011-04-12
        相关资源
        最近更新 更多