banner



How To Use Opengl In Windows

Programming OpenGL in C/C++

How To Setup and Become Started

We need a C/C++ compiler, either GCC (GNU Compiler Collection) from MinGW or Cygwin (for Windows), or Visual C/C++ Compiler, or others.

We need the post-obit sets of libraries in programming OpenGL:

  1. Core OpenGL (GL): consists of hundreds of functions, which begin with a prefix "gl" (e.g., glColor, glVertex, glTranslate, glRotate). The Cadre OpenGL models an object via a set of geometric primitives, such as signal, line, and polygon.
  2. OpenGL Utility Library (GLU): congenital on-elevation of the core OpenGL to provide of import utilities and more building models (such equally qradric surfaces). GLU functions starting time with a prefix "glu" (e.g., gluLookAt, gluPerspective)
  3. OpenGL Utilities Toolkit (Glut): provides support to interact with the Operating System (such every bit creating a window, handling key and mouse inputs); and more building models (such as sphere and torus). GLUT functions showtime with a prefix of "glut" (e.g., glutCreatewindow, glutMouseFunc).
    Quoting from the opengl.org: "Glut is designed for constructing small-scale to medium sized OpenGL programs. While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits. Glut is simple, piece of cake, and small."
    Alternative of GLUT includes SDL, ....
  4. OpenGL Extension Wrangler Library (GLEW): "GLEW is a cross-platform open-source C/C++ extension loading library. GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform." Source and pre-build binary available at http://glew.sourceforge.net/.

Each of the software package consists of:

  1. A header file: "gl.h" for core OpenGL, "glu.h" for GLU, and "glut.h" (or "freeglut.h") for Overabundance, typically kept under "include\GL" directory.
  2. A static library: for case, in Win32, "libopengl32.a" for cadre OpenGL, "libglu32.a" for GLU, "libglut32.a" (or "libfreeglut.a" or "glut32.lib") for GLUT, typically kept under "lib" directory.
  3. An optional shared library: for example, "glut32.dll" (for "freeglut.dll") for GLUT nether Win32, typically kept under "bin " or "c:\windows\system32".

It is important to locate the directory path and the actual filename of these header files and libraries in your operating platform in social club to properly setup the OpenGL programming surroundings.

Eclipse CDT with Cygwin or MinGW

Installing Eclipse CDT / Cygwin or MinGW, OpenGL, GLU and GLUT

Stride 1: Setup the Eclipse CDT (C Development Toolkit)

Read "How to install Eclipse CDT".

Footstep 2: Setup a GCC Compiler

We could use either MinGW or Cygwin.

  • MinGW: For MinGW, we need to install GLUT separately. Download freeglut (@ http://freeglut.sourceforge.net/index.php). I recommend using the pre-parcel version for MinGW (freeglut 2.8.0 MinGW Package) available at http://www.transmissionzero.co.united kingdom/software/freeglut-devel/.
    Download, unzip and copy header files from "include\GL" to "<MINGW_HOME>\include\GL"; the libraries from "lib" to "<MINGW_HOME>\lib", and shared library from "bin" to "<MINGW_HOME>\bin" (which should be included in the PATH environs variable), where <MINGW_HOME> is the MinGW installed directory.
    Take note of the headers and libraries:
    1. Headers: the OpenGL header "gl.h", GLU header "glu.h" and GLUT header "overabundance.h" (or "freeglut.h") are kept in "<MINGW_HOME>\include\GL" directory. Since "<MINGW_HOME>\include" is in the implicit include-path. We can include the headers as <GL/glut.h>, <GL/glt.h>, and <GL/gl.h>.
    2. Libraries: the OpenGL library "libopengl32.a", GLU library "libglu32.a" and GLUT library "libfreeglut.a" are kept in "<MINGW_HOME>\lib" directory. This directory is in the implicit library-path.
      Nonetheless, we need to include these libraries in linking. They shall be referred to as "opengl32", "glu32", "freeglut" without the prefix "lib" and suffix ".a".
    (Alternatively, y'all could download Nate Robin's original Win32 port of Glut from @ http://www.xmission.com/~nate/overabundance.html, which has non been updated since 2001. Download, unzip and copy "overabundance.h" to "<MINGW_HOME>\include\GL", "glut32.lib" to "<MINGW_HOME>\lib", and "glut32.dll" to "<MINGW_HOME>\bin" (which should be included in the PATH))
  • Cygwin: We need to install "gcc", "g++", "gdb", "make" (under the "Devel" category) and "opengl", "freeglut" (under the "Graphics" category).
    1. Headers: the OpenGL header "gl.h", GLU header "glu.h", and GLUT header "overabundance.h" are provided in the "<CYGWIN_HOME>\usr\include\w32api\GL" directory. As "<CYGWIN_HOME>\usr\include\w32api" is in the implicit include-path. We tin can include the headers as <GL/glut.h> , <GL/glt.h>, and <GL/gl.h>.
    2. Libraries: the OpenGL library "libopengl32.a", GLU library "libglu32.a" and Overabundance library "libglut32.a" are provided in the "<CYGWIN_HOME>\lib\w32api" directory. This directory is in the implicit library-path.
      However, we need to include these libraries in linking. They shall exist referred to equally "opengl32", "glu32", "glut32" without the prefix "lib" and suffix ".a".

Stride 3: Configuring the Include-Path, Lib-Path and Library: Nosotros tin can configure on per-projection footing by right-click on the project ⇒ Properties ⇒ C/C++ full general ⇒ Paths and Symbols ⇒ Use "Includes" panel to configure the Include-Path; "Library Paths" panel for the Lib-Path; and "Libraries" panel for individual libraries. Nosotros will do this later on.

On command-line (for GCC), we could use option -I<dir> for include-path, -L<dir> for lib-path, and -50<lib> for library.

Writing Your First OpenGL Plan

  1. Launch Eclipse.
  2. Create a new C++ project: Select "File" menu ⇒ New ⇒ Project... ⇒ C/C++ ⇒ C++ Project ⇒ Next.
    In "Project proper name", enter "How-do-you-do" ⇒ In "Project type", select "Executable", "Empty Project" ⇒ In "Toolchain", select "Cygwin GCC" or "MinGW GCC" (depending on your setup) ⇒ Next ⇒ Finish.
  3. Create a new Source file: Correct-click on the projection node ⇒ New ⇒ Other... ⇒ C/C++ ⇒ Source file ⇒ Next.
    In "Source file", enter "GL01Hello.cpp" ⇒ Finish.
  4. In the editor console for "GL01Hello.cpp", type the following source codes:
    Annotation: For Windows, y'all should include "windows.h" header earlier the OpenGL headers.
                    #include <windows.h>   #include <GL/glut.h>      void display() {    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);              
    glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3f(one.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutCreateWindow("OpenGL Setup Exam"); glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50); glutDisplayFunc(display); glutMainLoop(); render 0; }
  5. Configuring the "include-paths", "library-paths" and "libraries":
    Right-click on the project ⇒ Property ⇒ C/C++ general ⇒ Paths and Symbols.
    Open up the "Libraries" tab ⇒ Add ⇒ Enter "glut32" (Cygwin) or "freeglut" (MinGW with freeglut) ⇒ Add ⇒ Enter "glu32" ⇒ Add together ⇒ Enter "opengl32".
    There is no need to configure the "include-paths" and "library-paths", as they are implicitly defined.
  6. Build (correct-click on the projection node ⇒ "Build Project") and Run (right-click on the project node ⇒ Run As ⇒ Local C/C++ Application).

GCC Compilation in Command-line

Observe the Eclipse'south console. It uses the following commands to build the project:

>          m++ -O0 -g3 -Wall -c -fmessage-length=0 -o GL01Hello.o "..\\GL01Hello.cpp"            >          g++ -o GL01Hello.exe GL01Hello.o -lglu32 -lopengl32 -lfreeglut        

The nearly important option is -fifty<lib>, which specifies the "library" to exist linked. Y'all might as well need -I<dir> to specify the "include-path" and -L<dir> to specify the "lib-path", if the header files and libraries are not kept in implicit directories used by the compiler.

You tin can find the commands used in Eclipse CDT for build the project at ".metadata\.plugins\org.eclipse.cdt.ui\global-build.log".

CodeBlocks / MinGW

Installing CodeBlocks, MinGW and Glut

  1. Install CodeBlocks and MinGW: Read "How to install CodeBlocks".
  2. Install GLUT: Refer to the to a higher place "step" in Eclipse/MinGW setup.

Writing Your Offset OpenGL Program

  1. Create a new project: File ⇒ New ⇒ Project... ⇒ Console Application ⇒ Become ⇒ C++ ⇒ In "Project title", enter "hello" ⇒ Next ⇒ Finish.
  2. Open "main.cpp", and replace with the code, as shown in the previous section.
  3. Configure the libraries: Right-click on the project ⇒ Build Selection... ⇒ Linker Settings ⇒ In "Link Libraries" ⇒ Add together ⇒ enter "freeglut" ⇒ Add together ⇒ enter "glu32" ⇒ Add ⇒ enter "opengl32".
    There is no need to configure the "include-paths" and the "library-paths", equally they are implicitly defined.
  4. Build (right-click on the project ⇒ Build) and Run (Select "Build" menu ⇒ Run).

GCC Compilation in Command-line

Refer to the above "department" on "Eclipse with GCC".

Visual C++ 2010 Express

Installing VC++, OpenGL, GLU and GLUT

You need to install:

  1. Visual C++ Express 2010: Read "How to install Visual C++ Express". VC++ would be installed in "C:\Program Files\Microsoft Visual Studio x.0\VC", with headers in sub-directory "include" and libraries in "lib".
  2. Windows SDK which includes OpenGL and GLU (OpenGL Utility). The Visual C++ 2010 Limited bundles the Microsoft Windows SDK, which would be installed in "C:\Program Files\Microsoft SDKs\Windows\v7.0A". (Otherwise, you lot need to download and install the Windows SDK separately).
    The followings are used from Windows SDK:
    • gl.h, glu.h: header for OpenGL and GLU in directory "C:\Plan Files\Microsoft SDKs\Windows\v7.0A\include\gl".
    • opengl32.lib, glu32.lib: libraries for OpenGL and GLU in directory "C:\Plan Files\Microsoft SDKs\Windows\v7.0A\lib".
    • opengl32.dll, glu32.dll: dynamic link libraries for OpenGL and GLU in directory "C:\Windows\System32". This directory is to be included in PATH environs variable.

    If you use the VC++ IDE, the include-path and lib-path would have been set correctly. If you use the CMD shell, you need to run the batch file "vcvarsall.bat" (in "C:\Program Files\Microsoft Visual Studio 10.0\VC\bin"), or "vcvars32.bat" in the earlier version, to set the environment variables.

  3. GLUT (OpenGL Utility Toolkit): Download Nate Robin'southward original Win32 port of Overabundance from @ http://world wide web.xmission.com/~nate/glut.html (or freeglut @ http://freeglut.sourceforge.net). Unzip and copy "overabundance.h" to "C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\gl", "glut32.lib" to "C:\Plan Files\Microsoft SDKs\Windows\v7.0A\lib", and "glut32.dll" to "C:\Windows\System32" (that is, the same locations as OpenGL and GLU).

Writing Your Start OpenGL Program

To Write your first OpenGL program with Glut:

  1. Launch Visual C++ 2010 Express.
  2. Create a new "Win32 Console Application" project: Select "File" carte ⇒ New ⇒ Project... ⇒ In "Project Types", select "Visual C++", "Win32". In "Templates", select "Win32 Panel Application". In "Location", set your working directory. In "Name", enter "hi" ⇒ Next ⇒ Bank check "Empty Project" ⇒ Terminate.
  3. Create a new Source file: Right-click on the "Source Files" of the project name ⇒ Add ⇒ New Particular... ⇒ In "Categories", select "Visual C++", "Code". In "Templates", select "C++ File (.cpp)". In "Proper noun", type "GL01Hello.cpp" ⇒ Add.
  4. In the editor panel for "GL01Hello.cpp", replace with the above lawmaking (in the previous section).
  5. Build the solution ("Build" menu ⇒ Build Solution) and run the program ("Debug" menu ⇒ "Start Without Debugging").

Common OpenGL Programming Errors (for VC++)

  • Linkage Error: Right-click on the projection name ⇒ In "Configuration" drop-down menu, select "All Configurations" (i.e., release and debug) ⇒ Expand the "Configuration Properties" node. Expand the "Linker" sub-node ⇒ Select "Input" ⇒ In "Additional Dependencies", type "opengl32.lib glu32.lib glut32.lib".
    Alternatively, you lot could also utilise pre-processor directives to instruct compiler about the libraries used (I prefer this arroyo, which avoids manual configuration for every project):
    #ifdef _MSC_VER                         #  pragma comment(lib, "opengl32.lib")   #  pragma comment(lib, "glu32.lib")      
    

0 Response to "How To Use Opengl In Windows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel