Constantly loading, white GLFW window

Hi, I’m relatively new to graphics programming, in fact I’m coming from a Java and algorithms background, but I’m using GLFW and GLEW to create one of many university projects. After maybe 3/4 days of Visual Studio 2015 giving me problems like I’ve never dreamed I finally got the program to compile, I can paste the basic code here if you want, although it’s little more than creating a basic window and initialising (initializing to my American friends) both APIs. When the program launches, the application immediately ends, there are no errors and nothing crashes (at least not in the way a normal Windows app does). To combat this, I’ve placed a getchar() statement before the end of main, like I was taught to, although this feels weird to me, and if this is the problem and it’s my fault I’ll delete this post.

The GLFW window, when open once the program hits the getchar(), is white (I can provide pictures if you want), which is weird because online tutorials have it as a light/white border with a black main component (although on mine the border AND the main window are both white), and I cannot interact, click, move or close the window in any way. If I hover over, the cursor changes to the loading icon and obviously I can’t see any graphics on the window at all. What and where have I messed up?

Hi,

Does your program have an event loop at all? It sounds as though glfw is successfully creating the window for you, but then you are doing nothing with it. At minimum you should have a loop something like this:

Edit: Fixed typo

while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
    glfwWaitEvents();
}

The getchar trick just forces MSVC to keep the program running where it would otherwise end, and should have no effect on the rest of the program. Are you calling glfwTerminate anywhere? You should see the glfw window disappear before you get to the getchar.

Thanks for the quick reply, I did program a loop in alongside the tutorial I was following, and it seemed to work fine for the programmer doing it. I’m new to the site, I apologise if this isn’t formatted correctly, I will edit it.

int main()
{
	if (!glfwInit()) {
		//GLFW failed to initialise
		//closing program
		fprintf(stderr, "GLFW Failed.");
		return -1;
	}

	
	glfwWindowHint(GLFW_SAMPLES, 4); //sets 4 anti-aliasing, which is 4 fragments per pixel
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //asking for opengl version 3
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //and then .3, so 3.3
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //so we're using ONLY the core profile functions

	GLFWwindow* window = glfwCreateWindow(640, 480, "My OpenGL", NULL, NULL);
	//creates our window
	//width, height, title, null, null

	if (!window) {
		//window failed
		//closing program
		fprintf(stderr, "Window Failed.");
		glfwTerminate();
		return -1;
	}
	
	glfwMakeContextCurrent(window); //this is the window OpenGL will be applied to
	glewExperimental = true; //sets newest glew for program

	if (glewInit() != GLEW_OK)
	{
		//Glew failed
		fprintf(stderr, "GLEW Failed.");
		glfwTerminate();
		return -1;
	}

	//Generate VAO
	GLuint vaoID; 
	glGenVertexArrays(1, &vaoID);  //create 1 array, referencing the ID for our vertex object, to be sent BACK to us when edited
	glBindVertexArray(vaoID); //generate vertex arrays

	static const GLfloat verts[] = {
		 // X, Y, Z
		-1.0f, -1.0f, +0.0f,
		+1.0f, -1.0f, +0.0f,
		+0.0f, +1.0f, +0.0f
	};

	//Generate VBO
	GLuint vboID;
	glGenBuffers(1, &vboID);
	glBindBuffer(GL_ARRAY_BUFFER, vboID); //create our vertices data buffer for use
	glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

	do {
		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vboID); //paste again for safety
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

		glDrawArrays(GL_TRIANGLES, 0, 3);
		
		glDisableVertexAttribArray(0);
                glfwSwapBuffers(window);
		glfwPollEvents(); //to check for any changes to the data/input
	} while (glfwWindowShouldClose(window) == false);

	return 0;
}

Edit: Thanks to mods for fixing my box.

Obviously I’ve left out the includes and comments at the top of the file, but any alterations that need to be made to this will.

This is all the programming I’ve done so far on this file, so this is all I can give you.

EDIT:
The window has mysteriously turned from white to black, so the window itself may be being used properly now. I switched from 32-bit to 64-bit, including libraries, dlls, etc. The window is still blank though. To make things weirder, I just pasted this into the loop to test:

glColor3f(1.0, 1.0, 1.0);
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();

and still received nothing on the window. The window is likely programmed incorrectly, or I’ve messed up somewhere else

Edit: Forgot the most important thing; you should set a glfw error callback to see if window creation is failing.

Not sure what the error is, but a couple of pointers:

  1. Since you are using OpenGL 3.3 core, you must somewhere be compiling and linking GLSL shaders? If nothing is drawing, this would be the first thing to check.
    Edit: Just saw your edit. The old-style glBegin drawing will not work in core profile, only in compatibility.

  2. You are not clearing the screen anywhere, which could explain why it is sometimes white, sometimes black.

  3. You are not doing any OpenGL error checking, either with glGetError() or using a debug context. I would strongly recommend doing both of these (if possible) to help find where the program is going wrong.

  4. You are not calling glfwTerminate at the end of your program. This should be done to clean up correctly.

  5. Your use of GL vertex arrays is a bit strange (i.e. repeatedly binding the vbo and calling glVertexAttribPointer, when it only needs to be done once).

I’ll write back if I think of anything else.

Thanks for the reply,

as I mentioned I’m only following the tutorial, I don’t know what half these things are yet, but I’ll change what I can if the teaching is obviously missing crucial/important information. I’m still receiving a black screen, but on the tutorial’s video it’s drawing so I’m not sure what part I’m missing exactly. So when you say I should be linking to GLSL shaders, what exactly are these and how would I do that? Is there a link you can provide me or should I just google?

Can you link to the tutorial?

I apologise but the tutorial only comes in video form. The link is the following:
Part 1:

Part 2:

If I’m not allowed YouTube links, then you’ll have to refer to my code above. Be warned, they are about 20 minutes each, but thanks if you decide to take a look.