feat(OpenGL): hello
This commit is contained in:
parent
b1dc69c442
commit
a3e1bc1e00
1 changed files with 52 additions and 21 deletions
|
@ -3,25 +3,46 @@
|
|||
#include <iostream>
|
||||
|
||||
// 三角形顶点
|
||||
float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f};
|
||||
float vertices[] = {
|
||||
// 第一个三角形
|
||||
0.4f, 0.4f, 0.0f, // 右上角
|
||||
0.4f, -0.4f, 0.0f, // 右下角
|
||||
-0.4f, -0.4f, 0.0f, // 左下角
|
||||
-0.4f, 0.4f, 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 *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 =
|
||||
"#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";
|
||||
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 processInput(GLFWwindow *window);
|
||||
|
@ -112,20 +133,24 @@ int main() {
|
|||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
|
||||
unsigned int VAO,VBO,EBO;
|
||||
// 创建顶点数组对象
|
||||
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);
|
||||
|
||||
// 设置顶点数据解析方式
|
||||
// 创建索引缓冲对象
|
||||
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);
|
||||
|
||||
|
@ -141,7 +166,13 @@ int main() {
|
|||
// 绘制三角形, 激活程序
|
||||
glUseProgram(shaderProgram);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
//使用线框模式绘制
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
// 设置线宽
|
||||
glLineWidth(3.0f);
|
||||
// 绘制
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// 检查并调用事件,交换缓冲
|
||||
glfwSwapBuffers(window);
|
||||
|
|
Reference in a new issue