How To Code OpenGl In Visual Studio

Mohammad Javadian Farzaneh
3 min readMar 26, 2020

Introduction

OpenGL is a graphic library that help us to write and create 2D/3D graphic programs as well as games.For using opengl functions and APIs we must install some libraries which run on top of the opengl and ease the process of developing graphic soft-wares.Some of these libraries are necessary and some of the are optional,let check some of them.

GLUT

It is one of the libraries using for making windows,changing colors and showing object or points on the screen,at all we can say it is library for managing and handling windows.Of course,there are some libraries stronger than glut and also faster but glut is the right decision for beginning because we can learn the basics with it. For installing glut on Visual Studio check the link below which was really helpful for me. It is worth to mention that we have two version of glut which are freeglut and glut.We use freeglut because it is free and opensource.

if the upper link dose not work try other ways because there are a lot of ways to install freeglut in visual studio.

GLEW

By glut or free glut or other libraries which are used for creation and handling windows you can not do some graphic work directly on GPU,for instance,you want to draw some triangles in a window,by glut you must tell the CPU to call GPU for the drawings but with GLEW you can tell GPU to draw the points or triangles that you want.For installing GLEW follow below steps:

Finally

for doing the basics works and learning opengl coding these two libraries is enough. After installation,you can use them in visual studio as below:

#define GLEW_STATIC
#include <GL/glew.h> or #include <glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

These are library headers which help us to use glut and glew.Line 1 & 2 is for glew,the first line is written when you want to use glew in windows.In this OS static version of glew is used as mentioned in the above link.The last three headers are related to glut library.

Testing

In this part of tutorial we want to create simple window with glut to see that glut is successfully installed.

Below code is in our main function:

    glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(800, 800);
glutInitWindowPosition(50, 100);
glutCreateWindow("simple Pong");
glutDisplayFunc(myDisplay);
glutMainLoop();

In the above code we are telling to opengl that we want to use RGB colors in program and make window in 800*800 pixel from position of x=50 and y=100.The title of window should be “simple Pong”.

For managing the works which are related to displaying objects we should prepare a callback function.just make void function in any name and pass the name to glutDisplayFunc and then your program is ready to build.

glutMainLoop is for handling events in our program and it is necessary to have it in our code. if every things is okay you should see a white window.

Sorry for my bad english.This is my first story in english.

--

--

Mohammad Javadian Farzaneh

M.Sc in Artificial Inteligence, Computer Vision Enthusiast, and looking for Ph.D positions