Minimizing < Windows - How To Setup And Get Started With Opengl In C++

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

Đang xem: Windows

We need the following sets of libraries in programming OpenGL:

Core OpenGL (GL): consists of hundreds of functions, which begin with a prefix “gl” (e.g., glColor, glVertex, glTranslate, glRotate). The Core OpenGL models an object via a set of geometric primitives, such as point, line, and polygon.OpenGL Utility Library (GLU): built on-top of the core OpenGL to provide important utilities and more building models (such as qradric surfaces). GLU functions start with a prefix “glu” (e.g., gluLookAt, gluPerspective)

Each of the software package consists of:

A header file: “gl.h” for core OpenGL, “glu.h” for GLU, and “glut.h” (or “freeglut.h”) for GLUT, typically kept under “includeGL” directory.A static library: for example, in Win32, “libopengl32.a” for core OpenGL, “libglu32.a” for GLU, “libglut32.a” (or “libfreeglut.a” or “glut32.lib”) for GLUT, typically kept under “lib” directory.An optional shared library: for example, “glut32.dll” (for “freeglut.dll”) for GLUT under Win32, typically kept under “bin” or “c:windowssystem32”.

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

Eclipse CDT with Cygwin or MinGW

Installing Eclipse CDT / Cygwin or MinGW, OpenGL, GLU and GLUTStep 1: Setup the Eclipse CDT (C Development Toolkit)

Read “How to install Eclipse CDT”.

Step 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-package version for MinGW (freeglut 2.8.0 MinGW Package) available at http://www.transmissionzero.co.uk/software/freeglut-devel/. Download, unzip and copy header files from “includeGL” to “includeGL”; the libraries from “lib” to “lib”, and shared library from “bin” to “in” (which should be included in the PATH environment variable), where is the MinGW installed directory.Take note of the headers and libraries:Headers: the OpenGL header “gl.h”, GLU header “glu.h” and GLUT header “glut.h” (or “freeglut.h”) are kept in “includeGL” directory. Since “include” is in the implicit include-path. We can include the headers as , , and . Cygwin: We need to install “gcc”, “g++”, “gdb”, “make” (under the “Devel” category) and “opengl”, “freeglut” (under the “Graphics” category).Headers: the OpenGL header “gl.h”, GLU header “glu.h”, and GLUT header “glut.h” are provided in the “usrincludew32apiGL” directory. As “usrincludew32api” is in the implicit include-path. We can include the headers as , , and .Libraries: the OpenGL library “libopengl32.a”, GLU library “libglu32.a” and GLUT library “libglut32.a” are provided in the “libw32api” 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”, “glut32” without the prefix “lib” and suffix “.a”.

Step 3: Configuring the Include-Path, Lib-Path and Library: We can configure on per-project basis by right-click on the project ⇒ Properties ⇒ C/C++ 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. We will do this later.

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

Xem thêm: windows server cal

Writing Your First OpenGL ProgramLaunch Eclipse.Create a new C++ project: Select “File” menu ⇒ New ⇒ Project… ⇒ C/C++ ⇒ C++ Project ⇒ Next.In “Project name”, enter “Hello” ⇒ In “Project type”, select “Executable”, “Empty Project” ⇒ In “Toolchain”, select “Cygwin GCC” or “MinGW GCC” (depending on your setup) ⇒ Next ⇒ Finish.Create a new Source file: Right-click on the project node ⇒ New ⇒ Other… ⇒ C/C++ ⇒ Source file ⇒ Next. In “Source file”, enter “GL01Hello.cpp” ⇒ Finish.In the editor panel for “GL01Hello.cpp”, type the following source codes: NOTE: For Windows, you should include “windows.h” header before the OpenGL headers.

/* * GL01Hello.cpp: Test OpenGL C/C++ Setup */#include // For MS Windows#include // GLUT, includes glu.h and gl.h /* Handler for window-repaint event. Call back when the window first appears and whenever the window needs to be re-painted. */void display() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer // Draw a Red 1×1 Square centered at origin glBegin(GL_QUADS); // Each set of 4 vertices form a quad glColor3f(1.0f, 0.0f, 0.0f); // Red glVertex2f(-0.5f, -0.5f); // x, y glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd(); glFlush(); // Render now} /* Main function: GLUT runs as a console application starting at main() */int main(int argc, char** argv) { glutInit(&argc, argv); // Initialize GLUT glutCreateWindow(“OpenGL Setup Test”); // Create a window with the given title glutInitWindowSize(320, 320); // Set the window”s initial width & height glutInitWindowPosition(50, 50); // Position the window”s initial top-left corner glutDisplayFunc(display); // Register display callback handler for window re-paint glutMainLoop(); // Enter the infinitely event-processing loop return 0;}

*

Configuring the “include-paths”, “library-paths” and “libraries”: Right-click on the project ⇒ Property ⇒ C/C++ general ⇒ Paths and Symbols.Open the “Libraries” tab ⇒ Add ⇒ Enter “glut32” (Cygwin) or “freeglut” (MinGW with freeglut) ⇒ Add ⇒ Enter “glu32” ⇒ Add ⇒ Enter “opengl32”.There is no need to configure the “include-paths” and “library-paths”, as they are implicitly defined.Build (right-click on the project node ⇒ “Build Project”) and Run (right-click on the project node ⇒ Run As ⇒ Local C/C++ Application).GCC Compilation in Command-lineObserve the Eclipse”s console. It uses the following commands to build the project:

> g++ -O0 -g3 -Wall -c -fmessage-length=0 -o GL01Hello.o “..\GL01Hello.cpp” // -c indicates compile-only // -o : specifies the output filename // -Wall shows all warning messages // -O0 specifies the optimization level // -g3 specifies the debugging information level, level 3 with extra information // -fmessage-length=0 specifies the formatted error message length, 0 for no line wrapping> g++ -o GL01Hello.exe GL01Hello.o -lglu32 -lopengl32 -lfreeglut // -o : specifies the output filename // -l specifies the library to be linked, which should be in the lib-pathThe most important option is -l, which specifies the “library” to be linked. You might also need -I to specify the “include-path” and -L to specify the “lib-path”, if the header files and libraries are not kept in implicit directories used by the compiler.

You can find the commands used in Eclipse CDT for build the project at “.metadata.pluginsorg.eclipse.cdt.uiglobal-build.log”.

CodeBlocks / MinGW

Installing CodeBlocks, MinGW and GLUTWriting Your First OpenGL ProgramCreate a new project: File ⇒ New ⇒ Project… ⇒ Console Application ⇒ GO ⇒ C++ ⇒ In “Project title”, enter “hello” ⇒ Next ⇒ Finish.Open “main.cpp”, and replace with the code, as shown in the previous section.Configure the libraries: Right-click on the project ⇒ Build Option… ⇒ Linker Settings ⇒ In “Link Libraries” ⇒ Add ⇒ enter “freeglut” ⇒ Add ⇒ enter “glu32” ⇒ Add ⇒ enter “opengl32”. There is no need to configure the “include-paths” and the “library-paths”, as they are implicitly defined.Build (right-click on the project ⇒ Build) and Run (Select “Build” menu ⇒ Run).GCC Compilation in Command-line

Refer to the above “section” on “Eclipse with GCC”.

Visual C++ 2010 Express

Installing VC++, OpenGL, GLU and GLUT

You need to install:

Windows SDK which includes OpenGL and GLU (OpenGL Utility). The Visual C++ 2010 Express bundles the Microsoft Windows SDK, which would be installed in “C:Program FilesMicrosoft SDKsWindowsv7.0A”. (Otherwise, you 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:Program FilesMicrosoft SDKsWindowsv7.0Aincludegl”.opengl32.lib, glu32.lib: libraries for OpenGL and GLU in directory “C:Program FilesMicrosoft SDKsWindowsv7.0Alib”.opengl32.dll, glu32.dll: dynamic link libraries for OpenGL and GLU in directory “C:WindowsSystem32”. This directory is to be included in PATH environment variable.

Xem thêm: trò chơi miễn phí không cần tải

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 FilesMicrosoft Visual Studio 10.0VCin”), or “vcvars32.bat” in the earlier version, to set the environment variables.

Writing Your First OpenGL Program

To Write your first OpenGL program with GLUT:

Launch Visual C++ 2010 Express.Create a new “Win32 Console Application” project: Select “File” menu ⇒ New ⇒ Project… ⇒ In “Project Types”, select “Visual C++”, “Win32”. In “Templates”, select “Win32 Console Application”. In “Location”, set your working directory. In “Name”, enter “hello” ⇒ Next ⇒ Check “Empty Project” ⇒ Finish.Create a new Source file: Right-click on the “Source Files” of the project name ⇒ Add ⇒ New Item… ⇒ In “Categories”, select “Visual C++”, “Code”. In “Templates”, select “C++ File (.cpp)”. In “Name”, type “GL01Hello.cpp” ⇒ Add.In the editor panel for “GL01Hello.cpp”, replace with the above code (in the previous section).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 project 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 could also use pre-processor directives to instruct compiler about the libraries used (I prefer this approach, which avoids manual configuration for every project):

#ifdef _MSC_VER // Check if MS Visual C compiler# pragma comment(lib, “opengl32.lib”) // Compiler-specific directive to avoid manually configuration# pragma comment(lib, “glu32.lib”) // Link libraries#endif”error C2664: “xxxx” : cannot convert parameter 2 from “const char ” to “LPCWSTR””: Right-click on the project name ⇒ In “Configuration” drop-down menu, select “All Configurations” (i.e., release and debug) ⇒ Expand the “Configuration Properties” node ⇒ General ⇒ Character Set ⇒ Set to “Use Multi-Byte Character Set”.Alternative, you can use preprocessor directives:#ifdef _MSC_VER // Check if MS Visual C compiler# ifndef _MBCS# define _MBCS // Uses Multi-byte character set# endif# ifdef _UNICODE // Not using Unicode character set# undef _UNICODE # endif# ifdef UNICODE# undef UNICODE # endif#endifCannot find “glaux.h”: Try remove “#include ” from the source code. (Nehe”s tutorial codes included a header “glglaux.h” for reading BMP file from Lesson #6 onwards. Lesson #1 to #5 do not require this header. GLUAX is obsolete and excluded from Windows SDK v6.0a.) If that does not work, you may have to download and install GLUAX package.VC Compilation in Command-lineYou can use the following command-line options to compile and link an OpenGL program. (You should study the command-line options produced by VC++. Right-click on the project name ⇒ Properties ⇒ Expand “Configuration Properties” node ⇒ Expand “C/C++” or “Linker” sub-node ⇒ Command-line.) Assume that the headers (gl.h, glu.h, glut.h) path is set in the INCLUDE environment variable, and the link libraries (opengl32.lib, glu32.lib, glut32.lib) path is set in the LIB environment variable:

> cl /D “_MBCS” /c FirstOpenGLProgram.cpp /c option for compile only without linking, /D “_MBCS” for multi-byte character set, /D “_UNICODE” for Unicode character set.> link /link opengl32.lib glu32.lib glut32.lib kernel32.lib user32.lib gdi32.lib FirstOpenGLProgram.obj /link option specifies the link libraries

Nehe”s OpenGL Tutorial

Nehe”s production maintains an excellent OpenGL tutorial

Leave a comment

Your email address will not be published. Required fields are marked *