I've installed WSL2 on Windows 10 along with GLFW. When I try to run the default GLFW example program, I run into the following error X11: The DISPLAY environment variable is missing. I build the program using this command-line:
g++ main.cpp -lglfw -lGL -o main
Program:
#include <stdio.h>
#include <iostream>
#include <GLFW/glfw3.h>
static void glfwError(int /*id*/, const char* description)
{
std::cerr << description << '\n';
exit(0);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(&glfwError);
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
How can I fix this issue?
Related
I'm trying to run the example GLFW program on WSL2 with vcxsrv as the X server. There are no problems running the current program with the two lines commented out. When I uncomment only glCear(CL_COLOR_BUFFER_BIT);, it lags a bit when I drag the window around. When I uncomment only glfwSwapBuffers(window) the program crashes as soon as the window pops up, where dragging the window around becomes very laggy, and I am unable to close the window without terminating the app through the terminal. Why is this happening? Is there any way to improve the speed of these programs on WSL2?
Program:
#include <iostream>
#include <GLFW/glfw3.h>
static void glfwError(int /*id*/, const char* description)
{
std::cerr << description << '\n';
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(&glfwError);
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
// glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
// glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I'm starting to learn openGL. I prefer to use codeblocks but the setup for GLFW3 is weird in codeblocks. I copied the example code from the documentation of glfw which was for visual studio. But in code blocks I needed to add an extra line #define GLFW_DLL to make it work?
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
so why do I need to add an extra #define line in codeblocks and not in visual studio to make it work?
I followed this instruction
GLFW documentation example code here
I was setting up opengl using GLFW + GLAD or GLEW (#include glad/glad.h #include <GL/glew.h>).
After the function glfwMakeContextCurrent(window); I need to check if GLAD or GLEW was initialized correctly or I will run into exception errors in Visual Studio 2019.
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int main(void) {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Check if GLEW is ok after valid context */
/*if (glewInit() != GLEW_OK) {
cout << "Failed to initialize GLEW" << endl;
}*/
/* or Check if GLAD is ok after valid context */
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
...
}
glfwTerminate();
return 0;
}
Here I am using GLAD. I really can't understand why I have to check this. Thanks in advance.
Here I am using GLAD. I really can't understand why I have to check this
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
The key point here is not the check, but the call to gladLoadGLLoader which actually initializes GLAD and loads all the GL function pointers. So what you do here is initializing GLAD, and just checking the return value of that function which tells you if GLAD was able to correctly initialize.
So if you don't want the "check", the correct code would be just
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
Ommiting the check is of course not a good idea, because it means that you might attempt to call GL function pointers which might not be loaded, which will most likely crash your application - the same errors you will see when not even attempting to intialize it, as you already did.
I'm starting to learn the OpenGL stuff, but unfortunately, I can't make the initialization right.
I've added the glfw and glew libraries and these functions give me a strange error,
how can I make it work?
The code:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
std::cout << "GLFW initialization failed.\n";
return -1;
}
if (glewInit()!=GLEW_OK)
{
std::cout << "GLEW initialization failed.\n";
return -1;
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "Wiondow failed.\n";
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
The errors:
To link the GLEW library correctly, proper preprocessor definitions have to be set. See GLEW - Installation:
[...] On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll [...]
I am trying to get the GLFW3 working on my workstation. I did install GLFW3 on my Ubuntu 12.04 from http://www.glfw.org/download.html
Here is the program I am trying. Got it from GLFW official site
http://www.glfw.org/documentation.html
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
std::cout<< "xxx\n";
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
std::cout<< "yyy\n";
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Followed this stackoverflow question How to build & install GLFW 3 and use it in a Linux project
compiled it as -
g++ -I/usr/local/include glfw.cpp -L/usr/local/lib -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lrt
It could successfully compile. However when I launch the executable, I it prints
yyy
Which means the window was not created. What could have been wrong, how can I fix it?