How to rotate a texture before drawing on the screen using OpenGL ES on Pi

I recently found out that the Raspberry Pi GPU supports OpenGL ES. I have a task to complete, and the problem is that whenever I search for OpenGL ES, I get results based on Android and IOS.

Fortunately, I only have a small problem. I came across a simple2d library that abstracts the OpenGL ES interface with a Video core IV GPU on pi. Its an open source library that doesn't seem to support rotating textures. This is the only function that I want to clear my path of all obstacles. This is a call to DrawTextures. I would be very grateful to everyone who helps me in solving this problem.

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); } 

My task is to change the function described above so that it takes an additional rotation parameter to allow it to rotate it before drawing the texture.

I know OpenGL and I am sure that calling Rotatef will not be useful. I would be glad if someone told me how to do this in OpenGL ES for Pi.

+4
source share
1 answer

You need to adjust the rotation matrix. Depending on what you want, convert the coordinates of the vertices or the coordinates of the texture to the rotation matrix in the vertex shader.

Create a vertex shader as follows:

 attribute vec3 texPosition; .... uniform mat4 u_rotateMat44; void main() { .... vec4 rotPos = u_rotateMat44 * vec4(texPosition, 1.0); gl_Position = rotPos; } 

or that:

 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; .... } 

The rotation matrix can be configured as follows:

 #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 }; 

Install the glUniformMatrix4fv uniform:

 GLuint program = ....; // the shader program GLint rotMatLoc = glGetUniformLocation( program, "u_rotateMat44" ); glUniformMatrix4fv( rotMatLoc, 1, GL_FALSE, rotMat ); 
+4
source

Source: https://habr.com/ru/post/1275559/


All Articles