how to write an opengl code to rotate a texture
- User
how to write an opengl code to rotate a texture
- Assistant
how to write an opengl code to rotate a texture
To rotate a texture in OpenGL, you'll need to apply a rotation transformation to the texture coordinates. Below is a simple example of how you can achieve this in modern OpenGL (using shaders). This example assumes you have a basic understanding of OpenGL setup, shaders, and texture loading.
#version 330 core
layout(location = 0) in vec3 aPos; // Vertex position
layout(location = 1) in vec2 aTexCoord; // Texture coordinate
out vec2 TexCoord;
uniform float rotationAngle;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
// Calculate the new texture coordinates with rotation
float cosAngle = cos(rotationAngle);
float sinAngle = sin(rotationAngle);
mat2 rotationMatrix = mat2(cosAngle, -sinAngle,
sinAngle, cosAngle);
TexCoord = rotationMatrix * aTexCoord;
// Transform the vertex position
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture1;
void main()
{
// Sample and output the texture color
FragColor = texture(texture1, TexCoord);
}
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include <shader.h> // Your shader class for handling shader compilation & linking
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
int main()
{
// Initialize and configure
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(800, 600, "Rotate Texture", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Load GLAD to get function pointers and extensions
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
return -1;
}
// Build and compile our shader program
Shader shader("rotate_vertex_shader.glsl", "rotate_fragment_shader.glsl");
// Set up vertex data (and buffer(s)) and configure vertex attributes
float vertices[] = {
// positions // texture coordinates
0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f
};
unsigned int indices[] = {
0, 1, 3,
1, 2, 3
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// Load and create a texture
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// Set the texture wrapping/filtering options (on the currently bound texture object)
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_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // if the loaded texture appears upside down
unsigned char *data = stbi_load("path/to/your/texture.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// shader configuration
shader.use();
shader.setInt("texture1", 0);
// Rendering loop
while (!glfwWindowShouldClose(window))
{
// input
processInput(window);
// render
glClear(GL_COLOR_BUFFER_BIT);
// activate shader
shader.use();
// bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
// apply rotation
float timeValue = glfwGetTime();
float angle = timeValue; // Rotate over time
shader.setFloat("rotationAngle", angle);
// create transformation matrices (identity in this case, can modify for translation/scale)
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
// render box
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(shader.ID);
// glfw: terminate, clearing all previously allocated GLFW resources.
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
In this example:
Ensure to replace "path/to/your/texture.jpg" with the actual path to your texture image. Additionally, you'll need to implement or use a provided shader class (Shader) to handle shader compilation and program linking. You can find implementations of such a class in many OpenGL tutorials.
Also, remember to link the necessary libraries and include the headers (glad, GLFW, and stb_image, if needed).
Not yet included in a published release.