Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

OpenGL Basics

Main Function:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("___");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

glutInit(&argc, argv):
The function glutInit(&argc, argv) is part of the GLUT (OpenGL Utility Toolkit) library and is used to initialize
the GLUT system and process command line arguments.
● &argc: argc stands for "argument count" and is the number of command line arguments passed to
your program when it is executed.
● argv: argv stands for "argument vector" and is an array of strings that holds the command line
arguments.

glutInit(&argc, argv) is called at the beginning of the main() function to initialize GLUT and process any
command line arguments. After the initialization, you can proceed with additional GLUT setup, such as
creating windows, registering callbacks, and entering the GLUT event processing loop.

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB):
● GLUT_SINGLE: This means that the content is drawn and displayed immediately.
● GLUT_RGB: This flag indicates that the color model used for the window will be RGB (Red, Green,
Blue).
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) is used to set the initial display mode for the window.
The window created by GLUT will have a single buffer and use the RGB color model.

glutInitWindowSize(500,500); This function call sets the initial size of the window created by GLUT to 500
pixels in width and 500 pixels in height.

glutInitWindowPosition(100,100): This function call sets the initial position of the window on the screen.
The window will be positioned 100 pixels from the left and 100 pixels from the top of the screen.
glutCreateWindow("___"): This function call creates a window with the specified title.

init():
void init(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
}

● glClear(GL_COLOR_BUFFER_BIT): It specifies that the color buffer should be cleared, removing all
previously rendered colors.
● glClearColor(0,0,0,0): It specifies the red, green, blue, and alpha components of the clear color.
The values provided (0, 0, 0, 0) represent black with full transparency.
● glMatrixMode(GL_PROJECTION);
● glLoadIdentity(): This ensures that you start with a clean slate and any previous transformations
are cleared.
● gluOrtho2D(-100,100,-100,100): call sets up an orthographic projection with a viewing volume
ranging from -100 to 100 in both the X and Y directions. This means that objects within this range
will be visible, while those outside will be clipped.

glutDisplayFunc(display): This function call sets the display callback function for the current window. The
display callback function (display() in this case) will be responsible for rendering the contents of the
window.

glutMainLoop(): This function enters the GLUT event processing loop, which continuously checks for user
input events, redraws the window when necessary, and handles other system events. The program
remains in this loop until the application is closed.

Plotting a Point:
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();

glBegin() function:
The glBegin() function in OpenGL is used to indicate the start of a block of vertex data specification. It is
used to define a geometric primitive or a group of primitives that you want to render.
The glBegin() function takes a single argument that specifies the type of primitive you want to render.
Some of the common primitive types include:

● GL_POINTS: Treats each vertex as a single point.


● GL_LINES: Treats each pair of vertices as a separate line segment.
● GL_TRIANGLES: Treats every three consecutive vertices as a triangle.
● GL_QUADS: Treats every four consecutive vertices as a quadrilateral.
● GL_POLYGON: Treats the vertices as a single polygon with an arbitrary number of sides.

One can use other OpenGL functions like glVertex2f() or glVertex3f() to specify the coordinates of each
individual point you want to render. Once one has specified all the vertices, you end the block of vertex
data specification with glEnd().

glVertex2f():
The glVertex2f() function is used to specify the coordinates of a vertex in a two-dimensional space. It takes
two floating-point arguments: the x-coordinate and the y-coordinate of the vertex. The "2" in glVertex2f()
represents the dimensionality of the vertex (in this case, 2D).

glFlush():
In OpenGL, the glFlush() function is used to ensure that all previously issued commands are executed by
the rendering system as soon as possible. It forces any pending rendering operations to be performed
immediately.

You might also like