feat(OpenGL): moving color changing rectangle

This commit is contained in:
iridiumR 2023-05-23 15:03:04 +08:00
parent 51b09ffe33
commit 8012d47b28
4 changed files with 35 additions and 89 deletions

View file

@ -1,6 +1,7 @@
#include "glad/glad.h" #include "glad/glad.h"
#include "shader.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <cmath>
// 三角形顶点 // 三角形顶点
float vertices[] = { float vertices[] = {
@ -17,33 +18,6 @@ unsigned int indices[] = {
1, 2, 3 // 第二个三角形 1, 2, 3 // 第二个三角形
}; };
// 顶点着色器源码
const char *vertexShaderSource =R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 color;
void main()
{
gl_Position = vec4(aPos, 1.0);
color = aColor;
}
)";
// 片段着色器源码
const char *fragmentShaderSource = R"(
#version 330 core
in vec3 color;
out vec4 FragColor;
void main()
{
FragColor = vec4(color, 1.0);
}
)";
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
@ -78,60 +52,8 @@ int main() {
// 设置视口 // 设置视口
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 创建顶点着色器
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
// 将着色器源码附加到着色器对象上
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
// 编译着色器
glCompileShader(vertexShader);
// 检查编译是否成功
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
// 获取错误信息
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
// 创建片段着色器
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// 将着色器源码附加到着色器对象上
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
// 编译着色器
glCompileShader(fragmentShader);
// 检查编译是否成功
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
// 获取错误信息
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
// 创建着色器程序 // 创建着色器程序
unsigned int shaderProgram; Shader shader = Shader("src/simple.vs", "src/uniformcolor.fs");
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 << "ERROR::SHADER::PROGRAM::LINK_FAILED\n"
<< infoLog << std::endl;
}
// 删除着色器
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
unsigned int VAO, VBO, EBO; unsigned int VAO, VBO, EBO;
@ -163,14 +85,19 @@ int main() {
glClearColor(1.0f, 1.0f, 1.0f, 0.0f); glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// 绘制三角形, 激活程序 // 激活着色器
glUseProgram(shaderProgram); shader.use();
// 更新uniform颜色
float timeValue = glfwGetTime();
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
shader.setFloat4("ourColor", 1.0f, greenValue, 0.0f, 0.0f);
shader.setFloat1("xOffset", cos(timeValue) / 2.0f);
shader.setFloat1("yOffset", sin(timeValue) / 2.0f);
// 绘制三角形
glBindVertexArray(VAO); glBindVertexArray(VAO);
//使用线框模式绘制 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// 设置线宽
glLineWidth(3.0f);
// 绘制
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);

View file

@ -0,0 +1,8 @@
#version 330 core
layout(location = 0) in vec3 aPos; // 位置变量的属性位置值为0
uniform float xOffset;
uniform float yOffset;
void main() {
gl_Position = vec4(aPos.x + xOffset, aPos.y + yOffset, aPos.z,
1.0); // 注意我们如何把一个vec3作为vec4的构造器的参数
}

2
OpenGL/hello/src/stb.cpp Normal file
View file

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

View file

@ -0,0 +1,9 @@
#version 330 core
out vec4 FragColor;
uniform vec4 ourColor; // OpenGL
void main()
{
FragColor = ourColor;
}