码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-22 18:33:36      阅读:492      评论:0      收藏:0      [点我收藏+]

标签:adp   col   version   events   解决   while   stream   mic   cap   

学习地址: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);
    }
}

 

运行结果:

技术分享图片

 

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

标签:adp   col   version   events   解决   while   stream   mic   cap   

原文地址:https://www.cnblogs.com/--zz/p/9690569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!