Opengl 纹理映射和图像分辨率

发布于 2024-11-09 17:55:07 字数 3704 浏览 3 评论 0原文

我正在使用 opengl 和纹理映射。 问题是,当应用的纹理为 512×32 像素时,一切工作正常,但当应用的纹理为 128×128 时,纹理应用不正确。

纹理没有正确重复,它应用于对象的开头,而对象的其余部分则没有任何纹理。

代码中:length参数约为100。

LoadTexture:读取.bmp文件并返回纹理索引的函数。

这是我正在使用的代码。

int LoadTexture(char *filename,int alpha) 
{
    using namespace std;
    int i, j=0; //Index variables
    static int num_texture;
    ifstream l_file(filename);
    unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture

    // windows.h gives us these types to work with the Bitmap files
    BITMAPFILEHEADER fileheader; 
    BITMAPINFOHEADER infoheader;
    RGBTRIPLE rgb;

    num_texture++; // The counter of the current texture is increased

    if(!l_file) return (-1); // Open the file for reading

    l_file.read(reinterpret_cast<char *>(&fileheader), sizeof(fileheader)); // Read the fileheader

    //fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader
    l_file.read(reinterpret_cast<char *>(&infoheader), sizeof(infoheader)); // and read the infoheader

    // Now we need to allocate the memory for our image (width * height * color deep)
    l_texture = new byte [infoheader.biWidth * infoheader.biHeight * 4];
    // And fill it with zeros
    memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

    // At this point we can read every pixel of the image
    for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
    {            
          // We load an RGB value from the file
          l_file.read(reinterpret_cast<char *>(&rgb), sizeof(rgb)); 

          // And store it
          l_texture[j+0] = rgb.rgbtRed; // Red component
          l_texture[j+1] = rgb.rgbtGreen; // Green component
          l_texture[j+2] = rgb.rgbtBlue; // Blue component
          l_texture[j+3] = alpha; // Alpha value
          j += 4; // Go to the next position
     }

     l_file.close(); // Closes the file stream

     glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

     // The next commands sets the texture parameters
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //The minifying function

     // Finally we define the 2d texture
     glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

     // And create 2d mipmaps for the minifying function
     gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

     delete [] l_texture; // Free the memory we used to load the texture

     return num_texture; // Returns the current texture OpenGL ID
}

这就是我如何绘制坐标

void drawStreetStraight(Vector2d & point1,Vector2d & point2,Vector2d & point3,Vector2d & point4,double length)
{
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECR); 
    glBindTexture(GL_TEXTURE_2D,streetTexture);
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);
    glVertex3d(point1.x,0,point1.y);
    glTexCoord2f(1,0);
    glVertex3d(point2.x,0,point2.y);
    glTexCoord2f(1,length/2.0);
    glVertex3d(point3.x,0,point3.y);
    glTexCoord2f(0,length/2.0);
    glVertex3d(point4.x,0,point4.y);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}

i'm working with opengl and texture mapping.
the problem is when the applied texture is 512×32 pixels every thing is working fine, but when it's 128×128 the texture is not applied properly.

the texture is not repeated properly, it's applied to the beginning of object and the rest of him is left without any texture.

in the code: length parameter is about 100.

LoadTexture: a function reading .bmp file and returning texture index.

Here's the code i'm working with.

int LoadTexture(char *filename,int alpha) 
{
    using namespace std;
    int i, j=0; //Index variables
    static int num_texture;
    ifstream l_file(filename);
    unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture

    // windows.h gives us these types to work with the Bitmap files
    BITMAPFILEHEADER fileheader; 
    BITMAPINFOHEADER infoheader;
    RGBTRIPLE rgb;

    num_texture++; // The counter of the current texture is increased

    if(!l_file) return (-1); // Open the file for reading

    l_file.read(reinterpret_cast<char *>(&fileheader), sizeof(fileheader)); // Read the fileheader

    //fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader
    l_file.read(reinterpret_cast<char *>(&infoheader), sizeof(infoheader)); // and read the infoheader

    // Now we need to allocate the memory for our image (width * height * color deep)
    l_texture = new byte [infoheader.biWidth * infoheader.biHeight * 4];
    // And fill it with zeros
    memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

    // At this point we can read every pixel of the image
    for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
    {            
          // We load an RGB value from the file
          l_file.read(reinterpret_cast<char *>(&rgb), sizeof(rgb)); 

          // And store it
          l_texture[j+0] = rgb.rgbtRed; // Red component
          l_texture[j+1] = rgb.rgbtGreen; // Green component
          l_texture[j+2] = rgb.rgbtBlue; // Blue component
          l_texture[j+3] = alpha; // Alpha value
          j += 4; // Go to the next position
     }

     l_file.close(); // Closes the file stream

     glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

     // The next commands sets the texture parameters
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //The minifying function

     // Finally we define the 2d texture
     glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

     // And create 2d mipmaps for the minifying function
     gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

     delete [] l_texture; // Free the memory we used to load the texture

     return num_texture; // Returns the current texture OpenGL ID
}

and Here's how i'm doing mapping coordinates

void drawStreetStraight(Vector2d & point1,Vector2d & point2,Vector2d & point3,Vector2d & point4,double length)
{
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECR); 
    glBindTexture(GL_TEXTURE_2D,streetTexture);
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);
    glVertex3d(point1.x,0,point1.y);
    glTexCoord2f(1,0);
    glVertex3d(point2.x,0,point2.y);
    glTexCoord2f(1,length/2.0);
    glVertex3d(point3.x,0,point3.y);
    glTexCoord2f(0,length/2.0);
    glVertex3d(point4.x,0,point4.y);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-11-16 17:55:07

我不确定这是否适合您的示例,但看来您没有在任何地方使用 glGenTextures。

这是有原因的吗?

我像这样准备纹理:

    glGenTextures( 1, &texPlane);
glBindTexture( GL_TEXTURE_2D, texPlane );
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
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_GENERATE_MIPMAP, GL_TRUE);  
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, texPlanes);

I'm not sure if this is a fix for your example, but it appears that you are not using glGenTextures anywhere.

Is there a reason for this?

I prep textures like so:

    glGenTextures( 1, &texPlane);
glBindTexture( GL_TEXTURE_2D, texPlane );
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
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_GENERATE_MIPMAP, GL_TRUE);  
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, texPlanes);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文