#include<iostream>
#define GLEW_STATIC
#include<GL/glew.h>
#include<GLFW/glfw3.h>
const char* vertexShaderSource =
"#version 330 core \n"
"layout(location = 0) in vec3 position; \n"
"void main() { \n"
" gl_Position = vec4(position.x, position.y, position.z, 1.0);} \n ";
const char* fragmentShaderSource =
"#version 330 core \n"
"out vec4 color; \n"
"void main(){ \n"
" color = vec4(1.0f, 0.5f, 0.2f, 1.0f);} \n";
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
//key表示按下的键,action表示键按下/释放,mode表示是否有Ctrl、Shift、Alt等按钮操作
void key_callback(GLFWwindow * window, int key, int scancode, int action, int mode)
{
//用户按下ESC键,设置窗口WindowShouldClose属性为true,关闭应用程序
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//核心模式
GLFWwindow* window = glfwCreateWindow(800,600,"OpenGL Game",NULL,NULL); //创建窗口
if (window == NULL)
{
std::cout << "Open window failed!" << std::endl;
glfwTerminate();
return -1;
}
//将窗口的上下文设置为当前线程的主上下文
glfwMakeContextCurrent(window);
//GLEW是用来管理OpenGL的函数指针,调用任何OpenGL的函数之前都需要初始化GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Init Glew failed!" << std::endl;
glfwTerminate();
return -1;
}
int width, height;
//从GLFW中获取视口大小
glfwGetFramebufferSize(window,&width,&height);
//窗口左下角的位置、宽度、高度
glViewport(0,0, width, height);
//注册按键回调函数
glfwSetKeyCallback(window, key_callback);
//顶点数组对象
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 vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
//创建片段着色器
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
//创建着色器程序
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
//顶点属性配置,告诉OpenGL该如何解析顶点数据
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//检查GLFW是否要求退出
while (!glfwWindowShouldClose(window))
{
//检查是否触发事件(鼠标移动、键盘输入等),如果有则调用相应的回调函数
glfwPollEvents();
//渲染操作放入循环中
//清屏
glClearColor(0.2f, 0.3f, 0.3f, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
//启用着色器程序
glUseProgram(shaderProgram);
//绘制
glDrawArrays(GL_TRIANGLES, 0, 3);
//交换颜色缓冲
glfwSwapBuffers(window);
}
//结束时释放资源
glfwTerminate();
return 0;
}
运行结果: