所以我一直在通过 C++ 使用 GLFW 和 GLEW 开发游戏,一切都很顺利,直到我实现了 GLEW,然后我得到了一个我以前从未遇到过的错误,我不知道该怎么做,我用谷歌搜索并做了一些研究,但无济于事。我注意到在堆栈框架部分,它说了与我的 main.cpp 中的这行代码有关
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;
}
主要.h
#pragma once
class main
{
public:
main();
~main();
};
窗口.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);
}
}}
窗口.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();
};
}
}
最佳答案
在 Window.cpp 中:
if (glewInit != GLEW_OK) {
std::cout << "GLEW FAILED!" << std::endl;
return false;
}
glewInit()
是函数,不是变量。您需要将其作为函数调用。我很惊讶这甚至编译。
1.1 版本之后的所有其他 OpenGL 函数将抛出错误,导致 ACCESS_VIOLATION READING ADDRESS 0x00000000
或一些类似的错误,因为如果 glewInit()
不是如果调用得当,GLEW 提供的函数宏都不会指向有效的函数指针。
关于c++ - GLEW _First 是 nullptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42425176/