所以我一直在通過C++與GLFW和GLEW一起工作,一切都很順利,直到我實現了GLEW,然後我得到了一個我以前從未遇到的錯誤,並且我沒有模糊該怎麼辦,我已經搜索了它並做了一些研究,但無濟於事。我注意到,在棧幀部分,它說什麼做這行代碼在我的main.cppGLEW _First was nullptr
std::cout << glGetString(GL_VERSION) << std::endl;
而且它說是與記憶。我會留下樓下的我的代碼的其餘部分,如果有任何信息,你只需要問,我會盡我所能提供它
於是我就發現,如果我拿出
std::cout << glGetString(GL_VERSION) << std::endl;
則但是該窗口並未創建。
我該從哪裏出發? 有什麼想法?
#include "src\graphics\window.h"
int main() {
using namespace benji;
using namespace graphics;
Window window("Benji Engine", 1280, 720);
glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
std::cout << glGetString(GL_VERSION) << std::endl;``
while (!window.closed()) {
std::cout << window.getWidth() << ", " << window.getHeight() << std::endl;
window.clear();
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
window.update();
}
return 0;
}
main.h
#pragma once
class main
{
public:
main();
~main();
};
window.cpp
#include "window.h"
namespace benji { namespace graphics {
void windowResize(GLFWwindow *window, int width, int height);
Window::Window(const char *title, int width, int height) {
m_Title = title;
m_Width = width;
m_Height = height;
if (!init()) {
glfwTerminate();
}
}
Window::~Window() {
glfwTerminate();
}
bool Window::init() {
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW!" << std::endl;
return false;
}
m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!m_Window) {
std::cout << "Failed to create GLFW window!" << std::endl;
return false;
}
glfwMakeContextCurrent(m_Window);
glfwSetWindowSizeCallback(m_Window, windowResize);
if (glewInit != GLEW_OK) {
std::cout << "GLEW FAILED!" << std::endl;
return false;
}
return true;
}
void Window::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::update(){
glfwPollEvents();
glfwSwapBuffers(m_Window);
}
bool Window::closed() const {
return glfwWindowShouldClose(m_Window) == 1;
}
void windowResize(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
}}
window.h中
#pragma once
#include <iostream>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
namespace benji {
namespace graphics {
class Window {
private:
const char *m_Title;
int m_Width, m_Height;
GLFWwindow *m_Window;
bool m_Closed;
public:
Window(const char *title, int width, int height);
~Window();
bool closed() const;
void update();
void clear() const;
int getWidth() const {
return m_Width;
}
int getHeight() const { return m_Height; }
private:
bool init();
};
}
}
什麼是完整錯誤?你有一個調試callstack或任何其他信息? – ssell
我能看到的唯一的其他錯誤是拋出異常:讀訪問衝突。 _First是nullptr。 –
如果'glGetString'確實崩潰,那是因爲你的OpenGL沒有被正確初始化。你在'Window :: init'期間是否收到錯誤輸出?當你移除對'glGetString'的調用時,它也不會崩潰,但是沒有窗口打開。另請參閱「[爲什麼glGetString(GL_VERSION)可能導致Seg錯誤?](http://stackoverflow.com/questions/6288759/why-could-glgetstringgl-version-be-causing-a-seg-fault)」 – ssell