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
Related
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)
I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a
I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.
The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.
Please see these screenshots: http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD
C++.cpp
#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"
int main(int argv, char** args){
display x(800,600,"something");
while(!x.isClosed()){
glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
x.Update();
}
return 0;
}
display.cpp
#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit(); //NO ERRORS OCCUR
if(status != GLEW_OK){
std::cerr << "glew failed to initialize" << std::endl;
}
m_isClosed = false;
}
display::~display(){
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool display::isClosed(){
return m_isClosed;
}
void display::Update(){
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
m_isClosed = true;
}
}
}
display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/
class display{
public:
display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~display();
private:
display(const display& other){}
display& operator=(const display& other){}
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
#endif /* DISPLAY_H_ */
To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).
Create OpenGL context and window
A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).
Initilize SDL
If you are using SDL you have to create the window and you have to create the OpenGL context.
SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
SDL_GLContext glContext = SDL_GL_CreateContext( window );
Note, you should check for errors with SDL_GetError.
The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.
SDL_GL_MakeCurrent( window, glContext );
Initilize GLUT
To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.
glutInit(&argc, argv);
glutCreateWindow("OGL window");
Initilize GLFW
Note, glfwInit returns GLFW_TRUE if succeded:
if ( glfwInit() != GLFW_TRUE )
return;
GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
return;
}
glfwMakeContextCurrent( wnd );
After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.
Set up GLEW
Note that glewInit returns GLEW_OK if succeded:
if ( glewInit() != GLEW_OK )
return;
To link the GLEW library correctly you have to set up proper preprocessor definitions:
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
See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
and finnally:
gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)
Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test
Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.
Ok, so I compiled SDL/C++ on terminal easily by just doing:
g++ main.cpp -o main.out -pthread -std=c++11 `sdl2-config --cflags --libs` .
And then this is my main.cpp:
// #include <SDL/SDL.h>
#include "SDL2.0.4/SDL2-2.0.4/include/SDL.h"
int main(int argc, char** argv)
{
SDL_Window *__window;
//Init SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Creation Window
__window = SDL_CreateWindow(
"Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
430,
SDL_SWSURFACE
);
while(true){
}
return 0;
}
Well when I do ./main.out to run it, it just stops iterating over while loop. There is no window popping up. I am using Ubuntu Linux. And as I said I want to run and compile sdl on terminal, I'm pretty sure I passed the compiling stage, it's the running that's irritating me.
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"
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?