1.
Setting up OpenGL environment (GLUT, GLFW) on Windows10 and above
1. Install and Setup MSYS
Head over to this link [Link] and download the mysy2 installer.
From there you can download the complete program.
2. Open MSYS2
Search for MSYS2 in search box and open it. You will get a Command Prompt-like
UI as:
3. Install Pacman in the MSYS2
Hit the below command in your brand new MSYS2 CLI
$ pacman -Syu
3. Install MinGW package via CLI
After Pacman is installed you will be able to use Pacman manager to install the
MinGW package easily with the command below
$ pacman -S mingw-w64-x86_64-toolchain
4. Install Freeglut
Hit the below command to install freeglut
$ pacman -S mingw-w64-x86_64-freeglut
5. Install Glew
Now install glew with the following command
$ pacman -S mingw-w64-x86_64-glew
6. Download Code Blocks
Here you have to be super careful on how to download the specific version of
code blocks.
Since we have already set up MinGW package we don’t need code block with
mingw setup so install simple code block without mingw-setup as shown below :
Head over to this link [Link] and download the
binary release of code blocks as shown below:
7. Set up the compiler in Code Blocks
After you are done with the installation of the code blocks, we have to do some
compiler configuration in it.
Click on Settings > Compiler > Global Compiler Settings
Under Toolchain Executables Tab there is Compiler’s Installation Directory
You have to replace that with the mingw64 which resides inside the msys64 in
the Local Disk C which is C:\msys64\mingw64 as shown in the below figure:
After setting up the path in Toolchain executables now Click on the Linker
Settings Tab. In the Right Portion, there is Other linker options where you have to
add the following command.
-lfreeglut -lglew32 -lopengl32
And in the same Linker Settings Tab, In the Left portion add the following
commands one after the other
glu32
gdi32
winmm
freeglut
opengl32
After adding it, the tab looks like as :
All done. Now you can create a new project for creating your OpenGL program.
But before that we have to get some ground idea to start a project for OpenGL
program in code blocks.
8. Creating a OpenGL program
Click on File > New > Project
Now a New From Template Tab pops out in which you have to select Console
Application and Click Go as illustrated below:
Now a console application tab pops out as
Click on Next.
Now there will be given an option for C and C++. Choose the language in which
you are going to write the program. For now we are choosing C++ language as :
After selecting the language, click Next
Now give a project title and click on Next as
Finally click on Finish
Now our IDE starts and a sample Hello World Program is given
under Sources > [Link] file. Build and Run it and check the Setup runs
successfully.
Press enter or click to view image in full size
Check your OpenGL programs in the same file [Link] by replacing the hello
world program with the following sample code :
#include <GL/glut.h>void displayMe(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glFlush();
}int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("SAMPLE TEST");
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}
After build and run you will get a pop up as :
2. Drawing basic 2D shapes using OpenGL (Points, Lines, Polygons)
#include <GL/glut.h>
// Display function
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
// Draw Point
glPointSize(6);
glColor3f(1.0, 0.0, 0.0); // Red color
glBegin(GL_POINTS);
glVertex2f(-8.0, 8.0);
glEnd();
// Draw Line
glLineWidth(3);
glColor3f(0.0, 1.0, 0.0); // Green color
glBegin(GL_LINES);
glVertex2f(-5.0, 0.0);
glVertex2f(5.0, 0.0);
glEnd();
// Draw Triangle (Polygon)
glColor3f(0.0, 0.0, 1.0); // Blue color
glBegin(GL_POLYGON);
glVertex2f(0.0, 5.0);
glVertex2f(-4.0, 1.0);
glVertex2f(4.0, 1.0);
glEnd();
// Draw Rectangle (Polygon)
glColor3f(1.0, 1.0, 0.0); // Yellow color
glBegin(GL_POLYGON);
glVertex2f(-4.0, -2.0);
glVertex2f(4.0, -2.0);
glVertex2f(4.0, -6.0);
glVertex2f(-4.0, -6.0);
glEnd();
glFlush();
}
// Initialization function
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // Black background
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-10, 10, -10, 10); // 2D coordinate system
}
// Main function
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Basic 2D Shapes using OpenGL");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
3. Implementing 2D transformations using OpenGL
#include <GL/glut.h>
// Triangle vertices
float x1 = -2, y1 = -2;
float x2 = 2, y2 = -2;
float x3 = 0, y3 = 2;
// Transformation values
float tx = 4, ty = 2; // Translation
float angle = 45; // Rotation angle
float sx = 1.5, sy = 1.5; // Scaling factors
// Function to draw original triangle
void drawOriginal()
{
glColor3f(1, 0, 0); // Red
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
}
// Function to draw transformed triangle
void drawTransformed()
{
glColor3f(0, 1, 0); // Green
glPushMatrix();
// Apply transformations
glTranslatef(tx, ty, 0);
glRotatef(angle, 0, 0, 1);
glScalef(sx, sy, 1);
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
glPopMatrix();
}
// Display function
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawOriginal(); // Original triangle
drawTransformed(); // Transformed triangle
glFlush();
}
// Initialization
void init()
{
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -1, 1);
}
// Main function
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("2D Transformations using OpenGL");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
4. Implementing 2D and 3D transformations in OpenGL
a. 2D Transformation
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Translation
glTranslatef(0.5f, 0.0f, 0.0f);
// Rotation (45 degrees around Z-axis)
glRotatef(45, 0.0f, 0.0f, 1.0f);
// Scaling
glScalef(0.5f, 0.5f, 1.0f);
// Draw square
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("2D Transformations");
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
b. 3D Transformation
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Move cube backward
glTranslatef(0.0f, 0.0f, -5.0f);
// Rotate cube
glRotatef(45, 1.0f, 1.0f, 0.0f);
// Scale cube
glScalef(1.0f, 1.0f, 1.0f);
glColor3f(0.0, 1.0, 0.0);
glutWireCube(2.0);
glutSwapBuffers();
}
void init() {
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Transformations");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
5. Setting up projection models (Orthographic vs. Perspective)
#include <GL/glut.h>
int mode = 0; // 0 = Orthographic, 1 = Perspective
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if(mode == 0)
{
// Orthographic view
glTranslatef(0.0, 0.0, -5.0);
}
else
{
// Perspective view
glTranslatef(0.0, 0.0, -5.0);
}
// Draw a cube
glColor3f(0.2, 0.6, 1.0);
glutWireCube(2.0);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(mode == 0)
{
// Orthographic Projection
glOrtho(-4, 4, -4, 4, 1, 20);
}
else
{
// Perspective Projection
gluPerspective(60.0, (float)w/h, 1.0, 20.0);
}
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
if(key == 'o' || key == 'O')
{
mode = 0; // Orthographic
reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
}
if(key == 'p' || key == 'P')
{
mode = 1; // Perspective
reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
}
glutPostRedisplay();
}
void init()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Orthographic vs Perspective Projection");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
6. Implementing line and polygon clipping
#include <GL/glut.h>
float xmin = 100, ymin = 100, xmax = 300, ymax = 300;
/* Line endpoints */
float x1 = 50, y1 = 150, x2 = 350, y2 = 250;
/* Polygon vertices */
float poly[4][2] = {
{50, 50},
{350, 80},
{320, 350},
{80, 320}
};
void drawClippingWindow()
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
}
void drawLine()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void drawPolygon()
{
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
for(int i = 0; i < 4; i++)
glVertex2f(poly[i][0], poly[i][1]);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawClippingWindow(); // clipping rectangle
drawLine(); // line to be clipped
drawPolygon(); // polygon to be clipped
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(0, 400, 0, 400);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Line and Polygon Clipping");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
7. Writing OpenGL programs using GLUT/GLFW
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // Red
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0); // Green
glVertex2f(0.5, -0.5);
glColor3f(0.0, 0.0, 1.0); // Blue
glVertex2f(0.0, 0.5);
glEnd();
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // Background color
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Simple OpenGL Program");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}