标签:arc 坐标 arrays gets osi ini config 拆分 manage
在创建窗口的基础上,添加代码实现三角形的绘制。
在屏幕上绘制一个三角形需要的变量有:
float vertices[] = {-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f};
unsigned int VBO;
unsigned int VAO;
int vertexShader;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 Pos;"
"void main()"
"{gl_Position = vec4(Pos.x, Pos.y, Pos.z, 1.0f);}";
int fragmentShader;
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 fragColor;"
"void main()"
"{fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);}";
int shaderProgram;
绘制一个对象的过程略繁琐,可以拆分成几个部分:
调用glCreateShader函数创建一个Shader对象,传递参数GL_VERTEX_SHADER使该对象为Vertex Shader对象。
调用glShaderSource函数将Vertex Shader源码附加到Shader对象上。
调用glCompileShader函数编译源码。
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
和上一步的区别在于传递参数不同。
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
调用glCreateProgram函数创建Shader Program对象。
调用两次glAttachShader函数将Shader附加到Shader Program对象上。
调用glLinkProgr函数进行链接。
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
链接成功后调用glDeleteShader函数移除Shader对象,释放被占用的资源。
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glGenVertexArrays函数用来创建VAO,并生成对象ID。第一个参数指定需要创建的VAO数量。
glGenVertexArrays(1, &VAO);
和创建VAO类似,调用glGenBuffers函数创建VBO。
glGenBuffers(1, &VBO);
调用glBindVertexArray函数绑定VAO,接下来对顶点属性的使能、顶点属性指针的配置和相应的VBO,都将存储在这个VAO中。
需要解绑当前VAO时,将参数设置为0即可。
glBindVertexArray(VAO);
VBO的Buffer类型是GL_ARRAY_BUFFER,通过调用glBindBuffer函数绑定VBO和GL_ARRAY_BUFFER。
之后,当再次调用GL_ARRAY_BUFFER,便可完成对VBO的配置。glBufferData函数能够把顶点数据复制到Buffer内存中供GPU使用。
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
当顶点数据被复制到Buffer内存中之后,还需要配置对顶点数据的解析。就是告诉GPU,Vertex Shader的哪一个顶点属性,对应着Buffer中的哪一部分数据。
在编写Vertex Shader源码时,只定义了一个顶点属性:位置,该属性的索引为0,因此设置glVertexAttribPointer的第一个参数为0。
每一个顶点数据有x,y,z三个维度,用来表示该顶点的位置,因此glVertexAttribPointer的第二个参数为3。
第三个参数指定顶点数据的类型。第四个参数设置是否需要将顶点数据标准化,即映射到[-1, 1]。
第五个参数指定同一顶点属性的相邻数据之间的步长,这里为3个float类型的长度。
第六个参数指定某一顶点属性下第一个数据的起始位置。
最后调用glEnableVertexAttribArray函数使能该顶点属性。
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
配置完成后,解绑VAO和VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
在while循环中添加三个函数,激活Shader Program,绑定VAO并在视口中绘制三角形。
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
╮(╯▽╰)╭画个三角形真不容易。。。
完整代码如下:
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
using namespace std;
/*
void frambuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
*/
void processInput(GLFWwindow* window)
{
//check if ESCAPE is pressed
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
const unsigned int window_width = 800;
const unsigned int window_height = 600;
//coordnate (x,y,z) of vertices
float vertices[] = {-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f};
//vertex buffer object(VBO)
unsigned int VBO;
//vertex array object(VAO)
unsigned int VAO;
//vertext shader
int vertexShader;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 Pos;"
"void main()"
"{gl_Position = vec4(Pos.x, Pos.y, Pos.z, 1.0f);}";
//fragment shader
int fragmentShader;
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 fragColor;"
"void main()"
"{fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);}";
//shader program
int shaderProgram;
int main()
{
//initialize GLFW
if(!glfwInit())
return -1;
//configure GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
//creat a window object
GLFWwindow *window = glfwCreateWindow(window_width, window_height, "OpenGL_Demo", NULL, NULL);
if (window == NULL){
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//initialize GLAD to manage function pointers for OpenGL
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
cout << "Failed to initialize GLAD" << endl;
return -1;
}
//set width and height of Viewport
glViewport(0, 0, window_width, window_height);
//glfwSetFramebufferSizeCallback(window, frambuffer_size_callback);
//compile vertex shader
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
//check if compilation of vertex shader is successful
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success){
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
cout << "ERROR::VERTEXSHADER::COMPILATION_FAILED\n" << infoLog << endl;
}
else cout << "VERTEXSHADER_COMPILATION_SUCCESS" << endl;
//cmpile fragment shader
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
//check if compilation of fragment shader is successful
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success){
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
cout << "ERROR::FRAGMENTSHADER::COMPILATION_FAILED\n" << infoLog << endl;
}
else cout << "FRAGMENTSHADER_COMPILATION_SUCCESS" << endl;
//link shader program
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
//check if linking is successful
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success){
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
cout << "ERROR::LINKING_FAILED\n" << infoLog << endl;
}
else cout << "LINKING_SUCCESS" << endl;
//clear resource of shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
//generate vertex array objext
glGenVertexArrays(1, &VAO);
//generate vertex buffer object
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//link vertex attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//render loop
while(!glfwWindowShouldClose(window)){
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
//clear resource
glfwTerminate();
return 0;
}
标签:arc 坐标 arrays gets osi ini config 拆分 manage
原文地址:https://www.cnblogs.com/yiqian/p/10733545.html