Cannot get GLFW3 Library working - c++

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?

Related

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?

glad.c unable to find glad/glad.h, but main.cpp can

I've been trying to set up a build environment for OpenGL using glfw3 and GLAD. I'm currently using WSL2 Ubuntu with an X Server for compilation and a makefile.
However, when I run my make I receive the following error:
src/glad.c:25:10: fatal error: glad/glad.h: No such file or directory
25 | #include <glad/glad.h>
This is odd to me because it seems that the makefile is able to compile the main.cpp file and create a main.o despite also including "glad/glad.h"
File structure:
-HelloTriangle
--include
---glad
----glad.h
---KHR
----khrplatform.h
--src
---glad.c
---main.cpp
--makefile
This is my make file:
BASE_OBJS = main.o glad.o
SRC_PATH = src
OBJS = $(addprefix $(SRC_PATH)/, $(BASE_OBJS))
CXX = g++
CXXFLAGS = -g -Iinclude
LDFLAGS =
LDLIBS = -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl
HelloTriangle: $(OBJS)
$(CXX) -o $# $(LDFLAGS) $^ $(LDLIBS)
clean:
rm $(OBJS)
This is my main.cpp:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
/*
Function to handle window resizing
*/
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main() {
/*
Initialize GLFW
Sets version to Core profile 3.3
*/
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/*
Initialize a window context for OpenGL
Defines the windows width, height, and title
*/
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Triangle", NULL, NULL);
if(window == NULL) {
std::cout << "Failed to create GLFW window" <<std::endl;
glfwTerminate();
return -1;
}
/*
Initialize GLAD
Handles OS-specific function pointers
*/
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
/*
Handle window resizing
*/
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
/*
Render loop
*/
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
You seem to set the CXXFLAGS (for the C++ compiler), but your glad.c is compiled with the C-compiler (which checks CFLAGS)

Calling glewInit() causes main function to not be called

I'm attempting to use GLFW and GLEW. Currently my code works, and I see a window with triangles:
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <time.h>
#include "constants.h"
int main(void) {
GLFWwindow* window;
if (!glfwInit()) {
glfwTerminate();
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Window", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// if (glewInit() != GLEW_OK) {
// fprintf(stderr, "failure");
// return -1;
// }
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
while (glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0, 0);
glVertex2f(0, 1);
glVertex2f(1, 0);
glEnd();
glfwSwapBuffers(window); /* Swap front and back buffers */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
The above code works. However, when I uncomment the lines:
if (glewInit() != GLEW_OK) {
fprintf(stderr, "failure");
return -1;
}
The program exits silently. I confirmed with a breakpoint that the main function is not even being called. If it helps, here is my compilation command:
g++ -g main.cpp -I\glfw-3.3.2\include -I\glew-2.1.0\include -L\glfw-3.3.2\lib-mingw-w64 -L\glew-2.1.0\lib\Release\x64 -lglfw3 -lgdi32 -lopengl32 -luser32 -lshell32 -lglew32 -o main.exe
I can't figure this out for the life of me. I'm calling glewInit after making the OpenGL window, I'm importing glew before glfw. What am I doing wrong?
Crash before main is either because of static initialization or dynamic libraries.
It seems you are on windows, in that case glew release files contain two libraries glew32.lib and glew32s.lib.
You are linking against the non-static one, try -lglew32s. The first one is just a stub that will search for .dll, usually located in glew/bin folder, you would have to copy it to the working directory with main.exe. Remove GLEW_STATIC if you choose to go this way.
To prevent future errors, add glewExperimental=GL_TRUE; somewhere before glewInit. It enables OpenGL 3.3+ functions on some drivers.
As for why is it working now, the whole reason for existence of extension loading libraries in the first place is that the default drivers include only OpenGL 1.x functionality. All newer functions must be loaded directly at run-time from .dll drivers supplied by your GPU using dllopen() and GetProcAddress() which is exactly what these libraries do for you. That is why most newer functions are actually implemented as macros (wrapping the function pointer). That said, you are not using any new functions (you really should), so everything is working fine.

GLFW window not responding

I am working on an GLFW desktop application that uses openGL ES 2.0 instead of the normal openGL!
The code that i wrote compiles great but when i run the application i hear some weird sound coming from my laptop and after a few seconds the window of the application goes unresponsive when i close the window the sound stops!
Is it a hardware/software problem or i did something wrong?
this my main.cpp:
#ifndef GLFW_INCLUDE_ES2
#define GLFW_INCLUDE_ES2
#endif
#include "game.h"
#include <GLFW/glfw3.h>
int init_gl();
void shutdown_gl();
void set_main_loop();
GLFWwindow* window;
int main()
{
if (init_gl() == GL_TRUE) {
on_surface_created();
on_surface_changed();
set_main_loop();
}
shutdown_gl();
return 0;
}
void shutdown_gl()
{
glfwDestroyWindow(window);
glfwTerminate();
}
int init_gl()
{
const int width = 480,
height = 800;
if (glfwInit() != GL_TRUE) {
return GL_FALSE;
}
window = glfwCreateWindow(width, height, "Simple example", NULL, NULL);
if (!window) {
return GL_FALSE;
}
glfwMakeContextCurrent(window);
return GL_TRUE;
}
void set_main_loop()
{
while (!glfwWindowShouldClose(window))
{
on_draw_frame();
glfwSwapBuffers(window);
}
}
tell me if u need the code from game.cpp!
The code is compiled on Ubuntu 14.04 using g++ with the commands:
g++ -I. -I../common -c main.cpp ../common/game.cpp
g++ main.o game.o -o main.exec -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread
You need to call glfwPollEvents() in your loop, however I'm not 100% sure as the sound could be caused by the application not being vsync-d.
Reference:
http://www.glfw.org/docs/latest/quick.html

GLEW and glfw compile error: undefined reference to symbol 'XConvertSelection'

I'm trying to compile this code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Playground", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
It's a code from a tutorial I found about OpenGL, I program mostly in Java when it comes to OpenGL, but I wanted to try something new so I went to try in C++.
I'm using QtCreator for this project.
At first I included GLEW and glfw3 libraries:
And the same for the glfw library file.
And then, when I try compiling the program I get this error:
In text:
$ /home/sapir/Qt/5.4/gcc_64/bin/qmake -spec linux-g++ CONFIG+=debug -o Makefile ../Test/Test.pro
$ g++ -Wl,-rpath,/home/sapir/Qt/5.4/gcc_64 -o Test main.o -L/home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib/ -lglfw3 -lGLEW -lGLEWmx
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(glx_context.c.o): undefined reference to symbol 'glXQueryExtension'
//usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [Test] Error 1
23:12:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Test (kit: Desktop Qt 5.4.0 GCC 64bit)
When executing step "Make"
23:12:13: Elapsed time: 00:00.
I tired searching for an answer in forums and here, but I couldn't find anything that solved this problem.
Anybody got any Ideas?
After adding
-lXxf86vm -lXrandr -lGL -lGLU -lXi
to the gcc compiler, I get a different error, which contains:
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
This is my make file: http://pastebin.com/xL5Hpwsf
And this is my .pro file: http://pastebin.com/yhkV7nn7
Ok, after some research I found that the DSO error which I got means that the order of the includes I've implemented is incorrect and cause the compilation to fail.
So what I did is I used the command:
pkg-config --static --libs x11 xrandr xi xxf86vm glew glfw3
To get the packages I need for them to run and in the right order.
Then I compiled the project accordingly.
That's it :)
I got same error "undefined reference to symbol 'XConvertSelection'" while compiling example from Irrlicht 3D, solved by adding "-lX11".
Then I got error "undefined reference to symbol 'XF86VidModeGetGamma'", solved by adding "-lXxf86vm"