【问题标题】:How to Crop and Scale a Texture in OpenGL如何在 OpenGL 中裁剪和缩放纹理
【发布时间】:2012-02-20 03:19:32
【问题描述】:

我有一个 852x640 的输入纹理和一个 612x612 的输出纹理。我正在通过着色器传递输入,并希望正确缩放和裁剪输出。我无法让 squareCoordinatestextureCoordinatesviewPorts 一起正常工作。

我不想只是裁剪,我还想缩放它以获得尽可能多的图像。如果我使用 Photoshop,我会分两步完成(在 OpenGL 中,我尝试一步完成):

  1. 将图像缩放为 612x814
  2. 剪掉每边多余的 101 像素

我使用的是标准方形顶点和纹理顶点:

static const GLfloat squareVertices[] = {
    -1.0f, -1.0f,
    1.0f, -1.0f,
    -1.0f,  1.0f,
    1.0f,  1.0f,
};

static const GLfloat squareTextureVertices[] = {
    0.0f, 0.0f,
    1.0f, 0.0f,
    0.0f,  1.0f,
    1.0f,  1.0f
}

我不完全知道视口应该是什么。

【问题讨论】:

    标签: opengl-es textures


    【解决方案1】:

    视口为 612x612 像素。

    要缩放和裁剪原始四边形,最简单的方法是将顶点设置为覆盖 612x612 矩形(在您的情况下,我们将 squareVertices 保持不变),但设置纹理坐标以便裁剪掉左右两侧:

    static const GLfloat squareTextureVertices[] = {
        (852.0f-640.0f)/852.0f*0.5f, 0.0f,
        1.0f - (852.0f-640.0f)/852.0f*0.5f, 0.0f,
        (852.0f-640.0f)/852.0f*0.5f,  1.0f,
        1.0f - (852.0f-640.0f)/852.0f*0.5f,  1.0f
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 2011-05-21
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      相关资源
      最近更新 更多