【问题标题】:How to crop an SOIL loaded image如何裁剪 SOIL 加载的图像
【发布时间】:2020-02-15 02:45:03
【问题描述】:

我正在尝试裁剪通过 SOIL 库加载的图像,然后将其用作纹理。

  • 首先,我如何加载图像,然后然后将其转换为纹理?
  • 其次,如何修改(裁剪等)加载的图像?

这是我想做的:

unsigned char * img = SOIL_load_image("img.png", &w, &h, &ch, SOIL_LOAD_RGBA);
// crop img ...
// cast it into GLuint texture ...

【问题讨论】:

    标签: c opengl soil


    【解决方案1】:

    您可以利用glPixelStorei 功能加载图像的一部分:

    // the location and size of the region to crop, in pixels:
    int cropx = ..., cropy = ..., cropw = ..., croph = ...;
    
    // tell OpenGL where to start reading the data:
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, cropx);
    glPixelStorei(GL_UNPACK_SKIP_ROWS, cropy);
    
    // tell OpenGL how many pixels are in a row of the full image:
    glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
    
    // load the data to a previously created texture
    glTextureSubImage2D(texure, 0, 0, 0, cropw, croph, GL_SRGB8_ALPHA8, GL_UNSIGNED_BYTE, img);
    

    以下是来自 OpenGL 规范的图表,可能会有所帮助:

    编辑:如果您使用的是较旧的 OpenGL(早于 4.5),请将 glTextureSubImage2D 调用替换为:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, cropw, croph, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
    

    确保在此调用之前创建并绑定纹理(与通常创建纹理的方式相同)。

    【讨论】:

    • 谢谢。我首先加载了图像并保存了它的尺寸。然后,我加载了相同的文件,但作为纹理。但是当我想用glTextureSubImage2D 裁剪它时,函数没有定义(OpenGL v 3.0)。然而glTexSubImage2D 是可调用的,但第一个参数不再是纹理。我从那个开始,医生让我有点困惑......
    • 现在它显示裁剪后的图像,但 Y 反转。这似乎是正常的,所以我会处理它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2017-05-22
    • 2012-11-16
    • 1970-01-01
    • 2010-11-04
    相关资源
    最近更新 更多