1.2 Graphics

https://www.khronos.org/opengl/wiki/Get_Context_Info

Complete Source Code:

Download


Main.cpp

#define GLEW_STATIC

#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <iostream>

#include "Graphics.h"

using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height)

{

Graphics::Resize(width, height);

}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)

{

if (key == GLFW_KEY_E && action == GLFW_PRESS)

{ }

}

void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)

{

}

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)

{

if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)

{ }

}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)

{

}

void joystick_callback(int joy, int event)

{

if (event == GLFW_CONNECTED)

{ }

else if (event == GLFW_DISCONNECTED)

{ }

}

int main(int argc, char* argv[])

{

/* Initialize GLFW */

if (!glfwInit())

return -1;

/* Create a window and its OpenGL context */

bool fullscreen = false;

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWmonitor* monitor = fullscreen ? glfwGetPrimaryMonitor() : NULL;

GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", monitor, NULL);

if (!window)

{

glfwTerminate();

return -1;

}

/* Make the window's context current */

glfwMakeContextCurrent(window);

/* Set callback functions */

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

glfwSetKeyCallback(window, key_callback);

glfwSetCursorPosCallback(window, cursor_position_callback);

glfwSetMouseButtonCallback(window, mouse_button_callback);

glfwSetScrollCallback(window, scroll_callback);

glfwSetJoystickCallback(joystick_callback);

/* Initialize GLEW */

if (glewInit() != GLEW_OK)

{

glfwTerminate();

return -2;

}

/* Show OpenGL context information */

GLContextInfo infos = Graphics::GetContextInfos();

cout << "OpenGL version: " << infos.Version.Major << "." << infos.Version.Minor << endl;

cout << "Driver version: " << infos.Version.Driver << endl;

cout << "GLSL version: " << infos.Version.ShadingLanguage << endl;

cout << "Vendor: " << infos.Vendor << endl;

cout << "Renderer: " << infos.Renderer << endl;

/*cout << "Supported GLSL Versions: " << endl;

for (std::list<std::string>::iterator it = infos.SupportedGLSLVersions.begin(); it != infos.SupportedGLSLVersions.end(); it++)

cout << "GLSL " << *it << endl;

cout << "Supported Extensions: " << endl;

for (std::list<std::string>::iterator it = infos.SupportedExtensions.begin(); it != infos.SupportedExtensions.end(); it++)

cout << "Extension: " << *it << endl;*/

/* Initialize graphics subsystem */

int width = 0, height = 0;

glfwGetFramebufferSize(window, &width, &height);

if (!Graphics::Initialize(width, height))

{

glfwTerminate();

return -3;

}

/* Loop until the user closes the window */

while (!glfwWindowShouldClose(window))

{

/* Render here */

Graphics::Render();

/* Swap front and back buffers */

glfwSwapBuffers(window);

/* Poll for and process events */

glfwPollEvents();

}

Graphics::CleanUp();

glfwTerminate();

return 0;

}

Graphics.h

#ifndef GRAPHICS_H

#define GRAPHICS_H

#define GLEW_STATIC

#include <GL/glew.h>

#include <string>

#include <list>

struct GLContextInfo

{

struct {

GLint Major, Minor;

std::string Driver;

std::string ShadingLanguage;

} Version;

std::string Vendor;

std::string Renderer;

std::list<std::string> SupportedExtensions;

std::list<std::string> SupportedGLSLVersions;

};

namespace Graphics

{

bool Initialize(unsigned int width, unsigned int height);

void CleanUp();

void Render();

void Resize(unsigned int width, unsigned int height);

GLContextInfo GetContextInfos();

};

#endif // GRAPHICS_H

Graphics.cpp

#include "Graphics.h"

#include <glm/glm.hpp>

#include <iostream>

glm::uvec2 framebuffersize;

bool Graphics::Initialize(unsigned int width, unsigned int height)

{

Resize(width, height);

/* successfully initialized */

return true;

}

void Graphics::CleanUp()

{

}

void Graphics::Render()

{

glClear(GL_COLOR_BUFFER_BIT);

}

void Graphics::Resize(unsigned int width, unsigned int height)

{

framebuffersize = glm::uvec2(width, height);

glViewport(0, 0, width, height);

}

GLContextInfo Graphics::GetContextInfos()

{

GLContextInfo infos;

glGetIntegerv(GL_MAJOR_VERSION, &infos.Version.Major);

glGetIntegerv(GL_MINOR_VERSION, &infos.Version.Minor);

infos.Version.Driver = (const char*)glGetString(GL_VERSION);

infos.Vendor = (const char*)glGetString(GL_VENDOR);

infos.Renderer = (const char*)glGetString(GL_RENDERER);

infos.Version.ShadingLanguage = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);

GLint numberofextensions = 0;

glGetIntegerv(GL_NUM_EXTENSIONS, &numberofextensions);

for (int i = 0; i < numberofextensions; i++)

infos.SupportedExtensions.push_back((const char*)glGetStringi(GL_EXTENSIONS, i));

GLint numberofsupportedglslversions = 0;

glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &numberofsupportedglslversions);

for (int i = 0; i < numberofsupportedglslversions; i++)

infos.SupportedGLSLVersions.push_back((const char*)glGetStringi(GL_SHADING_LANGUAGE_VERSION, i));

return infos;

}