feat(OpenGL): lab1
This commit is contained in:
parent
a3e1bc1e00
commit
6cad5afdb8
3 changed files with 2238 additions and 2394 deletions
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -2,39 +2,6 @@
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#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, 3, // 第一个三角形
|
|
||||||
1, 2, 3 // 第二个三角形
|
|
||||||
};
|
|
||||||
|
|
||||||
// 顶点着色器源码
|
|
||||||
const char *vertexShaderSource =
|
|
||||||
"#version 330 core\n"
|
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
|
||||||
"}\0";
|
|
||||||
|
|
||||||
// 片段着色器源码
|
|
||||||
const char *fragmentShaderSource =
|
|
||||||
"#version 330 core\n"
|
|
||||||
"out vec4 FragColor;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
|
||||||
"}\n\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);
|
||||||
|
|
||||||
|
@ -42,14 +9,16 @@ void processInput(GLFWwindow *window);
|
||||||
const unsigned int SCR_WIDTH = 800;
|
const unsigned int SCR_WIDTH = 800;
|
||||||
const unsigned int SCR_HEIGHT = 600;
|
const unsigned int SCR_HEIGHT = 600;
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
// 设置 glfw
|
// 设置 glfw
|
||||||
glfwInit();
|
glfwInit();
|
||||||
|
// 设置 OpenGL 版本为 3.3
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
// 设置 OpenGL 管线为兼容模式
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
|
||||||
|
|
||||||
// 创建窗口
|
// 创建窗口
|
||||||
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
|
@ -69,87 +38,6 @@ 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;
|
|
||||||
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;
|
|
||||||
glGenVertexArrays(1, &VAO);
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
|
|
||||||
// 创建顶点缓冲对象
|
|
||||||
unsigned int VBO;
|
|
||||||
glGenBuffers(1, &VBO);
|
|
||||||
|
|
||||||
// 将顶点数据复制到缓冲中
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
// 创建索引缓冲对象
|
|
||||||
unsigned int EBO;
|
|
||||||
glGenBuffers(1, &EBO);
|
|
||||||
|
|
||||||
// 将索引数据复制到缓冲中
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
|
|
||||||
GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
// 设置顶点数据解析方式
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
|
|
||||||
// 循环渲染
|
// 循环渲染
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
// 处理输入
|
// 处理输入
|
||||||
|
@ -159,16 +47,48 @@ 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);
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
//使用线框模式绘制
|
//使用线框模式绘制
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
// 设置线宽
|
// 设置线宽
|
||||||
glLineWidth(5.0f);
|
glLineWidth(3.0f);
|
||||||
// 绘制
|
// 设置为 Gouraud 着色模式
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glShadeModel(GL_SMOOTH);
|
||||||
glBindVertexArray(0);
|
// 绘制三角形
|
||||||
|
glBegin(GL_TRIANGLES);
|
||||||
|
glColor3f(1.0f, 0.0f, 0.0f); // 红色
|
||||||
|
glVertex2f(-0.4f, -0.4f);
|
||||||
|
glColor3f(0.0f, 1.0f, 0.0f); // 绿色
|
||||||
|
glVertex2f(0.4f, -0.4f);
|
||||||
|
glColor3f(0.0f, 0.0f, 1.0f); // 蓝色
|
||||||
|
glVertex2f(-0.4f, 0.4f);
|
||||||
|
|
||||||
|
glColor3f(0.0f, 1.0f, 0.0f); // 绿色
|
||||||
|
glVertex2f(0.4f, -0.4f);
|
||||||
|
glColor3f(0.0f, 0.0f, 1.0f); // 蓝色
|
||||||
|
glVertex2f(-0.4f, 0.4f);
|
||||||
|
glColor3f(0.0f, 0.0f, 0.0f); // 黑色
|
||||||
|
glVertex2f(0.4f, 0.4f);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
//使用面模式
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
// 设置为 FLAT 着色模式
|
||||||
|
glShadeModel(GL_FLAT);
|
||||||
|
|
||||||
|
glNewList(1, GL_COMPILE);
|
||||||
|
glBegin(GL_TRIANGLE_FAN);
|
||||||
|
glColor3f(0.0f, 0.0f, 1.0f); // 蓝色
|
||||||
|
glVertex2f(0.5f, 0.5f);
|
||||||
|
glVertex2f(0.5f, 0.7f);
|
||||||
|
glVertex2f(0.7f, 0.5f);
|
||||||
|
|
||||||
|
glVertex2f(0.5f, 0.7f);
|
||||||
|
glVertex2f(0.7f, 0.5f);
|
||||||
|
glVertex2f(0.7f, 0.7f);
|
||||||
|
glEnd();
|
||||||
|
glEndList();
|
||||||
|
|
||||||
|
glCallList(1);
|
||||||
|
|
||||||
// 检查并调用事件,交换缓冲
|
// 检查并调用事件,交换缓冲
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
Reference in a new issue