如何在Pi上使用OpenGL ES绘制到屏幕之前旋转纹理

时间:2018-02-24 09:21:34

标签: c++ c opengl-es raspberry-pi

我最近才知道Raspberry Pi的GPU仅支持OpenGL ES。 我有一个任务要完成,问题是,每当我搜索OpenGL ES时,我都会得到基于Android和IOS的结果。

谢天谢地,我只有一个小问题。我偶然发现了simple2d库,它抽象了OpenGL ES与pi上的Video core IV GPU的接口。 它是一个开源库,似乎不支持旋转纹理。这是我想要的唯一功能,以便从所有障碍中清除我的路径。这是对DrawTextures的调用。我非常感谢那些帮助我解决这个问题的人。

static void S2D_GLES_DrawTexture(int x, int y, int w, int h,
                                 GLfloat r, GLfloat g, GLfloat b, GLfloat a,
                                 GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
                                 GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
                                 GLuint texture_id) {

  GLfloat vertices[] =
  //  x, y coords      | x, y texture coords
    { x,     y,     0.f, tx1, ty1,
      x + w, y,     0.f, tx2, ty2,
      x + w, y + h, 0.f, tx3, ty3,
      x,     y + h, 0.f, tx4, ty4 };

  GLfloat colors[] =
    { r, g, b, a,
      r, g, b, a,
      r, g, b, a,
      r, g, b, a };

  glUseProgram(texShaderProgram);

  // Load the vertex position
  glVertexAttribPointer(texPositionLocation, 3, GL_FLOAT, GL_FALSE,
                        5 * sizeof(GLfloat), vertices);
  glEnableVertexAttribArray(texPositionLocation);

  // Load the colors
  glVertexAttribPointer(texColorLocation, 4, GL_FLOAT, GL_FALSE, 0, colors);
  glEnableVertexAttribArray(texColorLocation);

  // Load the texture coordinate
  glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE,
                        5 * sizeof(GLfloat), &vertices[3]);
  glEnableVertexAttribArray(texCoordLocation);

  // Bind the texture
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, texture_id);

  // Set the sampler texture unit to 0
  glUniform1i(samplerLocation, 0);

  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}

我的任务是修改上面的函数,使其接受额外的旋转参数,以便在绘制纹理之前使其旋转。

我知道OpenGL,我很确定调用Rotatef对我没有帮助。如果有人告诉我如何在OpenGL ES中为Pi做这个,我会很高兴。

1 个答案:

答案 0 :(得分:4)

您必须设置旋转矩阵。根据您的需要,在顶点着色器中通过旋转矩阵变换顶点坐标或纹理坐标。

创建一个这样的顶点着色器:

attribute vec3 texPosition;
....

uniform mat4 u_rotateMat44;

void main()
{
    ....

    vec4 rotPos = u_rotateMat44 * vec4(texPosition, 1.0);
    gl_Position = rotPos;
}

或者这个:

attribute vec2 texCoord;
....

varying vec2 outTexCoord:

uniform mat4 u_rotateMat44;

void main()
{
    ....

    vec4 rotCoord = u_rotateMat44 * vec4(texCoord, 0.0, 1.0);
    outTexCoord   = rotCoord.st;

    ....
}

旋转矩阵可以这样设置:

#include <math.h> // sin, cos

float ang_rad ....; // angle in radians

float rotMat[16] =
{
     cos(ang_rad), sin(ang_rad),  0.0f, 0.0f,
    -sin(ang_rad), cos(ang_rad ), 0.0f, 0.0f,
     0.0f,         0.0f,          1.0f, 0.0f,
     0.0f,         0.0f,          0.0f, 1.0f
};

glUniformMatrix4fv

设置制服
GLuint program = ....; // the shader program

GLint rotMatLoc = glGetUniformLocation( program, "u_rotateMat44" );

glUniformMatrix4fv( rotMatLoc, 1, GL_FALSE, rotMat );