在OpenGL中,要旋转一个正方形可以使用以下步骤:
以下是一个示例的代码片段:
顶点着色器代码:
#version 330 core
layout (location = 0) in vec3 aPosition;
uniform mat4 uModel;
uniform mat4 uView;
uniform mat4 uProjection;
void main()
{
gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
}
片段着色器代码:
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0); // 使用红色作为正方形的颜色
}
C++代码:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
// 正方形的顶点坐标
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
};
// 正方形的索引
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
// 初始化OpenGL窗口和上下文
void initGLFW()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
// 创建窗口和上下文
GLFWwindow* createWindow(int width, int height, const char* title)
{
GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
exit(-1);
}
glfwMakeContextCurrent(window);
return window;
}
// 初始化OpenGL函数指针
void initGLAD()
{
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
exit(-1);
}
}
// 创建顶点缓冲对象(VBO)
unsigned int createVBO()
{
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return vbo;
}
// 创建顶点数组对象(VAO)
unsigned int createVAO()
{
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
return vao;
}
// 绑定顶点属性指针
void bindVertexAttributes()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
// 创建索引缓冲对象(EBO)
unsigned int createEBO()
{
unsigned int ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return ebo;
}
// 编译和链接着色器程序
unsigned int createShaderProgram()
{
// 编译顶点着色器
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderCode, NULL);
glCompileShader(vertexShader);
// 检查顶点着色器是否编译成功
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "Failed to compile vertex shader" << std::endl;
exit(-1);
}
// 编译片段着色器
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderCode, NULL);
glCompileShader(fragmentShader);
// 检查片段着色器是否编译成功
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "Failed to compile fragment shader" << std::endl;
exit(-1);
}
// 创建着色器程序并链接着色器
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// 检查着色器程序是否链接成功
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "Failed to link shader program" << std::endl;
exit(-1);
}
// 删除着色器对象
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
// 渲染循环
void renderLoop(GLFWwindow* window, unsigned int vao, unsigned int ebo, unsigned int shaderProgram)
{
while (!glfwWindowShouldClose(window))
{
// 处理输入事件
processInput(window);
// 清空屏幕颜色
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 使用着色器程序
glUseProgram(shaderProgram);
// 更新模型矩阵以实现旋转
float angle = glfwGetTime() * 50.0f;
glm::mat4 model = glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(0.0f, 0.0f, 1.0f));
unsigned int modelLocation = glGetUniformLocation(shaderProgram, "uModel");
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(model));
// 绑定顶点数组对象(VAO)和索引缓冲对象(EBO)
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
// 绘制正方形
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// 交换缓冲区并查询事件
glfwSwapBuffers(window);
glfwPollEvents();
}
}
int main()
{
// 初始化GLFW
initGLFW();
// 创建窗口和上下文
GLFWwindow* window = createWindow(800, 600, "OpenGL");
// 初始化GLAD
initGLAD();
// 创建顶点缓冲对象(VBO)
unsigned int vbo = createVBO();
// 创建顶点数组对象(VAO)
unsigned int vao = createVAO();
// 绑定顶点属性指针
bindVertexAttributes();
// 创建索引缓冲对象(EBO)
unsigned int ebo = createEBO();
// 编译和链接着色器程序
unsigned int shaderProgram = createShaderProgram();
// 进入渲染循环
renderLoop(window, vao, ebo, shaderProgram);
// 清理资源
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
glDeleteProgram(shaderProgram);
// 终止GLFW
glfwTerminate();
return 0;
}
这个例子演示了如何在OpenGL中旋转一个正方形。你可以根据自己的需要自定义顶点位置、顶点颜色、旋转角度等参数。
领取专属 10元无门槛券
手把手带您无忧上云