opengl_triangle.cpp
#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
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";
/**
* 顶点数组对象:Vertex Array Object,VAO
* 顶点缓冲对象:Vertex Buffer Object,VBO
* 索引缓冲对象:Element Buffer Object,EBO或Index Buffer Object,IBO
* @return
*/
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_OPENGL_CORE_PROFILE);
//第三个参数是标题
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpengl", NULL, NULL);
if (window == NULL) {
std::cout << "Fail to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//奇特的写法,本应是个函数,写出来却像个变量,连参数都不需要传了
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "File to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);//指定视口大小,跟java一样
//设置窗口尺寸改变的回调,C++原来是直接把一个函数作为回调传进去
//原来它们的函数名是可以直接使用的,也解释了上面的现象
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//编译顶点着色器,unsigned 无符号,可表示有符号两倍大小,正负符号
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);
//然后就是跟java步骤一样,创建一个程序,把顶点着色器和片段着色器附加到程序对象上,然后链接起来
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
//这儿也可以通过上面方法检测链接器有没有成功,但是省事就不做了
//激活程序
glUseProgram(shaderProgram);
//用完就可以删除这个两个对象了
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
//c++的数组定义,方括号跟名字的前后位置不能互换
//三角形
/*float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
};*/
//矩形
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开始!
0, 1, 3, // 第一个三角形
1, 2, 3 // 第二个三角形
};
unsigned int VBO;//顶点缓冲对象
glGenBuffers(1, &VBO);//&符号应该是传引用对象的意思,不然会传值进去
/*
glBindBuffer(GL_ARRAY_BUFFER, VBO);//用glBindBuffer把新创建的缓冲绑定到GL_ARRAY_BUFFER目标上
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//把数据复制到缓冲区
//告诉opengl如何解析顶点数据
//大体跟java相同,第四参数意思是是否希望数据被标准化,第五个为步长,也就stride,跟java有点不一样
//最后一个则是偏移量
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glUseProgram(shaderProgram);*/
//设置索引数据
unsigned int EBO;//索引缓冲对象
glGenBuffers(1, &EBO);
/* glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);//GL_ELEMENT_ARRAY_BUFFER特别的目标
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),indices,GL_STATIC_DRAW);
glUseProgram(shaderProgram);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);*/
//创建一个VAO,看起来像是对上面的固定过程的封装,毕竟链接着色器程序,指定数据,都是一些固定操作
//且VAO是必须的
unsigned int VAO;
glGenVertexArrays(1, &VAO);
//初始化代码,一般只运行一次
//1.绑定VAP
glBindVertexArray(VAO);
//2.把顶点数组复制到缓冲中供OpenGl使用,GL_ARRAY_BUFFER这个是个特殊的东西
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//2.5 把索引数组复制到一个索引缓冲中,供opengl使用
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//3.设置顶点属性指针
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//画成线
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//循环绘制
while (!glfwWindowShouldClose(window)) {//判断界面是否关闭,没关闭就循环绘制
processInput(window);
//同java
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
// glDrawArrays(GL_TRIANGLES,0,3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
//双缓冲机制,前缓冲保存着最终输出的图像,后缓冲则进行绘制,绘制完成以后与前缓冲交换,就会立即显示
//单缓冲会存在闪烁问题
glfwSwapBuffers(window);//交换颜色缓冲
glfwPollEvents();//检查有没有什么触发事件,鼠标键盘等,并调用相关的回调
}
//回收数据
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glfwTerminate();//结束绘制
return 0;
}
//窗口尺寸改变的回调
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) {
//按下esc键的意思
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);//关闭窗户
}
}
网友评论