Unusual output when trying to Create a blank window in open GL - c++

I am trying to create a Blank Window in OpenGL with help of GLFW. below is my code
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
glfwInit();
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);
window = glfwCreateWindow(800,600,"learnopengl",NULL,NULL);
if (window == NULL)
{
fprintf(stderr,"there is a problem with window creation\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
fprintf(stderr,"Failed to initialize GLEW\n");
return -1;
}
int width, height;
glfwGetFramebufferSize(window,&width,&height);
glViewport(0,0,width,height);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
}
but when i try to run the above code instead of a black blank it shows an instance of my current screen in newly created window.

Why do you expect this code to result in a blank window?
As per the spec, the back buffer contents become undefined after you swap the buffers (and initially, they are of course undefined too). As a result, the output you should get is also undefined and basically anything might show up.
Add a glClear(GL_COLOR_BUFFER_BIT) to your render loop if you want some defined output.

I know the answer is late but may concern other people and help them, I ran this code and it's working pretty well, but the problem is not in your code, it is your video card driver, so what is happening ?
OpenGL has what is called "The Default Framebuffer", it is the Framebuffer (the buffer contains what you will see) that OpenGL is created with. It is created along with the OpenGL Context. Like Framebuffer Objects, the default framebuffer is a series of images. Unlike FBOs, one of these images usually represents what you actually see on some part of your screen. in other words the default framebuffer is the buffer used by the operating system to render your desktop and other application's windows, so your application is using the default framebuffer as the framebuffer is not edited by your app, so you may need to clear the framebuffer with some other values, let's say colors. this is your code edited with buffer clearing using colors.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
glfwInit();
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);
window = glfwCreateWindow(800,600,"learnopengl",NULL,NULL);
if (window == NULL)
{
fprintf(stderr,"there is a problem with window creation\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
fprintf(stderr,"Failed to initialize GLEW\n");
return -1;
}
int width, height;
glfwGetFramebufferSize(window,&width,&height);
glViewport(0,0,width,height);
//this is an added line
glClearColor(1, 1, 1, 1); //the RGBA color we will clear with
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
//this is an added line
glClear(GL_COLOR_BUFFER_BIT); //the buffer used to clear with
glfwSwapBuffers(window);
}
glfwTerminate();
}
Hope this answer and this code help other people who may have this situation.
for further information about the framebuffer check this link : https://learnopengl.com/Advanced-OpenGL/Framebuffers

Related

glfwWindowShouldClose(window); generate exeption, why?

Im starting with OpenGL and C++ but the first test code give me an error that I do not understend why or how to fix it. Im with some trobles to finally intall the libraries glfw3.h and glad.h, everithing works normal until I tap "glfwWindowShouldClose(window);"
enter image description here
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(640, 480, "teste", NULL, NULL);
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Im trying this tutorial, if someone ask: https://www.youtube.com/watch?v=45MIykWJ-C4&t=260s&ab_channel=freeCodeCamp.org
You appear to have a typo and be setting the major version twice and not setting the minor version so creating the window is likely failing.

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.

I cannot access some openGL functions

When I attempt to generate a buffer by executing the glGenBuffer() function - no function like that is found.
Some functions are still working, and from what I see most do work, for instance the following code works perfectly:
#include <iostream>
#include <GLFW/glfw3.h>
using namespace std;
class Window_Manager
{
public:
GLFWwindow* window;
int Create_Window(int width, int height, const char* title) {
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_FOCUSED, GL_TRUE);
window = glfwCreateWindow(width, height, title, NULL, NULL);
if (window == NULL) {
cout << "Failed to create a window! Aborting early..." << endl;
Terminate_Window(window);
return -1;
}
glfwMakeContextCurrent(window);
glViewport(0, 0, width, height);
return 1;
}
void Terminate_Window(GLFWwindow* window) {
glfwSetWindowShouldClose(window, GL_TRUE);
glfwTerminate();
}
};
but this code does not:
#include <GLFW/glfw3.h>
#include <Engine_Manager.h>
#include <iostream>
using namespace std;
class Shape2D
{
int VBO;
int Setting_Up(){
VBO = glGenBuffer();
return 1;
}
};
In addition to replacing glGenBuffer with glGenBuffers(1, &VBO) as rpress mentioned in the comments, OpenGL is a weird library in that you have to load most of the functions dynamically.
The exact details differ from platform to platform. On Windows, for example, you can use wglGetProcAddress to get pointers to desired functions.
Rather than do it manually, GLEW is an excellent library for getting OpenGL function pointers easily. Other options are documented on the OpenGL wiki.

c++ VS17 - GL window blank and title is solution name

I'm following a fairly simple tutorial and all the files compile, and I am using openGL, glew, and glfw in my current code. The window is not displaying correctly and I don't think it's even displaying. I have my main.cpp:
#include "stdafx.h"
#include "Libs.h"
int main(int argc, char **argv) {
glfwInit();
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
int FRAME_BUFFER_WIDTH = 0;
int FRAME_BUFFER_HEIGHT = 0;
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Custom Name", NULL, NULL);
glfwGetFramebufferSize(window, &FRAME_BUFFER_WIDTH, &FRAME_BUFFER_HEIGHT);
glViewport(0, 0, FRAME_BUFFER_WIDTH, FRAME_BUFFER_HEIGHT);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glfwInit() != GLEW_OK) {
std::cout << "GLFW INIT != GL_OK \n";
glfwTerminate();
}
//Main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glfwSwapBuffers(window);
glFlush();
}
glfwTerminate();
return 1;
}
and my Libs.h
#pragma once
#include <iostream>
#include <glew.h>
#include <glfw3.h>
#include <glm.hpp>
#include <vec2.hpp>
#include <vec3.hpp>
#include <vec4.hpp>
#include <mat4x4.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include <SOIL2.h>
The whole thing compiles just fine and opens without any errors. I can't see any of the console text that I'm displaying (std::cout) as I'm running in visual studio.
The problem is when it opens, the window is displayed like this: Window that's displayed
As you can see, the window is completely blank and there is no green (which it should be). Also, the window name is my solution name and not the one that I gave it ("Custom Name"). Lastly, the icon for the window is a package, which I think means that it's not GL but rather Visual Studio making it. I have no idea why this is happening and no errors are occurring.
If anyone has a possible fix that would be great, thanks.
Fixed:
Pretty simple, I made my project with extra files already there. I needed to make it from an empty project, hopefully this can help somebody in the future.

Simple GLFW freezes (thinks like crazy, not stopping)

I've created a very simple implementation of a GLFW window. My implementation looks like so
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include glfw for window handling
#include <GLFW/glfw3.h>
int SCREEN_WIDTH = 1280;
int SCREEN_HEIGHT = 720;
GLFWwindow* window;
int main() {
if(!glfwInit()) {
fprintf( stderr, "Failed to initialize GLFW!\n" );
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Title", NULL, NULL);
if( !window )
{
fprintf( stderr, "Failed to create window!\n" );
glfwTerminate();
return -1;
}
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
do {
glfwSwapBuffers(window);
} while(!glfwGetKey(window, GLFW_KEY_ESCAPE));
glfwTerminate();
return 0;
}
It compiles fine, but when I run the application it just "freezes", not showing the correct clear color or anything. It just thinks like crazy (that "thinking"-cursor icon in Windows 7 spins and never stops).
I'm wondering why it freezes like such, does anyone have an idea?
EDIT:
Found the solution to my problem. I was learning from a GLFW2 example, but having the newest version (GLFW3) required me to redo the code some; the thing I didn't realize was that the glSwapBuffers(window) call doens't call glfwPollEvents() by itself, giving me the problem I had.
You're not clearing the buffers, hence the clear colour is not showing through.
You're not adding any kind of delay, so you're swapping empty buffers flat out ("thinking like crazy").