opengl initialization problem with glew and glfw - c++

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 [...]

Related

Visual studio is unable to start program when trying to render something on the glfw window with glfw(C++)

When i'm rendering nothing on the glfw window everything works fine but when I try to add something like glClear(GL_COLOR_BUFFER_BIT) or legacy OpenGL visual studio gives me an error :
"Unable to start prgram {path to the .exe file} file specified is not found".
Here is my code:
#include <iostream>
#include <GLFW/glfw3.h>
int main()
{
GLFWwindow* window;
if (!glwfInit())
return -1
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
So as you see, there is this glClear(params) but when there isnt glClear(params) i do not get any errors and the window is rendered to the screen but there is glClear(params) i get the error "Unable to start program..."
"file specified is not found" shows that compilation failed and no executable was built. Try a rebuild and check the list of compilation errors.

GLFW WSl2: Display Environment Variable missing

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?

glfw program crashing in WSL2

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;
}

Why do I need to use #define in code blocks but not on visual studio? (GLFW)

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

Why is needed to check if GLAD or GLEW was initialized correctly?

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.