Group C Assignment 6 OpenGL
Group C Assignment 6 OpenGL
6
Title Implementation of OpenGL functions
Aim Write C++ program to draw 3-D cube and perform following transformations
on it using OpenGL i) Scaling ii) Translation iii) Rotation about an axis (X/Y/Z).
Theory OpenGL Basics:
Overview of an
OpenGL
program
Main
Open window and configure frame buffer (using GLUT for example)
Initialize GL states and display (Double buffer, color mode, etc.)
Loop
Check for events
if window event (resize, unhide, maximize etc.) modify the viewport
and Redraw
else if input event (keyboard and mouse etc.)
handle the event (such as move the camera or change the state)
and usually draw the scene
Redraw
Clear the screen (and buffers e.g., z-buffer)
Change states (if desired)
Render
Swap buffers (if double buffer)
OpenGL Syntax
All functions have the form: gl*
glVertex3f() – 3 means that this function take three arguments,
and f means that the type of those arguments is float.
glVertex2i() – 2 means that this function take two arguments,
and i means that the type of those arguments is integer
All variable types have the form: GL*
In OpenGL program it is better to use OpenGL variable types
(portability)
Glfloat instead of float
Glint instead of int
OpenGL primitives
Drawing in 3D
Depth buffer (or z-buffer) allows scene to remove hidden surfaces.
Use
glEnable(GL_DEPTH_TEST) to enable it.
An OpenGL program using the three libraries listed above must include
the appropriate headers. This requires the following three lines:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Double buffering means that there are two buffers, a front buffer and a
back buffer. The front buffer is displayed to the user, while the back buffer
is used for rendering operations. This prevents flickering that would
occur if we rendered directly to the front buffer.
Next, a window is created with GLUT that will contain the viewport which
displays the OpenGL front buffer with the following three lines:
glutInitWindowPosition(px, py);
glutInitWindowSize(sx, sy);
glutCreateWindow(name);
To register callback functions, we simply pass the name of the function that
handles the event to the appropriate GLUT function.
glutReshapeFunc(reshape);
glutDisplayFunc(display);
In this example, when the user resizes the window, reshape is called by
GLUT, and when the display needs to be refreshed, the display function is
called. For animation, an idle event handler that takes no arguments can
be created to call the display function to constantly redraw the scene with
glutIdleFunc. Once all the callbacks have been set up, a call to
glutMainLoop allows the program to run.
In the display function, typically the image buffer is cleared, primitives are
rendered to it, and the results are presented to the user. The following line
clears the image buffer, setting each pixel color to the clear color, which
can be configured to be any color:
glClear(GL_COLOR_BUFFER_BIT);
The next line sets the current rendering color to blue. OpenGL behaves like
a state machine, so certain state such as the rendering color is saved by
OpenGL and used automatically later as it is needed.
glBegin(GL_LINES);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glutSwapBuffers();
3D
transformations Translation
Scaling
Matrix for Scaling
Rotation
Matrix for representing three-dimensional rotations about the Z axis