GLFW fails to initialize on Ubuntu? - c++

Here is the code which I'm trying to run:
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Tutorial 01", 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" );
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
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{
// Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
glClear( GL_COLOR_BUFFER_BIT );
// 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 only shows a black window with the text:
Failed to initialize GLFW
Also, I've already linked GL, GLEW, SDL2 and glfw3 to the build options.

Related

glClear() crashes GLFW when including GLAD

When I started I used the GLFW example code:
#include <GLFW/glfw3.h>
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;
}
When running this it gives me a black screen titled "Hello world" which is exactly what I want. But by simply adding GLAD:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
...and giving window hints:
//Specify the OpenGL versions we're using
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Suddenly it tells me the window failed to initialize.
But by simply adding this:
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
...it allows the window to initialize but crashes when glClear(GL_COLOR_BUFFER_BIT) is called and it gives me the error
'./Voxel\ Game' terminated by signal SIGSEGV (Address boundary error)
I know this because when I remove that line it works, it just doesn't clear the screen.
This is the full code I have now in case the error is somewhere else in there:
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define SCREEN_WIDTH 640
#define SCREEN_HIEGHT 480
int main()
{
GLFWwindow* window;
//Initialize the library
if (!glfwInit())
{
std::cout << "Failed to initalize GLFW" << std::endl;
return -1;
}
//Specify the OpenGL versions we're using
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
//Create a windowed mode window and its OpenGL context
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HIEGHT, "Voxel Game", NULL, NULL);
if (!window)
{
std::cout << "Failed create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
//Make the window's context current
glfwMakeContextCurrent(window);
//Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
//Render here
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Swap front and back buffers
glfwSwapBuffers(window);
//Poll for and process events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
You are not initializing glad, add gladLoadGL(glfwGetProcAddress); after glfwMakeContextCurrent() and check it returns OK.
In general, there is no OpenGL library apart from some old OpenGL1.1 stuff (in Windows at least), all those GL calls are implemented in the graphics drivers directly, GLAD library just defines a lot of function pointers and wraps them in nicer macros. Then during initialization, it will dynamically load the functions from the drivers present on the machine. Hence the need to generate GLAD for specific OpenGL version.
So if you get segfaults on some GL calls, a good guess is some of those functions were not found, maybe because they are not supported on the HW or because you did not setup GLAD/GLFW correctly.

Why would the window created with this OpenGL context open as transparent?

Upon execution, the background should be rendered as a darker-blue, but I have clearly missed something. The window is instead rendered with a background that is identical to the image immediately behind it (e.g. other open windows or the desktop, etc.). I cannot identify the problem.
Currently I am not using any -std during compilation and I am utilizing the following linkages with the output executable:
-lGL -lGLU -lGLEW -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXcursor -lXinerama
Here are the contents of my .cpp file:
#include <stdio.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
//Initialize GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failes to intialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a Window and create it's OpenGL context
window = glfwCreateWindow( 1024, 768, "playground", NULL, NULL);
if( window == NULL) {
fprintf( stderr, "Failed to open GLFW window.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
glfwTerminate();
return 0;
}
I am an OpenGL newcomer. Feel free to remark on the over-all structure or if anything should/must be rewritten entirely. Thanks!
You just forgot to clear your window in your render loop:
do{
glClear(GL_COLOR_BUFFER_BIT); // Clear background with clear color.
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
glClear is often one of the first call of every render loop using OpenGL, giving you a new fresh frame to work on.

Can't render triangle in OpenGL C++

I've tried to follow this tutorial for OpenGL in C but when it comes to the second tutorial, the one that is supposed to draw a triangle on the window, I couldn't see anything. So this is what I did, I took the code that creates the OpenGL context, window and stuff and I tried to make it simpler: instead of using VAO I tried glBegin/glEnd.
I get this error: 1282 "invalid operation". I'm just using the same sentences taking directly from my LWJGL project. The main loop is so simple I can't understand how it does not work and the 1282 error is not giving me any information. Why do I still get an error?
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#define GLEW_STATIC
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
using namespace glm;
void checkErrors() {
int error = glGetError();
if (error != GL_NO_ERROR) {
printf("%s (%d)\n", gluErrorString(error), error);
}
}
int main(void)
{
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow(1024, 768, "Tutorial 01", 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
glewExperimental = true; // Needed in core profile
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);
do{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glEnd();
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
// Check for errors
checkErrors();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
return 0;
}
glBegin/glEnd, along with all other immediate mode drawing functions and some more, have been deprecated, and cannot be used with an OpenGL 3.1 (and up) core and forward compatible contexts.
You can try requesting a 3.0 compatibility context (which includes all of the deprecated functionality). To do this, remove the glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); and glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); lines, and change the minor version hint to 0. Indeed, according to the OpenGL wiki, you should not explicitly request a forward compatible context with 3.1 and newer anyway. However, your best bet is to figure out what's wrong with the VAO code instead of mucking around with deprecated functionality.

Can't use certain functions in OpenGL (glGenBuffers)

I have seen similar questions to mine else where but none have answered or fixed my problem.
I have GLEW initiated properly, right after the Context creation but before I call glfwMakeContextCurrent()
After I have initiated I try to use glGenBuffers() but it doesn't work. Throws an error.
My OpenGL version is 3.2 so from what I have read I can use this functionality in my program. Please to let me know otherwise though if I can't. I will try to figure out a different way to do this.
I am using Windows 7 and VS2012, and it seems that everything is linked properly. Hope I provided all the info I needed.
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <thread>
int main(){
//Initialize GLFW
if(!glfwInit()){
printf("GLFW was not initialized successfully!\n");
std::this_thread::sleep_for(std::chrono::seconds(5));
exit(EXIT_FAILURE);
}
else{
printf("GLFW was initialized successfully!\n");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "PONG!", nullptr, nullptr); // Windowed
glewExperimental = GL_TRUE;
if(!glewInit()){
printf("GLEW was not initialized successfully!\n");
std::this_thread::sleep_for(std::chrono::seconds(5));
exit(EXIT_FAILURE);
}
else{
printf("GLEW was initialized successfully!\n");
}
glfwMakeContextCurrent(window);
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
printf("%u\n", vertexbuffer);
fprintf(
stdout,
"INFO: The OpenGL version is: %s\n",
glGetString(GL_VERSION)
);
fprintf(
stdout,
"INFO: The GLEW version is: %s\n",
glewGetString(GLEW_VERSION)
);
while(!glfwWindowShouldClose(window)){
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
glfwDestroyWindow(window);
window = glfwCreateWindow(800, 600, "PONG!", nullptr, nullptr);
}
else if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS){
glfwDestroyWindow(window);
window = glfwCreateWindow(800, 600, "PONG!", glfwGetPrimaryMonitor(), nullptr);
}
}
}
I have GLEW initiated properly, right after the Context creation but before I call glfwMakeContextCurrent()
Welp, there's your problem right there:
Successful creation does not change which context is current. Before you can use the newly created context, you need to make it current using glfwMakeContextCurrent.
Call glewInit() after glfwMakeContextCurrent().

glewInit() Failed, OpenGL App

I'm trying to build an OpenGL App with glew/glfw. I've downloaded the binaries, placed them in the root of my folder, added the paths to the include and lib directories and told my project to require glew32.lib, GLFW.lib and opengl32.lib.
I even copied glew32.lib to the root directory because my project couldn't see it.
I must keep all the dependencies in the project directory since I will be distributing this. I'm at a loss.
Now when I run my program, it fails at glewInit()
This is my implementation so far:
#include "Common.h"
GameEngine::GameEngine()
{
InitWithSize(1024, 768);
InitInput();
}
void GameEngine::InitWithSize(int _width, int _height)
{
try {
// Initialise GLFW
if( !glfwInit() )
throw std::exception("Failed to initialize GLFW\n");
//glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
if( !glfwOpenWindow( _width, _height, 0,0,0,0, 32,0, GLFW_WINDOW ) )
throw std::exception("Failed to initialize GLFW\n");
glfwSetWindowTitle( "Tutorial 01" );
// Initialize GLEW
if (glewInit() != GLEW_OK)
throw std::exception("Failed to initialize GLEW\n");
} catch ( std::system_error const& err) {
fprintf(stdout, "System Error: %s", err.what());
glfwTerminate(); // Free glfw if it has been allocated
// Try Again
this->InitWithSize(_width, _height);
} catch( std::exception const& err) {
fprintf(stdout, "Exception Found: %s", err.what());
} catch ( ... ) {
fprintf(stdout,"Unknown Exception Occurred\n");
}
}
void GameEngine::InitInput()
{
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
}
void GameEngine::StartGameLoop()
{
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
}
void GameEngine::InitTestData()
{
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
}
With my Common Header:
#ifndef _COMMON_H
#define _COMMON_H
// OpenGL Libraries
#define GLEW_STATIC
//#pragma comment(lib, "glew32.lib")
#include <GL\glew.h>
#include <GL\glfw.h>
// Core Libraries
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <fstream>
// C++ 11 Libraries
#include <memory>
#include <exception>
#include <thread>
#include <chrono>
// Manager Classes
#include "ThreadManager.h"
#include "GameEngine.h"
#include "ShaderManager.h"
// Lesser Classes
#include "Shader.h"
#endif
I know this answer comes a bit late, but I don't see you making the OpenGL context current by calling glfwMakeContextCurrent(window). You have to do that before calling glewInit()
If no error is returned from glewInit() it returns 0. For some reason the return wasn't comparing well to GLEW_OK but if the value is anything but 0, there's an error.
if(glewInit()) {
//Handle Error
}
You are using Core OpenGL version 3.3 so you must specify you are using "new" and by GLEW terms "experimental" API. Add this line before calling glewInit();
glewExperimental=GL_TRUE;
Here is a complete init code from my engine .It is for 4.2 but should work the same for you:
void Engine::InitWithGLFW(){
if(!glfwInit()){
exit(EXIT_FAILURE);
}
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
//glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
glfwDisable(GLFW_AUTO_POLL_EVENTS);
#ifdef DEBUG
glfwOpenWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
#endif
if(!glfwOpenWindow(_width,_height,8, 8, 8, 8, 24, 8,GLFW_WINDOW)){
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetWindowTitle("XDEngine V-1.0");
InitGlew();
}
void Engine::InitGlew(){
glewExperimental=GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_CLAMP);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glfwSetWindowSizeCallback(Reshape);
}
I had a similar problem and finally got the solution on opengl.org.
It seems that GLEW developers haven't fixed a core context problem yet.
Though there is a quite easy solution:
glewExperimental=TRUE;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
cout<<"glewInit failed, aborting."<<endl;
}
HTH
Compare the glewInit() return type with GLenum
if (glewInit() != GLEW_OK) {
printf("glew initialization failed!");
}