GLEW _First was nullptr - c++

So I have been working on a game through C++ with GLFW and GLEW and everything was going fine until I implemented GLEW then I get an error that I have never encountered before and I don't have the foggiest on what to do, I have googled it and done some research but to no avail. I have noticed that in the stack frame section it says something to do with this line of code in my main.cpp
std::cout << glGetString(GL_VERSION) << std::endl;
Also it says something to do with memory. I'll leave the rest of my code down below and if there's any info you need just ask and I will try my best to provide it
So I just discovered if i take out
std::cout << glGetString(GL_VERSION) << std::endl;
then it works however the window isn't created.
Where do I go from here?
Any idea?
#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();
};
}
}

In Window.cpp:
if (glewInit != GLEW_OK) {
std::cout << "GLEW FAILED!" << std::endl;
return false;
}
glewInit() is a function, not a variable. You need to call it as a function. I'm surprised this even compiled.
All other OpenGL functions that come from after version 1.1 will throw errors to the effect of ACCESS_VIOLATION READING ADDRESS 0x00000000 or some similar error, because if glewInit() is not properly called, none of the function macros provided by GLEW will point to valid function pointers.

Related

Continuously render Gtk::GLArea in gtkmm

I've followed a gtkmm4 + opengl example, the window loads and displays without any errors and renders one frame but then seems to ignore my queue_render() and queue_draw() calls as evidenced by the print statements in the console. I've set auto render to true as well.
#include <iostream>
#include <string>
#include <gtkmm.h>
#include <giomm/resource.h>
#define GLEW_STATIC
#include <gl/glew.h>
class TestWindow : public Gtk::Window {
protected:
Gtk::GLArea mGlArea;
void onRealize() {
std::cout << "onRealize()" << std::endl;
mGlArea.make_current();
glewExperimental = true;
if (glewInit() != GLEW_OK) {
std::cout << "ERROR: Failed to initialize GLEW" << std::endl;
}
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void onUnrealize() {
}
bool onRender(const Glib::RefPtr<Gdk::GLContext>& context) {
std::cout << "onRender()" << std::endl;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mGlArea.queue_render();
mGlArea.queue_draw();
return true;
}
public:
TestWindow() {
set_title("Window Title");
set_child(mGlArea);
mGlArea.set_auto_render(true);
mGlArea.set_size_request(800, 600);
mGlArea.set_required_version(4, 0);
mGlArea.signal_realize().connect(sigc::mem_fun(*this, &TestWindow::onRealize));
mGlArea.signal_unrealize().connect(sigc::mem_fun(*this, &TestWindow::onUnrealize), false);
mGlArea.signal_render().connect(sigc::mem_fun(*this, &TestWindow::onRender), false);
mGlArea.show();
}
};
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("test.gtkmm");
return app->make_window_and_run<TestWindow>(argc, argv);
}

opengl window initialization issue

I'm trying to encapsulate GLFW in a class to make my code cleaner.
So in my class, I've made the basic window creation but when I execute the program, it always enter in the if statement where I check if the initialization went right or not and I don't know why.
This is what I have
#include "include/Window.h"
#include <iostream>
Window::Window(int x, int y) {
_window = glfwCreateWindow(x, y, "Heightmap Visualizer", NULL, NULL);
}
Window::~Window() {
glfwTerminate();
}
int Window::createWindow() {
if (!_window) {
std::cerr << "went here" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(_window);
run();
glfwTerminate();
return 0;
}
void Window::run() {
while (!glfwWindowShouldClose(_window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(_window);
glfwPollEvents();
}
}
This is the header file
#include <GLFW/glfw3.h>
class Window {
public:
Window(int x, int y);
~Window();
int createWindow();
void run();
private:
GLFWwindow* _window;
};
Make sure to glfwInit().
Also good luck in your jorney learning GL and C++!

std::cout << glGetString(GL_RENDER) << std::endl; Throws an Error but not GL_Renderer or GL_Verision and I can't figure out why?

First time asking a question on here so if it's not layed out correctly or something is missing I'll try to get everything else up.
I'm trying to learn OpenGl because I'm interested in developing games of my own and I would rather create my own engine than one already out there.
I'm using GLEW also and I know it inits because the code doesn't output an Error
I spend alittle while googling the error code and the OpenGL but none of the problems really match what I'm getting.
I also tried the GLEW_Experimental = true but that did not change anything.
Code: main
#include "src/graphics/window.h"
int main()
{
using namespace FutureGamingEngine;
using namespace graphics;
Window window("Test", 1080, 720);
glClearColor(0, 255, 0, 1.0f); //Red, Green, Blue, No Idea
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << glGetString(GL_RENDERER) << std::endl;
std::cout << glGetString(GL_RENDER) << std::endl;
while (!window.closed())
{
window.clear();
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f,-0.5f);
glEnd();
window.update();
}
//system("PAUSE");
return 0;
}
Window:
#include "window.h"
namespace FutureGamingEngine
{
namespace graphics
{
void windowResize(GLFWwindow *window, int width, int height);
Window::Window(const char *title, int width, int height)
{
c_title = title;
c_height = height;
c_width = width;
if (!init())
{
glfwTerminate();
}
}
Window::~Window()
{
glfwTerminate();
}
bool Window::init()
{
glewExperimental = true;
if (!glfwInit())
{
std::cout << "Error Code: 0" << std::endl;
return false;
}
//Create a windowed mode and it's OpenGl Content
c_window = glfwCreateWindow(c_width, c_height, c_title, NULL, NULL);
//If we fail to make the window Terminate OpenGL
if (!c_window)
{
glfwTerminate();
return false;
}
//std::cout << "Open GL: " << glGetString(GL_VERSION) << std::endl;
glfwMakeContextCurrent(c_window);
glfwSetWindowSizeCallback(c_window, windowResize);
if (glewInit() != GLEW_OK)
{
std::cout << "Error Code: 1" << std::endl;
return false;
}
return true;
}
bool Window::closed() const
{
return glfwWindowShouldClose(c_window);
}
void Window::update()
{
//poll for and process events
glfwPollEvents();
glfwGetFramebufferSize(c_window, &c_width, &c_height);
//swap front and backbuffers
glfwSwapBuffers(c_window);
}
void Window::clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void windowResize(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
}
}
The Header for window:
#pragma once
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
namespace FutureGamingEngine
{
namespace graphics
{
class Window
{
private:
const char *c_title;
int c_width, c_height;
GLFWwindow *c_window;
bool c_closed;
public:
Window(const char *a_name, int a_width, int a_height);
~Window();
bool closed() const;
void update();
void clear() const;
inline int getHeight() const { return c_height; };
inline int getWidth() const { return c_width; };
private:
bool init();
};
}
}
I'm expecting it to tell me the Render Version. What I'm actually getting is Error Produced on std::cout << glGetString(GL_RENDER) << std::endl; :
Exception thrown at 0x7C50F6E0 (ucrtbased.dll) in FutureGamingEngine-Core.exe: 0xC0000005: Access violation reading location 0x00000000.
(https://i.imgur.com/uKu3hxX.png)
and the doesn't get to rendering the Triangle like it did prior to me asking for Gl_render
GL_RENDERER is valid enum, GL_RENDER is not. See OpenGL doc
Thank you Ripi2! I apperently did not look hard enough :D

Failing to create a window in GLFW C++

Some background if you care/if it helps. I am pretty knew to programming and C++, I tried to learn it a couple times before but couldn't stay interested. As I couldn't think of an interesting project to do as I learn. Eventually I thought of making a game engine(dumb idea for a noob, but it will be a good learning experience and I'm interested in doing it.), so I searched for a series on making one to get an idea and found TheChernoProject's series and here I am.
I'm trying to make a make a window in GLFW and C++ but it keeps failing, I have no idea what to do.
Main.cpp
#include "src\graphics\window.h"
int main() {
using namespace tmge;
using namespace graphics;
Window window("Test", 1280, 720);
while (!window.windowClosed()) {
window.update();
}
return 0;
}
Window.h
#pragma once
#include <GLFW\glfw3.h>
#include <iostream>
namespace tmge { namespace graphics {
class Window {
private:
GLFWwindow *privateWindow;
int privateWidth, privateHeight;
const char *privateTitle;
bool privateWindowClosed();
public:
Window(const char *title, int width, int height);
~Window();
bool init();
void update() const;
bool windowClosed() const;
};
} }
Window.cpp
#include "window.h"
namespace tmge { namespace graphics {
Window::Window(const char *title, int width, int height) {
privateTitle = title;
privateWidth = width;
privateHeight = height;
if (!init()) {
glfwTerminate();
}
}
Window::~Window() {
glfwTerminate();
}
bool Window::init() {
if (!glfwInit) {
std::cout << "GLFW initialaztion failed" << std::endl;
return false;
}
privateWindow = glfwCreateWindow(privateWidth, privateHeight, privateTitle, NULL, NULL);
if (!privateWindow) {
std::cout << "Failed to create GLFW window" << std::endl;
return false;
}
glfwMakeContextCurrent(privateWindow);
return true;
}
bool Window::windowClosed() const{
return glfwWindowShouldClose(privateWindow);
}
void Window::update() const{
glfwPollEvents();
glfwSwapBuffers(privateWindow);
}
} }

How to set (0, 0) coordinate position for GLFW window

I'm creating a GLFW window, and I would like the bottom left corner to be the origin. For some reason, the top left corner is the origin.
I should also mention that I'm using OSX.
Here's my window class:
#include "window.h"
namespace graphics {
void window_size_callback(GLFWwindow* window, int width, int height);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mode);
void cursor_position_callback(GLFWwindow* window, double mouse_x, double mouse_y);
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 << "GLFW failed to initialize!" << std::endl;
return false;
}
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
m_window = glfwCreateWindow(m_width, m_height, m_title, NULL, NULL);
if (!m_window) {
glfwTerminate();
std::cout << "GLFW failed to create a window!" << std::endl;
return false;
}
glfwMakeContextCurrent(m_window);
glfwSetWindowUserPointer(m_window, this);
glfwSetWindowSizeCallback(m_window, window_size_callback);
glfwSetKeyCallback(m_window, key_callback);
glfwSetMouseButtonCallback(m_window, mouse_button_callback);
glfwSetCursorPosCallback(m_window, cursor_position_callback);
if (glewInit() != GLEW_OK) {
std::cout << "GLEW failed to initialize!" << std::endl;
return false;
}
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "OpenGL Shading Language Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
return true;
}
void Window::update() {
glfwPollEvents();
glfwSwapBuffers(m_window);
}
void Window::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
bool Window::isClosed() const {
return glfwWindowShouldClose(m_window);
}
bool Window::isKeyPressed(unsigned int keycode) const {
if (keycode >= MAX_KEYS)
return false;
return m_keys[keycode];
}
bool Window::isMouseButtonPressed(unsigned int button) const {
if (button >= MAX_BUTTONS)
return false;
return m_mouse_buttons[button];
}
void Window::getMousePosition(double& x, double& y) const {
x = m_mouse_x;
y = m_mouse_y;
}
void window_size_callback(GLFWwindow* window, int width, int height) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
win->m_width = width;
win->m_height = height;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
win->m_keys[key] = action != GLFW_RELEASE;
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mode) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
win->m_mouse_buttons[button] = action != GLFW_RELEASE;
}
void cursor_position_callback(GLFWwindow* window, double mouse_x, double mouse_y) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
win->m_mouse_x = mouse_x;
win->m_mouse_y = mouse_y;
}
}
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
But here is a better way to visualize this : use the Right Hand Rule
X is your thumb
Y is your index
Z is your middle finger. If you put
your thumb to the right and your index to the sky, it will point to
your back, too. Having the Z in this direction is weird, so why is it
so ? Short answer : because 100 years of Right Hand Rule Math will
give you lots of useful tools. The only downside is an unintuitive Z.
This is something you can’t change, it’s built in your graphics card.
So (-1,-1) is the bottom left corner of your screen. (1,-1) is the
bottom right, and (0,1) is the middle top. So this triangle should
take most of the screen.