OpenGL 入门指南(配置GLFW ,着色语言高亮,以及一些常见问题)
本文内容:
配置OPENGL
学习地址:
https://learnopengl-cn.github.io
学习成果:

1.阅读配置教程
https://blog.csdn.net/qq_37338983/article/details/78997179
感谢 Bruce_wjh 博主的配置教程,比官方的好很多.(可惜还有些缺陷)
2.补充教程的不足
(1) GLFW 的安装是可以在VS中NuGet的完成的(如图)
(2) GLAD得自己安装,不过看上面的配置教程是很快的.
(3) 用到的GLSL语言代码高亮需要自己去装个插件.(如图)


GLSL 高亮插件
工具-获取工具与功能
 
问题1:

直接往项目中添加glad.c 会报错:
----glad.c在查找预编译头遇到意外的文件结尾,是否忘记向源中添加#include "stdafx.h" ?

解决方法:
右击glad.c -> 属性 -> C/C++ -> 预编译头 -> 不使用预编译头

问题2: OpenGL窗口白屏并且未响应

解决方案:
1.检查是否忘记使用缓存 glfwSwapBuffers(window); (一般原因都是缓冲忘记交换)
2.是否 glfwWindowShouldClose(window) 打错
glfwWindowShouldClose
(ps: 第二种是可以通过编译的.....这里坑了好久)
附上代码:
// 头文件位置不一定都一样
#include "stdafx.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
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, "Oh!I see you!", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create the windows" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    
    while (!glfwWindowShouldClose(window)) {
        //输入处理
        processInput(window);
        //渲染指令
        glClearColor(0.2f,0.3f,0.3f,1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}
void framebuffer_size_callback(GLFWwindow* windows, int width, int height) {
    glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    }
}
运行结果:

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号