Você está na página 1de 4

What is OpenGL:

Open Graphics Library (OpenGL) is a cross-language, cross-platform application programming


interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a
graphics processing unit (GPU), to achieve hardware-accelerated rendering.

Silicon Graphics Inc. (SGI) started developing OpenGL in 1991 and released it in January 1992,
applications use it extensively in the fields of computer-aided design (CAD), virtual reality, scientific
visualization, information visualization, flight simulation, and video games. Since 2006 OpenGL has
been managed by the non-profit technology consortium Khronos Group.

Writing an OpenGL Application:

If you are using C/C++, then you must first set up a build environment (Visual Studio project, GNU
makefile, CMake file, etc) that can link to OpenGL. Under Windows, you need to statically link to a
library called OpenGL32.lib (note that you still link to OpenGL32.lib if you're building a 64-bit
executable. The "32" part is meaningless). Visual Studio, and most Windows compilers, come with
this library.

Initialization:

Before you can actually use OpenGL in a program, you must first initialize it. Because OpenGL is
platform-independent, there is not a standard way to initialize OpenGL; each platform handles it
differently. There are two phases of OpenGL initialization. The first phase is the creation of an
OpenGL context; the second phase is to load all of the necessary functions to use OpenGL. Some
non-C/C++ language bindings merge these into one.

OpenGL Context Creation:

If you are using the C/C++ language binding for OpenGL, then you are strongly advised to use a
window toolkit for managing this task. These libraries create a window, attach an OpenGL context to
this window, and manage basic input for that window. Once you are comfortable with OpenGL, you
can then start learning how to do this manually.

Getting Functions:

In order to use OpenGL, you must get OpenGL API functions. For most libraries you are familiar with,
you simply #include a header file, make sure a library is linked into your project or makefile, and it all
works. OpenGL doesn't work that way. For reasons that are ultimately irrelevant to this discussion,
you must manually load functions via a platform-specific API call. This boilerplate work is done with
various OpenGL loading libraries; these make this process smooth. You are strongly advised to use
one.

Using OpenGL:

OpenGL is a rendering library. What OpenGL does not do is retain information about an "object". All
OpenGL sees is a ball of triangles and a bag of state with which to render them. It does not
remember that you drew a line in one location and a sphere in another.

Because of that, the general way to use OpenGL is to draw everything you need to draw, then show
this image with a platform-dependent buffer swapping command. If you need to update the image,
you draw everything again, even if you only need to update part of the image. If you want to
animate objects moving on the screen, you need a loop that constantly clears and redraws the
screen.
There are techniques for only updating a portion of the screen. And you can use OpenGL with these
techniques. But OpenGL itself doesn't do it internally; you must remember where you drew
everything. You must figure out what needs updating and clear only that part of the screen. And so
forth.

GLUT - The OpenGL Utility Toolkit:

GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system
independent toolkit for writing OpenGL programs. The GLUT library has both C, C++ (same as C),
FORTRAN, and Ada programming bindings. The GLUT source code distribution is portable to nearly
all OpenGL implementations and platforms. The current version is 3.7. Additional releases of the
library are not anticipated.
GLUT Setup:

In order to write applications with GLUT you should have the latest version – 3.7.6. You can find the
windows binary files (h lib and dll), as well as GLUT’s source code in Nate Robins site.

In order to write a C application using GLUT you’ll need three files:

 glut.h – This is the file you’ll have to include in your source code. The common place to put
this file is in the gl folder which should be inside the include folder of your system.
 glut32.lib (Windows version) – This file must be linked to your application so make sure to
put it your lib folder.
 glut32.dll (Windows) – You could place the dll file in your exe’s folder.

Here is a little bit of code to perform all the initializations:


The render function and callback registration:

GLUT event processing loop:

One last thing is missing, telling GLUT that we’re ready to get in the event processing loop. GLUT
provides a function that gets the application in a never ending loop, always waiting for the next
event to process. The GLUT function is glutMainLoop, and the syntax is as follows:

void glutMainLoop(void)

lets tell GLUT that the function renderScene should be used whenever the window requires to be
painted. GLUT has a function that takes as a parameter the name of the function to use when
redrawing is needed. Note: the function you supply as a parameter is also called the first time the
window is created. The syntax is as follows:

void glutDisplayFunc(void (*funcName)(void));

Output:
Built in Functions:

glutInit is used to initialize the GLUT library:

void glutInit(int *argcp, char **argv);

glutInitWindowPosition and glutInitWindowSize set the initial window position and size respectively:

void glutInitWindowSize(int width, int height);

void glutInitWindowPosition(int x, int y);

glutInitDisplayMode sets the initial display mode:

void glutInitDisplayMode(unsigned int mode);

GLUT DEPTH Bit mask to select a window with a depth buffer.

GLUT RGBA Bit mask to select an RGBA mode window. This is the default if neither GLUT RGBA nor
GLUT INDEX are specified.

GLUT DOUBLE Bit mask to select a double buffered window. This overrides GLUT SINGLE if it is also
specified.

glutMainLoop enters the GLUT event processing loop:

void glutMainLoop(void);

glutCreateWindow creates a top-level window:

int glutCreateWindow(char *name);

glutSwapBuffers swaps the buffers of the current window if double buffered:

void glutSwapBuffers(void);

glColor — set the current color:

glColor3d(1,0,0);

glClear — clear buffers to preset values:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin — delimit the vertices of a primitive or a group of like primitives:

glBegin(GL_TRIANGLES);

GL_POINTS, GL_LINES, GL_TRIANGLES, GL_POLYGON

glVertex — specify a vertex:

glVertex3f(-0.5,-0.5,0.0);

Você também pode gostar