Table of Contents
Now, remember why we added those libraries? It was because they handled creating a window and OpenGL context. It's time we put that to use, so let's go ahead and open up the project in Visual Studio. Replace your "Main.cpp" code with the following code:
#include "Include\freeglut\freeglut.h"
#include <iostream>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(400, 150); // optional
glutInitWindowSize(800, 600); // optional
glutCreateWindow("Hello World");
glutMainLoop();
return 0;
}#include "Include\glfw\glfw3.h"
#include <iostream>
int main(int argc, char **argv)
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(800, 600, "Hello World", nullptr, nullptr);
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)) // Main Loop
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}#include "Include\sfml\include\SFML\window.hpp"
#include <iostream>
int main(int argc, char **argv)
{
sf::Window window;
sf::ContextSettings settings;
window.create(sf::VideoMode(800, 600), "Hello World", sf::Style::Close, settings);
while(window.isOpen())
{
sf::Event windowEvent;
while (window.pollEvent(windowEvent))
{
switch (windowEvent.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
}
return 0;
}
If you compile and run this, you should have a window and console appear. Closing one will close the other. If that does not happen, check your code and ensure you properly added FreeGLUTGLFWSFML to your project.
Now let's talk about these functions:
Now let's draw a blank screen with a solid color on it. In order to do this, we'll have to create a render() function that does the drawing. In the main method, we call glEnable(GL_DEPTH_TEST) to enable depth testing on the video card. Sometimes, you don't need this test and you can disable it in the render function.
You will also need to register the callback function for render (this tells FreeGLUT that this is the function that does the drawing), so call glutDisplayFunc(render). In the while loop, before glfwSwapBuffers, add a call to render(). In the outer while loop, after the inner while loop (so after the end curly brace for the window.pollEvent loop), add a call to render(). It should look like this:
#include "Include\freeglut\freeglut.h"
#include <iostream>
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 1.0, 0.0, 1.0);//clear green
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(400, 150); // optional
glutInitWindowSize(800, 600); // optional
glutCreateWindow("Hello World");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(render);
glutMainLoop();
return 0;
}#include "Include\glfw\glfw3.h"
#include <iostream>
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 1.0, 0.0, 1.0);//clear green
}
int main(int argc, char **argv)
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(800, 600, "HelloWorld", nullptr, nullptr);
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)) // Main Loop
{
render();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}#include "Include\sfml\include\SFML\window.hpp"
#include <iostream>
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 1.0, 0.0, 1.0);//clear green
window.display();
}
int main(int argc, char **argv)
{
sf::Window window;
sf::ContextSettings settings;
window.create(sf::VideoMode(800, 600), "Hello World", sf::Style::Close, settings);
while(window.isOpen())
{
sf::Event windowEvent;
while (window.pollEvent(windowEvent))
{
switch (windowEvent.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
render();
}
return 0;
}
In the render function, not much is necessary:
Up until this point, we haven't used GLEW once. We don't need it yet, but it will be useful to familiarize yourself with it. In order to do this, you'll need to add the include statement at the top. Add #include "Include\glew\glew.h" to the top of your program. If you used the static library for GLEW, you will also need to include a #define GLEW_STATIC at the top. Right underneath the glfwMakeContextCurrent(window)glutCreateWindow() call, insert the following lines:
glewInit();
printf("OpenGL version supported by this platform: (%s)\n", glGetString(GL_VERSION));
if (glewIsSupported("GL_VERSION_4_5"))
{
std::cout << "OpenGL Version is 4.5\n";
}
else
{
std::cout << "OpenGL 4.5 is not supported\n";
}
The printf (which does not technically require GLEW) will tell you your maximum OpenGL version supported. The if statement checks to see if your graphics card is compatible with OpenGL Version 4.5 and then displays the result. You could easily change GL_VERSION_4_5 to earlier versions to test those too (such as GL_VERSION_4_0 for 4.0). It's okay if yours is not compatible with the latest version of OpenGL, it's just good to be aware of which version you're working with.
If you were not able to get it to compile and run, be sure you properly added GLEW to your project and have your code written correctly. If you're ready to move on, it's time to draw our first triangle.