How exactly does the OpenGL program write?

Source: Internet
Author: User

The beginner OpenGL person and I have the same feeling, read so many OpenGL books just let me add Glu GLUT glew GLFW These things exactly what is it? We learn OpenGL and OpenGL has nothing. We must be very puzzled.

I learned three months of OpenGL and didn't understand it. What exactly is OpenGL? If I make a simple analogy: OpenGL is the grammar of C + +, in the drawing is not C + + syntax This is OpenGL, is the drawing description rendering processing, he has his own grammar, Type and your own action commands, just as you learned C + + what you can do and can't do, must rely on tools, external framework, GLUT Glew GLFW QT are all. We use graphics.h under windows in VS Easyx.h we want to include GLUT. H That means he's a graphics library that encapsulates the OpenGL graphics operation function. Just call the function in glut and you can draw.

------------------glew OpenGL Extension Library installation using the--------------------------------------------------------

The OpenGL extension Library is a simple tool that helps C + + developers write portable application extensions and initializations. Currently supports a variety of operating systems, including Windows,linux and Solaris, Darwin, IRIX.

OpenGL extension

  1. https://sourceforge.net/project/downloading.php?group_id=67586&filename=glew-1.5.1-win32.zip   

  2. Click on the link above to download the latest Glew (Support opengl 3.0), unzip,  /bin/glew32.dll  copy to  c:/windows/system32  below, will  /lib/glew32.lib   Copy to the  lib  directory under the VC installation directory (e.g./microsoft visual studio 9.0/vc/lib/), the  /include/ glew.h  and  /include/wglew.h  Copy to  /include/gl/  directory under  VC  installation directory (e.g.:/microsoft  visual studio 9.0/vc/include/gl/). In the program we only need to include  glew.h before including gl,glu  or  glut.h  (Note: Be sure to include  glew.h first), add such a sentence in the Code:    

  3. #pragma comment (lib, "Glew32.lib")   

  4. Example:

  5. #include <GL/glew.h>   

  6. #include <GL/glut.h>   

  7. #progrma Comment (lib, "Glew32.lib")   

  8. After creating the OpenGL rendering context, call Glewinit (); Initialize the Glew to the right.

---------------------GLFW Installation--------------------------------------------------

GLFW -Unfortunately, the exact meaning of the FW was not found, not on the wiki, nor on the GLFW homepage. Guess f means for,w means window

GLFW a lightweight, open-source, cross-platform library. Supports OpenGL and OpenGL ES for managing windows, reading input, handling events, and more. Because OpenGL does not have window management capabilities, many enthusiastic people write tools to support these features, such as early glut, freeglut, and so on. So what are the advantages of GLFW? Glut is too old, the last version is the 90 's. Freeglut is fully compatible with glut, is a glut substitute, full-featured, but too many bugs. Stability is not good (not I said AH), GLFW came into being.

1. Open Visual Studio 2012 and create a new console program

2. Right-click Project to select Properties to open the property page

3. In vc++directories-include directories write the GLFW header file directory, here I am. /glfw-3.0.4.bin.win32/include

4. In VC + + directories-library directory write GLFW library file directory, I here is. /glfw-3.0.4.bin.win32/lib-msvc110

5. Fill in the Linker-input-additional dependencies glfw3dll.lib

Note: If you use a static link, glfw3dll.lib should be replaced with Glfw3.lib in the fifth step above, and in the project Properties page, C + +-Code Generation will set the Runtime Library to/MT or/MTD


#include  <glfw/glfw3.h>int main (void) {    glfwwindow* window;     /* Initialize the library */    if  (!glfwinit () )         return -1;    /* create a  windowed mode window and its OpenGL context */     window = glfwcreatewindow (480, 320,  "Hello world",  NULL, NULL);     if  (!window)     {         Glfwterminate ();        return -1;    }     /* make the window ' s context current */     glfwmakecontextcurrent (window);    /* loop until the user  Closes the window */    while  (!glfwwindowshouldclose (window))     {         /* Draw a triangle */         glbegin (Gl_triangles);         glcolor3f (1.0,  0.0, 0.0);    // red         glvertex3f (0.0, 1.0, 0.0);         glcolor3f (0.0, 1.0,  0.0);     // green        glvertex3f (- 1.0, -1.0, 0.0);         glcolor3f (0.0, 0.0, 1.0);     // blue        glvertex3f (1.0, -1.0,  0.0);         glend ();         /* swap front&Nbsp;and back buffers */        glfwswapbuffers (window) ;        /* poll for and process events  */        glfwpollevents ();    }     glfwterminate ();     return 0;}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

The code is simple, just look at the annotations, no more explanations. If everything works, a window will appear with a colorful triangle in it.

650) this.width=650; "src=" Http://image.mamicode.com/info/201607/20180930193628222264.png "style=" border:0px; "/ >

Common Errors and workarounds

Using a library consists of three steps:

    • Include header file

    • link library files

    • Provide runtime DLL files

As long as these three steps are correct, there is basically no problem.

Error One

1 Error C1083:cannot open include file: ' glfw/glfw3.h ': No such file or directory
2 intellisense:cannot Open source file "Glfw/glfw3.h"
3 Intellisense:identifier "Glfwwindow" is undefined
4 Intellisense:identifier "window" is undefined
5 Intellisense:identifier "Glfwinit" is undefined
6 Intellisense:identifier "Glfwcreatewindow" is undefined
7 Intellisense:identifier "NULL" is undefined
8 Intellisense:identifier "Glfwterminate" is undefined
9 Intellisense:identifier "Glfwmakecontextcurrent" is undefined
Ten Intellisense:identifier "Glfwwindowshouldclose" is undefined
intellisense:identifier "Glfwswapbuffers" is undefined
intellisense:identifier "glfwpollevents" is undefined
intellisense:identifier "Glfwterminate" is undefine

This is obviously a header file that was not found, causing all of the following GLFW related types to be found without definition. The path to the header file needs to be set correctly, as long as it is included in the Include directory level, such as \glfw-3.0.4.bin.win32\include

Error two

Error 1 Error lnk2019:unresolved external symbol _glfwinit referenced in function _main
Error 2 Error lnk2019:unresolved external symbol _glfwterminate referenced in function _main
Error 3 Error lnk2019:unresolved external symbol _glfwcreatewindow referenced in function _main
Error 4 error lnk2019:unresolved external symbol _glfwwindowshouldclose referenced in function _main
Error 5 Error lnk2019:unresolved external symbol _glfwpollevents referenced in function _main
Error 6 Error lnk2019:unresolved external symbol _glfwmakecontextcurrent referenced in function _main
ERROR 7 Error lnk2019:unresolved external symbol _glfwswapbuffers referenced in function _main
Error 8 Error lnk1120:7 unresolved externals
This error occurred during the link phase, stating that the library file (. lib) file could not be found, that you need to set the Lib file in the project's Properties page, right-click Project in Visual Studio, select Properties-configuration Propterties-linker-input-additional Dependencies, click the lower triangle on the right to go to the edit page and add Glfw3dll.lib to it.

Note: GFLW provides a different version of the library file, if you are using Visual Studio 2012, use the lib-msvc110 library file below, and if it is Visual Studio 2013, you will need to use the library file lib-msvc120 below. LIB file has two, one is Glfw3.lib, one is Glfw3dll.lib, the former is static link (after linking this file, run without DLL file, but the program volume will become larger), the latter is dynamic link with (with DLL use), do not confuse


How exactly does the OpenGL program write?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.