#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
int main()
{
glfwInit();//初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//TODO: Core Profile
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
int windowWidth = 1280;
int windowHeigth = 720;
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeigth, "MMOpenGL", NULL, NULL);
//OpenGL运行模式->单线程
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);//初始化GLAD
char* shaderStr = (char*)"";
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader, 1, &shaderStr, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);//获取错误
GLint logLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);//获取错误
if (logLen > 0)
{
char* log = (char*)malloc(logLen);
glGetShaderInfoLog(shader, logLen, NULL, log);
std::cout << log << std::endl;
free(log);
}
while (!glfwWindowShouldClose(window))
{
//TODO 绘制操作
//TODO
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<iostream>
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;
int main()
{
//GLFW:initialize and configure
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//(选项名称,选项值)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfw window creation
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();//正确释放/删除之前分配的资源
return -1;
}
glfwMakeContextCurrent(window);//将该窗口作为该线程的主上下文
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))//初始化GLAD
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
//渲染循环
while (!glfwWindowShouldClose(window))//每次循环开始前检查一次GLFW是否被要求退出
{
processInput(window);
//渲染指令
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//状态设置函数
glClear(GL_COLOR_BUFFER_BIT);//状态使用函数
//检查并调用事件,交换缓冲
glfwSwapBuffers(window);//交换颜色缓冲(是一个存储着GLFW窗口每一个像素颜色值的大缓冲),它在这一迭代中被用来绘制,并作为输出显示在屏幕上
glfwPollEvents();//检查是否触发什么事件,更新窗口状态并调用对应的回调函数
}
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);
}