初入 OpenGL ---白屏問題 -- glad.c在查找預編譯頭遇到意外的文件結尾,是否忘記向源中添加#include "stdafx.h" ?
阿新 • • 發佈:2018-09-22
setw 檢查 wid event buffers cout stream swap turn
學習地址: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" ?