opengl 三角形

news/2024/12/3 3:50:07/

最后效果:
在这里插入图片描述
OpenGL version: 4.1 Metal
不知道为啥必须使用VAO 才行。

#include <glad/glad.h>
#include <GLFW/glfw3.h>#include <iostream>
#include <vector>void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;const char *vertexShaderSource = R"(#version 100attribute vec2 a_position;void main() {gl_Position = vec4(a_position, 0.0, 1.0);}
)";const char *fragmentShaderSource = R"(#version 100precision mediump float;void main() {gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);  // Red color}
)";GLuint compileShader(GLenum type, const char *source)
{GLuint shader = glCreateShader(type);glShaderSource(shader, 1, &source, nullptr);glCompileShader(shader);GLint compiled;glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);if (!compiled){GLint infoLen;glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);std::vector<char> infoLog(infoLen);glGetShaderInfoLog(shader, infoLen, nullptr, infoLog.data());std::cerr << "Error compiling shader: " << infoLog.data() << std::endl;glDeleteShader(shader);return 0;}return shader;
}GLuint createProgram(const char *vertexSource, const char *fragmentSource)
{GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSource);GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentSource);GLuint program = glCreateProgram();glAttachShader(program, vertexShader);glAttachShader(program, fragmentShader);glLinkProgram(program);GLint linked;glGetProgramiv(program, GL_LINK_STATUS, &linked);if (!linked){GLint infoLen;glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);std::vector<char> infoLog(infoLen);glGetProgramInfoLog(program, infoLen, nullptr, infoLog.data());std::cerr << "Error linking program: " << infoLog.data() << std::endl;glDeleteProgram(program);return 0;}glDeleteShader(vertexShader);glDeleteShader(fragmentShader);return program;
}int main()
{// glfw: initialize and configureglfwInit();glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);#ifdef __APPLE__glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif// glfw window creationGLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);if (window == NULL){std::cout << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;}glfwMakeContextCurrent(window);glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){std::cout << "Failed to initialize GLAD" << std::endl;return -1;}glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);GLuint program = createProgram(vertexShaderSource, fragmentShaderSource);glUseProgram(program);if (glGetError() != GL_NO_ERROR){std::cerr << "Error in glUseProgram." << std::endl;}// Define vertex dataGLfloat vertices[] = {0.0f, 0.5f,   // Vertex 1-0.5f, -0.5f, // Vertex 20.5f, -0.5f   // Vertex 3};// 创建 VAO 并绑定GLuint vao;glGenVertexArrays(1, &vao);glBindVertexArray(vao);// Create and bind vertex buffer (VBO)GLuint vbo;glGenBuffers(1, &vbo);GLenum error = glGetError();if (error != GL_NO_ERROR){std::cerr << "Error during drawing 1: " << error << std::endl;}glBindBuffer(GL_ARRAY_BUFFER, vbo);error = glGetError();if (error != GL_NO_ERROR){std::cerr << "Error during drawing 2: " << error << std::endl;}glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);error = glGetError();if (error != GL_NO_ERROR){std::cerr << "Error during drawing 3: " << error << std::endl;}// Get the location of the attributeGLint aPositionLocation = glGetAttribLocation(program, "a_position");if (aPositionLocation == -1){std::cerr << "Error: Attribute 'a_position' not found in shader." << std::endl;return -1;}glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid *)0);glEnableVertexAttribArray(aPositionLocation);GLenum err = glGetError();if (err != GL_NO_ERROR){std::cerr << "OpenGL error: " << err << std::endl;}glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// Get OpenGL versionconst char *version = (const char *)glGetString(GL_VERSION);std::cout << "OpenGL version: " << version << std::endl;while (!glfwWindowShouldClose(window)){glClear(GL_COLOR_BUFFER_BIT);glBindBuffer(GL_ARRAY_BUFFER, vbo);GLenum drawError = glGetError();glBindVertexArray(vao);glUseProgram(program);glDrawArrays(GL_TRIANGLES, 0, 3);glfwSwapBuffers(window);glfwPollEvents();}// CleanupglDeleteBuffers(1, &vbo);glfwTerminate();return 0;
}void processInput(GLFWwindow *window)
{if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)glfwSetWindowShouldClose(window, true);
}void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{glViewport(0, 0, width, height);
}

http://www.ppmy.cn/news/1551907.html

相关文章

Unity类银河战士恶魔城学习总结(P150 End Screen结束重启按钮)

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili 教程源地址&#xff1a;https://www.udemy.com/course/2d-rpg-alexdev/ 本章节实现了死亡后重新启动游戏&#xff0c;并且加入了游戏管理器 加入了重新开始游戏的按钮 GameManager.cs using System.Collection…

vue3中 axios 发送请求 刷新token 封装axios

service.js 页面 import axios from axios // 创建axios实例 const instance axios.create({baseURL: http://gcm-test.jhzhkj.cn:8600/h5card/,timeout: 5000, // 请求超时时间headers: {get: {Content-Type: application/x-www-form-urlencoded},post: {Content-Type: appl…

根据后台数据结构,构建搜索目录树

效果图&#xff1a; 数据源 const data [{"categoryidf": "761525000288210944","categoryids": "766314364226637824","menunamef": "经济运行","menunames": "经济运行总览","tempn…

[Go] slice切片详解

切片详解 切片的实现 Go 中的切片本质上是一个结构体&#xff0c;包含以下三个部分&#xff1a; 指向底层数组的指针&#xff08;array&#xff09;&#xff1a;切片指向一个底层数组&#xff0c;数组中存储着切片的数据。切片的长度&#xff08;len&#xff09;&#xff1a…

通信原理实验:PCM编译码

目录 一、实验目的和要求 二、实验内容和原理 实验器材 实验原理 实验原理框图 实验框图说明 三、实验步骤 实验项目二 PCM 编码规则验证 四、实验记录与处理(数据、图表、计算等) 五、实验结果及分析 一、实验目的和要求 掌握脉冲编码调制与解调的原理。掌握脉冲编…

百度 文心一言 vs 阿里 通义千问 哪个好?

背景介绍&#xff1a; 在当前的人工智能领域&#xff0c;随着大模型技术的快速发展&#xff0c;市场上涌现出了众多的大规模语言模型。然而&#xff0c;由于缺乏统一且权威的评估标准&#xff0c;很多关于这些模型能力的文章往往基于主观测试或自行设定的排行榜来评价模型性能…

手机卡限速丨中国移动5G变3G,网速500kb

以下猜测错误&#xff0c;又有新的猜测&#xff1a;河南移动的卡出省限速。可能是因为流量结算。 “2024年7月1日起&#xff0c;中国移动集团内部将开启跨省流量结算” 在深圳四五年了&#xff0c;之前没有过&#xff0c;就从上个月开始。 中国移动会自动把近期使用流量较少…

安装SQL Server 2022提示需要Microsoft .NET Framework 4.7.2 或更高版本

安装SQL Server 2022提示需要Microsoft .NET Framework 4.7.2 或更高版本。 原因是&#xff1a;当前操作系统版本为Windows Server 2016 Standard版本&#xff0c;其自带的Microsoft .NET Framework 版本为4.6太低&#xff0c;不满足要求。 根据报错的提示&#xff0c;点击链接…