Modern OpenGL program crash with white background

Hello,

I’ve been working on an OpenGL program (whose source I’ll attach). I’m compiling it with CMake, under Windows 7 (64 bits) and MinGW.
The program compiles just fine, however it crashes when it is run, displaying a white window.

A screenshot of the problem:

physbox.cpp:


#define GLEW_STATIC 1 /* GLEW_STATIC has to be defined in order for glew32s.lib to link correctly */

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <string>

#include "renderer.cpp"

int main(int argc, const char* argv[])
{
	if (!glfwInit())
	{
		std::cerr << "Could not initialize GLFW!" << std::endl;
		return -1;
	}

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* window = glfwCreateWindow(800, 600, "Test", nullptr, nullptr);

	if (!window)
	{
		std::cerr << "Could not create GLFW window!" << std::endl;
		return -1;
	}

	glfwMakeContextCurrent(window);

	if (glewInit() != GLEW_OK)
	{
		std::cerr << "Could not initialize GLEW!" << std::endl;
		return -1;
	}

	renderer::initialize();

	GLfloat vertices[] = {
		0.0, -1.0, 0.0,
		-1.0, -1.0, 0.0,
		1.0, -1.0, 0.0
	};

	renderer::loadVertexData(vertices, 3, GL_STATIC_DRAW);

	while (!glfwWindowShouldClose(window)) {

		renderer::drawTris();

		glfwSwapBuffers(window);

		glfwPollEvents();
	}

	glfwTerminate();

	return 0;
}

renderer.cpp:


#include <GL/glew.h>

namespace renderer {
	GLuint vertexArrayObject;
	GLuint vertexBuffer;
	GLuint vertexCount;

	void initialize() {
		glGenVertexArrays(1, &vertexArrayObject);
		glBindVertexArray(vertexArrayObject);
		glGenBuffers(1, &vertexBuffer);
	}

	void loadVertexData(GLfloat* vertices, GLuint amount, GLenum usage) {
		glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * amount, vertices, usage);
	
		vertexCount = amount;
	}

	void drawTris() {
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, vertexCount, GL_FLOAT, GL_FALSE, 0, 0);
	}
}

CMakeLists.txt


cmake_minimum_required(VERSION 2.6)

project(physbox)

# Paths
set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
set(LIB_DIR ${PROJECT_SOURCE_DIR}/lib)
set(INC_DIR ${PROJECT_SOURCE_DIR}/include)

# Source files
set(SOURCES
	${SOURCE_DIR}/physbox.cpp)

# Set standard to C++11, set library and include paths
set(CMAKE_CXX_FLAGS "-std=c++11 -I${INC_DIR} -L${LIB_DIR}")

# Append -mwindows if building on Windows
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwindows")
endif()

add_executable(physbox ${SOURCES})

# Link library depending on system
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
	target_link_libraries(physbox glfw3 glew32s opengl32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	target_link_libraries(physbox glfw3 glew)
endif()

Thanks in advance

I think you still need to set glewExperimental = GL_TRUE before calling glewInit on core profile contexts - unless that has been fixed recently in GLEW.
The underlying issue is that GLEW uses glGetString(GL_EXTENSIONS) which is unavailable in core profile (where glGetStringi(GL_EXTENSIONS, i) must be used instead).

Note also that your buffer for vertex data is too small, you only allocate space for 3 floats, but you want space for 3 positions (with 3 floats each).

Thanks a lot, that solved it :slight_smile: