glfw openGL c++ window background and title - c++

This is my source code from a series of tutorials I'm taking a look regarding opengl 3+.
//#include <stdio.h>
//#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
using namespace glm;
#include <iostream>
using namespace std;
int main()
{
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window\n" );
glfwTerminate();
return -1;
}
else
{
glfwSetWindowTitle( "Tutorial 01" );
}
// Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwEnable( GLFW_STICKY_KEYS );
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 ) );
return 0;
}
Everything works great except that when the window initializes it has a background color of white and the title 'GLFW WINDOW' and but after 1-2 secs the title changes to Tutorial 01 as it should have been in the first place and the background becomes black, as it should have been also.
In previous studies of opengl (2.x) with glut that I did a couple of years back I didnt have issues like that could someone explain what is wrong here?

I get the same behaviour (even running a release exe outside of the IDE) on an ATI FirePro V5700. If you're really bothered by it, download the GLFW source and change line 764 of carbon_window.c, line 1210 of win32_window.c, and line 962 of x11_window.c.
.\lib\carbon\carbon_window.c
(void)SetWindowTitleWithCFString( _glfwWin.window, CFSTR( "GLFW Window" ) );
.\lib\win32\win32_window.c
_glfwWin.window = CreateWindowEx( _glfwWin.dwExStyle, // Extended style
_GLFW_WNDCLASSNAME, // Class name
"GLFW Window", // Window title
_glfwWin.dwStyle, // Defined window style
wa.left, wa.top, // Window position
fullWidth, // Decorated window width
fullHeight, // Decorated window height
NULL, // No parent window
NULL, // No menu
_glfwLibrary.instance, // Instance
NULL ); // Nothing to WM_CREATE
.\lib\x11\x11_window.c
_glfwPlatformSetWindowTitle( "GLFW Window" );

I'm not experiencing any problems that you describe. I copied and pasted, compiled, then ran your code as you posted it (commenting out references to GLM as I don't have that library installed). The title changes instantaneously for me -- I never even see the window having the title "GLFW WINDOW", and the color of the graphics area is immediately black. Could it be that your computer simply isn't very fast?
What happens if you do the following?
do{
// Draw nothing, see you in tutorial 2 !
glClear(GL_COLOR_BUFFER_BIT);
// Swap buffers
glfwSwapBuffers();
glfwSleep(0.016);
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ) );
Edit: My GPU is an Nvidia GTX 580 (capable of at least OpenGL 4.3).

The background color of the window is configured by the call to the openGL function glClearColor() and refreshed with the glClear() function.
The delay in the changing of the title of the window may perhaps have something to do with the fact that to create a openGL 3.x it is required to create a standard OpenGL context (version 1.x or 2.x) and then get your application to opt-in to using an OpenGL 3.x context. A 1-2 sec delay though seems a lot.

Well as it seems it has to do with the IDE, if you run the actual .exe it works as intended and with no delay at all.

Related

How to check OpenGL and GLSL Version without creating a window?

I tried with following methods -
1. using glew
2. using glut
both almost similar ways as follows -
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int agrc, char **argv)
{
//do windowing related stuff here
if ( !glfwInit())
{
printf("Error: Failed to initialize GLFW\n");
return -1;
}
GLFWwindow* window = glfwCreateWindow(800, 600, "Triangle", NULL, NULL);
if (window == NULL)
{
printf("Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("Error: Failed to initialize GLEW\n");
return -1;
}
printf("GL version: %s\n", glGetString(GL_VERSION));
printf("GL shading language version: %s\n",
glGetString(GL_SHADING_LANGUAGE_VERSION));
}
Question - Is it possible to check GL and GLSL Version with creating native window?
As per my understanding it is necessary to create GL context which is usually done by creating a window, Please tell me alternative without creating window.
According to the OpenGL Wiki FAQ (emphasis mine).
You must create a GL context in order for your GL function calls to make sense. You can't just write a minimal program such as this:
int main(int argc, char **argv)
{
char *GL_version=(char *)glGetString(GL_VERSION);
char *GL_vendor=(char *)glGetString(GL_VENDOR);
char *GL_renderer=(char *)glGetString(GL_RENDERER);
return 0;
}
In the above, the programmer simply wants to get information about this system (without rendering anything) but it simply won't work because no communication has been established with the GL driver. The GL driver also needs to allocate resources with respect to the window such as a backbuffer. Based on the pixelformat you have chosen, there can be a color buffer with some format such as GL_BGRA8. There may or may not be a depth buffer. The depth might contain 24 bits. There might be a 8 bit stencil. There might be an accumulation buffer. Perhaps the pixelformat you have chosen can do multisampling. Up until now, no one has introduced a windowless context.
You must create a window. You must select a pixelformat. You must create a GL context. You must make the GL context current (wglMakeCurrent for Windows and glXMakeCurrent for *nix).
That said, if you're just looking to avoid a temporary window popping up, create a non-visible window, so the end-user doesn't have any idea that you're creating the window. In GLFW, it appears you can do this by setting the window hint GLFW_VISIBLE to false with glfwWindowHint before creating the window. All other windowing systems I've worked with have a similar concept of setting the visibility of a window.

Setting a GLFW window as not resizable

I have a GLFW3 window that I am trying to change from resizable to not resizable.
I tried changing the Window Hint after the window was created but this doesn't do anything as the hints only affect window to be created.
what I tried:
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE)
Is this possible? One way of doing it that I thought of was having a onResize function that changes the window size back to the current size after being set not resizable. This seems very hacky.
Your approach works as of GLFW 3.2.1-1 in Ubuntu 18.10:
main.cpp
#include <GLFW/glfw3.h>
int main(void) {
GLFWwindow* window;
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(640, 480, __FILE__, NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Compile and run:
g++ -std=c++11 -Wall -Wextra -pedantic-errors -o main.out main.cpp -lglfw
./main.out
As I hover the borders of the created window, the cursor never changes to resize mode.
GLFW currently has no API for changing that state after the window is created.
When you want to use GLFW, I see two options:
The workaround you already have.
Create a new window when you switch that state.
Use the GLFW native access to obtain the real window handles and implement the feature for each platform (you care about).
All variants don't seem too appealing to me. Option 2 is especially bad because of the way GL contexts are tied to windows in GLFW, it should be possible by using an extra (invisible) window and shared GL contexts, but it will be ugly.
Option 3 has the advantage that it should work flawlessly once it is implemented for all relevant platforms. As GLFW is open source, this enables also option 3b): implement this directly in GLFW and extend the API. You might even be able to get this integrated into the official GLFW version.
You can change the GLFW_RESIZABLE attribute of an existing window with the following code:
bool enable;
glfwSetWindowAttrib(window, GLFW_RESIZABLE, enable);
This works but I highly recommend the other solutions, as this is only if you strictly need to be able to toggle it.
IntBuffer wid = BufferUtils.createIntBuffer(1);
IntBuffer hei = BufferUtils.createIntBuffer(1);
glfwGetWindowSize(window, wid, hei);
int windowWidth = wid.get();
int windowHeight = hei.get(); // I recommend making this public
while(!glfwWindowShouldClose(window)) {
glfwSetWindowSize(window, windowWidth, windowHeight);
// People can still maximize the window ... Comment if you have a solution :)
}
My solution:
// before create:
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
// create window
// ...
// after create
void setResizable(arg) {
if(arg)
glfwSetWindowSizeLimits(window, 0, 0, 0xffff, 0xffff);
else {
int w, h;
glfwGetWindowSize(window, &w, &h);
glfwSetWindowSizeLimits(window, w, h, w, h);
}
}
This worked to me with GLFW 3.3 but be carefull to put it after glfwInit()
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -1;
}
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
...
}

OpenGL with SDL2 doesn't return the right version

I have an SDL2 application in which I want to create an OpenGL 3.2 context. I googled a bit and started following this tutorial: http://open.gl/context#SDL
Everything seems to work except for the last step. When I had to implement this piece of code:
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
printf("%u\n", vertexBuffer);
My application doesn't seem to have a reference to the functor that should be there. I know there are some people here who had the same problem but I didn't find a solution there. When I output the GL_VERSION it says it's 1.1.0 although I say it should be 3.2.0. Here's my code:
// START SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
logSDLError(std::cout, "SDL_Init");
return 1;
}
// SETUP OPENGL SETTINGS
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// OPENING WINDOW
m_pWindow = SDL_CreateWindow("SDL/OpenGL Game Client - With Networking Library", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
if (m_pWindow == nullptr)
{
logSDLError(std::cout, "CreateWindow");
return 2;
}
// CREATE AN OPENGL CONTEXT ASSOCIATED WITH THE WINDOW.
m_GlContext = SDL_GL_CreateContext(m_pWindow);
if( m_GlContext == NULL )
{
printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() );
}
//Initialize GLEW
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if( glewError != GLEW_OK )
{
printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) );
}
printf((char*)glGetString(GL_VERSION));
I have a FirePro graphics card that should be able to run OpenGL 4.0. I checked my driver updates and everything should be fine + I get no compile warnings saying that something might be wrong with OpenGL or Glew or SDL.
One thing I had to do to make glGetString() working was to include GL\freeglut.h. I don't really know why that is because it doesn't say so in the tutorial I followed.
I found the problem. I forgot to mention that I'm using MSTSC to remote from my desktop to my laptop (which is easier to work) and apparently this changes the graphics device.
I tried opening my application from the actual laptop and suddenly it worked, giving the right OpenGL version and everything.
Stupid mistake, I should've found this earlier.

Fatal error: GL/glfw.h: no such file or directory exsists

I installed GLFW on MinGW and followed the tutorial on the GLFW website on how to write code with Opengl (http://www.glfw.org/GLFWUsersGuide277.pdf). Then when I tried to compile the code MinGW reply's with (Fatal error:gl\glfw.h: No such file or directory exists compilation terminated).
Here's the website's code that I compiled
#include <GL/glfw.h>
#include <stdlib.h>
int main(void)
{
int running = GL_TRUE;
// Initialize GLFW
if( !glfwInit() )
{
exit( EXIT_FAILURE );
}
// Open an OpenGL window
if( !glfwOpenWindow( 300,300, 0,0,0,0,0,0, GLFW_WINDOW ) )
{
glfwTerminate();
exit( EXIT_FAILURE );
}
// Main loop
while( running )
{
// OpenGL rendering goes here...
glClear( GL_COLOR_BUFFER_BIT );
// Swap front and back rendering buffers
glfwSwapBuffers();
// Check if ESC key was pressed or window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) &&
glfwGetWindowParam( GLFW_OPENED );
}
// Close window and terminate GLFW
glfwTerminate();
// Exit program
exit( EXIT_SUCCESS );
}
I don't know how to do it on MinGW.
But for those, who found this question by searching for the same error on debian systems, you need the portable library for OpenGL, window and input (development files):
apt-get install libglfw-dev
I installed GLFW on MinGW
Obviously something in your installation went wrong. How exactly did you install GLFW? What were the exact steps you took starting with the download?

C++ Semi-Transparent Window SDL

I wish to have a semi-transparent SDL background (nothing to do with sub-surfaces or images), such that instead of having a black background it is actually transparent, but the other things I draw are not. My current code is a slightly modified copy of Code::Blocks' SDL project, similar to how various applications have rounded borders or odd shapes besides rectangles.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
int main ( int argc, char** argv )
{
putenv("SDL_VIDEO_WINDOW_POS");
putenv("SDL_VIDEO_CENTERED=1");
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_NOFRAME);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
I think what you're trying to do is in fact a shaped window (parts of the window are transparent depending on a mask that you provide). It seems there's no way to do that with SDL 1.2, however there is a SDL_SetWindowShape function just for this in SDL 1.3 for which you can find a pre-release snapshot here but it's not even in beta yet so I suggest waiting until it's officialy released :)
this is a link to a pretty neat article about development of an older application for Mac OS 9, which did not have support for shaped windows, either.
It's actually a neat article in general about software development.
But the idea seems pretty smart, and I wonder if you might be able to get it working here, too. Instead of trying to make a transparent background, they actually take a screen-shot of the computer right where their window is going to go, and then use that screen shot for their background. When the user drags the window around on the screen, they continue to update the background with new screen-shots. I think this might be more complicated than you were hoping for, but it's certainly an interesting idea.