how to use GLFW with openGL? - c++

I'm trying to compile a short openGl and GLFW code but I'm failing in..
I've included all the .h like this :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
I'm on linux so I've made a :
sudo zypper install libglfw2
When I'm compiling like this :
g++ main.cpp -lGLU
I have :
main.cpp:14:22: fatal error: GL/glfw2.h:Any file or directory
#include <GL/glfw2.h>
^
compilation terminated.
I've also tried to include #include <GL/glfw3.h> but without success.
This is my complet code :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
int main()
{
if (glfwInit() == false)
{
fprintf(stderr, "GLFW failed to initialise.\n");
return (-1);
}
glfwWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(640, 480, "OPENGL", NULL, NULL);
if (!window)
{
fprintf(stderr, "Window failed to create\n");
glfwTerminate();
return (-1);
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return (-1);
}
return (0);
}
Could someone know what I have to do to compile my code ?
Thanks

There is no GL/glfw2.h and never has been, and the code above uses the GLFW 3 API. See Renamed library and header file for more information about header paths.
There is no reason to use GLFW 2 for new projects, so I would avoid the libglfw2 package. Instead see if there is a GLFW 3 package.

Related

multiple definition of `main' working with SDL_main

Basically im trying to create a game with SDL2 and Opengl, but I cant get passed this error. I've tried changing SDL_main(int argc, char *argv[]) to regular main but nothing changes. I know this error is due to the SDL_main library but I don't know how to fix it. I've been searching info for some hours now but can't figure it out, the only thing related to this problem I've found is this post, which didn't fix my problem.
Common.h
#ifndef _COMMON_H
#define _COMMON_H
/*=======STANDARD TEMPLATE LIBRARY=======*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <memory>
#include <ctime>
#include <vector>
using namespace std;
/*=======GLU/GLEW=======*/
#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>
/*=======SDL2=======*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
/*=======CLASS DEFINITIONS=======*/
#endif
Main.cpp
#include "common.h"
bool isRunning = true;
int SDL_main(int argc, char *argv[])
{
//Error Checking/Initialisation
if (SDL_Init(SDL_INIT_NOPARACHUTE & SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return -1;
}
else
{
//Specify OpenGL Version (4.2)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Log("SDL Initialised");
}
//Create Window Instance
SDL_Window* window = SDL_CreateWindow(
"Game Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_OPENGL
);
//Check that the window was succesfully created
if(window == NULL)
{
//Print error, if null
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
else
SDL_Log("Window Successful Generated");
//Map OpenGL Context to Window
SDL_GLContext glContext = SDL_GL_CreateContext(window);
//Render
//Swap Render Buffers
SDL_GL_SwapWindow(window);
//Free up resources
SDL_GL_DeleteContext(glContext);
SDL_Quit();
return 0;
}
Error output
./src/lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main]+0x0): multiple definition of `main'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0): first defined here
Makefile
all:
g++ -I ./src/include -L ./src/lib -o ./Builds/Win_Build/engine main.cpp -lmingw32 -lSDL2main -lSDL2 -lmingw32 -lopengl32 -lglew32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf --verbose
Lib folder
EDIT:
I fixed it by redownloading the libraries and using the .a extension ones. And also I changed the version of Glew to 64 bits. My new lib folder:

Failed to create GLFW window?

I tried to create a window with the simplest code possible:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h> // Always include it before glfw.h
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
int main(void) {
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // Mac
GLFWwindow* window = glfwCreateWindow(720, 480, "OpenGL", NULL, NULL); // Create a window
if (window == NULL) {
fprintf(stderr, "Failed to create window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
return 0;
}
This is my Makefile:
game:
g++ src/main.cpp -std=c++17 -o play -I include -L lib -l glfw.3 -l GLEW.2.2
When I compile my code there is no error, but when I try to play my code I have this error:
Failed to create window
Invalid window hint 0x00000003
Invalid window hint 0x00000003
Context profiles are only defined for OpenGL version 3.2 and above
Can someone help me? I don't know why my window doesn't want to be created...
GLFW_VERSION_MAJOR & GLFW_VERSION_MINOR are not valid arguments to glfwWindowHint:
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
^^^^^^^^^^^^^^^^^^ nope
glfwWindowHint(GLFW_VERSION_MINOR, 3);
^^^^^^^^^^^^^^^^^^ also nope
Use GLFW_CONTEXT_VERSION_MAJOR & GLFW_CONTEXT_VERSION_MINOR instead.
As per the docs, emphasis mine:
GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR specify the client API version that the created context must be compatible with. The exact behavior of these hints depend on the requested client API.
Note: Do not confuse these hints with GLFW_VERSION_MAJOR and GLFW_VERSION_MINOR, which provide the API version of the GLFW header.

I am struggling to set up OpenGL and GLAD

I am reading a book called learn OpenGL and I am struggling with the set up. I am using code blocks. What I have done so far is:
I downloaded the 32 bit binaries for GLFW from this website: http://www.glfw.org/download.html
I also downloaded GLAD from this website:
http://glad.dav1d.de/
I then added all the files from these downloads to an include folder. After this I added glad.c and glad.h to my project. Then in codeblocks I clicked the project tab then went to build options - search directories - compiler and added the 2 include folders that I got from the downloads and the src folder. Then in search directories I went to linker and added lib-mingw.
The book I am reading told me to add this code:
#include <GLFW/glfw3.h>
#include <iostream>
int main()
{
//setting the version of openGL
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//creating a window
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window = NULL)
{
std::cout <<"Failed to create GLFW window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
return 0;
}
My problem is I keep getting the error: 'GLADloadproc' was not declared in this scope and 'GLADLoadGLLoader' was not declared in this scope. Also I have tried putting the glad folder into my project file directory so that the #include <glad/glad.h> can be found. What could be the problem?

Seemingly Random LNK2005 Errors With GLFW3

I'm getting the error LNK2005 for seemingly no reason, or at least none that can I can identify. Essentially, using the following code I can compile without issue.
test.h
#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
namespace test
{
//Objects and variables
GLFWwindow* window;
//Function prototypes
bool Initialize();
}
test.cpp
#include "test.h"
#include <iostream>
bool test::Initialize()
{
std::cout << "Initializing GLFW: OpenGL version 3.3 \n";
//Initialize GLFW
glfwInit();
//Set window properties (version 3.3, core profile, not resizeable)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//Create Window
window = glfwCreateWindow(800, 800, "Learn OpenGL", nullptr, nullptr);
if (window = nullptr)
{
std::cout << "Failed to create GLFW window \n";
glfwTerminate();
return false;
}
return true;
}
int main()
{
test::Initialize();
return 0;
}
However, when compiling nearly the exact same thing (http://pastebin.com/VpPep9pM), along with some other code, it gives the errors:
Error LNK2005 "struct GLFWwindow * window" (?window##3PAUGLFWwindow##A) already defined in Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj
Error LNK2005 "struct GLFWwindow * System::window" (?window#System##3PAUGLFWwindow##A) already defined in Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj
Error LNK1169 one or more multiply defined symbols found OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\Debug\OpenGL.exe
So, I would like to know what causes the errors, I'm assuming it has something to do with the "context".
In your header file, you are defining you variable, when it needs to be declared in the header and defined in one source file.
In test.h
namespace test {
extern GLFWwindow* window;
}
and in main.cpp
namespace test {
GLFWwindow* window;
}
Without the extern in the header, you create one instance of test::window in every source file that includes it, which is a problem if there are two or more definitions.

Why does glGetString(GL_VERSION) return null / zero instead of the OpenGL version?

I'm on Linux Mint 13 XFCE. My problem is that when I run in terminal the command:
glxinfo | grep "OpenGL version"
I get the following output:
OpenGL version string: 3.3.0 NVIDIA 295.40
But when I run the glGetString(GL_VERSION) in my application then the result is null. Why doesn't this code get the gl_version?
#include <stdio.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>
int main(int argc, char **argv) {
glutInit(&argc, argv);
glewInit();
printf("OpenGL version supported by this platform (%s): \n",
glGetString(GL_VERSION));
}
glutInit() doesn't create a GL context or make one current. You need a current GL context for glewInit() and glGetString() to work.
Try this:
#include <GL/glew.h>
#include <GL/glut.h>
#include <cstdio>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("GLUT");
glewInit();
printf("OpenGL version supported by this platform (%s): \n", glGetString(GL_VERSION));
}
You can also use glfw in order to create GL context and then query the version:
Include this files:
#include "GL/glew.h"
#include "GLFW/glfw3.h"
And then you can do:
// Initialise GLFW
glewExperimental = true; // Needed for core profile
if (!glfwInit())
{
return "";
}
// We are rendering off-screen, but a window is still needed for the context
// creation. There are hints that this is no longer needed in GL 3.3, but that
// windows still wants it. So just in case.
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); //dont show the window
// Open a window and create its OpenGL context
GLFWwindow* window;
window = glfwCreateWindow(100, 100, "Dummy window", NULL, NULL);
if (window == NULL) {
return "";
}
glfwMakeContextCurrent(window); // Initialize GLEW
if (glewInit() != GLEW_OK)
{
return "";
}
std::string versionString = std::string((const char*)glGetString(GL_VERSION));