Content Introduction Opengl Programming

Embed Size (px)

Citation preview

  • 8/10/2019 Content Introduction Opengl Programming

    1/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Skip to Navigation

    An Introduction to OpenGL Programming

    Nov 12, 2014 By MihalisTsoukalos

    in HOW-TOs OpenGL Programming

    207

    OpenGL is awell-known standard for generating 3-D as well as 2-D graphics that is

    extremely powerful and has many capabilities. OpenGL is defined and released by

    the OpenGL Architecture Review Board (ARB).

    This article is a gentle introduction to OpenGL that will help you understand drawing

    using OpenGL.

    The latest version of OpenGL at the time of this writing is 4.4, which uses a different

    technique for drawing from the one presented here. Nevertheless, the purpose of this

    article is to help you understand OpenGL philosophy, not teach you how to code using

    the latest OpenGL version. Therefore, the presented source code can be run on Linux

    machines that have an older OpenGL version installed.

    Installing OpenGL

    TRENDING TOPICS

    SysAdmin

    DevOps

    Security

    Cloud

    Search

    42 people like this. Sign Upto see w hat

    your f riends like.Like

    http://www.linuxjournal.com/tag/how-toshttp://www.linuxjournal.com/tag/openglhttp://www.linuxjournal.com/tag/openglhttp://www.linuxjournal.com/tag/programminghttp://www.linuxjournal.com/user/801328http://www.linuxjournal.com/user/801328http://www.linuxjournal.com/user/801328http://www.linuxjournal.com/subscriptionshttp://www.linuxjournal.com/bloghttp://www.linuxjournal.com/tag/reviewshttp://www.linuxjournal.com/tag/how-toshttp://www.linuxjournal.com/whitepapershttp://www.linuxjournal.com/tag/tech-tipshttp://www.linuxjournal.com/getlinuxhttp://www.linuxjournal.com/gettinghelphttp://www.linuxjournal.com/http://googleads.g.doubleclick.net/aclk?sa=L&ai=Bnxq4z8-cVIvhI8-PMOD5gNgDh_SCvwUAAAAQASDz9J8GOABYr63D0cABYMkGsgEUd3d3LmxpbnV4am91cm5hbC5jb226AQk3Mjh4OTBfYXPIAQLaAUNodHRwOi8vd3d3LmxpbnV4am91cm5hbC5jb20vY29udGVudC9pbnRyb2R1Y3Rpb24tb3BlbmdsLXByb2dyYW1taW5nwAIC4AIA6gIRTGVhZGVyYm9hcmQ3Mjh4OTD4AoTSHpADpAOYA-gCqAMByAOdBNAEkE7gBAGgBhQ&num=0&sig=AOD64_0K9ywrzF26dhkBOp1pwHPzUhc9lw&client=ca-pub-3600556074671070&adurl=https://www.suse.com/promo/zero-downtime/%3Fscr%3Dljhttp://www.linuxjournal.com/http://googleads.g.doubleclick.net/aclk?sa=L&ai=Bnxq4z8-cVIvhI8-PMOD5gNgDh_SCvwUAAAAQASDz9J8GOABYr63D0cABYMkGsgEUd3d3LmxpbnV4am91cm5hbC5jb226AQk3Mjh4OTBfYXPIAQLaAUNodHRwOi8vd3d3LmxpbnV4am91cm5hbC5jb20vY29udGVudC9pbnRyb2R1Y3Rpb24tb3BlbmdsLXByb2dyYW1taW5nwAIC4AIA6gIRTGVhZGVyYm9hcmQ3Mjh4OTD4AoTSHpADpAOYA-gCqAMByAOdBNAEkE7gBAGgBhQ&num=0&sig=AOD64_0K9ywrzF26dhkBOp1pwHPzUhc9lw&client=ca-pub-3600556074671070&adurl=https://www.suse.com/promo/zero-downtime/%3Fscr%3Dljhttp://www.linuxjournal.com/http://googleads.g.doubleclick.net/aclk?sa=L&ai=Bnxq4z8-cVIvhI8-PMOD5gNgDh_SCvwUAAAAQASDz9J8GOABYr63D0cABYMkGsgEUd3d3LmxpbnV4am91cm5hbC5jb226AQk3Mjh4OTBfYXPIAQLaAUNodHRwOi8vd3d3LmxpbnV4am91cm5hbC5jb20vY29udGVudC9pbnRyb2R1Y3Rpb24tb3BlbmdsLXByb2dyYW1taW5nwAIC4AIA6gIRTGVhZGVyYm9hcmQ3Mjh4OTD4AoTSHpADpAOYA-gCqAMByAOdBNAEkE7gBAGgBhQ&num=0&sig=AOD64_0K9ywrzF26dhkBOp1pwHPzUhc9lw&client=ca-pub-3600556074671070&adurl=https://www.suse.com/promo/zero-downtime/%3Fscr%3Dljhttp://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/customize/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.linuxjournal.com/http://www.linuxjournal.com/userhttp://www.linuxjournal.com/subscribehttp://googleads.g.doubleclick.net/aclk?sa=L&ai=Bnxq4z8-cVIvhI8-PMOD5gNgDh_SCvwUAAAAQASDz9J8GOABYr63D0cABYMkGsgEUd3d3LmxpbnV4am91cm5hbC5jb226AQk3Mjh4OTBfYXPIAQLaAUNodHRwOi8vd3d3LmxpbnV4am91cm5hbC5jb20vY29udGVudC9pbnRyb2R1Y3Rpb24tb3BlbmdsLXByb2dyYW1taW5nwAIC4AIA6gIRTGVhZGVyYm9hcmQ3Mjh4OTD4AoTSHpADpAOYA-gCqAMByAOdBNAEkE7gBAGgBhQ&num=0&sig=AOD64_0K9ywrzF26dhkBOp1pwHPzUhc9lw&client=ca-pub-3600556074671070&adurl=https://www.suse.com/promo/zero-downtime/%3Fscr%3Dljhttp://www.linuxjournal.com/user/801328http://www.linuxjournal.com/tag/how-toshttp://www.linuxjournal.com/tag/openglhttp://www.linuxjournal.com/tag/programminghttp://www.linuxjournal.com/subscriptionshttp://www.linuxjournal.com/bloghttp://www.linuxjournal.com/tag/reviewshttp://www.linuxjournal.com/tag/how-toshttp://www.linuxjournal.com/whitepapershttp://www.linuxjournal.com/tag/tech-tipshttp://www.linuxjournal.com/getlinuxhttp://www.linuxjournal.com/gettinghelphttp://www.linuxjournal.com/tag/sysadminhttp://devops.linuxjournal.com/http://www.linuxjournal.com/tag/securityhttp://www.linuxjournal.com/tag/cloudhttp://googleads.g.doubleclick.net/aclk?sa=L&ai=BwfWf0M-cVPyeDc2DMae4gPADh_SCvwUAAAAQASDz9J8GOABYt6zD0cABYMkGsgEUd3d3LmxpbnV4am91cm5hbC5jb226AQozMzZ4MjgwX2FzyAEC2gFDaHR0cDovL3d3dy5saW51eGpvdXJuYWwuY29tL2NvbnRlbnQvaW50cm9kdWN0aW9uLW9wZW5nbC1wcm9ncmFtbWluZ-ABA8ACAuACAOoCFkxhcmdlX1JlY3RhbmdsZTMzNngyODD4AoTSHpADpAOYA-gCqAMByAOdBNAEkE7gBAGgBhQ&num=0&sig=AOD64_3wuXmnYbvgoGgmdDMbY8-QaVDPLA&client=ca-pub-3600556074671070&adurl=https://www.suse.com/promo/zero-downtime/%3Fscr%3Dljhttps://www.facebook.com/campaign/landing.php?campaign_id=137675572948107&partner_id=linuxjournal.com&placement=like_plugin&extra_1=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&extra_2=US
  • 8/10/2019 Content Introduction Opengl Programming

    2/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    I you run t e o ow ng comman on a De an 7 system to n a pac ages t at

    contain the word "opengl", you'll get plenty of output (Figure 1):

    $ apt-cache search opengl

    Figure 1. Running apt-cache search opengl

    There are many free OpenGL implementations for Linux, but you need only one. I

    installed FreeGLUT, because it is the most up to date. FreeGLUT is an open-source

    HPC

    Mobile

    Virtualization

    Web Development

    The Latest Popular Recent Comments

    New Products Dec 22, 2014

    Android Candy: Google Keep Dec 19, 2014Handling the workloads of the Future Dec 18, 2014

    Raspi-Sump Dec 16, 2014

    diff -u: What's New in KernelDevelopment

    Dec 12, 2014

    Non-Linux FOSS: Don't Type AllThose Words!

    Dec 10, 2014

    more

    RELATED JOBS

    Linux Unix Systems ArchitectRequest Technology-Craig Jo...Northbrook, IL

    Senior Linux and Unix Platform ArchitectRequest Technology-Anthony ...Northbrook, IL

    IT Manager Linux/Windows Server Lifec...Request Technology-Robyn Ho...Hudson, OH

    Technisch Applicatiebeheer LinuxArrows Group - NLHilversum, Noord-Holland, N...

    Linux Project EngineerArrows Group - NLSon en Breugel, Noord-Braba...

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.linuxjournal.com/tag/hpchttp://www.linuxjournal.com/tag/mobilehttp://www.linuxjournal.com/tag/virtualizationhttp://www.linuxjournal.com/tag/web-developmenthttp://www.linuxjournal.com/content/introduction-opengl-programming?quicktabs_1=0#quicktabs-1http://www.linuxjournal.com/content/introduction-opengl-programming?quicktabs_1=1#quicktabs-1http://www.linuxjournal.com/content/introduction-opengl-programming?quicktabs_1=2#quicktabs-1http://www.linuxjournal.com/slideshow/new-products-6http://www.linuxjournal.com/content/android-candy-google-keephttp://www.linuxjournal.com/content/handling-workloads-futurehttp://www.linuxjournal.com/content/raspi-sumphttp://www.linuxjournal.com/content/diff-u-whats-new-kernel-development-5http://www.linuxjournal.com/content/non-linux-foss-dont-type-all-those-wordshttp://www.linuxjournal.com/latesthttp://jobs.linuxjournal.com/job/linux-unix-systems-architect-northbrook-il-request-technology-craig-johnson-947f7b6251/?d=1&source=jobrollhttp://jobs.linuxjournal.com/job/senior-linux-and-unix-platform-architect-northbrook-il-request-technology-anthony-honquest-123a1921e2/?d=1&source=jobrollhttp://jobs.linuxjournal.com/job/it-manager-linux-windows-server-lifecycle-change-hudson-oh-request-technology-robyn-honquest-e3b4b55999/?d=1&source=jobrollhttp://jobs.linuxjournal.com/job/technisch-applicatiebeheer-linux-hilversum-arrows-group-nl-4f154e9327/?d=1&source=jobrollhttp://jobs.linuxjournal.com/job/linux-project-engineer-son-en-breugel-arrows-group-nl-cf0b053378/?d=1&source=jobroll
  • 8/10/2019 Content Introduction Opengl Programming

    3/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    alternative to the OpenGL Utility Toolkit (GLUT) library:

    root@mail:~# apt-get install freeglut3 freeglut3-dev libglew-dev

    Reading package lists... Done

    Building dependency tree

    Reading state information... Done

    The following package was automatically installed

    and is no longer required: fonts-liberation

    Use 'apt-get autoremove' to remove it.

    The following extra packages will be installed:

    libgl1-mesa-dev libglu1-mesa-dev libice-dev libpthread-stubs0

    libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc

    libxau-dev libxcb1-dev libxdmcp-dev libxext-dev libxt-dev

    mesa-common-dev x11proto-core-dev x11proto-input-dev

    x11proto-kb-dev x11proto-xext-dev xorg-sgml-doctools xtrans-dev

    Suggested packages: libice-doc libsm-doc libxcb-doc libxext-doc libxt-doc

    The following NEW packages will be installed:

    freeglut3 freeglut3-dev libgl1-mesa-dev libglu1-mesa-dev

    libice-dev libpthread-stubs0 libpthread-stubs0-dev libsm-dev

    libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev

    libxext-dev libxt-dev mesa-common-dev x11proto-core-dev

    x11proto-input-dev x11proto-kb-dev x11proto-xext-dev

    xorg-sgml-doctools xtrans-dev

    0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.Need to get 7,651 kB of archives.

    After this operation, 24.9 MB of additional disk space

    will be used.

    Do you want to continue [Y/n]?

    You also will need a C++ compiler to compile the code.

    Finally, y ou may need to install the mesa-utils package in order to be able to use the

    Jeff ParentBrad BaillioNick BaronianSteve Case

    Chadalavada KalyanaCaleb CullenKeir DavisMichael EagerNick FaltysDennis FreyPhilip JacobJay KruizengaSteve MarquezDave McAllisterCraig Oda

    Mike RobertsChris StarkPatrick SwartzDavid Lynch

    Alicia GibbThomas QuinlanCarson McDonaldKristen ShoemakerCharnell LuchichJames WalkerVictor GregorioHari BoukisBrian ConnerDavid Lane

    More Jobs >Post a Job >

    Subscribe to Jobs:

    Pow ered by

    EDITORIAL ADVISORY PANEL

    Thank you to our 2014 Editorial Advisors!

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://jobs.linuxjournal.com/http://jobs.linuxjournal.com/posthttp://jobs.linuxjournal.com/jt/jobs/addedit_alert.phphttp://jobs.linuxjournal.com/jt/syndication/feeds.phphttp://twitter.com/LinuxJournalJobhttp://googleads.g.doubleclick.net/aclk?sa=L&ai=BD-UP0M-cVNj1EJCBMZH0gvgC98XF-wEAAAAQASDz9J8GOABYx9KCiyBgyQayARR3d3cubGludXhqb3VybmFsLmNvbboBCWdmcF9pbWFnZcgBAtoBQ2h0dHA6Ly93d3cubGludXhqb3VybmFsLmNvbS9jb250ZW50L2ludHJvZHVjdGlvbi1vcGVuZ2wtcHJvZ3JhbW1pbmfgAQTAAgLgAgDqAg1zdWJzY3JpcHRpb25z-AKE0h6QA6QDmAPoAqgDAcgDnQTQBJBO4AQBoAYU&num=0&sig=AOD64_2YienzEs56PItLxziXnTchKh6O0g&client=ca-pub-3600556074671070&adurl=https://www.pubservice.com/Subnew2page.aspx%3FPC%3DLJhttp://googleads.g.doubleclick.net/aclk?sa=L&ai=BD-UP0M-cVNj1EJCBMZH0gvgC98XF-wEAAAAQASDz9J8GOABYx9KCiyBgyQayARR3d3cubGludXhqb3VybmFsLmNvbboBCWdmcF9pbWFnZcgBAtoBQ2h0dHA6Ly93d3cubGludXhqb3VybmFsLmNvbS9jb250ZW50L2ludHJvZHVjdGlvbi1vcGVuZ2wtcHJvZ3JhbW1pbmfgAQTAAgLgAgDqAg1zdWJzY3JpcHRpb25z-AKE0h6QA6QDmAPoAqgDAcgDnQTQBJBO4AQBoAYU&num=0&sig=AOD64_2YienzEs56PItLxziXnTchKh6O0g&client=ca-pub-3600556074671070&adurl=https://www.pubservice.com/Subnew2page.aspx%3FPC%3DLJ
  • 8/10/2019 Content Introduction Opengl Programming

    4/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    glxinfo command:

    # apt-get install mesa-utils

    The glxinfo command displays useful information about your OpenGL installation, as

    you can see in the following output:

    ...

    GLX version: 1.4

    GLX extensions:

    GLX_ARB_get_proc_address, GLX_ARB_multisample,

    GLX_EXT_import_context, GLX_EXT_texture_from_pixmap,

    GLX_EXT_visual_info, GLX_EXT_visual_rating,

    GLX_MESA_copy_sub_buffer, GLX_MESA_multithread_makecurrent,

    GLX_OML_swap_method, GLX_SGIS_multisample, GLX_SGIX_fbconfig,

    GLX_SGIX_pbuffer, GLX_SGI_make_current_read

    OpenGL vendor string: VMware, Inc.

    OpenGL renderer string: Gallium 0.4 on llvmpipe

    (LLVM 3.4, 128 bits)

    OpenGL version string: 2.1 Mesa 10.1.3

    OpenGL shading language version string: 1.30

    OpenGL extensions:

    ...

    Mesa is a 3-D graphics library with an API that is so very similar to OpenGL's, it is

    pretty much indistinguishable.

    OpenGL Pipeline

    Figure 2taken from the OpenGL Shading Language book (aka "T he Orange Book")

    shows the programmable OpenGL pipeline with the vertex and fragment

    processors. As you can imagine, the OpenGL pipeline is complex, but y ou do not have

    to understand it fully in order to be able to use OpenGL. T he Pipeline shows how

    OpenGL operates in the background. Newer versions of the OpenGL Pipeline are

    Already a subscriber? Click here for subscriber services.

    December 2014 Video Preview

    November 2014 VideoPreview

    October 2014 VideoPreview

    Click here for more videos

    http://googleads.g.doubleclick.net/aclk?sa=L&ai=BD-UP0M-cVNj1EJCBMZH0gvgC98XF-wEAAAAQASDz9J8GOABYx9KCiyBgyQayARR3d3cubGludXhqb3VybmFsLmNvbboBCWdmcF9pbWFnZcgBAtoBQ2h0dHA6Ly93d3cubGludXhqb3VybmFsLmNvbS9jb250ZW50L2ludHJvZHVjdGlvbi1vcGVuZ2wtcHJvZ3JhbW1pbmfgAQTAAgLgAgDqAg1zdWJzY3JpcHRpb25z-AKE0h6QA6QDmAPoAqgDAcgDnQTQBJBO4AQBoAYU&num=0&sig=AOD64_2YienzEs56PItLxziXnTchKh6O0g&client=ca-pub-3600556074671070&adurl=https://www.pubservice.com/Subnew2page.aspx%3FPC%3DLJhttp://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.linuxjournal.com/subs/customer_servicehttp://www.linuxjournal.com/248-videohttp://www.linuxjournal.com/247-videohttp://www.linuxjournal.com/247-videohttp://www.linuxjournal.com/246-videohttp://www.linuxjournal.com/246-videohttp://www.linuxjournal.com/videohttp://www.linuxjournal.com/enewslettershttp://googleads.g.doubleclick.net/aclk?sa=L&ai=BD-UP0M-cVNj1EJCBMZH0gvgC98XF-wEAAAAQASDz9J8GOABYx9KCiyBgyQayARR3d3cubGludXhqb3VybmFsLmNvbboBCWdmcF9pbWFnZcgBAtoBQ2h0dHA6Ly93d3cubGludXhqb3VybmFsLmNvbS9jb250ZW50L2ludHJvZHVjdGlvbi1vcGVuZ2wtcHJvZ3JhbW1pbmfgAQTAAgLgAgDqAg1zdWJzY3JpcHRpb25z-AKE0h6QA6QDmAPoAqgDAcgDnQTQBJBO4AQBoAYU&num=0&sig=AOD64_2YienzEs56PItLxziXnTchKh6O0g&client=ca-pub-3600556074671070&adurl=https://www.pubservice.com/Subnew2page.aspx%3FPC%3DLJhttp://www.linuxjournal.com/enewsletters
  • 8/10/2019 Content Introduction Opengl Programming

    5/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    even more complex!

    Figure 2. OpenGL Architecture

    OpenGL is a big state machine. Most calls to OpenGL functions modify a global state

    that you cannot access directly.

    The OpenGL Shading Language code that is intended for ex ecution on one of the

    OpenGL programmable processors is called a Shader. The OpenGL Shading

    Language has its roots in C (presenting the OpenGL Shading Language is beyond the

    scope of this article).

    OpenGL does not define a windowing layer, because it tries to be platform-neutral

    and leaves this functionality to the operating system. The OS must provide arendering context that accepts commands as well as a framebuffer that keeps the

    results of the drawing commands.

    Matrix Algebra is extensively used in 3-D graphics, so it is good for you to know how

    to add, multiply, subtract and divide matrices, although you will not need to code such

    operations yourself. It also is useful to become familiar with 3-D coordinates and be

    able to sketch on paper the 3-D scene you are trying to draw.

    http://www.linuxjournal.com/enewslettershttp://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.linuxjournal.com/enewslettershttp://www.linuxjournal.com/new-issue
  • 8/10/2019 Content Introduction Opengl Programming

    6/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Drawing a Triangle

    Now it's time for some real OpenGL code. The code in Listing 1, when ex ecuted,

    draws a triangle on-screen using OpenGL.

    Listing 1. triangle.cc

    // Programmer: Mihalis Tsoukalos

    // Date: Wednesday 04 June 2014

    //

    // A simple OpenGL program that draws a triangle.

    #include "GL/freeglut.h"

    #include "GL/gl.h"

    void drawTriangle()

    {

    glClearColor(0.4, 0.4, 0.4, 0.4);

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 1.0, 1.0);

    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glBegin(GL_TRIANGLES);

    glVertex3f(-0.7, 0.7, 0);

    glVertex3f(0.7, 0.7, 0);

    glVertex3f(0, -1, 0);

    glEnd();

    glFlush();

    }

    int main(int argc, char **argv)

    {

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdf
  • 8/10/2019 Content Introduction Opengl Programming

    7/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE);

    glutInitWindowSize(500, 500);

    glutInitWindowPosition(100, 100);

    glutCreateWindow("OpenGL - Creating a triangle");

    glutDisplayFunc(drawTriangle);

    glutMainLoop();

    return 0;

    }

    The code in Listing 1 for setting up OpenGL is large, but y ou will have to learn it only

    once.

    On a Debian 7 system, the following command compiled the triangle.cc OpenGL

    program without any error messages:

    $ g++ triangle.cc -lglut -o triangle

    On an Ubuntu Linux system, the same command produced the following error

    messages:

    /usr/bin/ld: /tmp/ccnBP5li.o: undefined reference to symbol

    ↪'glOrtho'

    //usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding

    //symbols: DSO missing from command line

    collect2: error: ld returned 1 exit status

    The solution is to compile the triangle.cc program by linking the executable to an

    additional library (-lGL):

    mtsouk@mtsouk-VirtualBox:/media/sf_OpenGL.LJ/code$ g++

    ↪triangle.cc -lglut -lGL -o triangle

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdf
  • 8/10/2019 Content Introduction Opengl Programming

    8/12

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The libGL.so library accepts OpenGL commands and makes sure that they are put on

    the screen in some way. If y our graphics card does not have 3-D acceleration, libGL

    contains a software renderer that gives a 2-D image as output to the X Window

    System. This is the case with Mesa. libGL also can pass the OpenGL information to

    the X Window System, if the GLX extension is present. Then, the X Window System

    can do software rendering with the help of Mesa or use hardware acceleration.

    The output from the executable will produce the triangle shown in Figure 3. The

    correct compilation of triangle.cc is proof that your Linux system can be used fordeveloping OpenGL applications.

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdf
  • 8/10/2019 Content Introduction Opengl Programming

    9/12

    df d mi b PRO i Are you a developer? Try out the HTML to PDF API

    Figure 3. Drawing a T riangle Using OpenGL

    1 2 3 next last

    ______________________

    Login to save this as favorite

    4 Comments

    Juan de Seven Wexford

    https://disqus.com/home/user/juandesevenwexford/http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttps://disqus.com/home/discussions/linuxjournal/an_introduction_to_opengl_programming/https://disqus.com/home/forums/linuxjournal/http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#https://disqus.com/home/user/juandesevenwexford/https://disqus.com/home/user/juandesevenwexford/http://www.linuxjournal.com/content/introduction-opengl-programming?page=0,1http://www.linuxjournal.com/content/introduction-opengl-programming?page=0,2http://www.linuxjournal.com/content/introduction-opengl-programming?page=0,1http://www.linuxjournal.com/content/introduction-opengl-programming?page=0,2http://www.linuxjournal.com/u?destination=node%2F1338480http://del.icio.us/post?url=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&title=An+Introduction+to+OpenGL+Programminghttp://digg.com/submit?phase=2&url=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&title=An+Introduction+to+OpenGL+Programminghttp://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t=An+Introduction+to+OpenGL+Programminghttp://reddit.com/submit?url=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&title=An+Introduction+to+OpenGL+Programminghttp://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&title=An+Introduction+to+OpenGL+Programminghttp://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&title=An+Introduction+to+OpenGL+Programminghttp://twitter.com/home?status=+An+Introduction+to+OpenGL+Programming%3A+http%3A/%2Fwww.linuxjournal.com/content/introduction-opengl-programminghttp://googleads.g.doubleclick.net/aclk?sa=l&ai=CMgc_z8-cVJKqKpSBMZHegKANoMOp7AXoz7Sv1wHVh4-uBBABIPP0nwZgyQagAZys6vMDyAECqQJ6hQ6NcX20PuACAKgDAcgDnQSqBMcBT9CgjS4Pc-hyqVvQLJdxr7QyPWhcOqc8QT64QpktGknQR41oZxhSLKTTA4aLRvE-wMIBsrz4HuTJItUQ4DmFnKNmB7QPeWIU6yU3i7QXF1EVouW5j7zjsS7KQCWLdb0AbZGXv7yHI21W-Ba0FGPxnm588KUVR-3H5qfC72rRcQL9w717udT04rPL-nHCSzJns27uWcuMel2yqlnTJ-9OU-AFNXwi1oI8Gjfb4xT0If6J00gMtQBWNPAsEQcn67rERMb9b8GJg-AEAaAGAoAHzNOVDA&num=1&sig=AOD64_1wKHOnoBcuGp7bYWaL2TsipSiJQg&client=ca-pub-3600556074671070&adurl=https://www.elearnsecurity.com/landing/xmas.php%3FLeadSourceID%3D587https://disqus.com/home/user/juandesevenwexford/
  • 8/10/2019 Content Introduction Opengl Programming

    10/12

    df di b PRO i Are you a developer? Try out the HTML to PDF API

    our own OS, and unlike Apple we have basically no advertising budget, so maybe highligh

    community more than free nudges and winks to Apple.

    Shahbaz Youssefi

    To anyone reading this article!

    This is thecode style of OpenGL 1. It is virtually deprecated and no one codes like this an

    have an idea how OpenGL works, don't waste your time much learning ancient stuff like t

    You can learn on modern computer graphics (with OpenGL of course) for free here: http:

    Sam

    I get this on your last example:

    g++ rotateCube.cc-lglut -lGL -o rotateCube

    /tmp/ccwd9YdJ.o: In function handleResize(int, int)':

    rotateCube.cc:(.text+0xa65): undefined reference to gluPerspective'

    collect2: error: ld returned 1 exit status

    Is there another library required?

    Julin

    I've found that you have to add the -lGLU flag to compile this in Ubuntu 13.04. Hop

    https://disqus.com/home/user/juandesevenwexford/http://rotatecube.cc/http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://www.arcsynthesis.org/gltut/https://disqus.com/home/user/shahbazyoussefi/https://disqus.com/home/user/shahbazyoussefi/https://disqus.com/home/user/shahbazyoussefi/https://disqus.com/home/user/shahbazyoussefi/http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#https://disqus.com/home/user/shahbazyoussefi/http://www.arcsynthesis.org/gltut/http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://rotatecube.cc/http://rotatecube.cc/http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#http://disqus.com/http://disqus.com/embed/comments/?base=default&disqus_version=366531e3&f=linuxjournal&t_i=node%2F1338480&t_u=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fintroduction-opengl-programming&t_e=An%20Introduction%20to%20OpenGL%20Programming&t_d=An%20Introduction%20to%20OpenGL%20Programming&t_t=An%20Introduction%20to%20OpenGL%20Programming&s_o=default#https://disqus.com/websites/?utm_source=linuxjournal&utm_medium=Disqus-Footerhttps://help.disqus.com/customer/portal/articles/1657951?utm_source=disqus&utm_medium=embed-footer&utm_content=privacy-btnhttps://disqus.com/home/user/juandesevenwexford/https://disqus.com/home/user/shahbazyoussefi/
  • 8/10/2019 Content Introduction Opengl Programming

    11/12

    df di b PRO i A d l ? T t th HTML t PDF API

    Webinar More Resources

    One Click, Universal Protection:

    Implementing Centralized SecurityPolicies on Linux Systems

    As Linux continues to play an ever increasingrole in corporate data centers and institutions,ensuring the integrity and protection of thesesystems mus t be a priority. With 60% of theworld's websites and an increasing share oforganization's mission-critical workloadsrunning on Linux, failing to stop malware andother advanced threats on Linux can increasinglyimpact an organization's reputation and bo ttom

    line.

    Learn More

    Sponsored by Bit9

    Webinar More Resources

    Linux Backup and Recovery Webinar

    Most companies incorporate backup proceduresfor critical data, which can be res tored quickly if aloss occurs. However, fewer companies areprepared for catastrophic system failures, inwhich they lose all data, the entire operatingsystem, applications, settings, patches andmore, reducing their system(s) to bare metal.

    After all, before data can be restored to a system,there must be a system to restore it to.

    In this one hour webinar, learn how to enhanceyour existing backup strategies for better disaster

    recovery preparedness using Storix SystemBackup Adminis trator (SBAdmin), a highly flexiblebare-metal recovery solution for UNIX and Linuxsystems.

    Learn More

    Sponsored by Storix

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.linuxjournal.com/whitepapershttp://www.linuxjournal.com/content/watch-one-click-universal-protection-implementing-centralized-security-policies-linux-systemhttp://www.linuxjournal.com/content/watch-one-click-universal-protection-implementing-centralized-security-policies-linux-systemhttp://www.linuxjournal.com/whitepapershttp://www.linuxjournal.com/content/register-linux-backup-and-recovery-webinarhttp://www.linuxjournal.com/content/register-linux-backup-and-recovery-webinar
  • 8/10/2019 Content Introduction Opengl Programming

    12/12df di b PRO i A d l ? T h HTML PDF API

    First Name Last Name

    Address 1 Address 2

    City State Zip

    Email

    Canadian Residents| Foreign Residents| Gift Subscriptions|

    Customer Se rvice| Privacy PolicyDigital Edition

    Subscribe/RenewPay My Bill

    Customer Service

    Digital Downloads

    Gift Subscriptions

    Magazine Formats

    PC/MacAndroid

    iPhone/iPad

    EPUB

    Kindle

    The Store

    StickersT-shirts

    Posters

    Back Issues

    Archive CD

    About Us

    AdvertiseAuthor Info

    Write a Letter

    FAQ

    Masthead

    The Site

    CopyrightRSS Feeds

    Privacy Policy

    Events

    Contact UsCopyright 1994 - 2014 Linux Journal. All rights reserved.

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.linuxjournal.com%2fcontent%2fintroduction-opengl-programming&id=ma-141225220242-67a0eb82http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttps://www.pubservice.com/subnew2page.aspx?PC=LJ&PK=MPUB911&FC=CANADA&SA=Phttps://www.pubservice.com/Subnew1.aspx?PC=LJ&PK=MPUB911&FC=UNITED%20STATES&ref=1&SA=Phttps://www.pubservice.com/subgift.aspx?PC=LJhttp://www.linuxjournal.com/subs/customer_servicehttp://www.linuxjournal.com/content/privacystatementhttps://www.pubservice.com/subnew1.aspx?PC=LJhttps://www.pubservice.com/subinfo.aspx?PC=LJ&DO=Phttp://www.linuxjournal.com/subs/customer_servicehttp://www.linuxjournal.com/digitalhttps://www.pubservice.com/subgift.aspx?PC=LJhttp://www.linuxjournal.com/content/pdfhttp://www.linuxjournal.com/content/androidhttp://www.linuxjournal.com/content/ioshttp://www.linuxjournal.com/content/epubhttp://www.linuxjournal.com/content/kindlehttp://lj.mybigcommerce.com/categories/Buttons-%26-Stickers/http://lj.mybigcommerce.com/categories/T%252dShirts-%26-Apparel/http://lj.mybigcommerce.com/categories/Posters/http://lj.mybigcommerce.com/categories/Back-Issues-%252d-Digital-Downloads/http://linuxjournal.com/archive_cdhttp://www.linuxjournal.com/advertisinghttp://www.linuxjournal.com/authorhttp://www.linuxjournal.com/contacthttp://www.linuxjournal.com/aboutus/FAQhttp://www.linuxjournal.com/staffhttp://www.linuxjournal.com/content/copyrighthttp://www.linuxjournal.com/rss_feedshttp://www.linuxjournal.com/content/privacy-statementhttp://www.linuxjournal.com/eventshttp://www.linuxjournal.com/aboutus