Is there a solution to this error? GLFW C++ - c++

I am on a Ubuntu 20.04 LTS system, and I cannot get my CPP code to run. Everytime I try to compile this:
#include <iostream>
#include <GLFW/glfw3.h>
#include "glad/glad.h"
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, 800, "Endless Space", NULL, NULL);
if (window == NULL)
{
std::cout << "FAILED TO LAUNCH WINDOW! TERMINATING..." << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
do {
glfwPollEvents();
}
while (!glfwWindowShouldClose);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Via this command: g++ main.cpp -o EndlessSpace
I receive this error:
In file included from main.cpp:3:
glad/glad.h:27:2: error: #error OpenGL header already included, remove this include, glad already provides it
27 | #error OpenGL header already included, remove this include, glad already provides it
| ^~~~~
In file included from main.cpp:3:
glad/glad.h:1305: warning: "GL_INVALID_INDEX" redefined
1305 | #define GL_INVALID_INDEX 0xFFFFFFFF
|
In file included from /usr/include/GL/gl.h:2050,
from /usr/include/GLFW/glfw3.h:210,
from main.cpp:2:
/usr/include/GL/glext.h:1355: note: this is the location of the previous definition
1355 | #define GL_INVALID_INDEX 0xFFFFFFFFu
|
In file included from main.cpp:3:
glad/glad.h:1347: warning: "GL_TIMEOUT_IGNORED" redefined
1347 | #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFF
|
In file included from /usr/include/GL/gl.h:2050,
from /usr/include/GLFW/glfw3.h:210,
from main.cpp:2:
/usr/include/GL/glext.h:1430: note: this is the location of the previous definition
1430 | #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
|
I have in-fact tried removing the GLFW header, resulting in this error:
main.cpp: In function ‘int main()’:
main.cpp:6:5: error: ‘glfwInit’ was not declared in this scope
6 | glfwInit();
| ^~~~~~~~
main.cpp:8:20: error: ‘GLFW_CONTEXT_VERSION_MAJOR’ was not declared in this scope
8 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:8:5: error: ‘glfwWindowHint’ was not declared in this scope
8 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
| ^~~~~~~~~~~~~~
main.cpp:9:20: error: ‘GLFW_CONTEXT_VERSION_MINOR’ was not declared in this scope
9 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:10:20: error: ‘GLFW_OPENGL_PROFILE’ was not declared in this scope
10 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
| ^~~~~~~~~~~~~~~~~~~
main.cpp:10:41: error: ‘GLFW_OPENGL_CORE_PROFILE’ was not declared in this scope
10 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
| ^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:12:5: error: ‘GLFWwindow’ was not declared in this scope
12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
| ^~~~~~~~~~
main.cpp:12:17: error: ‘window’ was not declared in this scope
12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
| ^~~~~~
main.cpp:12:26: error: ‘glfwCreateWindow’ was not declared in this scope
12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
| ^~~~~~~~~~~~~~~~
main.cpp:17:9: error: ‘glfwTerminate’ was not declared in this scope
17 | glfwTerminate();
| ^~~~~~~~~~~~~
main.cpp:21:5: error: ‘glfwMakeContextCurrent’ was not declared in this scope
21 | glfwMakeContextCurrent(window);
| ^~~~~~~~~~~~~~~~~~~~~~
main.cpp:24:9: error: ‘glfwPollEvents’ was not declared in this scope
24 | glfwPollEvents();
| ^~~~~~~~~~~~~~
main.cpp:26:13: error: ‘glfwWindowShouldClose’ was not declared in this scope
26 | while (!glfwWindowShouldClose);
| ^~~~~~~~~~~~~~~~~~~~~
main.cpp:28:5: error: ‘glfwDestroyWindow’ was not declared in this scope
28 | glfwDestroyWindow(window);
| ^~~~~~~~~~~~~~~~~
main.cpp:29:5: error: ‘glfwTerminate’ was not declared in this scope
29 | glfwTerminate();
| ^~~~~~~~~~~~~
For your information, I indeed DID install GLFW and... I couldn't really figure out how to install GLAD, so I didn't really do much about that.
Please help thank you!!

Related

BadRequest in SDL2 with WSL2

With this minimal example I have a BadRequest error:
#include <SDL.h>
int main( int argc, char* args[] )
{
if(SDL_Init(SDL_INIT_VIDEO) < 0) abort();
SDL_Window *window = SDL_CreateWindow(
"An SDL2 window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);
if(window == NULL) abort();
SDL_Renderer* renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if(renderer == NULL) abort();
SDL_Delay(3000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
The error is the following
$ g++ $(sdl2-config --cflags) foo.cpp $(sdl2-config --libs)
$ ./a.out
X Error of failed request: BadRequest (invalid request code or no such operation)
Major opcode of failed request: 149 (GLX)
Minor opcode of failed request: 16 (X_GLXVendorPrivate)
Serial number of failed request: 197
Current serial number in output stream: 198
It seems the issue appears at the SDL_CreateRenderer function. Creating the window works which confirms my X server works too.

Can't Start OpenGL Window

I've got all the libraries properly installed as far as I can tell, but for some reason, glfwWindowCreate winds up returning NULL. I'm on a Dell XPS 15 at the moment, so I'm wondering if this has to do with the fact that I'm probably running this on the integrated graphics since it's not demanding enough for it to spin up the 1050ti. I'm brand new to OpenGL in general so I'm not certain that my code is properly written, so I'll post it here as well:
glewExperimental = true;
if (!glewInit())
{
fprintf(stderr, "Failed to initialize GLEW!\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(1920, 1080, "Test Window", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Failed to initialize the window.");
std::cin.ignore();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW!");
return -1;
}
std::cin.ignore();
std::cin.ignore();
I've just updated my NVIDIA drivers to the latest update, so it's (probably) not that I hope. Unfortunately, I just can't seem to get it to open a window.
You missed to initialize the GLFW libraray. GLFW has to be initialized by glfwInit, before it is used.
The GLEW libraray has to be initialized after a valid OpenGL context has been created and become current. See Initializing GLEW.
Change your code somehow like this, to solve your issue:
if ( glfwInit() != GLFW_TRUE ) // intialize GLFW
{
// error handling
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(1920, 1080, "Test Window", NULL, NULL);
if (window == NULL)
{
// error handling
}
glfwMakeContextCurrent(window);
// now the OpenGL context is valid and current
glewExperimental = true;
if (glewInit() != GLEW_OK) // initialize GLEW
{
// error handling
}
If on Windows, Optimus-enabled driver would look for an exported variable. That is application have to export it to be accessible to other modules. E.g:
extern "C" {
_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}
Value 1 would mean usage of high performance graphics. 0 or lack of any export would mean use of low profile.
Now, if You're on MacOs or Linux or problem might be somewhere else.. MAc doesn't like Core profiles... On linux you might forgot to disable kernel modesetting and default opensource driver.

SDL_RenderSetClipRect works different on different platforms - how detect whether measured from top or bottom?

I use libSDL2 and make use of the Cliprect.
Simplified I have this code:
SDL_Window* window = SDL_CreateWindow(
"foo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1024, 768, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
SDL_Texture* offscreenTexture = SDL_CreateTexture(
renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
1024, 768);
SDL_Rect sdlRect = { 100, 100, 200, 200 };
SDL_RenderSetClipRect(renderer, &sdlRect);
I noticed that SDL_RenderSetClipRect works different on different platforms: On each one the clipping rectangle has a size of 200x200.
On some platforms the y = 100 is measured from top of the screen (as I expected) and on some platforms it is measured from bottom.
1024 1024
|---------------------| |---------------------|
| 100 | | |
| ***** | | |
| ***** | | |
| ***** | 768 | |
| | | ***** |
| | | ***** |
| | | ***** |
| | | 100 |
|---------------------| |---------------------|
Why? How can I tell the two cases apart?
It seems to be due to an already fixed and then reopened bug of SDL.
See here for further details.
Trying to sum up the discussion, it could be due to the fact that OpenGL coordinate system has (0,0) at the bottom left and SDL didn't correctly abstract over it, while SDL should give the users the abstraction of a coherent coordinate system with origin set to the top left.
The information in the bug are consistent with what you provide as a response to my question in the comment. I suggest you to follow that bug and update SDL to get the latest from the upstream.

glew not initializing with SDL2

I've been trying to get GLew 1.10 to play nicely with SDL 2.0.3, but GLew won't initialize.
The problem I'm having is that GLew 1.10 requires a function GLEWContext* glewGetContext().
I've tried to use a the same solution used for GLew 1.10 with GLFW3, where you use a struct to handle the window and GLew context, but that method doesn't work with SDL2.
The 2 errors I'm receiving is this which points to glewInit():
C3861: 'glewGetContext': identifier not found
Intellisense: identifier "glewGetContext is undefined
code:
// Create window
_screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
/* Create Context */
_mainContext = SDL_GL_CreateContext(_screen);
/* swap syncronized */
SDL_GL_SetSwapInterval(0);
// Initialize GLew 1.10
glewExperimental = GL_TRUE;
GLenum glewError = glewInit(); <------------- error
if (glewError != GLEW_OK)
printf("Error with GLew. SDL Error: %s\n", SDL_GetError());

Troubles porting C++ SDL to Derelict3 in D (D)

I've gotten Derelict3 working under DMD 2.xx but now I'm having trouble porting my SDL code from C++ to D, the following code gives me this error:
C:\Documents and Settings\Kevin Kowalczyk\My Documents\Code\D\Snippets\Dereli
DL>dmd main.d
main.d(14): Error: undefined identifier SDL_SetVideoMode
main.d(14): Error: undefined identifier SDL_HWSURFACE
main.d(14): Error: undefined identifier SDL_DOUBLEBUF
main.d(15): Error: undefined identifier SDL_WM_SetCaption
main.d(21): Error: cannot implicitly convert expression (SDL_Quit) of type ex
n (C) void function() nothrow to uint
main.d(20): Error: non-final switch statement without a default is deprecated
This is the code:
import std.stdio;
import derelict.sdl2.sdl;
pragma(lib, "DerelictSDL2.lib");
pragma(lib, "DerelictUtil.lib");
SDL_Surface Surf_Display;
bool running = true;
void main() {
DerelictSDL2.load();
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Derelict3SDL test", null);
SDL_Event event;
while(running) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_Quit:
running = false;
}
}
}
}
Derelict3 uses SDL2, which doesn't have those functions/flags.
Here's the new API: http://wiki.libsdl.org/moin.cgi/CategoryAPI
I think your new init code should be:
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Derelict3SDL test",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_FULLSCREEN|SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
This is completely and utterly untested. I'm just copying code from the SDL wiki.