初入 OpenGL ---白屏问题 -- glad.c在查找预编译头遇到意外的文件结尾,是否忘记向源中添加#include "stdafx.h" ?

学习地址:https://learnopengl-cn.github.io 

学习成果:

前言:

跟着教程走,用VS2017 配置完了OpenGL的运行.

不得不说...这玩意配置比JAVA配置还麻烦...

好在终于跑起来了.

 感谢 Bruce_wjh 博主的配置教程,比官方的好很多.(可惜还有些缺陷)

https://blog.csdn.net/qq_37338983/article/details/78997179

 

问题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);
    }
}

运行结果:

猜你喜欢

转载自www.cnblogs.com/--zz/p/9690569.html
今日推荐