C++ - OpenGL - Setting up Visual Studio

The Visual Studio version for this tutorial will be the 2017(seems to work with the 2015 and 2019 as well). The glew version will be the 2.2.0. The freeglut version will be the 3.0.0. The glew library stands for GL E xtension W rangler. At the end of this OpenGL tutorial you will be able to display a window with a white square displayed on a black background. First of all We are going to use the 32-bit or the 64-bit version. ...

July 26, 2015

C++ - Algorithm - Using min(), max(), minmax() functions

To be precise, in this tutorial, we are going to see the min(), max(), minmax(), min_element(), max_element() and minmax_element() functions. You’ll have no excuse anymore to get the minimum and maximum value from an array or when comparing two variables. Oh by the way, two functions are taken from the C++ 11: minmax() and minmax_element(). Explanation The minmax() is a unique function to retrieve min and max values from 2 values or from an entire array. ...

May 31, 2015

C++ - Tips'n Tricks - Using interface and abstract classes to compute the shape's area

You’re maybe wondering what are interface and abstract classes? That’s a good point. In this tutorial we’re going to see how to compute the area of a shape by declaring an array of undefined shapes. After this, we will be able to use this shape as an object with this kind of Factory design pattern. Let’s see how with this example of using interface and abstract classes in C++. Explanation First of all, let’s create two directories, one for the .hh (headers) and the other for the .cpp (bodies): ...

November 4, 2012

C++ - Tips'n Tricks - Creating an object onto the stack and into the heap

Stack and heap enable data management inside the memory. But some data are added onto the stack and the other into the heap. For the stack, there is a stack pointer that enables to catch the last data added onto the stack. It means that the last data added will be the first used by the program. We call that a LIFO (last in, first out) system. For the heap, this is quite different. Indeed, data added into the heap are added randomly in the RAM by the OS. ...

October 26, 2012

C++ - OpenGL - Using glRotatef() to animate shapes

Rotating a shape is fun, but rotating it with animation is really great. That’s what we are going to see in this glRotatef() tutorial for animating shapes on a window. Our shape is made up of 3 squares with 3 different colours: the white the burgundy the green main.cpp /* * main.cpp */ #include "BadprogRotate.h" /* * Main */ int main(int ac, char* av[]) { BadprogRotate go(∾, av); return 0; } BadprogRotate.h /* * BadprogRotate.h */ #ifndef BADPROGROTATE_H_ #define BADPROGROTATE_H_ #include "GL/freeglut.h" #include #define SCREEN_TITLE "Square rotation by BadproG! :D" #define SCREEN_WIDTH 600 #define SCREEN_HEIGHT 600 #define SCREEN_POSITION_X 100 #define SCREEN_POSITION_Y 100 class BadprogRotate { public: static GLfloat S_ANGLE; BadprogRotate(int *ac, char *av[]); virtual ~BadprogRotate(); void init(void); static void managerDisplay(void); static void managerIdle(void); static void managerResize(int, int); static void managerMouse(int, int, int, int); static void managerKeyboard(unsigned char, int, int); }; #endif /* BADPROGROTATE_H_ */ BadprogRotate.cpp Add the unistd.h file for Windows OS in order to change usleep() by Sleep() in the following managerIdle() method. ...

July 14, 2012

C++ - Errors / Warnings - Cannot declare member function ‘static void MyClass::myMethod()’ to have static linkage [-fpermissive]

In C++, we don’t need to specify the static keyword in the declaration of the header and in the definition in the class. It means that this keyword has to be added only in the header. You probably know that using the static keyword means that the function is unique in program. So if you add static in the .h and in the .cpp, you will have like two methods with the same name. And the compiler won’t appreciate it. ...

July 12, 2012

C++ - OpenGL - Creating an equilateral triangle

You succeeded to display a white square on a black background with OpenGL. Great. Let’s see now how to render an equilateral triangle and changing some data. In this example we are going to: create a Makefile set the background color to purple set the drawing color to green (for the triangle) draw an equilateral triangle on the scene (thanks to the Pythagorean theorem) put this triangle in the middle of the window close the window by pressing the ESC key resize the window on the fly displaying the x and y of the mouse in the console during a mouse moving displaying the x and y of the mouse with the state (0 down and 1 up) and which button clicked All these things are basic to develop a program in OpenGL. It’s like a super Hello World! tutorial. ...

July 7, 2012

C++ - OpenGL - Using glRotatef() to rotate around the X, Y and Z axis

In this OpenGL 2.1 tutorial we are going to use the glRotatef() method. This method is designed to rotate around the 3 axis: X, Y and Z. In the example below I will use a black background and draw 3 triangles. The green will rotate around the X axis. The blue will rotate around the Y axis. The red will rotate around the Z axis. To better understand it, we have to imagine an eye seeing a point. Now around this point all triangles will rotate. ...

June 4, 2012

C++ - OpenGL - Hello World!

After installing the OpenGL libraries on your system, you would definitely want to display something on the screen. You are right! So let’s see this with an Hello World! OpenGL 2.1 tutorial. This is really a basic code to display a window and saying: “Great I can do it”. Such basic that it seems C implementation (indeed we don’t have any class in this example). Code // badprog.com #include 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("Hello world from Badprog.com :D"); glutDisplayFunc(displayMe); glutMainLoop(); return 0; } Compiling g++ main.cpp -o lookAtThis -lglut ; ./lookAtThis Result A black window with a white square displayed. ...

May 3, 2012

C++ - OpenGL - Installation

For this first OpenGL tutorial, we are going to see how to install OpenGL on GNU/Linux and Windows operating system. We need the OpenGL files, but also those from the GLU and GLUT for GNU/Linux and freeGLUT for Windows. The last ones, GLU and GLUT are libraries that add functionnalities from the basic OpenGL. OpenGL = O pen G raphics L ibrary GLU = G raphic L ibrary U tility GLUT = G raphic L ibrary U tility T oolkit Installing OpenGL on GNU/Linux Fedora If you do not have the glut.h file in your /usr/include/GL/ directory, let’s install it with the following command on your favorite shell: ...

May 3, 2012